def add_disk(self, name, size, pool=None, thin=True, template=None, shareable=False, existing=None):
conn = self.conn
# diskformat = 'qcow2'
if size < 1:
print("Incorrect size.Leaving...")
return {'result': 'failure', 'reason': "Incorrect size"}
# if not thin:
# diskformat = 'raw'
try:
vm = conn.find_machine(name)
except:
common.pprint("VM %s not found" % name, color='red')
return {'result': 'failure', 'reason': "VM %s not found" % name}
disks = []
for index, dev in enumerate(string.lowercase[:10]):
try:
vm.get_medium('SATA', index, 0)
disks.append(0)
except:
continue
index = len(disks)
if existing is None:
storagename = "%s_%d" % (name, index)
diskpath = self.create_disk(name=storagename, size=size, pool=pool, thin=thin, template=template)
else:
disks = self.list_disks()
if existing in disks:
diskpath = disks[existing]['path']
else:
diskpath = existing
session = Session()
vm.lock_machine(session, library.LockType.write)
machine = session.machine
disk = conn.open_medium(diskpath, library.DeviceType.hard_disk, library.AccessMode.read_write, True)
machine.attach_device("SATA", index, 0, library.DeviceType.hard_disk, disk)
machine.save_settings()
session.unlock_machine()
return {'result': 'success'}
评论列表
文章目录