Changeset 336 for trunk


Ignore:
Timestamp:
Mar 30, 2008, 2:49:43 AM (17 years ago)
Author:
price
Message:

copying installer works now (maybe?)

At least for the image 'ice3', which IIRC was created with
/usr/sbin/sipb-xen-make-iso. It's an etch amd64 image.
It also had to have package nullmailer installed, replacing exim4.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/sipb-xen-guest-installer/files/usr/sbin/sipb-xen-duplicate

    r245 r336  
    55import shutil
    66import tempfile
    7 from subprocess import call, check_call, Popen
     7import time
     8from subprocess import call, check_call, Popen, PIPE
    89
    910# TODO:
    1011# . pick a loop device sanely
    11 # - adapt size of partition to fit volume
    12 # - fix hostname, root password
     12# x adapt size of partition to fit volume
     13# . fix hostname, root password
    1314# - (once debugging diminishes) umount, losetup -d, maybe rm -r
    1415# - avoid race conditions with other loop-device users, too (somehow)
     
    3132    if loopdevice is not None:
    3233      call(['losetup', '-o', str(offset), loopdevice, source])
     34    else:
     35      raise RuntimeError('out of loop devices for copying VM image: too many at once?')
    3336  finally:
    3437    os.unlink(lockfilename) #unlock
     
    3841
    3942def duplicate_lv(source, dest):
    40   '''duplicate boot record, filesystem from LV source to LV dest
     43  '''OBSOLETE: duplicate boot record, filesystem from LV source to LV dest
    4144 
    4245  source, dest should be device filenames.
     46
     47  Now we just dd.  Doesn't support resizing, but it's easier,
     48  especially if we allow the partition table to vary between images.
    4349  '''
    4450  # XXX this is very specific to what the etch sipb-xen-make-iso installer does.
     
    5258  destfs = losetup(dest, 32256)
    5359  call(['mkfs.ext3', '-b', '4096', destfs, '987989'])
    54   tmptree = tempfile.mkdtemp('', 'auto-install.dup.', '/tmp')
     60  tmptree = tempfile.mkdtemp('', 'auto-install.dup.', '/tmp')#yes, args backward
    5561  os.mkdir(tmptree+"/source")
    5662  call(['mount', '-t', 'ext3', '-o', 'ro', sourcefs, tmptree+"/source"])
     
    7278  print 'losetup -d '+destfs
    7379
     80def frob_copy(target, *args):
     81  # 1. prepare arguments volume
     82  args_volume = prefix+target+'_args'
     83  args_device = '/dev/xenvg/' + args_volume
     84  check_call(['/sbin/lvcreate', 'xenvg', '--name', args_volume, '--size', '4K'])
     85  file(args_device, 'w').write('\n'.join(args) + '\n')
     86
     87  # 2. invoke frobber vm
     88  copier_device = '/dev/xenvg/d_wert_hda'
     89  check_call(['/usr/sbin/xm', 'create', 'sipb-database',
     90              'machine_name='+target,
     91              'disks=' + ' '.join(['phy:'+copier_device+',hda,w',
     92                                   'phy:'+target_device+',hdc,w',
     93                                   'phy:'+args_device+',hdd,w'])])
     94
     95  # XXX should check_call(['/sbin/lvremove', '-f', 'xenvg/'+args_volume])
     96
     97def frob_copy_simple(target, hostname, rootpw, *args):
     98  """target should be an LV device filename
     99
     100  This is highly specific to the etch install produced by sipb-xen-make-iso.
     101  Generalizing and customizing to other distros is left to the future...
     102  """
     103  # 1: mount filesystem
     104  fs = losetup(target, 32256)
     105  mntdir = tempfile.mkdtemp('', 'auto-install.frob.', '/tmp')
     106  call(['mount', '-t', 'ext3', fs, mntdir])
     107  # 2: do frobbing
     108  # 2a:  (printf "%s\n" "$ROOTPW"; sleep .3; printf "%s\n" "$ROOTPW")
     109  #      | /usr/sbin/chroot "$TARGET" /usr/bin/passwd root
     110  p = Popen(['/usr/sbin/chroot', mntdir, '/usr/bin/passwd', 'root'], stdin=PIPE)
     111  p.stdin.write(rootpw+'\n')
     112  time.sleep(1)
     113  p.stdin.write(rootpw+'\n')
     114  p.stdin.close()
     115  p.wait()
     116  os.chdir(mntdir)
     117  # 2b: clear generated file that has eth0's old MAC address
     118  #      rm $TARGET/etc/udev/rules.d/z25_persistent-net.rules
     119  os.unlink('etc/udev/rules.d/z25_persistent-net.rules')
     120  # 2c: hostname.
     121  # XX Use nullmailer in image, not exim4.  (Fewer copies of hostname.)
     122  # 2c1: rm /var/lib/dhcp3/dhclient.eth0.leases
     123  os.unlink('var/lib/dhcp3/dhclient.eth0.leases')
     124  # 2c2: /etc/hosts, /etc/hostname; /etc/motd? /etc/mailname?
     125  call(['perl', '-i', '-pe', 's/ice3.servers.csail.mit.edu/'+hostname+'/g',
     126        'etc/hosts', 'etc/hostname', 'etc/motd', 'etc/mailname'])
     127  # 3: clean up
     128  os.chdir('/')
     129  call(['umount', mntdir])
     130  call(['losetup', '-d', fs])
     131
    74132def duplicate_by_vm(source, target, *args):
    75133  # source, target should be machine names
     
    78136  source_device = '/dev/xenvg/' + prefix + source + '_hda'
    79137  target_device = '/dev/xenvg/' + prefix + target + '_hda'
    80   #check_call(['/bin/dd', 'bs=1M', 'conv=nocreat',
    81   #            'if='+source_device, 'of='+target_device])
    82   # 2. prepare arguments volume
    83   args_volume = prefix+target+'_args'
    84   args_device = '/dev/xenvg/' + args_volume
    85   check_call(['/sbin/lvcreate', 'xenvg', '--name', args_volume, '--size', '4K'])
    86   file(args_device, 'w').write('\n'.join(args) + '\n')
    87 
    88   copier_device = '/dev/xenvg/d_wert_hda'
    89   check_call(['/usr/sbin/xm', 'create', 'sipb-database',
    90               'machine_name='+target,
    91               'disks=' + ' '.join(['phy:'+copier_device+',hda,w',
    92                                    'phy:'+target_device+',hdc,w',
    93                                    'phy:'+args_device+',hdd,w'])])
    94   boot = 'c'
    95 
    96   # XXX make this happen! check_call(['/sbin/lvremove', '-f', 'xenvg/'+args_volume])
    97  
     138  check_call(['/bin/dd', 'bs=1M', 'conv=nocreat',
     139              'if='+source_device, 'of='+target_device])
     140  # 2. frob target
     141  frob_copy_simple(target_device, *args)
    98142
    99143if __name__ == '__main__':
Note: See TracChangeset for help on using the changeset viewer.