put the post-copy frobbing details in the image
authorGreg Price <price@mit.edu>
Thu, 3 Apr 2008 06:22:54 +0000 (02:22 -0400)
committerGreg Price <price@mit.edu>
Thu, 3 Apr 2008 06:22:54 +0000 (02:22 -0400)
svn path=/trunk/packages/sipb-xen-guest-installer/; revision=405

files/usr/sbin/sipb-xen-lvcopy

index ecfa8bf..5b83460 100755 (executable)
@@ -27,48 +27,10 @@ def losetup(source, offset=0):
   finally:
     os.unlink(lockfilename) #unlock
   return loopdevice
   finally:
     os.unlink(lockfilename) #unlock
   return loopdevice
-  # XX this means we can duplicate 9 at once.  since it takes around 30 seconds,
+  # XX this means we can duplicate 28 at once.  Since it takes around 30s,
   # this seems like an adequate capacity.
 
   # this seems like an adequate capacity.
 
-def duplicate_lv(source, dest):
-  '''OBSOLETE: duplicate boot record, filesystem from LV source to LV dest
-  
-  source, dest should be device filenames.
-
-  Now we just dd.  Doesn't support resizing, but it's easier,
-  especially if we allow the partition table to vary between images.
-  '''
-  # XXX this is very specific to what the etch sipb-xen-make-iso installer does.
-  # XXXX also, it's specific to four gigs.
-  # step 1: copy the MBR
-  call(['dd', 'if='+source, 'bs=512', 'count=63', 'of='+dest])
-  # step 2: fix up partition table
-  # XX actually do this; this is our opportunity to resize the filesystem
-  # step 3: make the filesystem, and copy its contents in
-  sourcefs = losetup(source, 32256)
-  destfs = losetup(dest, 32256)
-  call(['mkfs.ext3', '-b', '4096', destfs, '987989'])
-  tmptree = tempfile.mkdtemp('', 'auto-install.dup.', '/tmp')#yes, args backward
-  os.mkdir(tmptree+"/source")
-  call(['mount', '-t', 'ext3', '-o', 'ro', sourcefs, tmptree+"/source"])
-  os.mkdir(tmptree+"/dest")
-  call(['mount', '-t', 'ext3', destfs, tmptree+"/dest"])
-  call(['cp', '-aT', tmptree+"/source", tmptree+"/dest"])
-  # step 4: twiddle what we want to twiddle
-  # XX do this
-  # step 5: make the swap area
-  swapfs = losetup(dest, 4046870016)
-  call(['mkswap', swapfs, str(240943)])
-  # call(['losetup', '-d', swapfs])
-  print 'losetup -d '+swapfs
-  # step 6: clean up.
-  # XX actually unmount etc (leaving it is for debugging)
-  print 'umount '+tmptree+'/source'
-  call(['umount', tmptree+"/dest"]) # needed to flush writes
-  print 'losetup -d '+sourcefs
-  print 'losetup -d '+destfs
-
-def frob_copy(target, *args):
+def frob_copy_in_vm(target, *args):
   '''UNUSED: maybe we'll use this someday; it does isolate the frobber.'''
   # 1. prepare arguments volume
   args_volume = prefix+target+'_args'
   '''UNUSED: maybe we'll use this someday; it does isolate the frobber.'''
   # 1. prepare arguments volume
   args_volume = prefix+target+'_args'
@@ -86,43 +48,20 @@ def frob_copy(target, *args):
 
   # XXX should check_call(['/sbin/lvremove', '-f', 'xenvg/'+args_volume])
 
 
   # XXX should check_call(['/sbin/lvremove', '-f', 'xenvg/'+args_volume])
 
-def frob_copy_simple(target, hostname, rootpw):
-  """target should be an LV device filename
-
-  This is highly specific to the etch install produced by sipb-xen-make-iso.
-  Generalizing and customizing to other distros is left to the future...
-  """
+def frob_copy(target, hostname, rootpw):
+  """target should be an LV device filename"""
   # 1: mount filesystem
   fs = losetup(target, 32256)
   mntdir = tempfile.mkdtemp('', 'auto-install.frob.', '/tmp')
   call(['mount', '-t', 'ext3', fs, mntdir])
   # 2: do frobbing
   # 1: mount filesystem
   fs = losetup(target, 32256)
   mntdir = tempfile.mkdtemp('', 'auto-install.frob.', '/tmp')
   call(['mount', '-t', 'ext3', fs, mntdir])
   # 2: do frobbing
-  # 2a:  (printf "%s\n" "$ROOTPW"; sleep .3; printf "%s\n" "$ROOTPW") 
-  #      | /usr/sbin/chroot "$TARGET" /usr/bin/passwd root
-  p = Popen(['/usr/sbin/chroot', mntdir, '/usr/bin/passwd', 'root'], stdin=PIPE)
-  p.stdin.write(rootpw+'\n')
-  time.sleep(1)
-  p.stdin.write(rootpw+'\n')
-  p.stdin.close()
-  p.wait()
-  os.chdir(mntdir)
-  # 2b: clear generated file that has eth0's old MAC address
-  #      rm $TARGET/etc/udev/rules.d/z25_persistent-net.rules
-  os.unlink('etc/udev/rules.d/z25_persistent-net.rules')
-  # 2c: hostname.
-  # XX Use nullmailer in image, not exim4.  (Fewer copies of hostname.)
-  # 2c1: rm /var/lib/dhcp3/dhclient.eth0.leases
-  os.unlink('var/lib/dhcp3/dhclient.eth0.leases')
-  # 2c2: /etc/hosts, /etc/hostname; /etc/motd? /etc/mailname?
-  call(['perl', '-i', '-pe', 's/ice3.servers.csail.mit.edu/'+hostname+'/g',
-        'etc/hosts', 'etc/hostname', 'etc/motd', 'etc/mailname'])
+  call(['/usr/sbin/chroot', mntdir, '/post-copy', hostname, rootpw])
   # 3: clean up
   # 3: clean up
-  os.chdir('/')
   call(['umount', mntdir])
   os.rmdir(mntdir)
   call(['losetup', '-d', fs])
 
   call(['umount', mntdir])
   os.rmdir(mntdir)
   call(['losetup', '-d', fs])
 
-def duplicate_by_vm(source, target, rootpw, nodd=False):
+def duplicate_by_vm(source, target, rootpw, nodd=False, nofrob=False):
   # source, target should be machine names
   prefix = 'd_'
   # 1. copy (by dd) source to target
   # source, target should be machine names
   prefix = 'd_'
   # 1. copy (by dd) source to target
@@ -132,7 +71,8 @@ def duplicate_by_vm(source, target, rootpw, nodd=False):
     check_call(['/bin/dd', 'bs=1M', 'conv=nocreat',
                 'if='+source_device, 'of='+target_device])
   # 2. frob target
     check_call(['/bin/dd', 'bs=1M', 'conv=nocreat',
                 'if='+source_device, 'of='+target_device])
   # 2. frob target
-  frob_copy_simple(target_device, target, rootpw)
+  if not nofrob:
+    frob_copy(target_device, target, rootpw)
 
 def main(*argv):
   subcommand = argv[1]
 
 def main(*argv):
   subcommand = argv[1]
@@ -140,10 +80,18 @@ def main(*argv):
   os.environ['PATH'] = '/usr/sbin:/usr/bin:/sbin:/bin'
   if subcommand == 'lvcopy':
     kwargs = {}
   os.environ['PATH'] = '/usr/sbin:/usr/bin:/sbin:/bin'
   if subcommand == 'lvcopy':
     kwargs = {}
-    if args[0] == '--nodd':
-      args = args[1:]
-      kwargs['nodd'] = True
+    while True:
+      if args[0].startswith('--'):
+        kwargs[args[0][2:]] = True
+        args = args[1:]
+        continue
+      if len(args) != 3:
+        print >>sys.stderr, argv[0]+': bad argument list'
+        return 2
+      break
     duplicate_by_vm(*args, **kwargs)
     duplicate_by_vm(*args, **kwargs)
+  elif subcommand == 'test':
+    pass
   else:
     print >>sys.stderr, argv[0]+": unknown subcommand: "+subcommand
     return 2
   else:
     print >>sys.stderr, argv[0]+": unknown subcommand: "+subcommand
     return 2