X-Git-Url: https://git.siccegge.de//index.cgi?a=blobdiff_plain;f=vmdebootstrap;h=b6d6a48ce5a7cea5af156cd0b826b3fc00804043;hb=6ae74289015820ae2d2825790988fb882c7e3b8c;hp=67e5a409a87334f20be73884209e5a7e9de304e2;hpb=39a5154dfb8173800751e5ca6e938e63a3808b1e;p=forks%2Fvmdebootstrap.git diff --git a/vmdebootstrap b/vmdebootstrap index 67e5a40..b6d6a48 100755 --- a/vmdebootstrap +++ b/vmdebootstrap @@ -58,6 +58,11 @@ class VmDebootstrap(cliapp.Application): 'set name to HOSTNAME (%default)', metavar='HOSTNAME', default='debian') + self.settings.string_list(['user'], + 'create USER with PASSWORD', + metavar='USER/PASSWORD') + self.settings.boolean(['serial-console'], + 'configure image to use a serial console') def process_args(self, args): if not self.settings['image']: @@ -77,7 +82,9 @@ class VmDebootstrap(cliapp.Application): rootdir = self.mount(rootdev) self.debootstrap(rootdir) self.set_hostname(rootdir) + self.create_fstab(rootdir) self.set_root_password(rootdir) + self.create_users(rootdir) self.remove_udev_persistent_rules(rootdir) self.setup_networking(rootdir) self.install_extlinux(rootdev, rootdir) @@ -156,7 +163,7 @@ class VmDebootstrap(cliapp.Application): self.message('Debootstrapping') if self.settings['arch'] == 'i386': - kernel_arch = 'i686' + kernel_arch = '686' else: kernel_arch = self.settings['arch'] kernel_image = 'linux-image-2.6-%s' % kernel_arch @@ -172,20 +179,49 @@ class VmDebootstrap(cliapp.Application): def set_hostname(self, rootdir): hostname = self.settings['hostname'] - f = open(os.path.join(rootdir, 'etc', 'hostname'), 'w') - f.write(hostname) - f.close() + with open(os.path.join(rootdir, 'etc', 'hostname'), 'w') as f: + f.write('%s\n' % hostname) + + etc_hosts = os.path.join(rootdir, 'etc', 'hosts') + with open(etc_hosts, 'r') as f: + data = f.read() + with open(etc_hosts, 'w') as f: + for line in data.splitlines(): + if line.startswith('127.0.0.1'): + line += ' %s' % hostname + f.write('%s\n' % line) + + def create_fstab(self, rootdir): + fstab = os.path.join(rootdir, 'etc', 'fstab') + with open(fstab, 'w') as f: + f.write('proc /proc proc defaults 0 0\n') + f.write('/dev/sda1 / ext4 errors=remount-ro 0 1\n') def set_root_password(self, rootdir): if self.settings['root-password']: self.message('Setting root password') - encrypted = crypt.crypt(self.settings['root-password'], '..') - self.runcmd(['chroot', rootdir, 'usermod', '-p', encrypted, - 'root']) + self.set_password(rootdir, 'root', self.settings['root-password']) else: self.message('Locking root password') self.runcmd(['chroot', rootdir, 'passwd', '-l', 'root']) - + + def create_users(self, rootdir): + def create_user(user): + self.runcmd(['chroot', rootdir, 'adduser', '--gecos', user, + '--disabled-password', user]) + + for userpass in self.settings['user']: + if '/' in userpass: + user, password = userpass.split('/', 1) + create_user(user) + self.set_password(rootdir, user, password) + else: + create_user(userpass) + + def set_password(self, rootdir, user, password): + encrypted = crypt.crypt(password, '..') + self.runcmd(['chroot', rootdir, 'usermod', '-p', encrypted, user]) + def remove_udev_persistent_rules(self, rootdir): self.message('Removing udev persistent cd and net rules') for x in ['70-persistent-cd.rules', '70-persistent-net.rules']: @@ -238,13 +274,23 @@ timeout 1 label linux kernel %(kernel)s -append initrd=%(initrd)s root=UUID=%(uuid)s ro quiet +append initrd=%(initrd)s root=UUID=%(uuid)s ro quiet %(kserial)s +%(extserial)s ''' % { 'kernel': kernel_image, 'initrd': initrd_image, 'uuid': uuid, + 'kserial': + 'console=ttyS0,115200' if self.settings['serial-console'] else '', + 'extserial': 'serial 0 115200' if self.settings['serial-console'] else '', }) f.close() + + if self.settings['serial-console']: + logging.debug('adding getty to serial console') + inittab = os.path.join(rootdir, 'etc/inittab') + with open(inittab, 'a') as f: + f.write('\nS0:23:respawn:/sbin/getty -L ttyS0 115200 vt100\n') self.runcmd(['extlinux', '--install', rootdir]) self.runcmd(['sync'])