本文整理汇总了Python中virttest.libvirt_xml.devices.disk.Disk.new_disk_source方法的典型用法代码示例。如果您正苦于以下问题:Python Disk.new_disk_source方法的具体用法?Python Disk.new_disk_source怎么用?Python Disk.new_disk_source使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类virttest.libvirt_xml.devices.disk.Disk
的用法示例。
在下文中一共展示了Disk.new_disk_source方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: build_disk_xml
# 需要导入模块: from virttest.libvirt_xml.devices.disk import Disk [as 别名]
# 或者: from virttest.libvirt_xml.devices.disk.Disk import new_disk_source [as 别名]
def build_disk_xml(disk_img, disk_format, host_ip):
"""
Try to rebuild disk xml
"""
# Delete existed disks first.
vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name)
disks_dev = vmxml.get_devices(device_type="disk")
for disk in disks_dev:
vmxml.del_device(disk)
if default_pool:
disk_xml = Disk(type_name="file")
else:
disk_xml = Disk(type_name="network")
disk_xml.device = "disk"
driver_dict = {"name": "qemu",
"type": disk_format,
"cache": "none"}
if driver_iothread:
driver_dict.update({"iothread": driver_iothread})
disk_xml.driver = driver_dict
disk_xml.target = {"dev": "vda", "bus": "virtio"}
if default_pool:
utils_misc.mount("%s:%s" % (host_ip, vol_name),
default_pool, "glusterfs")
process.run("setsebool virt_use_fusefs on", shell=True)
virsh.pool_refresh("default")
source_dict = {"file": "%s/%s" % (default_pool, disk_img)}
disk_xml.source = disk_xml.new_disk_source(
**{"attrs": source_dict})
else:
source_dict = {"protocol": "gluster",
"name": "%s/%s" % (vol_name, disk_img)}
host_dict = {"name": host_ip, "port": "24007"}
if transport:
host_dict.update({"transport": transport})
disk_xml.source = disk_xml.new_disk_source(
**{"attrs": source_dict, "hosts": [host_dict]})
# set domain options
if dom_iothreads:
try:
vmxml.iothreads = int(dom_iothreads)
except ValueError:
# 'iothreads' may not invalid number in negative tests
logging.debug("Can't convert '%s' to integer type"
% dom_iothreads)
# Add the new disk xml.
vmxml.add_device(disk_xml)
vmxml.sync()
示例2: recompose_xml
# 需要导入模块: from virttest.libvirt_xml.devices.disk import Disk [as 别名]
# 或者: from virttest.libvirt_xml.devices.disk.Disk import new_disk_source [as 别名]
def recompose_xml(vm_name, scsi_disk):
"""
Add scsi disk, guest agent and scsi controller for guest
:param: vm_name: Name of domain
:param: scsi_disk: scsi_debug disk name
"""
# Get disk path of scsi_disk
path_cmd = "udevadm info --name %s | grep /dev/disk/by-path/ | " \
"cut -d' ' -f4" % scsi_disk
disk_path = utils.run(path_cmd).stdout.strip()
# Add qemu guest agent in guest xml
vm_xml.VMXML.set_agent_channel(vm_name)
vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name)
# Add scsi disk xml
scsi_disk = Disk(type_name="block")
scsi_disk.device = "lun"
scsi_disk.source = scsi_disk.new_disk_source(
**{'attrs': {'dev': disk_path}})
scsi_disk.target = {'dev': "sdb", 'bus': "scsi"}
vmxml.add_device(scsi_disk)
# Add scsi disk controller
scsi_controller = Controller("controller")
scsi_controller.type = "scsi"
scsi_controller.index = "0"
scsi_controller.model = "virtio-scsi"
vmxml.add_device(scsi_controller)
# Redefine guest
vmxml.sync()
示例3: recompose_xml
# 需要导入模块: from virttest.libvirt_xml.devices.disk import Disk [as 别名]
# 或者: from virttest.libvirt_xml.devices.disk.Disk import new_disk_source [as 别名]
def recompose_xml(vm_name, scsi_disk):
"""
Add scsi disk, guest agent and scsi controller for guest
:param: vm_name: Name of domain
:param: scsi_disk: scsi_debug disk name
"""
vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name)
disk_path = scsi_disk
# Add scsi disk xml
scsi_disk = Disk(type_name="block")
scsi_disk.device = "lun"
scsi_disk.source = scsi_disk.new_disk_source(
**{'attrs': {'dev': disk_path}})
scsi_disk.target = {'dev': "sdb", 'bus': "scsi"}
find_scsi = "no"
controllers = vmxml.xmltreefile.findall("devices/controller")
for controller in controllers:
if controller.get("type") == "scsi":
find_scsi = "yes"
vmxml.add_device(scsi_disk)
# Add scsi disk controller
if find_scsi == "no":
scsi_controller = Controller("controller")
scsi_controller.type = "scsi"
scsi_controller.index = "0"
scsi_controller.model = "virtio-scsi"
vmxml.add_device(scsi_controller)
# Redefine guest
vmxml.sync()
示例4: build_disk_xml
# 需要导入模块: from virttest.libvirt_xml.devices.disk import Disk [as 别名]
# 或者: from virttest.libvirt_xml.devices.disk.Disk import new_disk_source [as 别名]
def build_disk_xml(disk_img, disk_format, host_ip):
"""
Try to rebuild disk xml
"""
# Delete existed disks first.
vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name)
disks_dev = vmxml.get_devices(device_type="disk")
for disk in disks_dev:
vmxml.del_device(disk)
if default_pool:
disk_xml = Disk(type_name="file")
else:
disk_xml = Disk(type_name="network")
disk_xml.device = "disk"
driver_dict = {"name": "qemu",
"type": disk_format,
"cache": "none"}
disk_xml.driver = driver_dict
disk_xml.target = {"dev": "vda", "bus": "virtio"}
if default_pool:
utils.run("mount -t glusterfs %s:%s %s; setsebool virt_use_fusefs on" %
(host_ip, vol_name, default_pool))
virsh.pool_refresh("default")
source_dict = {"file": "%s/%s" % (default_pool, disk_img)}
disk_xml.source = disk_xml.new_disk_source(
**{"attrs": source_dict})
else:
source_dict = {"protocol": "gluster",
"name": "%s/%s" % (vol_name, disk_img)}
host_dict = {"name": host_ip, "port": "24007"}
if transport:
host_dict.update({"transport": transport})
disk_xml.source = disk_xml.new_disk_source(
**{"attrs": source_dict, "hosts": [host_dict]})
# Add the new disk xml.
vmxml.add_device(disk_xml)
vmxml.sync()
示例5: build_disk_xml
# 需要导入模块: from virttest.libvirt_xml.devices.disk import Disk [as 别名]
# 或者: from virttest.libvirt_xml.devices.disk.Disk import new_disk_source [as 别名]
def build_disk_xml(disk_img, disk_format, host_ip):
"""
Try to rebuild disk xml
"""
if default_pool:
disk_xml = Disk(type_name="file")
else:
disk_xml = Disk(type_name="network")
disk_xml.device = "disk"
driver_dict = {"name": "qemu",
"type": disk_format,
"cache": "none"}
if driver_iothread:
driver_dict.update({"iothread": driver_iothread})
disk_xml.driver = driver_dict
disk_xml.target = {"dev": "vdb", "bus": "virtio"}
if default_pool:
utils_misc.mount("%s:%s" % (host_ip, vol_name),
default_pool, "glusterfs")
process.run("setsebool virt_use_fusefs on", shell=True)
source_dict = {"file": "%s/%s" % (default_pool, disk_img)}
disk_xml.source = disk_xml.new_disk_source(
**{"attrs": source_dict})
else:
source_dict = {"protocol": "gluster",
"name": "%s/%s" % (vol_name, disk_img)}
host_dict = [{"name": host_ip, "port": "24007"}]
# If mutiple_hosts is True, attempt to add multiple hosts.
if multiple_hosts:
host_dict.append({"name": params.get("dummy_host1"), "port": "24007"})
host_dict.append({"name": params.get("dummy_host2"), "port": "24007"})
if transport:
host_dict[0]['transport'] = transport
disk_xml.source = disk_xml.new_disk_source(
**{"attrs": source_dict, "hosts": host_dict})
return disk_xml
示例6: get_vm_disk_xml
# 需要导入模块: from virttest.libvirt_xml.devices.disk import Disk [as 别名]
# 或者: from virttest.libvirt_xml.devices.disk.Disk import new_disk_source [as 别名]
def get_vm_disk_xml(dev_type, dev_name, **options):
"""
Create a disk xml object and return it.
:param dev_type. Disk type.
:param dev_name. Disk device name.
:param options. Disk options.
:return: Disk xml object.
"""
# Create disk xml
disk_xml = Disk(type_name=dev_type)
disk_xml.device = options["disk_device"]
if options.has_key("sgio") and options["sgio"] != "":
disk_xml.sgio = options["sgio"]
disk_xml.device = "lun"
disk_xml.rawio = "no"
if dev_type == "block":
disk_attr = "dev"
else:
disk_attr = "file"
disk_xml.target = {'dev': options["target"],
'bus': options["bus"]}
disk_xml.source = disk_xml.new_disk_source(
**{'attrs': {disk_attr: dev_name}})
# Add driver options from parameters.
driver_dict = {"name": "qemu"}
if options.has_key("driver"):
for driver_option in options["driver"].split(','):
if driver_option != "":
d = driver_option.split('=')
logging.debug("disk driver option: %s=%s", d[0], d[1])
driver_dict.update({d[0].strip(): d[1].strip()})
disk_xml.driver = driver_dict
if options.has_key("share"):
if options["share"] == "shareable":
disk_xml.share = True
if options.has_key("readonly"):
if options["readonly"] == "readonly":
disk_xml.readonly = True
logging.debug("The disk xml is: %s" % disk_xml.xmltreefile)
return disk_xml
示例7: add_cdrom_device
# 需要导入模块: from virttest.libvirt_xml.devices.disk import Disk [as 别名]
# 或者: from virttest.libvirt_xml.devices.disk.Disk import new_disk_source [as 别名]
def add_cdrom_device(v_xml, iso_file, target_dev, device_bus):
"""
Add cdrom disk in VM XML
:param v_xml: The instance of VMXML class
:param iso_file: The iso file path
:param target_dev: The target dev in Disk XML
:param device_bus: The target bus in Disk XML
"""
disk_xml = Disk(type_name="file")
disk_xml.device = "cdrom"
disk_xml.target = {"dev": target_dev, "bus": device_bus}
disk_xml.driver = {"name": "qemu", "type": "raw"}
src_dict = {"file": iso_file}
disk_xml.source = disk_xml.new_disk_source(
**{"attrs": src_dict})
disk_xml.readonly = False
v_xml.add_device(disk_xml)
return v_xml
示例8: prepare_virt_disk_xml
# 需要导入模块: from virttest.libvirt_xml.devices.disk import Disk [as 别名]
# 或者: from virttest.libvirt_xml.devices.disk.Disk import new_disk_source [as 别名]
def prepare_virt_disk_xml(image_path):
"""
Prepare the virtual disk xml to be attached/detached.
:param image_path: The path to the local image.
:return: The virtual disk xml.
"""
virt_disk_device = params.get("virt_disk_device", "disk")
virt_disk_device_type = params.get("virt_disk_device_type", "file")
virt_disk_device_format = params.get("virt_disk_device_format", "raw")
virt_disk_device_target = params.get("virt_disk_device_target", "sdb")
virt_disk_device_bus = params.get("virt_disk_device_bus", "usb")
disk_xml = Disk(type_name=virt_disk_device_type)
disk_xml.device = virt_disk_device
disk_src_dict = {'attrs': {'file': image_path, 'type_name': 'file'}}
disk_xml.source = disk_xml.new_disk_source(**disk_src_dict)
driver_dict = {"name": "qemu", "type": virt_disk_device_format}
disk_xml.driver = driver_dict
disk_xml.target = {"dev": virt_disk_device_target,
"bus": virt_disk_device_bus}
return disk_xml
示例9: run
# 需要导入模块: from virttest.libvirt_xml.devices.disk import Disk [as 别名]
# 或者: from virttest.libvirt_xml.devices.disk.Disk import new_disk_source [as 别名]
#.........这里部分代码省略.........
cmd = ("qemu-img create -f qcow2 iscsi://%s:%s/%s/%s %s"
% (iscsi_host, iscsi_port, iscsi_target, lun_num, emulated_size))
process.run(cmd, shell=True)
# Add disk xml.
vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name)
disk_xml = Disk(type_name=device_type)
disk_xml.device = device
disk_xml.target = {"dev": device_target, "bus": device_bus}
driver_dict = {"name": "qemu", "type": device_format}
# For lun type device, iothread attribute need to be set in controller.
if driver_iothread and device != "lun":
driver_dict.update({"iothread": driver_iothread})
vmxml.iothreads = int(driver_iothread)
elif driver_iothread:
vmxml.iothreads = int(driver_iothread)
disk_xml.driver = driver_dict
# Check if we want to use a faked uuid.
if not uuid:
uuid = secret_uuid
auth_dict = {}
if auth_uuid:
auth_dict = {"auth_user": chap_user,
"secret_type": secret_usage_type,
"secret_uuid": uuid}
elif auth_usage:
auth_dict = {"auth_user": chap_user,
"secret_type": secret_usage_type,
"secret_usage": secret_usage_target}
disk_source = disk_xml.new_disk_source(
**{"attrs": {"protocol": "iscsi",
"name": "%s/%s" % (iscsi_target, lun_num)},
"hosts": [{"name": iscsi_host, "port": iscsi_port}]})
if auth_dict:
disk_auth = disk_xml.new_auth(**auth_dict)
if 'source' in auth_place_in_location:
disk_source.auth = disk_auth
if 'disk' in auth_place_in_location:
disk_xml.auth = disk_auth
disk_xml.source = disk_source
# Sync VM xml.
vmxml.add_device(disk_xml)
# After virtio 1.0 is enabled, lun type device need use virtio-scsi
# instead of virtio, so additional controller is needed.
# Add controller.
if device == "lun":
ctrl = Controller(type_name=cntlr_type)
if cntlr_model is not None:
ctrl.model = cntlr_model
if cntlr_index is not None:
ctrl.index = cntlr_index
ctrl_addr_dict = {}
for addr_option in controller_addr_options.split(','):
if addr_option != "":
addr_part = addr_option.split('=')
ctrl_addr_dict.update({addr_part[0].strip(): addr_part[1].strip()})
ctrl.address = ctrl.new_controller_address(attrs=ctrl_addr_dict)
# If driver_iothread is true, need add iothread attribute in controller.
if driver_iothread:
示例10: len
# 需要导入模块: from virttest.libvirt_xml.devices.disk import Disk [as 别名]
# 或者: from virttest.libvirt_xml.devices.disk.Disk import new_disk_source [as 别名]
else:
disk_source = disks[i]["source"]
disk_xml.device = devices[i]
if device_with_source:
if device_types[i] == "file":
dev_attrs = "file"
elif device_types[i] == "dir":
dev_attrs = "dir"
else:
dev_attrs = "dev"
source_dict = {dev_attrs: disk_source}
if len(startup_policy) > i:
source_dict.update({"startupPolicy": startup_policy[i]})
disk_xml.source = disk_xml.new_disk_source(
**{"attrs": source_dict})
if len(device_bootorder) > i:
disk_xml.boot = device_bootorder[i]
if test_block_size:
disk_xml.blockio = {"logical_block_size": logical_block_size,
"physical_block_size": physical_block_size}
if wwn != "":
disk_xml.wwn = wwn
if serial != "":
disk_xml.serial = serial
if vendor != "":
disk_xml.vendor = vendor
if product != "":
示例11: run
# 需要导入模块: from virttest.libvirt_xml.devices.disk import Disk [as 别名]
# 或者: from virttest.libvirt_xml.devices.disk.Disk import new_disk_source [as 别名]
#.........这里部分代码省略.........
attach_cmd = "drive_add"
attach_cmd += " 0 id=drive-usb-disk%s,if=none,file=%s" % (i, path)
result = virsh.qemu_monitor_command(vm_name, attach_cmd, options=options)
if result.exit_status:
raise process.CmdError(result.command, result)
if keyboard:
attach_cmd = "device_add"
attach_cmd += " usb-kdb,bus=usb1.0,id=kdb"
result = virsh.qemu_monitor_command(vm_name, attach_cmd, options=options)
if result.exit_status:
raise process.CmdError(result.command, result)
if mouse:
attach_cmd = "device_add"
attach_cmd += " usb-mouse,bus=usb1.0,id=mouse"
result = virsh.qemu_monitor_command(vm_name, attach_cmd, options=options)
if result.exit_status:
raise process.CmdError(result.command, result)
if tablet:
attach_cmd = "device_add"
attach_cmd += " usb-tablet,bus=usb1.0,id=tablet"
result = virsh.qemu_monitor_command(vm_name, attach_cmd, options=options)
if result.exit_status:
raise process.CmdError(result.command, result)
else:
if disk:
utils_test.libvirt.create_local_disk("file", path, size="1M")
os.chmod(path, 0666)
disk_xml = Disk(type_name="file")
disk_xml.device = "disk"
disk_xml.source = disk_xml.new_disk_source(**{"attrs": {"file": path}})
disk_xml.driver = {"name": "qemu", "type": "raw", "cache": "none"}
disk_xml.target = {"dev": "sdb", "bus": "usb"}
attributes = {"type_name": "usb", "bus": "1", "port": "0"}
disk_xml.address = disk_xml.new_disk_address(**{"attrs": attributes})
result = virsh.attach_device(vm_name, disk_xml.xml)
if result.exit_status:
raise process.CmdError(result.command, result)
if mouse:
mouse_xml = Input("mouse")
mouse_xml.input_bus = "usb"
attributes = {"type_name": "usb", "bus": "1", "port": "0"}
mouse_xml.address = mouse_xml.new_input_address(**{"attrs": attributes})
result = virsh.attach_device(vm_name, mouse_xml.xml)
if result.exit_status:
raise process.CmdError(result.command, result)
if tablet:
tablet_xml = Input("tablet")
tablet_xml.input_bus = "usb"
attributes = {"type_name": "usb", "bus": "1", "port": "0"}
tablet_xml.address = tablet_xml.new_input_address(**{"attrs": attributes})
result = virsh.attach_device(vm_name, tablet_xml.xml)
if result.exit_status:
raise process.CmdError(result.command, result)
if keyboard:
kbd_xml = Input("keyboard")
kbd_xml.input_bus = "usb"
attributes = {"type_name": "usb", "bus": "1", "port": "0"}
kbd_xml.address = kbd_xml.new_input_address(**{"attrs": attributes})
示例12: run
# 需要导入模块: from virttest.libvirt_xml.devices.disk import Disk [as 别名]
# 或者: from virttest.libvirt_xml.devices.disk.Disk import new_disk_source [as 别名]
#.........这里部分代码省略.........
if not enable_namespace:
qemu_conf.namespaces = ''
logging.debug("the qemu.conf content is: %s" % qemu_conf)
libvirtd.restart()
else:
source_type = "file"
# set host_sestatus as nfs pool will reset it
utils_selinux.set_status(host_sestatus)
# set virt_use_nfs
result = process.run("setsebool virt_use_nfs %s" % virt_use_nfs,
shell=True)
if result.exit_status:
test.cancel("Failed to set virt_use_nfs value")
else:
source_type = "file"
# Init a QemuImg instance.
params['image_name'] = img_name
tmp_dir = data_dir.get_tmp_dir()
image = qemu_storage.QemuImg(params, tmp_dir, img_name)
# Create a image.
img_path, result = image.create(params)
# Set the context of the image.
if sec_relabel == "no":
utils_selinux.set_context_of_file(filename=img_path, context=img_label)
disk_xml.target = {"dev": device_target, "bus": device_bus}
disk_xml.driver = {"name": "qemu", "type": vol_format}
if disk_seclabel == "yes":
source_seclabel = []
sec_xml = seclabel.Seclabel()
sec_xml.update(sec_disk_dict)
source_seclabel.append(sec_xml)
disk_source = disk_xml.new_disk_source(**{"attrs": {source_type: img_path},
"seclabels": source_seclabel})
else:
disk_source = disk_xml.new_disk_source(**{"attrs": {source_type: img_path}})
# Set the context of the VM.
vmxml.set_seclabel([sec_dict])
vmxml.sync()
disk_xml.source = disk_source
logging.debug(disk_xml)
# Do the attach action.
cmd_result = virsh.attach_device(domainarg=vm_name, filearg=disk_xml.xml, flagstr='--persistent')
libvirt.check_exit_status(cmd_result, expect_error=False)
logging.debug("the domain xml is: %s" % vmxml.xmltreefile)
# Start VM to check the VM is able to access the image or not.
try:
vm.start()
# Start VM successfully.
# VM with set seclabel can access the image with the
# set context.
if status_error:
test.fail('Test succeeded in negative case.')
if check_cap_rawio:
cap_list = ['CapPrm', 'CapEff', 'CapBnd']
cap_dict = {}
pid = vm.get_pid()
pid_status_path = "/proc/%s/status" % pid
with open(pid_status_path) as f:
for line in f:
val_list = line.split(":")
示例13: Disk
# 需要导入模块: from virttest.libvirt_xml.devices.disk import Disk [as 别名]
# 或者: from virttest.libvirt_xml.devices.disk.Disk import new_disk_source [as 别名]
chap_passwd=chap_passwd,
portal_ip=iscsi_host)
# If we use qcow2 disk format, should format iscsi disk first.
if device_format == "qcow2":
cmd = ("qemu-img create -f qcow2 iscsi://%s:%s/%s/%s %s"
% (iscsi_host, iscsi_port, iscsi_target, lun_num, emulated_size))
process.run(cmd, shell=True)
# Add disk xml.
vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name)
disk_xml = Disk(type_name=device_type)
disk_xml.device = device
disk_xml.source = disk_xml.new_disk_source(
**{"attrs": {"protocol": "iscsi",
"name": "%s/%s" % (iscsi_target, lun_num)},
"hosts": [{"name": iscsi_host, "port": iscsi_port}]})
disk_xml.target = {"dev": device_target, "bus": device_bus}
driver_dict = {"name": "qemu", "type": device_format}
# For lun type device, iothread attribute need to be set in controller.
if driver_iothread and device != "lun":
driver_dict.update({"iothread": driver_iothread})
vmxml.iothreads = int(driver_iothread)
elif driver_iothread:
vmxml.iothreads = int(driver_iothread)
disk_xml.driver = driver_dict
# Check if we want to use a faked uuid.
if not uuid:
uuid = secret_uuid
示例14: run_libvirt_scsi
# 需要导入模块: from virttest.libvirt_xml.devices.disk import Disk [as 别名]
# 或者: from virttest.libvirt_xml.devices.disk.Disk import new_disk_source [as 别名]
def run_libvirt_scsi(test, params, env):
# Get variables.
status_error = ('yes' == params.get("status_error", 'no'))
img_type = ('yes' == params.get("libvirt_scsi_img_type", "no"))
cdrom_type = ('yes' == params.get("libvirt_scsi_cdrom_type", "no"))
partition_type = ('yes' == params.get("libvirt_scsi_partition_type", "no"))
partition = params.get("libvirt_scsi_partition",
"ENTER.YOUR.AVAILIBLE.PARTITION")
vm_name = params.get("main_vm", "virt-tests-vm1")
# Init a VM instance and a VMXML instance.
vm = env.get_vm(vm_name)
vmxml = VMXML.new_from_dumpxml(vm_name)
# Keep a backup of xml to restore it in cleanup.
backup_xml = vmxml.copy()
# Add a scsi controller if there is not.
controller_devices = vmxml.get_devices("controller")
scsi_controller_exists = False
for device in controller_devices:
if device.type == "scsi":
scsi_controller_exists = True
break
if not scsi_controller_exists:
scsi_controller = Controller("controller")
scsi_controller.type = "scsi"
scsi_controller.index = "0"
scsi_controller.model = "virtio-scsi"
vmxml.add_device(scsi_controller)
# Add disk with bus of scsi into vmxml.
if img_type:
# Init a QemuImg instance.
img_name = "libvirt_scsi"
params['image_name'] = img_name
image = qemu_storage.QemuImg(params, data_dir.get_tmp_dir(), img_name)
# Create a image.
img_path, _ = image.create(params)
img_disk = Disk(type_name="file")
img_disk.device = "disk"
img_disk.source = img_disk.new_disk_source(
**{'attrs': {'file': img_path}})
img_disk.target = {'dev': "vde",
'bus': "scsi"}
vmxml.add_device(img_disk)
if cdrom_type:
# Init a CdromDisk instance.
cdrom_path = os.path.join(data_dir.get_tmp_dir(), "libvirt_scsi")
cdrom = CdromDisk(cdrom_path, data_dir.get_tmp_dir())
cdrom.close()
cdrom_disk = Disk(type_name="file")
cdrom_disk.device = "cdrom"
cdrom_disk.target = {'dev': "vdf",
'bus': "scsi"}
cdrom_disk.source = cdrom_disk.new_disk_source(
**{'attrs': {'file': cdrom_path}})
vmxml.add_device(cdrom_disk)
if partition_type:
if partition.count("ENTER.YOUR"):
raise error.TestNAError("Partition for partition test"
"is not configured.")
partition_disk = Disk(type_name="block")
partition_disk.device = "disk"
partition_disk.target = {'dev': "vdg",
'bus': "scsi"}
partition_disk.source = partition_disk.new_disk_source(
**{'attrs': {'dev': partition}})
vmxml.add_device(partition_disk)
# sync the vmxml with VM.
vmxml.sync()
# Check the result of scsi disk.
try:
try:
vm.start()
# Start VM successfully.
if status_error:
raise error.TestFail('Starting VM successed in negative case.')
except virt_vm.VMStartError, e:
# Starting VM failed.
if not status_error:
raise error.TestFail("Test failed in positive case."
"error: %s" % e)
finally:
# clean up.
backup_xml.sync()
示例15: run
# 需要导入模块: from virttest.libvirt_xml.devices.disk import Disk [as 别名]
# 或者: from virttest.libvirt_xml.devices.disk.Disk import new_disk_source [as 别名]
def run(test, params, env):
"""
Test virsh domblkerror in 2 types error
1. unspecified error
2. no space
"""
if not virsh.has_help_command('domblkerror'):
raise error.TestNAError("This version of libvirt does not support "
"domblkerror test")
vm_name = params.get("main_vm", "virt-tests-vm1")
error_type = params.get("domblkerror_error_type")
timeout = params.get("domblkerror_timeout", 240)
mnt_dir = params.get("domblkerror_mnt_dir", "/home/test")
tmp_file = params.get("domblkerror_tmp_file", "/tmp/fdisk-cmd")
export_file = params.get("nfs_export_file", "/etc/exports")
img_name = params.get("domblkerror_img_name", "libvirt-disk")
img_size = params.get("domblkerror_img_size")
target_dev = params.get("domblkerror_target_dev", "vdb")
pool_name = params.get("domblkerror_pool_name", "fs_pool")
vol_name = params.get("domblkerror_vol_name", "vol1")
vm = env.get_vm(vm_name)
# backup /etc/exports
shutil.copyfile(export_file, "%s.bak" % export_file)
selinux_bak = ""
# backup xml
vmxml_backup = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name)
try:
# Gerenate tmp dir
tmp_dir = data_dir.get_tmp_dir()
img_dir = os.path.join(tmp_dir, 'images')
if not os.path.exists(img_dir):
os.mkdir(img_dir)
# Generate attached disk
utils.run("qemu-img create %s %s" %
(os.path.join(img_dir, img_name), img_size))
# Get unspecified error
if error_type == "unspecified error":
# In this situation, guest will attach a disk on nfs, stop nfs
# service will cause guest paused and get unspecified error
nfs_dir = os.path.join(tmp_dir, 'mnt')
if not os.path.exists(nfs_dir):
os.mkdir(nfs_dir)
mount_opt = "rw,no_root_squash,async"
res = utils_test.libvirt.setup_or_cleanup_nfs(
is_setup=True, mount_dir=nfs_dir, is_mount=False,
export_options=mount_opt, export_dir=img_dir)
selinux_bak = res["selinux_status_bak"]
utils.run("mount -o nolock,soft,timeo=1,retrans=1,retry=0 "
"127.0.0.1:%s %s" % (img_dir, nfs_dir))
img_path = os.path.join(nfs_dir, img_name)
nfs_service = Factory.create_service("nfs")
elif error_type == "no space":
# Steps to generate no space block error:
# 1. Prepare a iscsi disk and build fs pool with it
# 2. Create vol with larger capacity and 0 allocation
# 3. Attach this disk in guest
# 4. In guest, create large image in the vol, which may cause
# guest paused
pool_target = os.path.join(tmp_dir, pool_name)
_pool_vol = utils_test.libvirt.PoolVolumeTest(test, params)
_pool_vol.pre_pool(pool_name, "fs", pool_target, img_name,
image_size=img_size)
_pool_vol.pre_vol(vol_name, "raw", "100M", "0", pool_name)
img_path = os.path.join(pool_target, vol_name)
# Generate disk xml
# Guest will attach a disk with cache=none and error_policy=stop
img_disk = Disk(type_name="file")
img_disk.device = "disk"
img_disk.source = img_disk.new_disk_source(
**{'attrs': {'file': img_path}})
img_disk.driver = {'name': "qemu",
'type': "raw",
'cache': "none",
'error_policy': "stop"}
img_disk.target = {'dev': target_dev,
'bus': "virtio"}
logging.debug("disk xml is %s", img_disk.xml)
# Start guest and get session
if not vm.is_alive():
vm.start()
session = vm.wait_for_login()
# Get disk list before operation
get_disks_cmd = "fdisk -l|grep '^Disk /dev'|cut -d: -f1|cut -d' ' -f2"
bef_list = session.cmd_output(get_disks_cmd).split("\n")
# Attach disk to guest
ret = virsh.attach_device(domain_opt=vm_name,
file_opt=img_disk.xml)
if ret.exit_status != 0:
raise error.TestFail("Fail to attach device %s" % ret.stderr)
time.sleep(2)
#.........这里部分代码省略.........