Mercurial > hg > Applications > virsh-wrapper
annotate 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 |
rev | line source |
---|---|
3 | 1 #!/usr/bin/python |
2 # Used to create a vm from template | |
3 # By Curu Wong contact: prinbra(at)gmail(dot)com | |
32 | 4 import sys,os,string,re |
3 | 5 from optparse import OptionParser |
6 from virtinst.util import * | |
51 | 7 from virtinst.connection import * |
49 | 8 import string |
25
1c4fb56f3cd5
add extension .sh to libvirt
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
24
diff
changeset
|
9 |
3 | 10 if sys.version_info < (2,5): |
22 | 11 import lxml.etree as ET |
3 | 12 else: |
22 | 13 import xml.etree.ElementTree as ET |
14 | |
35 | 15 mount_point = '/media/iscsi/' # root directory |
32 | 16 |
6
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
17 # vm_name should be |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
18 # students/e10/e105730/01 |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
19 # teachers/kono/01 |
29 | 20 # managers/name/01 |
21 # guests/name/01 | |
10 | 22 # bad name returns 1 |
6
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
23 def ie_check_name(name): |
32 | 24 m=re.match('^students/[ek](\d\d)/[ek](\d\d)[58]\d\d\d/0[1-4]$',name) |
22 | 25 if m is not None: |
26 if m.group(1)==m.group(2): | |
27 return 0 | |
28 else: | |
29 return 1 | |
30 elif re.match('^teachers/[-a-z0-9]+/0[1-4]$',name): | |
31 return 0 | |
29 | 32 elif re.match('^managers/[-a-z0-9]+/0[1-4]$',name): |
22 | 33 return 0 |
50 | 34 elif re.match('^guests/[-a-zA-Z0-9]+/0[1-4]$',name): |
35 return 0 | |
36 elif re.match('^others/[-a-z0-9]+/0[1-4]$',name): | |
23 | 37 return 0 |
22 | 38 else: |
39 return 1 | |
6
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
40 |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
41 # make necessary sub directory |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
42 # /etc/libvirt/qemu/teachers |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
43 # /var/log/libvirt/qemu/teachers |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
44 # /var/run/libvirt/qemu/teachers |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
45 |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
46 def ie_mkdir1(name): |
22 | 47 if not os.path.exists(name): |
6
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
48 os.makedirs(name); |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
49 |
32 | 50 |
51 # sample : students/e14/k145740/01 | |
52 # : master students/k13/k138582/01 | |
6
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
53 def ie_mkdir(name): |
32 | 54 m=re.match('^(students/[ek]\d\d/[ek]\d\d[58]\d\d\d)/0[1-4]$',name) |
22 | 55 if m is None: |
56 m=re.match('^(teachers/[-a-z0-9]+)/0[1-4]$',name) | |
57 if m is None: | |
29 | 58 m=re.match('^(managers/[-a-z0-9]+)/0[1-4]$',name) |
23 | 59 if m is None: |
50 | 60 m=re.match('^(guests/[-a-zA-Z0-9]+)/0[1-4]$',name) |
61 if m is None: | |
62 m=re.match('^(others/[-a-z0-9]+)/0[1-4]$',name) | |
22 | 63 if m is not None: |
6
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
64 dir=m.group(1) |
32 | 65 ie_mkdir1(mount_point+dir) |
29 | 66 # ie_mkdir1('/usr/local/etc/libvirt/qemu/'+dir) |
67 # ie_mkdir1('/usr/local/var/log/libvirt/qemu/'+dir) | |
68 # ie_mkdir1('/usr/local/var/run/libvirt/qemu/'+dir) | |
69 # ie_mkdir1('/usr/local/var/lib/libvirt/qemu/'+dir) | |
70 ie_mkdir1('/etc/libvirt/qemu/'+dir) | |
71 ie_mkdir1('/var/log/libvirt/qemu/'+dir) | |
72 ie_mkdir1('/var/run/libvirt/qemu/'+dir) | |
73 ie_mkdir1('/var/lib/libvirt/qemu/'+dir) | |
32 | 74 os.system("/bin/chown "+os.getlogin()+" "+ mount_point+dir) |
19 | 75 |
38 | 76 # virtinst.utils.randomMac is omitted from RHEL 7. |
77 import random | |
49 | 78 from random import choice |
38 | 79 def randomMAC(): |
80 mac = [ 0x52, 0x54, 0x00, | |
81 random.randint(0x00, 0xff), | |
82 random.randint(0x00, 0xff), | |
83 random.randint(0x00, 0xff) ] | |
55 | 84 return ':'.join(["%02x" % x for x in mac]) |
38 | 85 |
51 | 86 # def randomUUID(): |
87 # u = [random.randint(0, 255) for ignore in range(0, 16)] | |
88 # u[6] = (u[6] & 0x0F) | (4 << 4) | |
89 # u[8] = (u[8] & 0x3F) | (2 << 6) | |
90 # return "-".join(["%02x" * 4, "%02x" * 2, "%02x" * 2, "%02x" * 2, | |
91 # "%02x" * 6]) % tuple(u) | |
50 | 92 |
93 | |
49 | 94 def genPassword(): |
95 chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
96 echars = "!@#$%^&*-,./" | |
97 passwd = "" | |
98 p = random.randint(0,7) | |
99 passwd += "".join(choice(chars) for _ in range (p)) | |
100 passwd += "".join(choice(echars) for _ in range (1)) | |
101 passwd += "".join(choice(chars) for _ in range (7-p)) | |
102 return passwd | |
103 | |
3 | 104 parser = OptionParser(); |
105 parser.add_option("-n", "--name", dest="name", | |
106 help="VM name"); | |
107 parser.add_option("-c", "--config", dest="config", | |
108 help="Template VM XML config file"); | |
25
1c4fb56f3cd5
add extension .sh to libvirt
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
24
diff
changeset
|
109 parser.add_option("-i", "--iso", dest="iso", |
1c4fb56f3cd5
add extension .sh to libvirt
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
24
diff
changeset
|
110 help="When boot VM from ISO"); |
46
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
45
diff
changeset
|
111 parser.add_option("-d", "--debug", dest="debug", type="int", |
42 | 112 help="Kernel debug mode"); |
25
1c4fb56f3cd5
add extension .sh to libvirt
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
24
diff
changeset
|
113 |
3 | 114 (options, args) = parser.parse_args(); |
22 | 115 |
3 | 116 if not options.name or not options.config: |
55 | 117 print(("Usage %s -n name -c template_xml [-d debug_port]" % sys.argv[0])) |
22 | 118 sys.exit(1) |
119 | |
3 | 120 config = ET.parse(options.config) |
121 vm_name = options.name | |
55 | 122 print((options.config)) |
6
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
123 if ie_check_name(vm_name): |
55 | 124 print(("Can't make new vm. Bad vmname %s. Try 01 - 04" % vm_name)) |
6
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
125 sys.exit(1) |
25
1c4fb56f3cd5
add extension .sh to libvirt
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
24
diff
changeset
|
126 |
6
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
127 ie_mkdir(vm_name) |
3 | 128 name = config.find('name') |
29 | 129 |
130 new_name = vm_name.translate(string.maketrans('/','_')) | |
131 | |
132 name.text = new_name | |
3 | 133 uuid = config.find('uuid') |
51 | 134 uuid.text = randomUUID(VirtualConnection("qemu:///system")) |
3 | 135 mac = config.find('devices/interface/mac') |
38 | 136 mac.attrib['address'] = randomMAC() |
3 | 137 disk = config.find('devices/disk/source') |
138 disk_old = disk.attrib['file'] | |
139 disk_path = os.path.dirname(disk_old) | |
140 disk_ext = os.path.splitext(disk_old)[1] | |
141 disk_image = disk_path + '/' + vm_name + disk_ext | |
142 disk.attrib['file'] = disk_image | |
49 | 143 vnc = config.find('devices/graphics') |
144 vnc.attrib['passwd'] = genPassword() | |
55 | 145 print(("vnc password : %s" % vnc.attrib['passwd'])) |
49 | 146 |
42 | 147 if (options.debug): |
43 | 148 # http://wiki.libvirt.org/page/QEMUSwitchToLibvirt |
44
546504699402
Generate xml which enabled kernel debug. but needs reload in virsh
atton
parents:
43
diff
changeset
|
149 domain = config.getroot() |
45 | 150 domain.attrib['xmlns:qemu'] = "http://libvirt.org/schemas/domain/qemu/1.0" |
44
546504699402
Generate xml which enabled kernel debug. but needs reload in virsh
atton
parents:
43
diff
changeset
|
151 qemu_elem = ET.Element('qemu:commandline') |
45 | 152 ET.SubElement(qemu_elem, 'qemu:arg', {'value' : '-S'}) |
47 | 153 ET.SubElement(qemu_elem, 'qemu:arg', {'value' : ('-gdb')}) |
154 ET.SubElement(qemu_elem, 'qemu:arg', {'value' : ('tcp::%d' % options.debug)}) | |
44
546504699402
Generate xml which enabled kernel debug. but needs reload in virsh
atton
parents:
43
diff
changeset
|
155 domain.append(qemu_elem) |
22 | 156 |
3 | 157 if os.path.exists(vm_name + '.xml'): |
55 | 158 print(("File %s.xml exists, abort" % vm_name)) |
22 | 159 sys.exit(1) |
29 | 160 # config.write('/usr/local/etc/libvirt/qemu/' + vm_name + '.xml') |
55 | 161 print(("VM_NAME:" + new_name)) |
29 | 162 config.write('/etc/libvirt/qemu/' + vm_name + '.xml') |
55 | 163 print(("Created vm config file %s.xml" % vm_name)) |
164 print(("Use disk image %s" % disk_image)) | |
165 print("Done!") |