3 # Copyright (C) 2014 Red Hat Inc.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 def cmdline (subparsers):
30 p = subparsers.add_parser (
32 help='build and run a new virtual machine',
35 '--memory', default=1024, type=int,
36 help='RAM to give to guest (default unit is megabytes)'
39 '--vcpus', default=4, type=int,
40 help='virtual CPUs to give to guest'
43 '--virtio', default=True,
44 help='use virtio disks and network'
47 '--size', required=True,
52 help='name of the new VM (or use "vm:name")'
56 help='OS & version of guest (see virt-builder docs)'
60 help='virt-builder arguments (after "--")'
62 p.set_defaults (run=run)
64 def run (c, args, nodes):
65 # Did the user request a particular node? If not, we'll run it
66 # on any node which is up.
67 m = re.match (r'^(.*):(.*)$', args.name)
69 node_name = m.group (1)
71 if node_name in nodes:
72 node = nodes[node_name]
74 sys.exit ("error: requested node (%s) is not up, use mclu wake %s" %
75 (node_name, node_name))
77 sys.exit ("error: requested node (%s) does not exist" % node_name)
79 node = lib.pick_any_node_which_is_up (nodes)
82 # Get all the guests, so we can tell if the name is a duplicate.
83 running, inactive = lib.get_all_guests (c, nodes.values ())
85 if vm_name in running or vm_name in inactive:
86 sys.exit ("error: node name (%s) already exists" % vm_name)
88 output = '%s/%s.img' % (c['images_dir'], vm_name)
90 # Call out to virt-builder to build the disk image.
98 vbargs.extend (['--size', args.size])
100 vbargs.extend (args.vbargs)
101 subprocess.check_call (vbargs)
103 # XXX Unfortunately this is necessary so qemu can access the disk.
104 os.chmod (output, 0666)
106 # Generate the XML. Would be nice to use virt-install here, but
107 # it doesn't work: RHBZ#1095789
108 network_model = "virtio"
110 network_model = "e1000"
112 # XXX Quoting, and we should use a real XML generator.
116 <memory unit='MiB'>%d</memory>
117 <currentMemory unit='MiB'>%d</currentMemory>
128 <cpu mode='host-model' fallback='allow' />
130 <timer name='rtc' tickpolicy='catchup'/>
131 <timer name='pit' tickpolicy='delay'/>
132 <timer name='hpet' present='no'/>
134 <on_poweroff>destroy</on_poweroff>
135 <on_reboot>restart</on_reboot>
136 <on_crash>restart</on_crash>
138 <interface type='bridge'>
139 <source bridge='br0'/>
143 <target type='virtio' port='0'/>
145 <input type='tablet' bus='usb'/>
146 <input type='mouse' bus='ps2'/>
147 <!-- <input type='keyboard' bus='ps2'/> -->
148 <graphics type='vnc' autoport='yes'/>
150 <model type='cirrus' vram='9216' heads='1'/>
152 """ % (vm_name, args.memory, args.memory, args.vcpus, network_model)
154 # virtio-scsi or IDE disk:
157 <controller type="scsi" index="0" model="virtio-scsi"/>
158 <disk type='file' device='disk'>
159 <driver name='qemu' type='qcow2' cache='none' io='native'/>
161 <target dev='sda' bus='scsi'/>
166 <disk type='file' device='disk'>
167 <driver name='qemu' type='qcow2' cache='none' io='native'/>
169 <target dev='sda' bus='ide'/>
178 # Write the XML to the xmls_dir.
179 fp = open ("%s/%s.xml" % (c['xmls_dir'], vm_name), "w")
184 lib.start_guest (c, node, vm_name)