1 # Copyright 2012 GRNET S.A. All rights reserved.
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
7 # 1. Redistributions of source code must retain the above
8 # copyright notice, this list of conditions and the following
11 # 2. Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following
13 # disclaimer in the documentation and/or other materials
14 # provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
29 # The views and conclusions contained in the software and
30 # documentation are those of the authors and should not be
31 # interpreted as representing official policies, either expressed
32 # or implied, of GRNET S.A.
34 from image_creator.os_type.unix import Unix
39 def __init__(self, rootdev, ghandler):
40 super(Linux, self).__init__(rootdev, ghandler)
42 self._persistent = re.compile('/dev/[hsv]d[a-z][1-9]*')
44 def is_persistent(self, dev):
45 return not self._persistent.match(dev)
47 def get_uuid(self, dev):
49 return self._uuid[dev]
51 for attr in self.g.blkid(dev):
53 self._uuid[dev] = attr[1]
57 """Prepere system for image creation."""
59 self.sysprep_persistent_net_rules()
60 self.sysprep_persistent_devs()
62 def sysprep_acpid(self):
63 """Replace acpid powerdown action scripts to automatically shutdown
64 the system without checking if a GUI is running.
66 action = '#!/bin/sh\n\nPATH=/sbin:/bin:/usr/bin\n shutdown -h now '
67 '\"Power button pressed\"'
69 if self.g.is_file('/etc/acpi/powerbtn.sh'):
70 self.g.write('/etc/acpi/powerbtn.sh', action)
71 elif self.g.is_file('/etc/acpi/actions/power.sh'):
72 self.g.write('/etc/acpi/actions/power.sh', action)
74 print "Warning: No acpid action file found"
76 def sysprep_persistent_net_rules(self):
77 """Remove udev rules that will keep network interface names persistent
78 after hardware changes and reboots. Those rules will be created again
79 the next time the image runs.
81 rule_file = '/etc/udev/rules.d/70-persistent-net.rules'
82 if self.g.is_file(rule_file):
85 def sysprep_persistent_devs(self):
86 """Scan fstab and grub configuration files and replace all
87 non-persistent device appearences with UUIDs.
89 # convert all devices in fstab to persistent
90 persistent_root = self._persistent_fstab()
92 # convert all devices in grub1 to persistent
93 self._persistent_grub1(persistent_root)
95 def _persistent_grub1(self, new_root):
96 if self.g.is_file('/boot/grub/menu.lst'):
97 grub1 = '/boot/grub/menu.lst'
98 elif self.g.is_file('/etc/grub.conf'):
99 grub1 = '/etc/grub.conf'
103 self.g.aug_init('/', 0)
105 roots = self.g.aug_match('/files%s/title[*]/kernel/root' % grub1)
107 dev = self.g.aug_get(root)
108 if not self.is_persistent(dev):
109 # This is not always correct. Grub may contain root entries
110 # for other systems, but we only support 1 OS per hard
111 # disk, so this shouldn't harm.
112 self.g.aug_set(root, new_root)
117 def _persistent_fstab(self):
118 mpoints = self.g.mountpoints()
119 if len(mpoints) == 0:
120 pass # TODO: error handling
122 device_dict = dict([[mpoint, dev] for dev, mpoint in mpoints])
126 fstab = self.g.cat('/etc/fstab')
127 for line in fstab.splitlines():
129 line, dev, mpoint = self._convert_fstab_line(line, device_dict)
130 new_fstab += "%s\n" % line
135 self.g.write('/etc/fstab', new_fstab)
137 pass # TODO: error handling
141 def _convert_fstab_line(self, line, devices):
143 line = line.split('#')[0].strip()
149 print "Warning: detected abnorman entry in fstab"
155 if not self.is_persistent(dev):
156 if mpoint in devices:
157 dev = "UUID=%s" % self.get_uuid(devices[mpoint])
160 # comment out the entry
161 entry[0] = "#%s" % dev
162 return " ".join(entry), dev, mpoint
164 return orig, dev, mpoint
166 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :