Mercurial > hg > Applications > virsh-wrapper
view newvm.py @ 55:2aa12d6f79e0 default tip
2to3
author | anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 22 Oct 2020 14:27:23 +0900 |
parents | fecd206e005f |
children |
line wrap: on
line source
#!/usr/bin/python # Used to create a vm from template # By Curu Wong contact: prinbra(at)gmail(dot)com import sys,os,string,re from optparse import OptionParser from virtinst.util import * from virtinst.connection import * import string if sys.version_info < (2,5): import lxml.etree as ET else: import xml.etree.ElementTree as ET mount_point = '/media/iscsi/' # root directory # vm_name should be # students/e10/e105730/01 # teachers/kono/01 # managers/name/01 # guests/name/01 # bad name returns 1 def ie_check_name(name): m=re.match('^students/[ek](\d\d)/[ek](\d\d)[58]\d\d\d/0[1-4]$',name) if m is not None: if m.group(1)==m.group(2): return 0 else: return 1 elif re.match('^teachers/[-a-z0-9]+/0[1-4]$',name): return 0 elif re.match('^managers/[-a-z0-9]+/0[1-4]$',name): return 0 elif re.match('^guests/[-a-zA-Z0-9]+/0[1-4]$',name): return 0 elif re.match('^others/[-a-z0-9]+/0[1-4]$',name): return 0 else: return 1 # make necessary sub directory # /etc/libvirt/qemu/teachers # /var/log/libvirt/qemu/teachers # /var/run/libvirt/qemu/teachers def ie_mkdir1(name): if not os.path.exists(name): os.makedirs(name); # sample : students/e14/k145740/01 # : master students/k13/k138582/01 def ie_mkdir(name): m=re.match('^(students/[ek]\d\d/[ek]\d\d[58]\d\d\d)/0[1-4]$',name) if m is None: m=re.match('^(teachers/[-a-z0-9]+)/0[1-4]$',name) if m is None: m=re.match('^(managers/[-a-z0-9]+)/0[1-4]$',name) if m is None: m=re.match('^(guests/[-a-zA-Z0-9]+)/0[1-4]$',name) if m is None: m=re.match('^(others/[-a-z0-9]+)/0[1-4]$',name) if m is not None: dir=m.group(1) ie_mkdir1(mount_point+dir) # ie_mkdir1('/usr/local/etc/libvirt/qemu/'+dir) # ie_mkdir1('/usr/local/var/log/libvirt/qemu/'+dir) # ie_mkdir1('/usr/local/var/run/libvirt/qemu/'+dir) # ie_mkdir1('/usr/local/var/lib/libvirt/qemu/'+dir) ie_mkdir1('/etc/libvirt/qemu/'+dir) ie_mkdir1('/var/log/libvirt/qemu/'+dir) ie_mkdir1('/var/run/libvirt/qemu/'+dir) ie_mkdir1('/var/lib/libvirt/qemu/'+dir) os.system("/bin/chown "+os.getlogin()+" "+ mount_point+dir) # virtinst.utils.randomMac is omitted from RHEL 7. import random from random import choice def randomMAC(): mac = [ 0x52, 0x54, 0x00, random.randint(0x00, 0xff), random.randint(0x00, 0xff), random.randint(0x00, 0xff) ] return ':'.join(["%02x" % x for x in mac]) # def randomUUID(): # u = [random.randint(0, 255) for ignore in range(0, 16)] # u[6] = (u[6] & 0x0F) | (4 << 4) # u[8] = (u[8] & 0x3F) | (2 << 6) # return "-".join(["%02x" * 4, "%02x" * 2, "%02x" * 2, "%02x" * 2, # "%02x" * 6]) % tuple(u) def genPassword(): chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" echars = "!@#$%^&*-,./" passwd = "" p = random.randint(0,7) passwd += "".join(choice(chars) for _ in range (p)) passwd += "".join(choice(echars) for _ in range (1)) passwd += "".join(choice(chars) for _ in range (7-p)) return passwd parser = OptionParser(); parser.add_option("-n", "--name", dest="name", help="VM name"); parser.add_option("-c", "--config", dest="config", help="Template VM XML config file"); parser.add_option("-i", "--iso", dest="iso", help="When boot VM from ISO"); parser.add_option("-d", "--debug", dest="debug", type="int", help="Kernel debug mode"); (options, args) = parser.parse_args(); if not options.name or not options.config: print(("Usage %s -n name -c template_xml [-d debug_port]" % sys.argv[0])) sys.exit(1) config = ET.parse(options.config) vm_name = options.name print((options.config)) if ie_check_name(vm_name): print(("Can't make new vm. Bad vmname %s. Try 01 - 04" % vm_name)) sys.exit(1) ie_mkdir(vm_name) name = config.find('name') new_name = vm_name.translate(string.maketrans('/','_')) name.text = new_name uuid = config.find('uuid') uuid.text = randomUUID(VirtualConnection("qemu:///system")) mac = config.find('devices/interface/mac') mac.attrib['address'] = randomMAC() disk = config.find('devices/disk/source') disk_old = disk.attrib['file'] disk_path = os.path.dirname(disk_old) disk_ext = os.path.splitext(disk_old)[1] disk_image = disk_path + '/' + vm_name + disk_ext disk.attrib['file'] = disk_image vnc = config.find('devices/graphics') vnc.attrib['passwd'] = genPassword() print(("vnc password : %s" % vnc.attrib['passwd'])) if (options.debug): # http://wiki.libvirt.org/page/QEMUSwitchToLibvirt domain = config.getroot() domain.attrib['xmlns:qemu'] = "http://libvirt.org/schemas/domain/qemu/1.0" qemu_elem = ET.Element('qemu:commandline') ET.SubElement(qemu_elem, 'qemu:arg', {'value' : '-S'}) ET.SubElement(qemu_elem, 'qemu:arg', {'value' : ('-gdb')}) ET.SubElement(qemu_elem, 'qemu:arg', {'value' : ('tcp::%d' % options.debug)}) domain.append(qemu_elem) if os.path.exists(vm_name + '.xml'): print(("File %s.xml exists, abort" % vm_name)) sys.exit(1) # config.write('/usr/local/etc/libvirt/qemu/' + vm_name + '.xml') print(("VM_NAME:" + new_name)) config.write('/etc/libvirt/qemu/' + vm_name + '.xml') print(("Created vm config file %s.xml" % vm_name)) print(("Use disk image %s" % disk_image)) print("Done!")