--- /dev/null
+#!/usr/bin/python
+# mclu (mini cluster)
+# Copyright (C) 2014 Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+import argparse
+import os
+import re
+import sys
+import subprocess
+
+import config
+import lib
+import libvirt_xml
+
+def cmdline (subparsers):
+ p = subparsers.add_parser (
+ 'import',
+ help='import and existing virtual machine',
+ )
+ p.add_argument (
+ '--memory', default=1024, type=int,
+ help='RAM to give to guest (default unit is megabytes)'
+ )
+ p.add_argument (
+ '--vcpus', default=4, type=int,
+ help='virtual CPUs to give to guest'
+ )
+ p.add_argument (
+ '--virtio', default=True,
+ help='use virtio disks and network'
+ )
+ p.add_argument (
+ 'name',
+ help='name of the new VM (or use "vm:name")'
+ )
+ p.add_argument (
+ 'disk',
+ help='disk image to import'
+ )
+ p.set_defaults (run=run)
+
+def run (c, args, nodes):
+ # Did the user request a particular node? If not, we'll run it
+ # on any node which is up.
+ m = re.match (r'^(.*):(.*)$', args.name)
+ if m:
+ node_name = m.group (1)
+ vm_name = m.group (2)
+ if node_name in nodes:
+ node = nodes[node_name]
+ if not node.ping ():
+ sys.exit ("error: requested node (%s) is not up, use mclu wake %s" %
+ (node_name, node_name))
+ else:
+ sys.exit ("error: requested node (%s) does not exist" % node_name)
+ else:
+ node = lib.pick_any_node_which_is_up (nodes)
+ vm_name = args.name
+
+ # Get all the guests, so we can tell if the name is a duplicate.
+ running, inactive = lib.get_all_guests (c, nodes.values ())
+
+ if vm_name in running or vm_name in inactive:
+ sys.exit ("error: node name (%s) already exists" % vm_name)
+
+ output = '%s/%s.img' % (c['images_dir'], vm_name)
+
+ # Use qemu-img to convert the input format (whatever it is) to qcow2,
+ # and also to copy it.
+ subprocess.check_call ([
+ "qemu-img",
+ "convert",
+ args.disk,
+ "-O", "qcow2",
+ output
+ ])
+
+ # XXX Unfortunately this is necessary so qemu can access the disk.
+ os.chmod (output, 0666)
+
+ # Generate the XML. Would be nice to use virt-install here, but
+ # it doesn't work: RHBZ#1095789
+ xml = libvirt_xml.generate_libvirt_xml (vm_name,
+ args.memory,
+ args.vcpus,
+ args.virtio,
+ output)
+
+ # Write the XML to the xmls_dir.
+ fp = open ("%s/%s.xml" % (c['xmls_dir'], vm_name), "w")
+ fp.write (xml)
+ fp.close ()
+
+ # Start the guest.
+ lib.start_guest (c, node, vm_name)
+ print "guest imported and started on node %s" % node.name