self.image = image
self.root = image.root
- self.g = image.g
self.out = image.out
self.needed_sysprep_params = {}
# Many guestfs compilations don't support scrub
self._scrub_support = True
try:
- self.g.available(['scrub'])
+ self.image.g.available(['scrub'])
except RuntimeError:
self._scrub_support = False
"""Umount all mounted filesystems."""
self.out.output("Umounting the media ...", False)
- self.g.umount_all()
+ self.image.g.umount_all()
self.mounted = False
self.out.success('done')
@add_prefix
def _ls(self, directory):
"""List the name of all files under a directory"""
- return self.g.ls(directory)
+ return self.image.g.ls(directory)
@add_prefix
def _find(self, directory):
"""List the name of all files recursively under a directory"""
- return self.g.find(directory)
+ return self.image.g.find(directory)
def _foreach_file(self, directory, action, **kargs):
"""Perform an action recursively on all files under a directory.
ftype = None if 'ftype' not in kargs else kargs['ftype']
has_ftype = lambda x, y: y is None and True or x['ftyp'] == y
- for f in self.g.readdir(directory):
+ for f in self.image.g.readdir(directory):
if f['name'] in ('.', '..'):
continue
def _do_collect_metadata(self):
"""helper method for collect_metadata"""
- self.meta['ROOT_PARTITION'] = "%d" % self.g.part_to_partnum(self.root)
- self.meta['OSFAMILY'] = self.g.inspect_get_type(self.root)
- self.meta['OS'] = self.g.inspect_get_distro(self.root)
+ self.meta['ROOT_PARTITION'] = \
+ "%d" % self.image.g.part_to_partnum(self.root)
+ self.meta['OSFAMILY'] = self.image.g.inspect_get_type(self.root)
+ self.meta['OS'] = self.image.g.inspect_get_distro(self.root)
if self.meta['OS'] == "unknown":
self.meta['OS'] = self.meta['OSFAMILY']
- self.meta['DESCRIPTION'] = self.g.inspect_get_product_name(self.root)
+ self.meta['DESCRIPTION'] = self.image.g.inspect_get_product_name(self.root)
def _do_mount(self, readonly):
"""helper method for mount"""
try:
- self.g.mount_options('ro' if readonly else 'rw', self.root, '/')
+ self.image.g.mount_options(
+ 'ro' if readonly else 'rw', self.root, '/')
except RuntimeError as msg:
self.out.warn("unable to mount the root partition: %s" % msg)
return False
master_passwd = []
- for line in self.g.cat('/etc/master.passwd').splitlines():
+ for line in self.image.g.cat('/etc/master.passwd').splitlines():
# Check for empty or comment lines
if len(line.split('#')[0]) == 0:
master_passwd.append(":".join(fields))
- self.g.write('/etc/master.passwd', "\n".join(master_passwd) + '\n')
+ self.image.g.write(
+ '/etc/master.passwd', "\n".join(master_passwd) + '\n')
# Make sure no one can login on the system
- self.g.rm_rf('/etc/spwd.db')
+ self.image.g.rm_rf('/etc/spwd.db')
def _do_collect_metadata(self):
"""Collect metadata about the OS"""
'^([^:]+):((?:![^:]+)|(?:[^!*][^:]+)|):(?:[^:]*:){7}(?:[^:]*)'
)
- for line in self.g.cat('/etc/master.passwd').splitlines():
+ for line in self.image.g.cat('/etc/master.passwd').splitlines():
line = line.split('#')[0]
match = regexp.match(line)
if not match:
group3 = int(match.group(3))
dev = '/dev/sd%c%d' % (chr(ord('a') + group2), group3)
try:
- self.g.mount_vfs(mopts, 'ufs', dev, mp)
+ self.image.g.mount_vfs(mopts, 'ufs', dev, mp)
except RuntimeError as msg:
if mp in critical_mpoints:
self.out.warn('unable to mount %s. Reason: %s' % (mp, msg))
passwd = []
removed_users = {}
metadata_users = self.meta['USERS'].split()
- for line in self.g.cat('/etc/passwd').splitlines():
+ for line in self.image.g.cat('/etc/passwd').splitlines():
fields = line.split(':')
if int(fields[2]) > 1000:
removed_users[fields[0]] = fields
if not len(self.meta['USERS']):
del self.meta['USERS']
- self.g.write('/etc/passwd', '\n'.join(passwd) + '\n')
+ self.image.g.write('/etc/passwd', '\n'.join(passwd) + '\n')
# Remove the corresponding /etc/shadow entries
shadow = []
- for line in self.g.cat('/etc/shadow').splitlines():
+ for line in self.image.g.cat('/etc/shadow').splitlines():
fields = line.split(':')
if fields[0] not in removed_users:
shadow.append(':'.join(fields))
- self.g.write('/etc/shadow', "\n".join(shadow) + '\n')
+ self.image.g.write('/etc/shadow', "\n".join(shadow) + '\n')
# Remove the corresponding /etc/group entries
group = []
- for line in self.g.cat('/etc/group').splitlines():
+ for line in self.image.g.cat('/etc/group').splitlines():
fields = line.split(':')
# Remove groups tha have the same name as the removed users
if fields[0] not in removed_users:
group.append(':'.join(fields))
- self.g.write('/etc/group', '\n'.join(group) + '\n')
+ self.image.g.write('/etc/group', '\n'.join(group) + '\n')
# Remove home directories
for home in [field[5] for field in removed_users.values()]:
- if self.g.is_dir(home) and home.startswith('/home/'):
- self.g.rm_rf(home)
+ if self.image.g.is_dir(home) and home.startswith('/home/'):
+ self.image.g.rm_rf(home)
@sysprep('Cleaning up password & locking all user accounts')
def cleanup_passwords(self):
shadow = []
- for line in self.g.cat('/etc/shadow').splitlines():
+ for line in self.image.g.cat('/etc/shadow').splitlines():
fields = line.split(':')
if fields[1] not in ('*', '!'):
fields[1] = '!'
shadow.append(":".join(fields))
- self.g.write('/etc/shadow', "\n".join(shadow) + '\n')
+ self.image.g.write('/etc/shadow', "\n".join(shadow) + '\n')
@sysprep('Fixing acpid powerdown action')
def fix_acpid(self):
'shutdown -h now "Power button pressed"\n'
events_dir = '/etc/acpi/events'
- if not self.g.is_dir(events_dir):
+ if not self.image.g.is_dir(events_dir):
self.out.warn("No acpid event directory found")
return
event_exp = re.compile('event=(.+)', re.I)
action_exp = re.compile('action=(.+)', re.I)
- for events_file in self.g.readdir(events_dir):
+ for events_file in self.image.g.readdir(events_dir):
if events_file['ftyp'] != 'r':
continue
fullpath = "%s/%s" % (events_dir, events_file['name'])
event = ""
action = ""
- for line in self.g.cat(fullpath).splitlines():
+ for line in self.image.g.cat(fullpath).splitlines():
match = event_exp.match(line)
if match:
event = match.group(1)
if event.strip() in ("button[ /]power", "button/power.*"):
if action:
- if not self.g.is_file(action):
+ if not self.image.g.is_file(action):
self.out.warn("Acpid action file: %s does not exist" %
action)
return
- self.g.copy_file_to_file(action,
+ self.image.g.copy_file_to_file(action,
"%s.orig.snf-image-creator-%d" %
(action, time.time()))
- self.g.write(action, powerbtn_action)
+ self.image.g.write(action, powerbtn_action)
return
else:
self.out.warn("Acpid event file %s does not contain and "
"""
rule_file = '/etc/udev/rules.d/70-persistent-net.rules'
- if self.g.is_file(rule_file):
- self.g.rm(rule_file)
+ if self.image.g.is_file(rule_file):
+ self.image.g.rm(rule_file)
@sysprep('Removing swap entry from fstab')
def remove_swap_entry(self):
"""
new_fstab = ""
- fstab = self.g.cat('/etc/fstab')
+ fstab = self.image.g.cat('/etc/fstab')
for line in fstab.splitlines():
entry = line.split('#')[0].strip().split()
new_fstab += "%s\n" % line
- self.g.write('/etc/fstab', new_fstab)
+ self.image.g.write('/etc/fstab', new_fstab)
@sysprep('Replacing fstab & grub non-persistent device references')
def use_persistent_block_device_names(self):
"""Replaces non-persistent device name occurencies with persistent
ones in GRUB1 configuration files.
"""
- if self.g.is_file('/boot/grub/menu.lst'):
+ if self.image.g.is_file('/boot/grub/menu.lst'):
grub1 = '/boot/grub/menu.lst'
- elif self.g.is_file('/etc/grub.conf'):
+ elif self.image.g.is_file('/etc/grub.conf'):
grub1 = '/etc/grub.conf'
else:
return
- self.g.aug_init('/', 0)
+ self.image.g.aug_init('/', 0)
try:
- roots = self.g.aug_match('/files%s/title[*]/kernel/root' % grub1)
+ roots = self.image.g.aug_match(
+ '/files%s/title[*]/kernel/root' % grub1)
for root in roots:
- dev = self.g.aug_get(root)
+ dev = self.image.g.aug_get(root)
if not self._is_persistent(dev):
# This is not always correct. Grub may contain root entries
# for other systems, but we only support 1 OS per hard
# disk, so this shouldn't harm.
- self.g.aug_set(root, new_root)
+ self.image.g.aug_set(root, new_root)
finally:
- self.g.aug_save()
- self.g.aug_close()
+ self.image.g.aug_save()
+ self.image.g.aug_close()
def _persistent_fstab(self):
"""Replaces non-persistent device name occurencies in /etc/fstab with
persistent ones.
"""
- mpoints = self.g.mountpoints()
+ mpoints = self.image.g.mountpoints()
if len(mpoints) == 0:
pass # TODO: error handling
root_dev = None
new_fstab = ""
- fstab = self.g.cat('/etc/fstab')
+ fstab = self.image.g.cat('/etc/fstab')
for line in fstab.splitlines():
line, dev, mpoint = self._convert_fstab_line(line, device_dict)
if mpoint == '/':
root_dev = dev
- self.g.write('/etc/fstab', new_fstab)
+ self.image.g.write('/etc/fstab', new_fstab)
if root_dev is None:
pass # TODO: error handling
users = []
regexp = re.compile(r'(\S+):((?:!\S+)|(?:[^!*]\S+)|):(?:\S*:){6}')
- for line in self.g.cat('/etc/shadow').splitlines():
+ for line in self.image.g.cat('/etc/shadow').splitlines():
match = regexp.match(line)
if not match:
continue
if dev in self._uuid:
return self._uuid[dev]
- uuid = self.g.vfs_uuid(dev)
+ uuid = self.image.g.vfs_uuid(dev)
assert len(uuid)
self._uuid[dev] = uuid
return uuid
# In slackware the metadata about installed packages are
# stored in /var/log/packages. Clearing all /var/log files
# will destroy the package management system.
- self._foreach_file('/var/log', self.g.truncate, ftype='r',
+ self._foreach_file('/var/log', self.image.g.truncate, ftype='r',
exclude='/var/log/packages')
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
"""Collect metadata about the OS"""
super(Ubuntu, self)._do_collect_metadata()
- apps = self.g.inspect_list_applications(self.root)
+ apps = self.image.g.inspect_list_applications(self.root)
for app in apps:
if app['app_name'] == 'kubuntu-desktop':
self.meta['OS'] = 'kubuntu'
"""Return mountpoints in the correct order.
/ should be mounted before /boot or /usr, /usr befor /usr/bin ...
"""
- mps = self.g.inspect_get_mountpoints(self.root)
+ mps = self.image.g.inspect_get_mountpoints(self.root)
def compare(a, b):
if len(a[0]) > len(b[0]):
mopts = 'ro' if readonly else 'rw'
for mp, dev in self._mountpoints():
try:
- self.g.mount_options(mopts, dev, mp)
+ self.image.g.mount_options(mopts, dev, mp)
except RuntimeError as msg:
if mp in critical_mpoints:
self.out.warn('unable to mount %s. Reason: %s' % (mp, msg))
def cleanup_cache(self):
"""Remove all regular files under /var/cache"""
- self._foreach_file('/var/cache', self.g.rm, ftype='r')
+ self._foreach_file('/var/cache', self.image.g.rm, ftype='r')
@sysprep('Removing files under /tmp and /var/tmp')
def cleanup_tmp(self):
"""Remove all files under /tmp and /var/tmp"""
- self._foreach_file('/tmp', self.g.rm_rf, maxdepth=1)
- self._foreach_file('/var/tmp', self.g.rm_rf, maxdepth=1)
+ self._foreach_file('/tmp', self.image.g.rm_rf, maxdepth=1)
+ self._foreach_file('/var/tmp', self.image.g.rm_rf, maxdepth=1)
@sysprep('Emptying all files under /var/log')
def cleanup_log(self):
"""Empty all files under /var/log"""
- self._foreach_file('/var/log', self.g.truncate, ftype='r')
+ self._foreach_file('/var/log', self.image.g.truncate, ftype='r')
@sysprep('Removing files under /var/mail & /var/spool/mail', enabled=False)
def cleanup_mail(self):
"""Remove all files under /var/mail and /var/spool/mail"""
- if self.g.is_dir('/var/spool/mail'):
- self._foreach_file('/var/spool/mail', self.g.rm_rf, maxdepth=1)
+ if self.image.g.is_dir('/var/spool/mail'):
+ self._foreach_file('/var/spool/mail', self.image.g.rm_rf,
+ maxdepth=1)
- self._foreach_file('/var/mail', self.g.rm_rf, maxdepth=1)
+ self._foreach_file('/var/mail', self.image.g.rm_rf, maxdepth=1)
@sysprep('Removing sensitive user data')
def cleanup_userdata(self):
"""Delete sensitive userdata"""
homedirs = ['/root']
- if self.g.is_dir('/home/'):
+ if self.image.g.is_dir('/home/'):
homedirs += self._ls('/home/')
- action = self.g.rm_rf
+ action = self.image.g.rm_rf
if self._scrub_support:
- action = self.g.scrub_file
+ action = self.image.g.scrub_file
else:
self.out.warn("Sensitive data won't be scrubbed (not supported)")
for homedir in homedirs:
for data in self.sensitive_userdata:
fname = "%s/%s" % (homedir, data)
- if self.g.is_file(fname):
+ if self.image.g.is_file(fname):
action(fname)
- elif self.g.is_dir(fname):
+ elif self.image.g.is_dir(fname):
self._foreach_file(fname, action, ftype='r')
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
def __init__(self, image, **kargs):
super(Windows, self).__init__(image, **kargs)
- device = self.g.part_to_dev(self.root)
+ device = self.image.g.part_to_dev(self.root)
- self.last_part_num = self.g.part_list(device)[-1]['part_num']
+ self.last_part_num = self.image.g.part_list(device)[-1]['part_num']
self.last_drive = None
self.system_drive = None
- for drive, partition in self.g.inspect_get_drive_mappings(self.root):
+ for drive, partition in self.image.g.inspect_get_drive_mappings(self.root):
if partition == "%s%d" % (device, self.last_part_num):
self.last_drive = drive
if partition == self.root:
assert self.system_drive
- self.product_name = self.g.inspect_get_product_name(self.root)
+ self.product_name = self.image.g.inspect_get_product_name(self.root)
self.syspreped = False
@sysprep('Disabling IPv6 privacy extensions')
firewall_states = self._update_firewalls(0, 0, 0)
# Delete the pagefile. It will be recreated when the system boots
- systemroot = self.g.inspect_get_windows_systemroot(self.root)
+ systemroot = self.image.g.inspect_get_windows_systemroot(self.root)
try:
pagefile = "%s/pagefile.sys" % systemroot
- self.g.rm_rf(self.g.case_sensitive_path(pagefile))
+ self.image.g.rm_rf(self.image.g.case_sensitive_path(pagefile))
except RuntimeError:
pass
self.umount()
self.out.output("Shutting down helper VM ...", False)
- self.g.sync()
+ self.image.g.sync()
# guestfs_shutdown which is the prefered way to shutdown the backend
# process was introduced in version 1.19.16
if check_guestfs_version(self.g, 1, 19, 16) >= 0:
- self.g.shutdown()
+ self.image.g.shutdown()
else:
- self.g.kill_subprocess()
+ self.image.g.kill_subprocess()
self.out.success('done')
finally:
self.out.output("Relaunching helper VM (may take a while) ...",
False)
- self.g.launch()
+ self.image.g.launch()
self.out.success('done')
self.mount(readonly=False)
def _registry_file_path(self, regfile):
"""Retrieves the case sensitive path to a registry file"""
- systemroot = self.g.inspect_get_windows_systemroot(self.root)
+ systemroot = self.image.g.inspect_get_windows_systemroot(self.root)
path = "%s/system32/config/%s" % (systemroot, regfile)
try:
- path = self.g.case_sensitive_path(path)
+ path = self.image.g.case_sensitive_path(path)
except RuntimeError as error:
raise FatalError("Unable to retrieve registry file: %s. Reason: %s"
% (regfile, str(error)))
softwarefd, software = tempfile.mkstemp()
try:
os.close(softwarefd)
- self.g.download(path, software)
+ self.image.g.download(path, software)
h = hivex.Hivex(software, write=True)
h.commit(None)
- self.g.upload(software, path)
+ self.image.g.upload(software, path)
finally:
os.unlink(software)
systemfd, system = tempfile.mkstemp()
try:
os.close(systemfd)
- self.g.download(path, system)
+ self.image.g.download(path, system)
h = hivex.Hivex(system, write=True)
'value': struct.pack("<I", new_values.pop(0))})
h.commit(None)
- self.g.upload(system, path)
+ self.image.g.upload(system, path)
finally:
os.unlink(system)
softwarefd, software = tempfile.mkstemp()
try:
os.close(softwarefd)
- self.g.download(path, software)
+ self.image.g.download(path, software)
h = hivex.Hivex(software, write=True)
h.node_set_value(key, new_value)
h.commit(None)
- self.g.upload(software, path)
+ self.image.g.upload(software, path)
finally:
os.unlink(software)
samfd, sam = tempfile.mkstemp()
try:
os.close(samfd)
- self.g.download(self._registry_file_path('SAM'), sam)
+ self.image.g.download(self._registry_file_path('SAM'), sam)
h = hivex.Hivex(sam)