1 from image_creator.os_type.unix import Unix
6 def __init__(self, rootdev, ghandler):
7 super(Linux, self).__init__(rootdev, ghandler)
9 self._persistent = re.compile('/dev/[hsv]d[a-z][1-9]*')
11 def is_persistent(self, dev):
12 return not self._persistent.match(dev)
14 def get_uuid(self, dev):
16 return self._uuid[dev]
18 for attr in self.g.blkid(dev):
20 self._uuid[dev] = attr[1]
24 """Prepere system for image creation."""
26 self.sysprep_persistent_net_rules()
27 self.sysprep_persistent_devs()
29 def sysprep_acpid(self):
30 """Replace acpid powerdown action scripts to automatically shutdown
31 the system without checking if a GUI is running.
33 action = '#!/bin/sh\n\nPATH=/sbin:/bin:/usr/bin\n shutdown -h now '
34 '\"Power button pressed\"'
36 if self.g.is_file('/etc/acpi/powerbtn.sh'):
37 self.g.write(action, '/etc/acpi/powerbtn.sh')
38 elif self.g.is_file('/etc/acpi/actions/power.sh'):
39 self.g.write(actions, '/etc/acpi/actions/power.sh')
41 print "Warning: No acpid action file found"
43 def sysprep_persistent_net_rules(self):
44 """Remove udev rules that will keep network interface names persistent
45 after hardware changes and reboots. Those rules will be created again
46 the next time the image runs.
48 rule_file = '/etc/udev/rules.d/70-persistent-net.rules'
49 if self.g.is_file(rule_file):
52 def sysprep_persistent_devs(self):
53 """Scan fstab and grub configuration files and replace all
54 non-persistent device appearences with UUIDs.
56 # convert all devices in fstab to persistent
57 persistent_root = self._persistent_fstab()
59 # convert all devices in grub1 to persistent
60 self._persistent_grub1(persistent_root)
62 def _persistent_grub1(self, new_root):
63 if self.g.is_file('/boot/grub/menu.lst'):
64 grub1 = '/boot/grub/menu.lst'
65 elif self.g.is_file('/etc/grub.conf'):
66 grub1 = '/etc/grub.conf'
70 self.g.aug_init('/', 0)
72 roots = self.g.aug_match('/files%s/title[*]/kernel/root' % grub1)
74 dev = self.g.aug_get(root)
75 if not self.is_persistent(dev):
76 # This is not always correct. Grub may contain root entries
77 # for other systems, but we only support 1 OS per hard
78 # disk, so this shouldn't harm.
79 self.g.aug_set(root, new_root)
84 def _persistent_fstab(self):
85 mpoints = self.g.mountpoints()
87 pass # TODO: error handling
89 device_dict = dict([[mpoint, dev] for dev, mpoint in mpoints])
93 fstab = self.g.cat('/etc/fstab')
94 for line in fstab.splitlines():
96 line, dev, mpoint = self._convert_fstab_line(line, device_dict)
97 new_fstab += "%s\n" % line
102 self.g.write('/etc/fstab', new_fstab)
104 pass # TODO: error handling
108 def _convert_fstab_line(self, line, devices):
110 line = line.split('#')[0].strip()
116 print "Warning: detected abnorman entry in fstab"
122 if not self.is_persistent(dev):
123 if mpoint in devices:
124 dev = "UUID=%s" % self.get_uuid(devices[mpoint])
127 # comment out the entry
128 entry[0] = "#%s" % dev
129 return " ".join(entry), dev, mpoint
131 return orig, dev, mpoint
133 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :