Revision 091c0335 image_creator/os_type/linux.py

b/image_creator/os_type/linux.py
59 59
        passwd = []
60 60
        removed_users = {}
61 61
        metadata_users = self.meta['USERS'].split()
62
        for line in self.g.cat('/etc/passwd').splitlines():
62
        for line in self.image.g.cat('/etc/passwd').splitlines():
63 63
            fields = line.split(':')
64 64
            if int(fields[2]) > 1000:
65 65
                removed_users[fields[0]] = fields
......
75 75
        if not len(self.meta['USERS']):
76 76
            del self.meta['USERS']
77 77

  
78
        self.g.write('/etc/passwd', '\n'.join(passwd) + '\n')
78
        self.image.g.write('/etc/passwd', '\n'.join(passwd) + '\n')
79 79

  
80 80
        # Remove the corresponding /etc/shadow entries
81 81
        shadow = []
82
        for line in self.g.cat('/etc/shadow').splitlines():
82
        for line in self.image.g.cat('/etc/shadow').splitlines():
83 83
            fields = line.split(':')
84 84
            if fields[0] not in removed_users:
85 85
                shadow.append(':'.join(fields))
86 86

  
87
        self.g.write('/etc/shadow', "\n".join(shadow) + '\n')
87
        self.image.g.write('/etc/shadow', "\n".join(shadow) + '\n')
88 88

  
89 89
        # Remove the corresponding /etc/group entries
90 90
        group = []
91
        for line in self.g.cat('/etc/group').splitlines():
91
        for line in self.image.g.cat('/etc/group').splitlines():
92 92
            fields = line.split(':')
93 93
            # Remove groups tha have the same name as the removed users
94 94
            if fields[0] not in removed_users:
95 95
                group.append(':'.join(fields))
96 96

  
97
        self.g.write('/etc/group', '\n'.join(group) + '\n')
97
        self.image.g.write('/etc/group', '\n'.join(group) + '\n')
98 98

  
99 99
        # Remove home directories
100 100
        for home in [field[5] for field in removed_users.values()]:
101
            if self.g.is_dir(home) and home.startswith('/home/'):
102
                self.g.rm_rf(home)
101
            if self.image.g.is_dir(home) and home.startswith('/home/'):
102
                self.image.g.rm_rf(home)
103 103

  
104 104
    @sysprep('Cleaning up password & locking all user accounts')
105 105
    def cleanup_passwords(self):
......
107 107

  
108 108
        shadow = []
109 109

  
110
        for line in self.g.cat('/etc/shadow').splitlines():
110
        for line in self.image.g.cat('/etc/shadow').splitlines():
111 111
            fields = line.split(':')
112 112
            if fields[1] not in ('*', '!'):
113 113
                fields[1] = '!'
114 114

  
115 115
            shadow.append(":".join(fields))
116 116

  
117
        self.g.write('/etc/shadow', "\n".join(shadow) + '\n')
117
        self.image.g.write('/etc/shadow', "\n".join(shadow) + '\n')
118 118

  
119 119
    @sysprep('Fixing acpid powerdown action')
120 120
    def fix_acpid(self):
......
126 126
                          'shutdown -h now "Power button pressed"\n'
127 127

  
128 128
        events_dir = '/etc/acpi/events'
129
        if not self.g.is_dir(events_dir):
129
        if not self.image.g.is_dir(events_dir):
130 130
            self.out.warn("No acpid event directory found")
131 131
            return
132 132

  
133 133
        event_exp = re.compile('event=(.+)', re.I)
134 134
        action_exp = re.compile('action=(.+)', re.I)
135
        for events_file in self.g.readdir(events_dir):
135
        for events_file in self.image.g.readdir(events_dir):
136 136
            if events_file['ftyp'] != 'r':
137 137
                continue
138 138

  
139 139
            fullpath = "%s/%s" % (events_dir, events_file['name'])
140 140
            event = ""
141 141
            action = ""
142
            for line in self.g.cat(fullpath).splitlines():
142
            for line in self.image.g.cat(fullpath).splitlines():
143 143
                match = event_exp.match(line)
144 144
                if match:
145 145
                    event = match.group(1)
......
151 151

  
152 152
            if event.strip() in ("button[ /]power", "button/power.*"):
153 153
                if action:
154
                    if not self.g.is_file(action):
154
                    if not self.image.g.is_file(action):
155 155
                        self.out.warn("Acpid action file: %s does not exist" %
156 156
                                      action)
157 157
                        return
