From 84512d6548d2ad9534da32f4ae0c7e12bacbf230 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Sat, 10 May 2014 13:36:55 +0100 Subject: [PATCH] mclu: Add import subcommand to import existing guests. --- Makefile.am | 1 + mclu.py | 2 ++ mclu_import.py | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 mclu_import.py diff --git a/Makefile.am b/Makefile.am index 2723187..b67553d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -32,6 +32,7 @@ pkgdata_SCRIPTS = \ mclu.py \ mclu_build.py \ mclu_console.py \ + mclu_import.py \ mclu_info.py \ mclu_list.py \ mclu_migrate.py \ diff --git a/mclu.py b/mclu.py index ee0c10b..16fdcca 100755 --- a/mclu.py +++ b/mclu.py @@ -42,6 +42,8 @@ import mclu_build mclu_build.cmdline (subparsers) import mclu_console mclu_console.cmdline (subparsers) +import mclu_import +mclu_import.cmdline (subparsers) import mclu_info mclu_info.cmdline (subparsers) import mclu_list diff --git a/mclu_import.py b/mclu_import.py new file mode 100644 index 0000000..a306f8d --- /dev/null +++ b/mclu_import.py @@ -0,0 +1,110 @@ +#!/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 -- 1.8.3.1