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
35 from image_creator.util import warn
37 from clint.textui import puts, indent
43 def __init__(self, rootdev, ghandler):
44 super(Linux, self).__init__(rootdev, ghandler)
46 self._persistent = re.compile('/dev/[hsv]d[a-z][1-9]*')
48 def is_persistent(self, dev):
49 return not self._persistent.match(dev)
51 def get_uuid(self, dev):
53 return self._uuid[dev]
55 for attr in self.g.blkid(dev):
57 self._uuid[dev] = attr[1]
60 def sysprep_acpid(self):
61 """Replace acpid powerdown action scripts to automatically shutdown
62 the system without checking if a GUI is running.
65 puts('* Fixing acpid powerdown action')
67 action = '#!/bin/sh\n\nPATH=/sbin:/bin:/usr/bin\n shutdown -h now '
68 '\"Power button pressed\"'
70 if self.g.is_file('/etc/acpi/powerbtn.sh'):
71 self.g.write('/etc/acpi/powerbtn.sh', action)
72 elif self.g.is_file('/etc/acpi/actions/power.sh'):
73 self.g.write('/etc/acpi/actions/power.sh', action)
76 warn("No acpid action file found")
78 def sysprep_persistent_net_rules(self):
79 """Remove udev rules that will keep network interface names persistent
80 after hardware changes and reboots. Those rules will be created again
81 the next time the image runs.
84 puts('* Removing persistent network interface names')
86 rule_file = '/etc/udev/rules.d/70-persistent-net.rules'
87 if self.g.is_file(rule_file):
90 def sysprep_persistent_devs(self):
91 """Scan fstab and grub configuration files and replace all
92 non-persistent device appearences with UUIDs.
95 puts('* Replacing fstab & grub non-persistent device appearences')
97 # convert all devices in fstab to persistent
98 persistent_root = self._persistent_fstab()
100 # convert all devices in grub1 to persistent
101 self._persistent_grub1(persistent_root)
103 def _persistent_grub1(self, new_root):
104 if self.g.is_file('/boot/grub/menu.lst'):
105 grub1 = '/boot/grub/menu.lst'
106 elif self.g.is_file('/etc/grub.conf'):
107 grub1 = '/etc/grub.conf'
111 self.g.aug_init('/', 0)
113 roots = self.g.aug_match('/files%s/title[*]/kernel/root' % grub1)
115 dev = self.g.aug_get(root)
116 if not self.is_persistent(dev):
117 # This is not always correct. Grub may contain root entries
118 # for other systems, but we only support 1 OS per hard
119 # disk, so this shouldn't harm.
120 self.g.aug_set(root, new_root)
125 def _persistent_fstab(self):
126 mpoints = self.g.mountpoints()
127 if len(mpoints) == 0:
128 pass # TODO: error handling
130 device_dict = dict([[mpoint, dev] for dev, mpoint in mpoints])
134 fstab = self.g.cat('/etc/fstab')
135 for line in fstab.splitlines():
137 line, dev, mpoint = self._convert_fstab_line(line, device_dict)
138 new_fstab += "%s\n" % line
143 self.g.write('/etc/fstab', new_fstab)
145 pass # TODO: error handling
149 def _convert_fstab_line(self, line, devices):
151 line = line.split('#')[0].strip()
157 warn("Detected abnormal entry in fstab")
163 if not self.is_persistent(dev):
164 if mpoint in devices:
165 dev = "UUID=%s" % self.get_uuid(devices[mpoint])
168 # comment out the entry
169 entry[0] = "#%s" % dev
170 return " ".join(entry), dev, mpoint
172 return orig, dev, mpoint
174 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :