windows_setup.py done

This commit is contained in:
Alan Mason 2017-12-01 10:12:59 -08:00
parent 33924c183e
commit deb7c76ffb

View file

@ -86,25 +86,17 @@ def find_windows_image(windows_version):
windows_version['Name'])) windows_version['Name']))
raise GeneralAbort raise GeneralAbort
def format_gpt(disk=None, windows_family=None): def format_gpt(disk, windows_family):
"""Format disk for use as a Windows OS drive using the GPT (UEFI) layout.""" """Format disk for use as a Windows OS drive using the GPT layout."""
# Bail early
if disk is None:
raise Exception('No disk provided.')
if windows_family is None:
raise Exception('No Windows family provided.')
# Format drive
# print_info('Drive will use a GPT (UEFI) layout.')
with open(DISKPART_SCRIPT, 'w') as script: with open(DISKPART_SCRIPT, 'w') as script:
# Partition table # Partition table
script.write('select disk {number}\n'.format(number=disk['Number'])) script.write('select disk {}\n'.format(disk['Number']))
script.write('clean\n') script.write('clean\n')
script.write('convert gpt\n') script.write('convert gpt\n')
# System partition # System partition
script.write('create partition efi size=260\n') # NOTE: Allows for Advanced Format 4K drives # NOTE: ESP needs to be >= 260 for Advanced Format 4K drives
script.write('create partition efi size=500\n')
script.write('format quick fs=fat32 label="System"\n') script.write('format quick fs=fat32 label="System"\n')
script.write('assign letter="S"\n') script.write('assign letter="S"\n')
@ -126,23 +118,14 @@ def format_gpt(disk=None, windows_family=None):
script.write('gpt attributes=0x8000000000000001\n') script.write('gpt attributes=0x8000000000000001\n')
# Run script # Run script
run_program('diskpart /s {script}'.format(script=DISKPART_SCRIPT)) run_program(['diskpart', '/s', DISKPART_SCRIPT])
time.sleep(2) time.sleep(2)
def format_mbr(disk=None, windows_family=None): def format_mbr(disk, windows_family):
"""Format disk for use as a Windows OS drive using the MBR (legacy) layout.""" """Format disk for use as a Windows OS drive using the MBR layout."""
# Bail early
if disk is None:
raise Exception('No disk provided.')
if windows_family is None:
raise Exception('No Windows family provided.')
# Format drive
# print_info('Drive will use a MBR (legacy) layout.')
with open(DISKPART_SCRIPT, 'w') as script: with open(DISKPART_SCRIPT, 'w') as script:
# Partition table # Partition table
script.write('select disk {number}\n'.format(number=disk['Number'])) script.write('select disk {}\n'.format(disk['Number']))
script.write('clean\n') script.write('clean\n')
# System partition # System partition
@ -165,7 +148,7 @@ def format_mbr(disk=None, windows_family=None):
script.write('set id=27\n') script.write('set id=27\n')
# Run script # Run script
run_program('diskpart /s {script}'.format(script=DISKPART_SCRIPT)) run_program(['diskpart', '/s', DISKPART_SCRIPT])
time.sleep(2) time.sleep(2)
def mount_windows_share(): def mount_windows_share():
@ -177,7 +160,9 @@ def mount_windows_share():
mount_network_share(WINDOWS_SERVER) mount_network_share(WINDOWS_SERVER)
def select_windows_version(): def select_windows_version():
actions = [{'Name': 'Main Menu', 'Letter': 'M'},] actions = [
{'Name': 'Main Menu', 'Letter': 'M'},
]
# Menu loop # Menu loop
selection = menu_select( selection = menu_select(
@ -188,7 +173,7 @@ def select_windows_version():
if selection.isnumeric(): if selection.isnumeric():
return WINDOWS_VERSIONS[int(selection)-1] return WINDOWS_VERSIONS[int(selection)-1]
elif selection == 'M': elif selection == 'M':
abort_to_main_menu() raise GeneralAbort
def setup_windows(windows_image, windows_version): def setup_windows(windows_image, windows_version):
cmd = [ cmd = [
@ -201,28 +186,30 @@ def setup_windows(windows_image, windows_version):
cmd.extend(windows_image['Glob']) cmd.extend(windows_image['Glob'])
run_program(cmd) run_program(cmd)
def setup_windows_re(windows_version=None, windows_letter='W', tools_letter='T'): def setup_windows_re(windows_version, windows_letter='W', tools_letter='T'):
# Bail early win = r'{}:\Windows'.format(windows_letter)
if windows_version is None: winre = r'{}\System32\Recovery\WinRE.wim'.format(win)
raise Exception('Windows version not specified.') dest = r'{}:\Recovery\WindowsRE'.format(tools_letter)
_win = '{win}:\\Windows'.format(win=windows_letter) # Copy WinRE.wim
_winre = '{win}\\System32\\Recovery\\WinRE.wim'.format(win=_win) os.makedirs(dest, exist_ok=True)
_dest = '{tools}:\\Recovery\\WindowsRE'.format(tools=tools_letter) shutil.copy(winre, r'{}\WinRE.wim'.format(dest))
if re.search(r'^(8|10)', windows_version['Family']): # Set location
# Copy WinRE.wim cmd = [
os.makedirs(_dest, exist_ok=True) r'{}\System32\ReAgentc.exe'.format(win),
shutil.copy(_winre, '{dest}\\WinRE.wim'.format(dest=_dest)) '/setreimage',
'/path', dest,
# Set location '/target', win]
run_program('{win}\\System32\\reagentc /setreimage /path {dest} /target {win}'.format(dest=_dest, win=_win)) run_program(cmd)
else:
# Only supported on Windows 8 and above
raise SetupError
def update_boot_partition(system_letter='S', windows_letter='W', mode='ALL'): def update_boot_partition(system_letter='S', windows_letter='W', mode='ALL'):
run_program('bcdboot {win}:\\Windows /s {sys}: /f {mode}'.format(win=windows_letter, sys=system_letter, mode=mode)) cmd = [
r'{}:\Windows\System32\bcdboot.exe'.format(windows_letter),
r'{}:\Windows'.format(windows_letter),
'/s', '{}:'.format(system_letter),
'/f', mode]
run_program(cmd)
def wim_contains_image(filename, imagename): def wim_contains_image(filename, imagename):
cmd = [ cmd = [