本文整理汇总了Python中virttest.virsh.snapshot_create_as函数的典型用法代码示例。如果您正苦于以下问题:Python snapshot_create_as函数的具体用法?Python snapshot_create_as怎么用?Python snapshot_create_as使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了snapshot_create_as函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: check_snapshot
def check_snapshot(bgjob=None):
"""
Do snapshot operation and check the results
"""
snapshot_name1 = "snap.s1"
snapshot_name2 = "snap.s2"
if not snapshot_vm_running:
vm.destroy(gracefully=False)
ret = virsh.snapshot_create_as(vm_name, snapshot_name1)
libvirt.check_exit_status(ret)
snap_lists = virsh.snapshot_list(vm_name)
if snapshot_name not in snap_lists:
test.fail("Snapshot %s doesn't exist"
% snapshot_name)
if snapshot_vm_running:
options = "--force"
else:
options = ""
ret = virsh.snapshot_revert(
vm_name, ("%s %s" % (snapshot_name, options)))
libvirt.check_exit_status(ret)
ret = virsh.dumpxml(vm_name)
if ret.stdout.count("<rng model="):
test.fail("Found rng device in xml")
if snapshot_with_rng:
if vm.is_alive():
vm.destroy(gracefully=False)
if bgjob:
bgjob.kill_func()
modify_rng_xml(params, False)
# Start the domain before disk-only snapshot
if vm.is_dead():
# Add random server
if params.get("backend_type") == "tcp":
cmd = "cat /dev/random | nc -4 -l localhost 1024"
bgjob = utils.AsyncJob(cmd)
vm.start()
vm.wait_for_login().close()
err_msgs = ("live disk snapshot not supported"
" with this QEMU binary")
ret = virsh.snapshot_create_as(vm_name,
"%s --disk-only"
% snapshot_name2)
if ret.exit_status:
if ret.stderr.count(err_msgs):
test.skip(err_msgs)
else:
test.fail("Failed to create external snapshot")
snap_lists = virsh.snapshot_list(vm_name)
if snapshot_name2 not in snap_lists:
test.fail("Failed to check snapshot list")
ret = virsh.domblklist(vm_name)
if not ret.stdout.count(snapshot_name2):
test.fail("Failed to find snapshot disk")
示例2: check_bootorder_snapshot
def check_bootorder_snapshot(disk_name):
"""
Check VM disk's bootorder option with snapshot.
:param disk_name. The target disk to be checked.
"""
logging.info("Checking diskorder option with snapshot...")
snapshot1 = "s1"
snapshot2 = "s2"
snapshot2_file = os.path.join(test.tmpdir, "s2")
ret = virsh.snapshot_create(vm_name, "", **virsh_dargs)
libvirt.check_exit_status(ret)
ret = virsh.snapshot_create_as(vm_name, "%s --disk-only" % snapshot1,
**virsh_dargs)
libvirt.check_exit_status(ret)
ret = virsh.snapshot_dumpxml(vm_name, snapshot1)
libvirt.check_exit_status(ret)
cmd = "echo \"%s\" | grep %s.%s" % (ret.stdout, disk_name, snapshot1)
if utils.run(cmd, ignore_status=True).exit_status:
raise error.TestError("Check snapshot disk failed")
ret = virsh.snapshot_create_as(vm_name,
"%s --memspec file=%s,snapshot=external"
% (snapshot2, snapshot2_file),
**virsh_dargs)
libvirt.check_exit_status(ret)
ret = virsh.dumpxml(vm_name)
libvirt.check_exit_status(ret)
cmd = ("echo \"%s\" | grep -A 16 %s.%s | grep \"boot order='%s'\""
% (ret.stdout, disk_name, snapshot2, bootorder))
if utils.run(cmd, ignore_status=True).exit_status:
raise error.TestError("Check snapshot disk with bootorder failed")
snap_lists = virsh.snapshot_list(vm_name)
if snapshot1 not in snap_lists or snapshot2 not in snap_lists:
raise error.TestError("Check snapshot list failed")
# Check virsh save command after snapshot.
save_file = "/tmp/%s.save" % vm_name
ret = virsh.save(vm_name, save_file, **virsh_dargs)
libvirt.check_exit_status(ret)
# Check virsh restore command after snapshot.
ret = virsh.restore(save_file, **virsh_dargs)
libvirt.check_exit_status(ret)
#Passed all test.
os.remove(save_file)
示例3: make_snapshot
def make_snapshot():
"""
make external snapshots.
:return external snapshot path list
"""
logging.info("Making snapshot...")
first_disk_source = vm.get_first_disk_devices()['source']
snapshot_path_list = []
snapshot2_file = os.path.join(data_dir.get_tmp_dir(), "mem.s2")
snapshot3_file = os.path.join(data_dir.get_tmp_dir(), "mem.s3")
snapshot4_file = os.path.join(data_dir.get_tmp_dir(), "mem.s4")
snapshot4_disk_file = os.path.join(data_dir.get_tmp_dir(), "disk.s4")
snapshot5_file = os.path.join(data_dir.get_tmp_dir(), "mem.s5")
snapshot5_disk_file = os.path.join(data_dir.get_tmp_dir(), "disk.s5")
# Attempt to take different types of snapshots.
snapshots_param_dict = {"s1": "s1 --disk-only --no-metadata",
"s2": "s2 --memspec %s --no-metadata" % snapshot2_file,
"s3": "s3 --memspec %s --no-metadata --live" % snapshot3_file,
"s4": "s4 --memspec %s --diskspec vda,file=%s --no-metadata" % (snapshot4_file, snapshot4_disk_file),
"s5": "s5 --memspec %s --diskspec vda,file=%s --live --no-metadata" % (snapshot5_file, snapshot5_disk_file)}
for snapshot_name in sorted(snapshots_param_dict.keys()):
ret = virsh.snapshot_create_as(vm_name, snapshots_param_dict[snapshot_name],
**virsh_dargs)
libvirt.check_exit_status(ret)
if snapshot_name != 's4' and snapshot_name != 's5':
snapshot_path_list.append(first_disk_source.replace('qcow2', snapshot_name))
return snapshot_path_list
示例4: check_snapshot
def check_snapshot():
"""
Test domain snapshot operation.
"""
snapshot1 = "s1"
snapshot2 = "s2"
ret = virsh.snapshot_create_as(vm_name, snapshot1)
libvirt.check_exit_status(ret)
ret = virsh.snapshot_create_as(vm_name, "%s --disk-only --diskspec vda," "file=/tmp/testvm-snap1" % snapshot2)
libvirt.check_exit_status(ret, True)
ret = virsh.snapshot_create_as(
vm_name,
"%s --memspec file=%s,snapshot=external" " --diskspec vda,file=/tmp/testvm-snap2" % (snapshot2, snapshot2),
)
libvirt.check_exit_status(ret, True)
示例5: make_disk_snapshot
def make_disk_snapshot(postfix_n, snapshot_take):
"""
Make external snapshots for disks only.
:param postfix_n: postfix option
:param snapshot_take: snapshots taken.
"""
# Add all disks into command line.
disks = vm.get_disk_devices()
# Make three external snapshots for disks only
for count in range(1, snapshot_take):
options = "%s_%s %s%s-desc " % (postfix_n, count,
postfix_n, count)
options += "--disk-only --atomic --no-metadata"
if needs_agent:
options += " --quiesce"
for disk in disks:
disk_detail = disks[disk]
basename = os.path.basename(disk_detail['source'])
# Remove the original suffix if any, appending
# ".postfix_n[0-9]"
diskname = basename.split(".")[0]
snap_name = "%s.%s%s" % (diskname, postfix_n, count)
disk_external = os.path.join(tmp_dir, snap_name)
snapshot_external_disks.append(disk_external)
options += " %s,snapshot=external,file=%s" % (disk,
disk_external)
cmd_result = virsh.snapshot_create_as(vm_name, options,
ignore_status=True,
debug=True)
status = cmd_result.exit_status
if status != 0:
test.fail("Failed to make snapshots for disks!")
# Create a file flag in VM after each snapshot
flag_file = tempfile.NamedTemporaryFile(prefix=("snapshot_test_"),
dir="/tmp")
file_path = flag_file.name
flag_file.close()
status, output = session.cmd_status_output("touch %s" % file_path)
if status:
test.fail("Touch file in vm failed. %s" % output)
snapshot_flag_files.append(file_path)
示例6: domainsnapshot_validate
def domainsnapshot_validate(vm_name, file=None, **virsh_dargs):
"""
Test for schema domainsnapshot
"""
snapshot_name = "snap-%s-%s" % (vm_name, time.time())
cmd_result = virsh.snapshot_create_as(vm_name, snapshot_name)
libvirt.check_exit_status(cmd_result)
def check_info(s1, s2, errorstr="Values differ"):
if s1 != s2:
error.TestFail("%s (%s != %s)" % (errorstr, s1, s2))
try:
ss_info = virsh.snapshot_info(vm_name, snapshot_name)
check_info(ss_info["Name"], snapshot_name, "Incorrect snapshot name")
check_info(ss_info["Domain"], vm_name, "Incorrect domain name")
except error.CmdError, e:
error.TestFail(str(e))
示例7: check_snapshot
def check_snapshot(snap_option, target_dev='vda'):
"""
Test snapshot operation.
"""
snap_name = "s1"
snap_mem = os.path.join(data_dir.get_tmp_dir(), "rbd.mem")
snap_disk = os.path.join(data_dir.get_tmp_dir(), "rbd.disk")
xml_snap_exp = ["disk name='%s' snapshot='external' type='file'" % target_dev]
xml_dom_exp = ["source file='%s'" % snap_disk,
"backingStore type='network' index='1'",
"source protocol='rbd' name='%s'" % disk_src_name]
if snap_option.count("disk-only"):
options = ("%s --diskspec %s,file=%s --disk-only" %
(snap_name, target_dev, snap_disk))
elif snap_option.count("disk-mem"):
options = ("%s --memspec file=%s --diskspec %s,file="
"%s" % (snap_name, snap_mem, target_dev, snap_disk))
xml_snap_exp.append("memory snapshot='external' file='%s'"
% snap_mem)
else:
options = snap_name
ret = virsh.snapshot_create_as(vm_name, options)
if test_disk_internal_snapshot or test_disk_readonly:
libvirt.check_result(ret, expected_fails=unsupported_err)
else:
libvirt.check_result(ret, skip_if=unsupported_err)
# check xml file.
if not ret.exit_status:
snap_xml = virsh.snapshot_dumpxml(vm_name, snap_name,
debug=True).stdout.strip()
dom_xml = virsh.dumpxml(vm_name, debug=True).stdout.strip()
# Delete snapshots.
libvirt.clean_up_snapshots(vm_name)
if os.path.exists(snap_mem):
os.remove(snap_mem)
if os.path.exists(snap_disk):
os.remove(snap_disk)
if not all([x in snap_xml for x in xml_snap_exp]):
test.fail("Failed to check snapshot xml")
if not all([x in dom_xml for x in xml_dom_exp]):
test.fail("Failed to check domain xml")
示例8: make_disk_snapshot
def make_disk_snapshot():
# Add all disks into commandline.
disks = vm.get_disk_devices()
# Make three external snapshots for disks only
for count in range(1, 4):
options = "snapshot%s snap%s-desc " \
"--disk-only --atomic --no-metadata" % (count, count)
for disk in disks:
disk_detail = disks[disk]
basename = os.path.basename(disk_detail['source'])
# Remove the original suffix if any, appending ".snap[0-9]"
diskname = basename.split(".")[0]
disk_external = os.path.join(tmp_dir,
"%s.snap%s" % (diskname, count))
snapshot_external_disks.append(disk_external)
options += " %s,snapshot=external,file=%s" % (disk,
disk_external)
cmd_result = virsh.snapshot_create_as(vm_name, options,
ignore_status=True,
debug=True)
status = cmd_result.exit_status
if status != 0:
raise error.TestFail("Failed to make snapshots for disks!")
# Create a file flag in VM after each snapshot
flag_file = tempfile.NamedTemporaryFile(prefix=("snapshot_test_"),
dir="/tmp")
file_path = flag_file.name
flag_file.close()
status, output = session.cmd_status_output("touch %s" % file_path)
if status:
raise error.TestFail("Touch file in vm failed. %s" % output)
snapshot_flag_files.append(file_path)
示例9: run
#.........这里部分代码省略.........
if start_vm:
if vm.is_dead():
vm.start()
else:
if not vm.is_dead():
vm.destroy()
attach_option = params.get("attach_option", "")
# Attach the iscsi network disk to domain
logging.debug("Attach disk by XML: %s", open(disk_xml).read())
cmd_result = virsh.attach_device(domainarg=vm_name, filearg=disk_xml,
flagstrs=attach_option,
dargs=virsh_dargs)
libvirt.check_exit_status(cmd_result, status_error)
if vm.is_dead():
vm.start()
cmd_result = virsh.start(vm_name, **virsh_dargs)
libvirt.check_exit_status(cmd_result)
domain_operation = params.get("domain_operation", "")
if domain_operation == "save":
save_file = os.path.join(test.tmpdir, "vm.save")
cmd_result = virsh.save(vm_name, save_file, **virsh_dargs)
libvirt.check_exit_status(cmd_result)
cmd_result = virsh.restore(save_file)
libvirt.check_exit_status(cmd_result)
if os.path.exists(save_file):
os.remove(save_file)
elif domain_operation == "snapshot":
# Run snapshot related commands: snapshot-create-as, snapshot-list
# snapshot-info, snapshot-dumpxml, snapshot-create
snapshot_name1 = "snap1"
snapshot_name2 = "snap2"
cmd_result = virsh.snapshot_create_as(vm_name, snapshot_name1,
**virsh_dargs)
libvirt.check_exit_status(cmd_result)
cmd_result = virsh.snapshot_list(vm_name, **virsh_dargs)
libvirt.check_exit_status(cmd_result)
cmd_result = virsh.snapshot_info(vm_name, snapshot_name1,
**virsh_dargs)
libvirt.check_exit_status(cmd_result)
cmd_result = virsh.snapshot_dumpxml(vm_name, snapshot_name1,
**virsh_dargs)
libvirt.check_exit_status(cmd_result)
cmd_result = virsh.snapshot_create(vm_name, **virsh_dargs)
libvirt.check_exit_status(cmd_result)
cmd_result = virsh.snapshot_current(vm_name, **virsh_dargs)
libvirt.check_exit_status(cmd_result)
sn_create_op = "%s --disk_ony %s" % (snapshot_name2, disk_target)
cmd_result = virsh.snapshot_create_as(vm_name, sn_create_op,
**virsh_dargs)
libvirt.check_exit_status(cmd_result)
cmd_result = virsh.snapshot_revert(vm_name, snapshot_name1,
**virsh_dargs)
cmd_result = virsh.snapshot_list(vm_name, **virsh_dargs)
libvirt.check_exit_status(cmd_result)
cmd_result = virsh.snapshot_delete(vm_name, snapshot_name2,
示例10: run
#.........这里部分代码省略.........
emulated_iscsi.append(blkdev_n)
else:
if copy_to_nfs:
tmp_dir = "%s/%s" % (tmp_dir, mnt_path_name)
dest_path = os.path.join(tmp_dir, tmp_file)
# Domain disk replacement with desire type
if replace_vm_disk:
# Calling 'set_vm_disk' is bad idea as it left lots of cleanup jobs
# after test, such as pool, volume, nfs, iscsi and so on
# TODO: remove this function in the future
utl.set_vm_disk(vm, params, tmp_dir, test)
emulated_iscsi.append(emu_image)
new_xml = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name)
if with_shallow:
_make_snapshot()
# Prepare transient/persistent vm
if persistent_vm == "no" and vm.is_persistent():
vm.undefine()
elif persistent_vm == "yes" and not vm.is_persistent():
new_xml.define()
# Run blockcopy command
if rerun_flag == 1:
options1 = "--wait %s --finish --verbose" % dest_format
if with_blockdev:
options1 += " --blockdev"
if with_shallow:
options1 += " --shallow"
cmd_result = virsh.blockcopy(vm_name, target,
dest_path, options1,
**extra_dict)
status = cmd_result.exit_status
if status != 0:
raise exceptions.TestFail("Run blockcopy command fail")
elif not os.path.exists(dest_path):
raise exceptions.TestFail("Cannot find the created copy")
cmd_result = utils_misc.wait_for(_blockcopy_cmd, 10)
if not cmd_result:
raise exceptions.TestFail("Run blockcopy command fail")
status = 0
else:
cmd_result = virsh.blockcopy(vm_name, target, dest_path,
options, **extra_dict)
_blockjob_and_libvirtd_chk(cmd_result)
status = cmd_result.exit_status
if not libvirtd_utl.is_running():
raise exceptions.TestFail("Libvirtd service is dead")
if not status_error:
if status == 0:
ret = utils_misc.wait_for(
lambda: check_xml(vm_name, target, dest_path, options), 5)
if not ret:
raise exceptions.TestFail("Domain xml not expected after"
" blockcopy")
if options.count("--bandwidth"):
utl.check_blockjob(vm_name, target, "bandwidth", bandwidth)
if check_state_lock:
# Run blockjob pivot in subprocess as it will hang
# for a while, run blockjob info again to check
# job state
command = "virsh blockjob %s %s --pivot" % (vm_name,
target)
session = aexpect.ShellSession(command)
ret = virsh.blockjob(vm_name, target, "--info")
err_info = "cannot acquire state change lock"
if err_info in ret.stderr:
raise exceptions.TestFail("Hit on bug: %s" % bug_url)
utl.check_exit_status(ret, status_error)
session.close()
val = options.count("--pivot") + options.count("--finish")
if val == 0:
try:
finish_job(vm_name, target, default_timeout)
except JobTimeout, excpt:
raise exceptions.TestFail("Run command failed: %s" %
excpt)
if options.count("--raw") and not with_blockdev:
check_format(dest_path, dest_extension, dest_format)
if active_snap:
snap_path = "%s/%s.snap" % (tmp_dir, vm_name)
snap_opt = "--disk-only --atomic --no-metadata "
snap_opt += "vda,snapshot=external,file=%s" % snap_path
ret = virsh.snapshot_create_as(vm_name, snap_opt,
ignore_statues=True,
debug=True)
utl.check_exit_status(ret, active_error)
if active_save:
save_path = "%s/%s.save" % (tmp_dir, vm_name)
ret = virsh.save(vm_name, save_path,
ignore_statues=True,
debug=True)
utl.check_exit_status(ret, active_error)
else:
raise exceptions.TestFail(cmd_result.stderr)
else:
示例11: run
#.........这里部分代码省略.........
disk_params.update(disk_params_src)
if chap_auth:
disk_params_auth = {'auth_user': chap_user,
'secret_type': disk_src_protocol,
'secret_usage': secret_xml.target}
disk_params.update(disk_params_auth)
disk_xml = libvirt.create_disk_xml(disk_params)
attach_option = params.get("attach_option", "")
cmd_result = virsh.attach_device(domainarg=vm_name, filearg=disk_xml,
flagstr=attach_option,
dargs=virsh_dargs)
libvirt.check_exit_status(cmd_result, status_error)
if vm.is_dead():
cmd_result = virsh.start(vm_name, **virsh_dargs)
libvirt.check_exit_status(cmd_result)
# Wait for domain is stable
vm.wait_for_login().close()
domain_operation = params.get("domain_operation", "")
if domain_operation == "save":
save_file = os.path.join(data_dir.get_tmp_dir(), "vm.save")
cmd_result = virsh.save(vm_name, save_file, **virsh_dargs)
libvirt.check_exit_status(cmd_result)
cmd_result = virsh.restore(save_file)
libvirt.check_exit_status(cmd_result)
if os.path.exists(save_file):
os.remove(save_file)
elif domain_operation == "snapshot":
# Run snapshot related commands: snapshot-create-as, snapshot-list
# snapshot-info, snapshot-dumpxml, snapshot-create
snapshot_name1 = "snap1"
snapshot_name2 = "snap2"
cmd_result = virsh.snapshot_create_as(vm_name, snapshot_name1,
**virsh_dargs)
libvirt.check_exit_status(cmd_result)
try:
virsh.snapshot_list(vm_name, **virsh_dargs)
except process.CmdError:
test.fail("Failed getting snapshots list for %s" % vm_name)
try:
virsh.snapshot_info(vm_name, snapshot_name1, **virsh_dargs)
except process.CmdError:
test.fail("Failed getting snapshots info for %s" % vm_name)
cmd_result = virsh.snapshot_dumpxml(vm_name, snapshot_name1,
**virsh_dargs)
libvirt.check_exit_status(cmd_result)
cmd_result = virsh.snapshot_create(vm_name, **virsh_dargs)
libvirt.check_exit_status(cmd_result)
cmd_result = virsh.snapshot_current(vm_name, **virsh_dargs)
libvirt.check_exit_status(cmd_result)
snapshot_file = os.path.join(data_dir.get_tmp_dir(), snapshot_name2)
sn_create_op = ("%s --disk-only --diskspec %s,file=%s"
% (snapshot_name2, disk_target, snapshot_file))
cmd_result = virsh.snapshot_create_as(vm_name, sn_create_op,
**virsh_dargs)
libvirt.check_exit_status(cmd_result)
cmd_result = virsh.snapshot_revert(vm_name, snapshot_name1,
**virsh_dargs)
示例12: run
#.........这里部分代码省略.........
if policy_only:
check_policy_update(startup_policy, update_policy_list,
disk_xml_policy_file, device_type, attach_option)
else:
check_source_update(disk_xml_policy_file)
elif disk_type == "file":
# Step 1. Start domain and destroy it normally
vm.start()
vm.destroy()
# Step 2. Remove the source_file then start the domain
rename_file(media_file, media_file_new)
result = virsh.start(vm_name, **virsh_dargs)
libvirt.check_exit_status(result, expect_error=start_error)
# For libvirt version >=2.0.0, feature is updated and startup policy attribute
# can not exist alone without source protocol.
if not start_error and not libvirt_version.version_compare(2, 0, 0):
check_disk_source(vm_name, target_dev, expect_value)
# Step 3. Move back the source file and start the domain(if needed).
rename_file(media_file, media_file_new, revert=True)
if not vm.is_alive():
vm.start()
# Step 4. Save the domain normally, then remove the source file
# and restore it back
vm.save_to_file(save_file)
rename_file(media_file, media_file_new)
result = virsh.restore(save_file, **virsh_dargs)
libvirt.check_exit_status(result, expect_error=restore_error)
if not restore_error and not libvirt_version.version_compare(2, 0, 0):
check_disk_source(vm_name, target_dev, expect_value)
# Step 5. Move back the source file and restore the domain(if needed)
rename_file(media_file, media_file_new, revert=True)
if not vm.is_alive():
result = virsh.restore(save_file, **virsh_dargs)
libvirt.check_exit_status(result, expect_error=False)
elif disk_type == "volume":
# Step 1. Start domain and destroy it normally.
vm.start()
# Step 1 Start VM successfully.
if not check_in_vm(old_parts):
test.fail("Check disk partitions in VM failed")
# Step 2 Move the volume to other place, refresh the pool, then reboot the guest.
rename_file(vol_path, vol_path_new)
cmd_result = virsh.pool_refresh(pool_name)
libvirt.check_exit_status(cmd_result)
vm.destroy()
result = virsh.start(vm_name, **virsh_dargs)
libvirt.check_exit_status(result, expect_error=start_error)
# Step 3 Move back the source file and start.
rename_file(vol_path, vol_path_new, revert=True)
cmd_result = virsh.pool_refresh(pool_name)
libvirt.check_exit_status(cmd_result)
if not vm.is_alive():
vm.start()
# Step 4 Save the domain normally, then remove the source file,then restore domain.
vm.save_to_file(save_file)
rename_file(vol_path, vol_path_new)
cmd_result = virsh.pool_refresh(pool_name)
libvirt.check_exit_status(cmd_result)
result = virsh.restore(save_file, **virsh_dargs)
libvirt.check_exit_status(result, expect_error=restore_error)
# Step 5, Create snapshot,move the source to other place,then revert snapshot.
if device_type == "disk":
rename_file(vol_path, vol_path_new, revert=True)
cmd_result = virsh.pool_refresh(pool_name)
libvirt.check_exit_status(cmd_result)
if restore_error:
result = virsh.restore(save_file, **virsh_dargs)
libvirt.check_exit_status(result)
ret = virsh.snapshot_create_as(vm_name, snapshot_name, **virsh_dargs)
libvirt.check_exit_status(ret)
rename_file(vol_path, vol_path_new)
ret = virsh.snapshot_revert(vm_name, snapshot_name, **virsh_dargs)
# Clean up snapshot.
libvirt.clean_up_snapshots(vm_name, domxml=vmxml_backup)
finally:
# Recover VM.
if vm.is_alive():
vm.destroy(gracefully=False)
vmxml_backup.sync()
if disk_type == "volume":
pvt.cleanup_pool(pool_name, pool_type, pool_target,
emulated_image, **virsh_dargs)
if os.path.exists(save_file):
os.remove(save_file)
if os.path.exists(disk_xml_file):
os.remove(disk_xml_file)
if os.path.exists(media_file):
os.remove(media_file)
if os.path.exists(disk_xml_policy_file):
os.remove(disk_xml_policy_file)
示例13: run
#.........这里部分代码省略.........
raise error.TestFail("%s format is not %s." % (dest_path, expect))
def blockcopy_chk():
"""
Raise TestFail when blockcopy hang with state change lock
"""
err_pattern = "Timed out during operation: cannot acquire"
err_pattern += " state change lock"
ret = chk_libvirtd_log(libvirtd_log_path, err_pattern, "error")
if ret:
raise error.TestFail("Hit on bug: %s" % bug_url)
snap_path = ''
save_path = ''
try:
# Domain disk replacement with desire type
if replace_vm_disk:
utl.set_vm_disk(vm, params, tmp_dir)
new_xml = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name)
# Prepare transient/persistent vm
if persistent_vm == "no" and vm.is_persistent():
vm.undefine()
elif persistent_vm == "yes" and not vm.is_persistent():
new_xml.define()
# Run blockcopy command
if rerun_flag == 1:
options1 = "--wait --raw --finish --verbose"
cmd_result = virsh.blockcopy(vm_name, target,
dest_path, options1,
**extra_dict)
status = cmd_result.exit_status
if status != 0:
raise error.TestFail("Run blockcopy command fail.")
elif not os.path.exists(dest_path):
raise error.TestFail("Cannot find the created copy.")
cmd_result = virsh.blockcopy(vm_name, target, dest_path,
options,
**extra_dict)
status = cmd_result.exit_status
if not libvirtd_utl.is_running():
raise error.TestFail("Libvirtd service is dead.")
if not status_error:
blockcopy_chk()
if status == 0:
ret = utils_misc.wait_for(
lambda: check_xml(vm_name, target, dest_path, options), 5)
if not ret:
raise error.TestFail("Domain xml not expected after"
" blockcopy")
if options.count("--bandwidth"):
utl.check_blockjob(vm_name, target,
"bandwidth", bandwidth)
if check_state_lock:
# Run blockjob pivot in subprocess as it will hang
# for a while, run blockjob info again to check
# job state
command = "virsh blockjob %s %s --pivot" % (vm_name,
target)
session = aexpect.ShellSession(command)
ret = virsh.blockjob(vm_name, target, "--info")
err_info = "cannot acquire state change lock"
if err_info in ret.stderr:
raise error.TestFail("Hit on bug: %s" % bug_url)
utl.check_exit_status(ret, status_error)
session.close()
val = options.count("--pivot") + options.count("--finish")
if val == 0:
try:
finish_job(vm_name, target, default_timeout)
except JobTimeout, excpt:
raise error.TestFail("Run command failed: %s" %
excpt)
if options.count("--raw"):
check_format(dest_path, dest_extension, dest_format)
if active_snap:
snap_path = "%s/%s.snap" % (tmp_dir, vm_name)
snap_opt = "--disk-only --atomic --no-metadata "
snap_opt += "vda,snapshot=external,file=%s" % snap_path
ret = virsh.snapshot_create_as(vm_name, snap_opt,
ignore_statues=True,
debug=True)
utl.check_exit_status(ret, active_error)
if active_save:
save_path = "%s/%s.save" % (tmp_dir, vm_name)
ret = virsh.save(vm_name, save_path,
ignore_statues=True,
debug=True)
utl.check_exit_status(ret, active_error)
else:
err_msg = "internal error: unable to execute QEMU command"
err_msg += " 'block-job-complete'"
if err_msg in cmd_result.stderr:
raise error.TestFail("Hit on bug: %s" % bug_url)
raise error.TestFail(cmd_result.stderr)
else:
示例14: mkfs_and_mount
mkfs_and_mount(session, mount_disk)
create_file_in_vm(session, "/mnt/before_snapshot.txt", "before")
# virsh snapshot-create-as vm s --disk-only --diskspec vda,file=path
if snapshot_disk_only:
vm_blks = get_vm_blks(vm_name)
options = "%s --disk-only" % snapshot_name
for vm_blk in vm_blks:
snapshot_file = snapshot_dir + "/" + vm_blk + "." + snapshot_name
if os.path.exists(snapshot_file):
os.remove(snapshot_file)
options = options + " --diskspec %s,file=%s" % (vm_blk,
snapshot_file)
else:
options = snapshot_name
utlv.check_exit_status(virsh.snapshot_create_as(vm_name, options))
# check virsh snapshot-list
logging.debug("Running: snapshot-list %s", vm_name)
snapshot_list = virsh.snapshot_list(vm_name)
logging.debug("snapshot list is: %s", snapshot_list)
if not snapshot_list:
raise exceptions.TestFail("snapshots not found after creation.")
# snapshot-revert doesn't support external snapshot for now. so
# only check this with internal snapshot.
if not snapshot_disk_only:
create_file_in_vm(session, "/mnt/after_snapshot.txt", "after")
logging.debug("Running: snapshot-revert %s %s",
vm_name, snapshot_name)
utlv.check_exit_status(virsh.snapshot_revert(vm_name, snapshot_name))
示例15: run
#.........这里部分代码省略.........
"guest")
if domain_state == "paused":
virsh.suspend(vm_name)
else:
# Remove channel if exist
if vm.is_alive():
vm.destroy(gracefully=False)
xml_inst = vm_xml.VMXML.new_from_dumpxml(vm_name)
xml_inst.remove_agent_channels()
vm.start()
# Record the previous snapshot-list
snaps_before = virsh.snapshot_list(vm_name)
# Attach disk before create snapshot if not print xml and multi disks
# specified in cfg
if dnum > 1 and "--print-xml" not in options:
for i in range(1, dnum):
disk_path = os.path.join(test.tmpdir, 'disk%s.qcow2' % i)
utils.run("qemu-img create -f qcow2 %s 200M" % disk_path)
virsh.attach_disk(vm_name, disk_path,
'vd%s' % list(string.lowercase)[i],
debug=True)
# Run virsh command
# May create several snapshots, according to configuration
for count in range(int(multi_num)):
if create_autodestroy:
# Run virsh command in interactive mode
vmxml_backup.undefine()
vp = virsh.VirshPersistent()
vp.create(vmxml_backup['xml'], '--autodestroy')
cmd_result = vp.snapshot_create_as(vm_name, options,
ignore_status=True,
debug=True)
vp.close_session()
vmxml_backup.define()
else:
cmd_result = virsh.snapshot_create_as(vm_name, options,
unprivileged_user=usr,
uri=uri,
ignore_status=True,
debug=True)
# for multi snapshots without specific snapshot name, the
# snapshot name is using time string with 1 second
# incremental, to avoid get snapshot failure with same name,
# sleep 1 second here.
if int(multi_num) > 1:
time.sleep(1.1)
output = cmd_result.stdout.strip()
status = cmd_result.exit_status
# check status_error
if status_error == "yes":
if status == 0:
raise error.TestFail("Run successfully with wrong command!")
else:
# Check memspec file should be removed if failed
if (options.find("memspec") >= 0 and
options.find("atomic") >= 0):
if os.path.isfile(option_dict['memspec']):
os.remove(option_dict['memspec'])
raise error.TestFail("Run failed but file %s exist"
% option_dict['memspec'])
else: