diff options
author | iap10@nidd.cl.cam.ac.uk <iap10@nidd.cl.cam.ac.uk> | 2003-12-20 23:39:49 +0000 |
---|---|---|
committer | iap10@nidd.cl.cam.ac.uk <iap10@nidd.cl.cam.ac.uk> | 2003-12-20 23:39:49 +0000 |
commit | b40567e09bc0409c67fda34546c8348ecce97aab (patch) | |
tree | c04f74a786e8f42be915fc129a35de51313ca9b1 /tools/examples/createlinuxdom.py | |
parent | 8e6dcfd56bd4bbb9befbbaf0620d98274fdd94ef (diff) | |
parent | af0ffa86fad43a78329e334a105d766b1b0acbb4 (diff) | |
download | xen-b40567e09bc0409c67fda34546c8348ecce97aab.tar.gz xen-b40567e09bc0409c67fda34546c8348ecce97aab.tar.bz2 xen-b40567e09bc0409c67fda34546c8348ecce97aab.zip |
bitkeeper revision 1.655 (3fe4ddc5kjuJ5C3c69KvtT7oUuzn4g)
Merge nidd.cl.cam.ac.uk:/auto/groups/xeno/BK/xeno-1.2.bk
into nidd.cl.cam.ac.uk:/auto/anfs/scratch/labyrinth/iap10/xeno-clone/xeno.bk
Diffstat (limited to 'tools/examples/createlinuxdom.py')
-rwxr-xr-x | tools/examples/createlinuxdom.py | 214 |
1 files changed, 138 insertions, 76 deletions
diff --git a/tools/examples/createlinuxdom.py b/tools/examples/createlinuxdom.py index d01a2ea0b7..e183b422f8 100755 --- a/tools/examples/createlinuxdom.py +++ b/tools/examples/createlinuxdom.py @@ -2,107 +2,169 @@ # # Example script for creating and building a new Linux guest OS for Xen. +# It takes an optional parameter that specifies offsets to be added to the +# ip address and root partition numbers, enabling multiple domains to be +# started from the one script. +# +# Edit as required... # -import Xc, XenoUtil, sys, os +import Xc, XenoUtil, string, sys, os, time, socket -# Variable declaration. Set these up properly later on, as needed. -nfsserv = nfspath = root_partn = usr_partn = "" +# initialize a few variables that might come in handy +thishostname = socket.gethostname() +guestid = 0 +if sys.argv >= 2: + guestid = string.atoi(sys.argv[1]) + print "Offset to add to guest's IP etc : %d\n" % guestid + +##### This section of the code establishes various settings to be used +##### for this guest virtual machine -# STEP 1. Specify kernel image file. -image = "FULL_PATH_TO_IMAGE" +# STEP 1. Specify kernel image file. Can be gzip'ed. +image = "../../../install/boot/xenolinux.gz" # STEP 2. How many megabytes of memory for the new domain? memory_megabytes = 64 # STEP 3. A handy name for your new domain. -domain_name = "My new domain" +domain_name = "This is VM %d" % guestid + +# STEP 4. Specify IP address(es), netmask and gateway for the new +# domain. You need to configure IP addrs within the domain just as +# you do normally. This is just to let Xen know about them so it can +# route packets appropriately. -# STEP 4. Specify IP address, netmask and gateway for the new domain. -ipaddr = "ADDRESS" +#ipaddr = ["111.222.333.444","222.333.444.555"] +ipaddr = [XenoUtil.add_offset_to_ip(XenoUtil.get_current_ipaddr(),guestid)] netmask = XenoUtil.get_current_ipmask() gateway = XenoUtil.get_current_ipgw() +nfsserv = '169.254.1.0' # You need to set this if you're using NFS root -# STEP 5a. Specify NFS server and path to rootfs (only needed for network boot) -nfsserv = "ADDRESS" -nfspath = "FULL_PATH_TO_ROOT_DIR" - -# STEP 5b. Specify root partition on local disc (if not NFS booting) -#root_partn = "/dev/sda2" -# (NB. The following is only needed for a separate shared read-only /usr) -# (usr_partn = "/dev/sda6") - -# STEP 6. Check that the following cmdline setup is to your taste. -cmdline = "ip="+ipaddr+":"+nfsserv+":"+gateway+":"+netmask+"::eth0:off" -if root_partn: - # Boot from local disc. May specify a separate /usr. - cmdline = cmdline + " root="+root_partn+" ro" - if usr_partn: - " usr="+usr_partn -elif nfsserv: - # NFS boot - cmdline = cmdline + " root=/dev/nfs" - cmdline = cmdline + " nfsroot="+nfspath - -if root_partn: - root_info = XenoUtil.lookup_blkdev_partn_info(root_partn) - if not root_info: - print "Could not obtain info on partition '" + root_partn + "'" - sys.exit() +# STEP 5. Identify any physcial partitions or virtual disks you want the +# domain to have access to, and what you want them accessible as +# e.g. vbds = [ ('phy:sda1','sda1', 'w'), +# ('phy:sda4','sda%d' % (3+guestid), 'r'), +# ('vd:as73gd784dh','hda1','w') ] -if usr_partn: - usr_info = XenoUtil.lookup_blkdev_partn_info(usr_partn) - if not usr_info: - print "Could not obtain info on partition '" + usr_partn + "'" - sys.exit() +vbds = [ ('phy:sda%d'%(7+guestid),'sda1','w' ), + ('phy:sda6','sda6','r'), + ('phy:cdrom','hdd','r') ] -if not os.path.isfile( image ): - print "Image file '" + image + "' does not exist" - sys.exit() +# STEP 6. Build the command line for the new domain. Edit as req'd. +# You only need the ip= line if you're NFS booting or the root file system +# doesn't set it later e.g. in ifcfg-eth0 or via DHCP +# You can use 'extrabit' to set the runlevel and custom environment +# variables used by custom rc scripts (e.g. DOMID=, usr= ) -xc = Xc.new() +ipbit = "ip="+ipaddr[0]+":"+nfsserv+":"+gateway+":"+netmask+"::eth0:off" +rootbit = "root=/dev/sda1 ro" +#rootbit = "root=/dev/nfs nfsroot=/full/path/to/root/directory" +extrabit = "4 DOMID=%d usr=/dev/sda6" % guestid +cmdline = ipbit +" "+ rootbit +" "+ extrabit -id = xc.domain_create( mem_kb=memory_megabytes*1024, name=domain_name ) -if id <= 0: - print "Error creating domain" - sys.exit() +# STEP 7. Set according to whether you want the script to watch the domain +# and auto-restart it should it die or exit. -if xc.linux_build( dom=id, image=image, cmdline=cmdline ): - print "Error building Linux guest OS" - xc.domain_destroy ( dom=id ) - sys.exit() +auto_restart = False +#auto_restart = True -if root_partn: - if xc.vbd_create( dom=id, vbd=root_info[0], writeable=1 ): - print "Error creating root VBD" - xc.domain_destroy ( dom=id ) + +##### Print some debug info just incase things don't work out... +##### + +print "Domain image : ", image +print "Domain memory : ", memory_megabytes +print "Domain IP address(es) : ", ipaddr +print "Domain block devices : ", vbds +print 'Domain cmdline : "%s"' % cmdline + + +##### Code beyond this point is actually used to manage the mechanics of +##### starting (and watching if necessary) guest virtual machines. + +# Obtain an instance of the Xen control interface +xc = Xc.new() + +# This function creates, builds and starts a domain, using the values +# in the global variables, set above. It is used in the subsequent +# code for starting the new domain and rebooting it if appropriate. +def make_domain(): + """Create, build and start a domain. + Returns: [int] the ID of the new domain. + """ + + # set up access to the global variables declared above + global image, memory_megabytes, domain_name, ipaddr, netmask + global vbds, cmdline, xc + + if not os.path.isfile( image ): + print "Image file '" + image + "' does not exist" sys.exit() - if xc.vbd_grow( dom=id, - vbd=root_info[0], - device=root_info[1], - start_sector=root_info[2], - nr_sectors=root_info[3] ): - print "Error populating root VBD" - xc.domain_destroy ( dom=id ) + + id = xc.domain_create( mem_kb=memory_megabytes*1024, name=domain_name ) + print "Created new domain with id = " + str(id) + if id <= 0: + print "Error creating domain" sys.exit() -if usr_partn: - if xc.vbd_create( dom=id, vbd=usr_info[0], writeable=0 ): - print "Error creating usr VBD" + ret = xc.linux_build( dom=id, image=image, cmdline=cmdline ) + if ret < 0: + print "Error building Linux guest OS: " + print "Return code from linux_build = " + str(ret) xc.domain_destroy ( dom=id ) sys.exit() - if xc.vbd_grow( dom=id, - vbd=usr_info[0], - device=usr_info[1], - start_sector=usr_info[2], - nr_sectors=usr_info[3] ): - print "Error populating usr VBD" + + # setup the virtual block devices + for ( uname, virt_name, rw ) in vbds: + virt_dev = XenoUtil.blkdev_name_to_number( virt_name ) + + segments = XenoUtil.lookup_disk_uname( uname ) + if not segments: + print "Error looking up %s\n" % uname + xc.domain_destroy ( dom=id ) + sys.exit() + + if xc.vbd_create( dom=id, vbd=virt_dev, writeable= rw=='w' ): + print "Error creating VBD vbd=%d writeable=%d\n" % (virt_dev,rw) + xc.domain_destroy ( dom=id ) + sys.exit() + + for (s_dev,s_start,s_len,s_type) in segments: + if xc.vbd_grow( dom=id, + vbd=virt_dev, + device=s_dev, + start_sector=s_start, + nr_sectors=s_len ): + print "Error populating VBD vbd=%d\n" % virt_dev + xc.domain_destroy ( dom=id ) + sys.exit() + + # setup virtual firewall rules for all aliases + for ip in ipaddr: + XenoUtil.setup_vfr_rules_for_vif( id, 0, ip ) + + if xc.domain_start( dom=id ) < 0: + print "Error starting domain" xc.domain_destroy ( dom=id ) sys.exit() -XenoUtil.setup_vfr_rules_for_vif( id, 0, ipaddr ) + return id +# end of make_domain() + + + +# The starting / monitoring of the domain actually happens here... + +# start the domain and record its ID number +current_id = make_domain() + +# if the auto_restart flag is set then keep polling to see if the domain is +# alive - restart if it is not by calling make_domain() again (it's necessary +# to update the id variable, since the new domain may have a new ID) -if xc.domain_start( dom=id ): - print "Error starting domain" - xc.domain_destroy ( dom=id ) - sys.exit() +while auto_restart: + time.sleep(1) + if not xc.domain_getinfo(current_id): + print "The virtual machine has terminated, restarting in a new domain" + current_id = make_domain() |