|
5 | 5 | import time |
6 | 6 | import atexit |
7 | 7 | import shutil |
8 | | -import struct |
9 | 8 | import tarfile |
10 | 9 | import os.path |
11 | 10 | import subprocess |
12 | 11 |
|
13 | 12 | from collections import OrderedDict |
14 | 13 | from ntfsea import ntfsea, lxattrb, stmode |
15 | | -from utils import Fore, ProgressFileObject, parse_image_arg, probe_wsl, get_label, show_cursor, hide_cursor, draw_progress, clear_progress, escape_ntfs_invalid |
| 14 | +from utils import * |
16 | 15 |
|
17 | 16 | try: |
18 | 17 | import PySquashfsImage |
|
45 | 44 |
|
46 | 45 | basedir, lxpath = probe_wsl() |
47 | 46 |
|
| 47 | +uid = 0 |
| 48 | +gid = 0 |
48 | 49 | user = '' |
49 | 50 | isroot = False |
50 | 51 | homedir = '' |
51 | 52 | homedirw = '' |
52 | 53 |
|
53 | | -# somewhat a major issue, stdout and stderr can't be redirected, so this script can't monitor the output |
54 | | -# of any of the launched commands. it can, however, receive the exit status, so that's something, I guess. |
55 | | -# ref: https://github.com/Microsoft/BashOnWindows/issues/2 |
56 | | - |
57 | 54 | try: |
58 | | - subprocess.check_call(['cmd', '/C', lxpath + '\\bash.exe', '-c', 'echo $HOME > /tmp/.wsl_usr.txt; echo $USER >> /tmp/.wsl_usr.txt']) |
59 | | - out = os.path.join(basedir, 'rootfs/tmp/.wsl_usr.txt') |
60 | | - |
61 | | - if not os.path.isfile(out): |
62 | | - print('%s[!]%s Failed to get home directory of default user in WSL: Output file %s%s%s not present.' % (Fore.RED, Fore.RESET, Fore.BLUE, out, Fore.RESET)) |
63 | | - sys.exit(-1) |
| 55 | + uid, gid, user = get_lxss_user() |
64 | 56 |
|
65 | | - with open(out) as f: |
66 | | - homedir = f.readline().strip() |
67 | | - homedirw = os.path.join(basedir, homedir.lstrip('/')) |
| 57 | + if uid == 0: |
| 58 | + isroot = True |
| 59 | + homedir = '/root' |
| 60 | + else: |
| 61 | + homedir = '/home/' + user |
68 | 62 |
|
69 | | - if len(homedir) == 0 or not os.path.isdir(homedirw): |
70 | | - print('%s[!]%s Failed to get home directory of default user in WSL: Returned path %s%s%s is not valid.' % (Fore.RED, Fore.RESET, Fore.BLUE, homedirw, Fore.RESET)) |
71 | | - sys.exit(-1) |
| 63 | + homedirw = os.path.join(basedir, homedir.lstrip('/')) |
72 | 64 |
|
73 | | - user = f.readline().strip() |
74 | | - isroot = user == 'root' |
| 65 | + if len(homedir) == 0 or not os.path.isdir(homedirw): |
| 66 | + print('%s[!]%s Failed to get home directory of default user in WSL: Returned path %s%s%s is not valid.' % (Fore.RED, Fore.RESET, Fore.BLUE, homedirw, Fore.RESET)) |
| 67 | + sys.exit(-1) |
75 | 68 |
|
76 | 69 | print('%s[*]%s Default user is %s%s%s at %s%s%s.' % (Fore.GREEN, Fore.RESET, Fore.YELLOW, user, Fore.RESET, Fore.BLUE, homedir, Fore.RESET)) |
77 | 70 |
|
78 | | - os.unlink(out) |
79 | | - |
80 | | -except subprocess.CalledProcessError as err: |
| 71 | +except BaseException as err: |
81 | 72 | print('%s[!]%s Failed to get home directory of default user in WSL: %s' % (Fore.RED, Fore.RESET, err)) |
82 | 73 | sys.exit(-1) |
83 | 74 |
|
@@ -456,54 +447,32 @@ def retry_rw(operation, name, exc): |
456 | 447 | print('%s[*]%s Switching default user to %sroot%s...' % (Fore.GREEN, Fore.RESET, Fore.YELLOW, Fore.RESET)) |
457 | 448 |
|
458 | 449 | try: |
459 | | - subprocess.check_output(['cmd', '/C', lxpath + '\\lxrun.exe', '/setdefaultuser', 'root']) |
| 450 | + set_lxss_user(0, 0, 'root') |
460 | 451 |
|
461 | | - except subprocess.CalledProcessError as err: |
| 452 | + except BaseException as err: |
462 | 453 | print('%s[!]%s Failed to switch default user in WSL: %s' % (Fore.RED, Fore.RESET, err)) |
463 | 454 | sys.exit(-1) |
464 | 455 |
|
465 | | - try: |
466 | | - subprocess.check_call(['cmd', '/C', lxpath + '\\bash.exe', '-c', |
467 | | - 'echo $HOME > /tmp/.wsl_usr.txt; echo $USER >> /tmp/.wsl_usr.txt']) |
468 | | - out = os.path.join(basedir, 'rootfs/tmp/.wsl_usr.txt') |
469 | | - |
470 | | - if not os.path.isfile(out): |
471 | | - print('%s[!]%s Failed to get home directory of default user in WSL: Output file %s%s%s not present.' % (Fore.RED, Fore.RESET, Fore.BLUE, out, Fore.RESET)) |
472 | | - sys.exit(-1) |
473 | | - |
474 | | - with open(out) as f: |
475 | | - homedir = f.readline().strip() |
476 | | - homedirw = os.path.join(basedir, homedir.lstrip('/')) |
477 | | - |
478 | | - if len(homedir) == 0 or not os.path.isdir(homedirw): |
479 | | - print('%s[!]%s Failed to get home directory of default user in WSL: Returned path %s%s%s is not valid.' % (Fore.RED, Fore.RESET, Fore.BLUE, homedirw, Fore.RESET)) |
480 | | - sys.exit(-1) |
481 | | - |
482 | | - user2 = f.readline().strip() |
483 | | - |
484 | | - if user2 != 'root': |
485 | | - print('%s[!]%s Failed to switch default user to %sroot%s.' % (Fore.RED, Fore.RESET, Fore.YELLOW, Fore.RESET)) |
486 | | - sys.exit(-1) |
| 456 | + homedir = '/root' |
| 457 | + homedirw = os.path.join(basedir, homedir.lstrip('/')) |
487 | 458 |
|
488 | | - os.unlink(out) |
489 | | - |
490 | | - except subprocess.CalledProcessError as err: |
491 | | - print('%s[!]%s Failed to get home directory of default user in WSL: %s' % (Fore.RED, Fore.RESET, err)) |
| 459 | + if not os.path.isdir(homedirw): |
| 460 | + print('%s[!]%s Failed to get home directory of default user in WSL: Returned path %s%s%s is not valid.' % (Fore.RED, Fore.RESET, Fore.BLUE, homedirw, Fore.RESET)) |
492 | 461 | sys.exit(-1) |
493 | 462 |
|
494 | 463 | # since we switched to root, switch back to regular user on exit |
495 | 464 |
|
496 | | - def switch_user_back(user): |
| 465 | + def switch_user_back(uid, gid, user): |
497 | 466 | print('%s[*]%s Switching default user back to %s%s%s...' % (Fore.GREEN, Fore.RESET, Fore.YELLOW, user, Fore.RESET)) |
498 | 467 |
|
499 | 468 | try: |
500 | | - subprocess.check_output(['cmd', '/C', lxpath + '\\lxrun.exe', '/setdefaultuser', user]) |
| 469 | + set_lxss_user(uid, gid, user) |
501 | 470 |
|
502 | | - except subprocess.CalledProcessError as err: |
| 471 | + except BaseException as err: |
503 | 472 | print('%s[!]%s Failed to switch default user in WSL: %s' % (Fore.RED, Fore.RESET, err)) |
504 | 473 | sys.exit(-1) |
505 | 474 |
|
506 | | - atexit.register(switch_user_back, user) |
| 475 | + atexit.register(switch_user_back, uid, gid, user) |
507 | 476 |
|
508 | 477 | # run post-install hooks, if any |
509 | 478 |
|
|
0 commit comments