Commit 5e0f6772 authored by Nikos Skalkotos's avatar Nikos Skalkotos

windows: Cleanup the VirtIO driver installation

Add a mechanism in OSBase to add cleanup jobs. Use this mechanism to
cleanup the VirtIO drivers installation.
parent 69730308
......@@ -221,6 +221,29 @@ class OSBase(object):
except RuntimeError:
self._scrub_support = False
self._cleanup_jobs = {}
def _add_cleanup(self, namespace, job, *args):
"""Add a new job in a cleanup list"""
if namespace not in self._cleanup_jobs:
self._cleanup_jobs[namespace] = []
self._cleanup_jobs[namespace].append((job, args))
def _cleanup(self, namespace):
"""Run the cleanup tasks that are defined under a specific namespace"""
if namespace not in self._cleanup_jobs:
self.out.warn("Cleanup namespace: `%s' is not defined", namespace)
return
while len(self._cleanup_jobs[namespace]):
job, args = self._cleanup_jobs[namespace].pop()
job(*args)
del self._cleanup_jobs[namespace]
def inspect(self):
"""Inspect the media to check if it is supported"""
......
......@@ -24,7 +24,8 @@ from image_creator.os_type.windows.vm import VM, RANDOM_TOKEN as TOKEN
from image_creator.os_type.windows.registry import Registry
from image_creator.os_type.windows.winexe import WinEXE
from image_creator.os_type.windows.powershell import DRVINST_HEAD, SAFEBOOT, \
DRVINST_TAIL, ADD_CERTIFICATE, ADD_DRIVER, INSTALL_DRIVER, REMOVE_DRIVER
DRVINST_TAIL, ADD_CERTIFICATE, ADD_DRIVER, INSTALL_DRIVER, REMOVE_DRIVER, \
DISABLE_AUTOLOGON
import tempfile
import re
......@@ -775,26 +776,45 @@ class Windows(OSBase):
self.out.warn('No suitable driver found to install!')
return
self._upload_virtio_drivers(dirname, valid_drvs, upgrade)
self._boot_virtio_vm()
try:
self._upload_virtio_drivers(dirname, valid_drvs, upgrade)
self._boot_virtio_vm()
finally:
with self.mount(readonly=False, silent=True, fatal=False):
if not self.ismounted:
self.out.warn("The boot changes cannot be reverted. "
"The image may be in a corrupted state.")
else:
self._cleanup('virtio')
self.out.output("VirtIO drivers were successfully installed")
self.out.output()
def _upload_virtio_drivers(self, dirname, drvs, delete_old=True):
"""Upload the VirtIO drivers and installation scripts to the media.
The functions returns the temporary directory under %SystemRoot% that
hosts the uploaded drivers.
"""
with self.mount(readonly=False, silent=True):
admin = self.sysprep_params['admin'].value
v_val = self.registry.reset_passwd(admin)
activated = self.registry.reset_account(admin)
self._add_cleanup('virtio',
self.registry.reset_passwd, admin, v_val)
active = self.registry.reset_account(admin)
self._add_cleanup('virtio',
self.registry.reset_account, admin, active)
# We disable this with powershell scripts
self.registry.enable_autologon(admin)
self.registry.reset_first_logon_animation(False)
active = self.registry.reset_first_logon_animation(False)
self._add_cleanup(
'virtio', self.registry.reset_first_logon_animation, active)
tmp = uuid.uuid4().hex
self.image.g.mkdir_p("%s/%s" % (self.systemroot, tmp))
self._add_cleanup('virtio', self.image.g.rm_rf,
"%s/%s" % (self.systemroot, tmp))
for fname in os.listdir(dirname):
full_path = os.path.join(dirname, fname)
......@@ -802,8 +822,6 @@ class Windows(OSBase):
self.image.g.upload(
full_path, "%s/%s/%s" % (self.systemroot, tmp, fname))
self.registry.update_devices_dirs("%SystemRoot%\\" + tmp)
drvs_install = DRVINST_HEAD
for dtype in drvs:
......@@ -820,6 +838,10 @@ class Windows(OSBase):
if self.check_version(6, 1) <= 0:
self._install_viostor_driver(dirname)
old = self.registry.update_devices_dirs("%SystemRoot%\\" + tmp)
self._add_cleanup(
'virtio', self.registry.update_devices_dirs, old, False)
drvs_install += DISABLE_AUTOLOGON
else:
# In newer windows, in order to reduce the boot process the
# boot drivers are cached. To be able to boot with viostor, we
......@@ -849,8 +871,6 @@ class Windows(OSBase):
# (*) to force the program to run even in Safe mode.
self.registry.runonce({'*InstallDrivers': cmd})
return tmp
def _boot_virtio_vm(self):
"""Boot the media and install the VirtIO drivers"""
......
......@@ -75,13 +75,45 @@ DRVINST_TAIL = COM1_WRITE + """
shutdown /s /t 0
"""
DISABLE_AUTOLOGON = r"""
Remove-ItemProperty -Path `
'HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\' `
DefaultUserName
Remove-ItemProperty -Path `
'HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\' `
DefaultPassword
Remove-ItemProperty -Path `
'HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\' `
AutoAdminLogon
"""
# Reboots system in safe mode
SAFEBOOT = r"""
bcdedit /set safeboot minimal
$winlogon = 'HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'
New-ItemProperty `
-Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce `
-Name *1snf-image-creator-safeboot -PropertyType String `
-Value 'bcdedit /deletevalue safeboot'
New-ItemProperty `
-Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce `
-Name *2snf-image-creator-safeboot -PropertyType String `
-Value "reg delete `"$winlogon`" /v DefaultUserName /f"
New-ItemProperty `
-Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce `
-Name *3snf-image-creator-safeboot -PropertyType String `
-Value "reg delete `"$winlogon`" /v DefaultPassword /f"
New-ItemProperty `
-Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce `
-Name *4snf-image-creator-safeboot -PropertyType String `
-Value "reg delete `"$winlogon`" /v AutoAdminLogon /f"
New-ItemProperty `
-Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce `
-Name *snf-image-creator-restart -PropertyType String `
-Value 'cmd /q /c "bcdedit /deletevalue safeboot & shutdown /s /t 0"'
-Name *5snf-image-creator-safeboot -PropertyType String `
-Value 'shutdown /s /t 5'
"""
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment