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.
30 def cmdline (subparsers):
31 p = subparsers.add_parser (
33 help='build and run a new virtual machine',
36 '--memory', default=1024, type=int,
37 help='RAM to give to guest (default unit is megabytes)'
40 '--vcpus', default=4, type=int,
41 help='virtual CPUs to give to guest'
44 '--virtio', default=True,
45 help='use virtio disks and network'
48 '--size', required=True,
53 help='name of the new VM (or use "vm:name")'
57 help='OS & version of guest (see virt-builder docs)'
61 help='virt-builder arguments (after "--")'
63 p.set_defaults (run=run)
66 # Did the user request a particular node? If not, we'll run it
67 # on any node which is up.
68 m = re.match (r'^(.*):(.*)$', args.name)
70 node_name = m.group (1)
73 node = lib.pick_any_node_which_is_up (c)
76 # Get all the guests, so we can tell if the name is a duplicate.
77 running, inactive = lib.get_all_guests (c)
79 if vm_name in running or vm_name in inactive:
80 sys.exit ("error: node name (%s) already exists" % vm_name)
82 output = '%s/%s.img' % (c['images_dir'], vm_name)
84 # Call out to virt-builder to build the disk image.
92 vbargs.extend (['--size', args.size])
94 vbargs.extend (args.vbargs)
95 subprocess.check_call (vbargs)
97 # XXX Unfortunately this is necessary so qemu can access the disk.
98 os.chmod (output, 0666)
100 # Generate the XML. Would be nice to use virt-install here, but
101 # it doesn't work: RHBZ#1095789
102 xml = libvirt_xml.generate_libvirt_xml (vm_name,
108 # Write the XML to the xmls_dir.
109 fp = open ("%s/%s.xml" % (c['xmls_dir'], vm_name), "w")
114 lib.start_guest (c, node_name, vm_name)
115 print "guest built and started on node %s" % node_name