158
                    self.g.copy_file_to_file(action,
158
                    self.image.g.copy_file_to_file(action,
159 159
                                             "%s.orig.snf-image-creator-%d" %
160 160
                                             (action, time.time()))
161
                    self.g.write(action, powerbtn_action)
161
                    self.image.g.write(action, powerbtn_action)
162 162
                    return
163 163
                else:
164 164
                    self.out.warn("Acpid event file %s does not contain and "
......
182 182
        """
183 183

  
184 184
        rule_file = '/etc/udev/rules.d/70-persistent-net.rules'
185
        if self.g.is_file(rule_file):
186
            self.g.rm(rule_file)
185
        if self.image.g.is_file(rule_file):
186
            self.image.g.rm(rule_file)
187 187

  
188 188
    @sysprep('Removing swap entry from fstab')
189 189
    def remove_swap_entry(self):
......
194 194
        """
195 195

  
196 196
        new_fstab = ""
197
        fstab = self.g.cat('/etc/fstab')
197
        fstab = self.image.g.cat('/etc/fstab')
198 198
        for line in fstab.splitlines():
199 199

  
200 200
            entry = line.split('#')[0].strip().split()
......
203 203

  
204 204
            new_fstab += "%s\n" % line
205 205

  
206
        self.g.write('/etc/fstab', new_fstab)
206
        self.image.g.write('/etc/fstab', new_fstab)
207 207

  
208 208
    @sysprep('Replacing fstab & grub non-persistent device references')
209 209
    def use_persistent_block_device_names(self):
......
221 221
        """Replaces non-persistent device name occurencies with persistent
222 222
        ones in GRUB1 configuration files.
223 223
        """
224
        if self.g.is_file('/boot/grub/menu.lst'):
224
        if self.image.g.is_file('/boot/grub/menu.lst'):
225 225
            grub1 = '/boot/grub/menu.lst'
226
        elif self.g.is_file('/etc/grub.conf'):
226
        elif self.image.g.is_file('/etc/grub.conf'):
227 227
            grub1 = '/etc/grub.conf'
228 228
        else:
229 229
            return
230 230

  
231
        self.g.aug_init('/', 0)
231
        self.image.g.aug_init('/', 0)
232 232
        try:
233
            roots = self.g.aug_match('/files%s/title[*]/kernel/root' % grub1)
233
            roots = self.image.g.aug_match(
234
                '/files%s/title[*]/kernel/root' % grub1)
234 235
            for root in roots:
235
                dev = self.g.aug_get(root)
236
                dev = self.image.g.aug_get(root)
236 237
                if not self._is_persistent(dev):
237 238
                    # This is not always correct. Grub may contain root entries
238 239
                    # for other systems, but we only support 1 OS per hard
239 240
                    # disk, so this shouldn't harm.
240
                    self.g.aug_set(root, new_root)
241
                    self.image.g.aug_set(root, new_root)
241 242
        finally:
242
            self.g.aug_save()
243
            self.g.aug_close()
243
            self.image.g.aug_save()
244
            self.image.g.aug_close()
244 245

  
245 246
    def _persistent_fstab(self):
246 247
        """Replaces non-persistent device name occurencies in /etc/fstab with
247 248
        persistent ones.
248 249
        """
249
        mpoints = self.g.mountpoints()
250
        mpoints = self.image.g.mountpoints()
250 251
        if len(mpoints) == 0:
251 252
            pass  # TODO: error handling
252 253

  
......
254 255

  
255 256
        root_dev = None
256 257
        new_fstab = ""
257
        fstab = self.g.cat('/etc/fstab')
258
        fstab = self.image.g.cat('/etc/fstab')
258 259
        for line in fstab.splitlines():
259 260

  
260 261
            line, dev, mpoint = self._convert_fstab_line(line, device_dict)
......
263 264
            if mpoint == '/':
264 265
                root_dev = dev
265 266

  
266
        self.g.write('/etc/fstab', new_fstab)
267
        self.image.g.write('/etc/fstab', new_fstab)
267 268
        if root_dev is None:
268 269
            pass  # TODO: error handling
269 270

  
......
312 313
        users = []
313 314
        regexp = re.compile(r'(\S+):((?:!\S+)|(?:[^!*]\S+)|):(?:\S*:){6}')
314 315

  
315
        for line in self.g.cat('/etc/shadow').splitlines():
316
        for line in self.image.g.cat('/etc/shadow').splitlines():
316 317
            match = regexp.match(line)
317 318
            if not match:
318 319
                continue
......
334 335
        if dev in self._uuid:
335 336
            return self._uuid[dev]
336 337

  
337
        uuid = self.g.vfs_uuid(dev)
338
        uuid = self.image.g.vfs_uuid(dev)
338 339
        assert len(uuid)
339 340
        self._uuid[dev] = uuid
340 341
        return uuid

Also available in: Unified diff