X-Git-Url: https://code.grnet.gr/git/snf-image-creator/blobdiff_plain/979096ddfddb0cc13151c7e223be64e80ec8db44..8bae613f329a2e5c6765914e51a7e1a1dc90f983:/image_creator/os_type/unix.py diff --git a/image_creator/os_type/unix.py b/image_creator/os_type/unix.py index 75c4797..9fe2c50 100644 --- a/image_creator/os_type/unix.py +++ b/image_creator/os_type/unix.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- +# # Copyright 2012 GRNET S.A. All rights reserved. # # Redistribution and use in source and binary forms, with or @@ -31,92 +33,102 @@ # interpreted as representing official policies, either expressed # or implied, of GRNET S.A. +"""This module hosts OS-specific code common to all Unix-like OSs.""" + import re -import sys -from image_creator.os_type import OSBase -from image_creator.util import warn, output +from image_creator.os_type import OSBase, sysprep class Unix(OSBase): - + """OS class for Unix""" sensitive_userdata = [ + '.history', '.bash_history', '.gnupg', '.ssh', - '.mozilla', - '.thunderbird' + '.kamakirc', + '.kamaki.history' ] - def get_metadata(self): - meta = super(Unix, self).get_metadata() - meta["USERS"] = " ".join(self.get_passworded_users()) - return meta + def _mountpoints(self): + """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) + + def compare(a, b): + if len(a[0]) > len(b[0]): + return 1 + elif len(a[0]) == len(b[0]): + return 0 + else: + return -1 + mps.sort(compare) - def get_passworded_users(self): - users = [] - regexp = re.compile('(\S+):((?:!\S+)|(?:[^!*]\S+)|):(?:\S*:){6}') + for mp in mps: + yield mp - for line in self.g.cat('/etc/shadow').splitlines(): - match = regexp.match(line) - if not match: - continue + def _do_mount(self, readonly): + """Mount partitions in the correct order""" - user, passwd = match.groups() - if len(passwd) > 0 and passwd[0] == '!': - warn("Ignoring locked %s account." % user) - else: - users.append(user) + critical_mpoints = ('/', '/etc', '/root', '/home', '/var') - return users + mopts = 'ro' if readonly else 'rw' + for mp, dev in self._mountpoints(): + try: + self.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)) + return False + else: + self.out.warn('%s (ignored)' % msg) - def data_cleanup_cache(self, print_header=True): - """Remove all regular files under /var/cache""" + return True - if print_header: - output('Removing files under /var/cache') + @sysprep('Removing files u)nder /var/cache') + 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.g.rm, ftype='r') - def data_cleanup_tmp(self, print_header=True): + @sysprep('Removing files under /tmp and /var/tmp') + def cleanup_tmp(self): """Remove all files under /tmp and /var/tmp""" - if print_header: - output('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) + 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, print_header=True): + @sysprep('Emptying all files under /var/log') + def cleanup_log(self): """Empty all files under /var/log""" - if print_header: - output('Emptying all files under /var/log') + self._foreach_file('/var/log', self.g.truncate, ftype='r') - self.foreach_file('/var/log', self.g.truncate, ftype='r') - - def data_cleanup_mail(self, print_header=True): + @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 print_header: - output('Removing 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) - self.foreach_file('/var/spool/mail', self.g.rm_rf, maxdepth=1) - self.foreach_file('/var/mail', self.g.rm_rf, maxdepth=1) + self._foreach_file('/var/mail', self.g.rm_rf, maxdepth=1) - def data_cleanup_userdata(self, print_header=True): + @sysprep('Removing sensitive user data') + def cleanup_userdata(self): """Delete sensitive userdata""" - homedirs = ['/root'] + self.ls('/home/') - - if print_header: - output('Removing sensitive user data under %s' % " ". - join(homedirs)) + homedirs = ['/root'] + if self.g.is_dir('/home/'): + homedirs += self._ls('/home/') for homedir in homedirs: for data in self.sensitive_userdata: fname = "%s/%s" % (homedir, data) if self.g.is_file(fname): self.g.scrub_file(fname) + elif self.g.is_dir(fname): + self._foreach_file(fname, self.g.scrub_file, ftype='r') # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :