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]
23 def convert_to_persistent_dev(self):
24 # convert all devices in fstab to persistent
25 persistent_root = self._persistent_fstab()
27 # convert all devices in grub1 to persistent
28 self._persistent_grub1(persistent_root)
30 def _persistent_grub1(self, conf, new_root):
31 if self.g.is_file('/boot/grub/menu.lst'):
32 grub1 = '/boot/grub/menu.lst'
33 elif self.g.is_file('/etc/grub.conf'):
34 grub1 = '/etc/grub.conf'
38 self.g.aug_init('/', 0)
40 roots = self.g.aug_match('/files%s/title[*]/kernel/root' % grub1)
42 dev = self.g.aug_get(root)
43 if not self.is_persistent(dev):
44 # This is not always correct. Grub may contain root entries
45 # for other systems, but we only support 1 OS per hard
46 # disk, so this shouldn't harm.
47 self.g.aug_set(root, new_root)
52 def _persistent_fstab(self):
53 mpoints = self.g.mountpoints()
55 pass # TODO: error handling
57 device_dict = dict([[mpoint, dev] for dev, mpoint in mpoints])
61 fstab = self.g.cat('/etc/fstab')
62 for line in fstab.splitlines():
64 line, dev, mpoint = self._convert_fstab_line(line, device_dict)
65 new_fstab += "%s\n" % line
70 self.g.write('/etc/fstab', new_fstab)
72 pass # TODO: error handling
76 def _convert_fstab_line(self, line, devices):
78 line = line.split('#')[0].strip()
84 print "Warning: detected abnorman entry in fstab"
90 if not self.is_persistent(dev):
92 dev = "UUID=%s" % self.get_uuid(devices[mpoint])
95 # comment out the entry
96 entry[0] = "#%s" % dev
97 return " ".join(entry), dev, mpoint
99 return orig, dev, mpoint
101 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :