mclu: Add import subcommand to import existing guests.
[mclu.git] / mclu_import.py
1 #!/usr/bin/python
2 # mclu (mini cluster)
3 # Copyright (C) 2014 Red Hat Inc.
4 #
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.
9 #
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.
14 #
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.
18
19 import argparse
20 import os
21 import re
22 import sys
23 import subprocess
24
25 import config
26 import lib
27 import libvirt_xml
28
29 def cmdline (subparsers):
30     p = subparsers.add_parser (
31         'import',
32         help='import and existing virtual machine',
33     )
34     p.add_argument (
35         '--memory', default=1024, type=int,
36         help='RAM to give to guest (default unit is megabytes)'
37     )
38     p.add_argument (
39         '--vcpus', default=4, type=int,
40         help='virtual CPUs to give to guest'
41     )
42     p.add_argument (
43         '--virtio', default=True,
44         help='use virtio disks and network'
45     )
46     p.add_argument (
47         'name',
48         help='name of the new VM (or use "vm:name")'
49     )
50     p.add_argument (
51         'disk',
52         help='disk image to import'
53     )
54     p.set_defaults (run=run)
55
56 def run (c, args, nodes):
57     # Did the user request a particular node?  If not, we'll run it
58     # on any node which is up.
59     m = re.match (r'^(.*):(.*)$', args.name)
60     if m:
61         node_name = m.group (1)
62         vm_name = m.group (2)
63         if node_name in nodes:
64             node = nodes[node_name]
65             if not node.ping ():
66                 sys.exit ("error: requested node (%s) is not up, use mclu wake %s" %
67                           (node_name, node_name))
68         else:
69             sys.exit ("error: requested node (%s) does not exist" % node_name)
70     else:
71         node = lib.pick_any_node_which_is_up (nodes)
72         vm_name = args.name
73
74     # Get all the guests, so we can tell if the name is a duplicate.
75     running, inactive = lib.get_all_guests (c, nodes.values ())
76
77     if vm_name in running or vm_name in inactive:
78         sys.exit ("error: node name (%s) already exists" % vm_name)
79
80     output = '%s/%s.img' % (c['images_dir'], vm_name)
81
82     # Use qemu-img to convert the input format (whatever it is) to qcow2,
83     # and also to copy it.
84     subprocess.check_call ([
85         "qemu-img",
86         "convert",
87         args.disk,
88         "-O", "qcow2",
89         output
90     ])
91
92     # XXX Unfortunately this is necessary so qemu can access the disk.
93     os.chmod (output, 0666)
94
95     # Generate the XML.  Would be nice to use virt-install here, but
96     # it doesn't work: RHBZ#1095789
97     xml = libvirt_xml.generate_libvirt_xml (vm_name,
98                                             args.memory,
99                                             args.vcpus,
100                                             args.virtio,
101                                             output)
102
103     # Write the XML to the xmls_dir.
104     fp = open ("%s/%s.xml" % (c['xmls_dir'], vm_name), "w")
105     fp.write (xml)
106     fp.close ()
107
108     # Start the guest.
109     lib.start_guest (c, node, vm_name)
110     print "guest imported and started on node %s" % node.name