aboutsummaryrefslogtreecommitdiffstats
path: root/tools/examples/createlinuxdom.py
blob: d13ce5dbcb97fb7728978557628e7bee06e9e887 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python

#
# Example script for creating and building a new Linux guest OS for Xen.
#

import Xc, XenoUtil, sys, os

# Variable declaration. Set these up properly later on, as needed.
nfsserv = nfspath = root_partn = usr_partn = ""

# STEP 1. Specify kernel image file.
image = "FULL_PATH_TO_IMAGE"

# 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"

# STEP 4. Specify IP address, netmask and gateway for the new domain.
ipaddr  = "ADDRESS"
netmask = XenoUtil.get_current_ipmask()
gateway = XenoUtil.get_current_ipgw()

# 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 (and possibly /usr) on local disc (if not NFS booting)
#root_partn = "/dev/sda2"
#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()

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()

if not os.path.isfile( image ):
    print "Image file '" + image + "' does not exist"
    sys.exit()

xc = Xc.new()

id = xc.domain_create( mem_kb=memory_megabytes*1024, name=domain_name )
if id <= 0:
    print "Error creating domain"
    sys.exit()

if xc.linux_build( dom=id, image=image, cmdline=cmdline ):
    print "Error building Linux guest OS"
    xc.domain_destroy ( dom=id )
    sys.exit()

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 )
        sys.exit()
    if xc.vbd_add_extent( 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 )
        sys.exit()

if usr_partn:
    if xc.vbd_create( dom=id, vbd=usr_info[0], writeable=0 ):
        print "Error creating usr VBD"
        xc.domain_destroy ( dom=id )
        sys.exit()
    if xc.vbd_add_extent( 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"
        xc.domain_destroy ( dom=id )
        sys.exit()

XenoUtil.setup_vfr_rules_for_vif( id, 0, ipaddr )

if xc.domain_start( dom=id ):
    print "Error starting domain"
    xc.domain_destroy ( dom=id )
    sys.exit()