Skip to content
Snippets Groups Projects
Commit 3f70f242 authored by Nikos Skalkotos's avatar Nikos Skalkotos
Browse files

Fix linux sysprep_acpid & remove output indents

parent e2cf65d3
No related branches found
No related tags found
No related merge requests found
......@@ -100,51 +100,47 @@ class Disk(object):
the Disk instance.
"""
puts("Examining source media `%s'" % self.source)
with indent(4):
sourcedev = self.source
mode = os.stat(self.source).st_mode
if stat.S_ISDIR(mode):
puts(colored.green('Looks like a directory'))
return self._losetup(self._dir_to_disk())
elif stat.S_ISREG(mode):
puts(colored.green('Looks like an image file'))
sourcedev = self._losetup(self.source)
elif not stat.S_ISBLK(mode):
raise ValueError("Invalid media source. Only block devices, "
"regular files and directories are supported.")
else:
puts(colored.green('Looks like a block device'))
#puts()
puts("Examining source media `%s'..." % self.source, False)
sourcedev = self.source
mode = os.stat(self.source).st_mode
if stat.S_ISDIR(mode):
puts(colored.green('looks like a directory'))
return self._losetup(self._dir_to_disk())
elif stat.S_ISREG(mode):
puts(colored.green('looks like an image file'))
sourcedev = self._losetup(self.source)
elif not stat.S_ISBLK(mode):
raise ValueError("Invalid media source. Only block devices, "
"regular files and directories are supported.")
else:
puts(colored.green('looks like a block device'))
# Take a snapshot and return it to the user
puts("Snapshotting media source")
with indent(4):
size = blockdev('--getsize', sourcedev)
cowfd, cow = tempfile.mkstemp()
os.close(cowfd)
self._add_cleanup(os.unlink, cow)
# Create 1G cow sparse file
dd('if=/dev/null', 'of=%s' % cow, 'bs=1k', \
'seek=%d' % (1024 * 1024))
cowdev = self._losetup(cow)
snapshot = uuid.uuid4().hex
tablefd, table = tempfile.mkstemp()
try:
os.write(tablefd, "0 %d snapshot %s %s n 8" % \
(int(size), sourcedev, cowdev))
dmsetup('create', snapshot, table)
self._add_cleanup(dmsetup, 'remove', snapshot)
# Sometimes dmsetup remove fails with Device or resource busy,
# although everything is cleaned up and the snapshot is not
# used by anyone. Add a 2 seconds delay to be on the safe side.
self._add_cleanup(time.sleep, 2)
finally:
os.unlink(table)
puts(colored.green('Done'))
# puts()
puts("Snapshotting media source...", False)
size = blockdev('--getsize', sourcedev)
cowfd, cow = tempfile.mkstemp()
os.close(cowfd)
self._add_cleanup(os.unlink, cow)
# Create 1G cow sparse file
dd('if=/dev/null', 'of=%s' % cow, 'bs=1k', \
'seek=%d' % (1024 * 1024))
cowdev = self._losetup(cow)
snapshot = uuid.uuid4().hex
tablefd, table = tempfile.mkstemp()
try:
os.write(tablefd, "0 %d snapshot %s %s n 8" % \
(int(size), sourcedev, cowdev))
dmsetup('create', snapshot, table)
self._add_cleanup(dmsetup, 'remove', snapshot)
# Sometimes dmsetup remove fails with Device or resource busy,
# although everything is cleaned up and the snapshot is not
# used by anyone. Add a 2 seconds delay to be on the safe side.
self._add_cleanup(time.sleep, 2)
finally:
os.unlink(table)
puts(colored.green('done'))
new_device = DiskDevice("/dev/mapper/%s" % snapshot)
self._devices.append(new_device)
new_device.enable()
......@@ -181,31 +177,27 @@ class DiskDevice(object):
def enable(self):
"""Enable a newly created DiskDevice"""
self.progressbar = progress_generator("Launching helper VM: ")
with indent(4):
self.progressbar.next()
eh = self.g.set_event_callback(self.progress_callback,
guestfs.EVENT_PROGRESS)
self.g.launch()
self.guestfs_enabled = True
self.g.delete_event_callback(eh)
if self.progressbar is not None:
self.progressbar.send(100)
self.progressbar = None
puts(colored.green('Done'))
puts('Inspecting Operating System')
with indent(4):
roots = self.g.inspect_os()
if len(roots) == 0:
raise FatalError("No operating system found")
if len(roots) > 1:
raise FatalError("Multiple operating systems found."
"We only support images with one filesystem.")
self.root = roots[0]
self.ostype = self.g.inspect_get_type(self.root)
self.distro = self.g.inspect_get_distro(self.root)
puts(colored.green('Found a %s system' % self.distro))
puts()
self.progressbar.next()
eh = self.g.set_event_callback(self.progress_callback,
guestfs.EVENT_PROGRESS)
self.g.launch()
self.guestfs_enabled = True
self.g.delete_event_callback(eh)
if self.progressbar is not None:
self.progressbar.send(100)
self.progressbar = None
puts('Inspecting Operating System...', False)
roots = self.g.inspect_os()
if len(roots) == 0:
raise FatalError("No operating system found")
if len(roots) > 1:
raise FatalError("Multiple operating systems found."
"We only support images with one filesystem.")
self.root = roots[0]
self.ostype = self.g.inspect_get_type(self.root)
self.distro = self.g.inspect_get_distro(self.root)
puts(colored.green('found a %s system' % self.distro))
def destroy(self):
"""Destroy this DiskDevice instance."""
......@@ -257,8 +249,8 @@ class DiskDevice(object):
disk and then updating the partition table. The new disk size
(in bytes) is returned.
"""
puts("Shrinking image (this may take a while)")
puts("Shrinking image (this may take a while)...", False)
dev = self.g.part_to_dev(self.root)
parttype = self.g.part_get_parttype(dev)
if parttype != 'msdos':
......@@ -277,26 +269,26 @@ class DiskDevice(object):
warn("Don't know how to resize %s partitions." % vfs_type)
return
with indent(4):
self.g.e2fsck_f(part_dev)
self.g.resize2fs_M(part_dev)
self.g.e2fsck_f(part_dev)
self.g.resize2fs_M(part_dev)
output = self.g.tune2fs_l(part_dev)
block_size = int(
filter(lambda x: x[0] == 'Block size', output)[0][1])
block_cnt = int(
filter(lambda x: x[0] == 'Block count', output)[0][1])
output = self.g.tune2fs_l(part_dev)
block_size = int(
filter(lambda x: x[0] == 'Block size', output)[0][1])
block_cnt = int(
filter(lambda x: x[0] == 'Block count', output)[0][1])
sector_size = self.g.blockdev_getss(dev)
sector_size = self.g.blockdev_getss(dev)
start = last_partition['part_start'] / sector_size
end = start + (block_size * block_cnt) / sector_size - 1
start = last_partition['part_start'] / sector_size
end = start + (block_size * block_cnt) / sector_size - 1
self.g.part_del(dev, last_partition['part_num'])
self.g.part_add(dev, 'p', start, end)
self.g.part_del(dev, last_partition['part_num'])
self.g.part_add(dev, 'p', start, end)
new_size = (end + 1) * sector_size
puts(colored.green("new image size is %dMB\n" % (new_size // 2 ** 20)))
new_size = (end + 1) * sector_size
puts(" New image size is %dMB\n" % (new_size // 2 ** 20))
return new_size
def size(self):
......
......@@ -159,6 +159,8 @@ def image_creator():
image_os = osclass(dev.root, dev.g)
metadata = image_os.get_metadata()
puts()
if options.sysprep:
image_os.sysprep()
......@@ -179,7 +181,6 @@ def image_creator():
f.close()
extract_image(dev.device, options.outfile, size)
finally:
puts('cleaning up...')
disk.cleanup()
......
......@@ -114,22 +114,32 @@ class OSBase(object):
"""Cleanup sensitive data out of the OS image."""
puts('Cleaning up sensitive data out of the OS image:')
with indent(4):
for name in dir(self):
attr = getattr(self, name)
if name.startswith('data_cleanup_') and callable(attr):
attr()
is_cleanup = lambda x: x.startswith('data_cleanup_') and \
callable(getattr(self, x))
tasks = [getattr(self, x) for x in dir(self) if is_cleanup(x)]
size = len(tasks)
cnt = 0
for task in tasks:
cnt += 1
puts(('(%d/%d)' % (cnt, size)).ljust(7), False)
task()
puts()
def sysprep(self):
"""Prepere system for image creation."""
puts('Preparing system for image creation:')
with indent(4):
for name in dir(self):
attr = getattr(self, name)
if name.startswith('sysprep_') and callable(attr):
attr()
is_sysprep = lambda x: x.startswith('sysprep_') and \
callable(getattr(self, x))
tasks = [getattr(self, x) for x in dir(self) if is_sysprep(x)]
size = len(tasks)
cnt = 0
for task in tasks:
cnt += 1
puts(('(%d/%d)' % (cnt, size)).ljust(7), False)
task()
puts()
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
......@@ -37,6 +37,7 @@ from image_creator.util import warn
from clint.textui import puts, indent
import re
import time
class Linux(Unix):
......@@ -57,42 +58,80 @@ class Linux(Unix):
self._uuid[dev] = attr[1]
return attr[1]
def sysprep_acpid(self):
def sysprep_acpid(self, print_header=True):
"""Replace acpid powerdown action scripts to automatically shutdown
the system without checking if a GUI is running.
"""
puts('* Fixing acpid powerdown action')
if print_header:
print 'Fixing acpid powerdown action'
action = '#!/bin/sh\n\nPATH=/sbin:/bin:/usr/bin\n shutdown -h now '
'\"Power button pressed\"'
powerbtn_action = '#!/bin/sh\n\nPATH=/sbin:/bin:/usr/bin\n' \
'shutdown -h now \"Power button pressed\"'
if self.g.is_file('/etc/acpi/powerbtn.sh'):
self.g.write('/etc/acpi/powerbtn.sh', action)
elif self.g.is_file('/etc/acpi/actions/power.sh'):
self.g.write('/etc/acpi/actions/power.sh', action)
else:
with indent(2):
warn("No acpid action file found")
events_dir = '/etc/acpi/events'
if not self.g.is_dir(events_dir):
warn("No acpid event directory found")
return
def sysprep_persistent_net_rules(self):
event_exp = re.compile('event=(.+)', re.I)
action_exp = re.compile('action=(.+)', re.I)
for f in self.g.readdir(events_dir):
if f['ftyp'] != 'r':
continue
fullpath = "%s/%s" % (events_dir, f['name'])
event = ""
action = ""
for line in self.g.cat(fullpath).splitlines():
m = event_exp.match(line)
if m:
event = m.group(1)
continue
m = action_exp.match(line)
if m:
action = m.group(1)
continue
if event.strip() == "button[ /]power":
if action:
if not self.g.is_file(action):
warn("Acpid action file: %s does not exist" % action)
return
self.g.copy_file_to_file(fullpath, \
"%s.orig.snf-image-creator-%d" % (fullpath, time.time()))
self.g.write(fullpath, powerbtn_action)
return
else:
warn("Acpid event file %s does not contain and action")
return
elif event.strip() == ".*":
warn("Found action `.*'. Don't know how to handle this." \
" Please edit \%s' image file manually to make the "
"system immediatelly shutdown when an power button acpi " \
"event occures")
return
def sysprep_persistent_net_rules(self, print_header=True):
"""Remove udev rules that will keep network interface names persistent
after hardware changes and reboots. Those rules will be created again
the next time the image runs.
"""
puts('* Removing persistent network interface names')
if print_header:
puts('Removing persistent network interface names')
rule_file = '/etc/udev/rules.d/70-persistent-net.rules'
if self.g.is_file(rule_file):
self.g.rm(rule_file)
def sysprep_persistent_devs(self):
def sysprep_persistent_devs(self, print_header=True):
"""Scan fstab and grub configuration files and replace all
non-persistent device appearences with UUIDs.
"""
puts('* Replacing fstab & grub non-persistent device appearences')
if print_header:
puts('Replacing fstab & grub non-persistent device appearences')
# convert all devices in fstab to persistent
persistent_root = self._persistent_fstab()
......
......@@ -71,43 +71,49 @@ class Unix(OSBase):
return users
def data_cleanup_cache(self):
def data_cleanup_cache(self, print_header=True):
"""Remove all regular files under /var/cache"""
puts('* Removing files under /var/cache')
if print_header:
puts('Removing files under /var/cache')
self.foreach_file('/var/cache', self.g.rm, ftype='r')
def data_cleanup_tmp(self):
def data_cleanup_tmp(self, print_header=True):
"""Remove all files under /tmp and /var/tmp"""
puts('* Removing files under /tmp and /var/tmp')
if print_header:
puts('Removing 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)
def data_cleanup_log(self):
def data_cleanup_log(self, print_header=True):
"""Empty all files under /var/log"""
puts('* Emptying all files under /var/log')
if print_header:
puts('Emptying all files under /var/log')
self.foreach_file('/var/log', self.g.truncate, ftype='r')
def data_cleanup_mail(self):
def data_cleanup_mail(self, print_header=True):
"""Remove all files under /var/mail and /var/spool/mail"""
puts('* Removing files under /var/mail and /var/spool/mail')
if print_header:
puts('Removing files under /var/mail and /var/spool/mail')
self.foreach_file('/var/spool/mail', self.g.rm_rf, maxdepth=1)
self.foreach_file('/var/mail', self.g.rm_rf, maxdepth=1)
def data_cleanup_userdata(self):
def data_cleanup_userdata(self, print_header=True):
"""Delete sensitive userdata"""
homedirs = ['/root'] + self.ls('/home/')
if print_header:
puts('Removing sensitive user data under %s' % " ".join(homedirs))
for homedir in homedirs:
puts('* Removing sensitive user data under %s' % homedir)
for data in self.sensitive_userdata:
fname = "%s/%s" % (homedir, data)
if self.g.is_file(fname):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment