1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
#!/usr/bin/python -tt
#
# Copyright(c) FUJITSU Limited 2007.
#
# Script to set up an cloning guest configuration and kick off an cloning
#
# 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 os, sys, string
from optparse import OptionParser, OptionValueError
import subprocess
import libxml2
import logging
import libvirt
import virtinst
import virtinst.CloneManager as clmgr
import gettext
import locale
import virtinst.cli as cli
from virtinst.cli import fail
from virtinst import _virtinst as _
locale.setlocale(locale.LC_ALL, '')
gettext.bindtextdomain(virtinst.gettext_app, virtinst.gettext_dir)
gettext.install(virtinst.gettext_app, virtinst.gettext_dir, unicode=1)
### General input gathering functions
def get_clone_name(new_name, design):
while 1:
new_name = cli.prompt_for_input(_("What is the name for the cloned virtual machine?"), new_name)
try:
design.clone_name = new_name
break
except (ValueError, RuntimeError), e:
print _("ERROR: "), e
new_name = None
def get_original_guest(guest, design):
while 1:
guest = cli.prompt_for_input(_("What is the name or uuid of the original virtual machine?"), guest)
try:
design.original_guest = guest
break
except (ValueError, RuntimeError), e:
print _("ERROR: "), e
guest = None
def get_clone_macaddr(new_mac, design):
if new_mac is None:
pass
elif new_mac[0] == "RANDOM":
new_mac = None
else:
for i in new_mac:
design.set_clone_mac(i)
def get_clone_uuid(new_uuid, design):
if new_uuid is not None:
design.set_clone_uuid(new_uuid)
def get_clone_diskfile(new_diskfiles, design, conn):
if new_diskfiles is None:
new_diskfiles = [None]
for i in range(0, len(new_diskfiles)):
disk = new_diskfiles[i]
while 1:
disk = cli.prompt_for_input(_("What would you like to use as the cloned disk (file path)?"), disk)
# Build disk object for validation
try:
d = virtinst.VirtualDisk(path=disk, size=0)
except ValueError, e:
print _("ERROR: "), e
disk = None
continue
if os.path.exists(d.path):
warnmsg = _("This will overwrite the existing path "
"'%s'!\n") % d.path
if not cli.prompt_for_yes_or_no(warnmsg + _("Do you really want to use this disk (yes or no)?")):
disk = None
continue
# Check disk conflicts
if d.is_conflict_disk(conn) is True:
warnmsg = _("Disk %s is already in use by another guest!\n") % d.path
if not cli.prompt_for_yes_or_no(warnmsg + _("Do you really want to use the disk (yes or no)? ")):
disk = None
continue
new_diskfiles[i] = d.path
break
for i in new_diskfiles:
design.set_clone_devices(i)
def get_clone_sparse(sparse, design):
design.set_clone_sparse(sparse)
def get_preserve(preserve, design):
design.set_preserve(preserve)
def get_force_target(target, design):
if target is None:
pass
else:
for i in target:
design.set_force_target(i)
def parse_args():
parser = cli.VirtOptionParser()
# original name
parser.add_option("-o", "--original", type="string", dest="original_guest",
action="callback", callback=cli.check_before_store,
help=_("Name or uuid for the original guest; The status must be shut off"))
# clone new name
parser.add_option("-n", "--name", type="string", dest="new_name",
action="callback", callback=cli.check_before_store,
help=_("Name for the new guest"))
# clone new uuid
parser.add_option("-u", "--uuid", type="string",
dest="new_uuid", action="callback", callback=cli.check_before_store,
help=_("New UUID for the clone guest; Default is a randomly generated UUID"))
# clone new macs
parser.add_option("-m", "--mac", type="string",
dest="new_mac", action="callback", callback=cli.check_before_append,
help=_("New fixed MAC address for the clone guest. Default is a randomly generated MAC"))
# clone new disks
parser.add_option("-f", "--file", type="string",
dest="new_diskfile", action="callback", callback=cli.check_before_append,
help=_("New file to use as the disk image for the new guest"))
# connect
parser.add_option("", "--connect", type="string",
dest="connect", action="callback", callback=cli.check_before_store,
help=_("Connect to hypervisor with URI"),
default=virtinst.util.default_connection())
# target
parser.add_option("", "--force-copy", type="string",
dest="target", action="callback", callback=cli.check_before_append,
help=_("Force to copy devices (eg, if 'hdc' is a readonly cdrom device, --force-copy=hdc)"))
# non sparse
parser.add_option("", "--nonsparse", action="store_false",
default=True, dest="sparse",
help=_("Do not use a sparse file for the clone's disk image"))
# preserve
parser.add_option("", "--preserve-data", action="store_false",
default=True, dest="preserve",
help=_("Preserve a new file to use as the disk image for the new guest"))
# Misc options
parser.add_option("-d", "--debug", action="store_true", dest="debug",
help=_("Print debugging information"))
parser.add_option("", "--force", action="store_true", dest="force",
help=_("Do not prompt for input. Answers yes where applicable, terminates for all other prompts"),
default=False)
(options,args) = parser.parse_args()
return options
### Let's do it!
def main():
options = parse_args()
cli.setupLogging("virt-clone", options.debug)
cli.set_force(options.force)
logging.debug("start clone with HV " + options.connect)
if options.connect is None or options.connect.lower()[0:3] == "xen":
if os.geteuid() != 0:
fail(_("Must be root to clone Xen guests"))
conn = cli.getConnection(options.connect)
design = clmgr.CloneDesign(connection=conn)
try:
get_clone_diskfile(options.new_diskfile, design, conn)
get_clone_macaddr(options.new_mac, design)
get_original_guest(options.original_guest, design)
get_clone_name(options.new_name, design)
get_clone_uuid(options.new_uuid, design)
get_clone_sparse(options.sparse, design)
get_force_target(options.target, design)
get_preserve(options.preserve, design)
# setup design object
design.setup()
# start cloning
clmgr.start_duplicate(design)
logging.debug("end clone")
except RuntimeError, e:
fail(e)
except SystemExit, e:
sys.exit(e.code)
except Exception, e:
fail(e)
if __name__ == "__main__":
try:
main()
except SystemExit, e:
sys.exit(e.code)
except KeyboardInterrupt, e:
print >> sys.stderr, _("Installation aborted at user request")
except Exception, e:
logging.exception(e)
sys.exit(1)
|