comparison 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
comparison
equal deleted inserted replaced
54:d2e05ee14974 55:2aa12d6f79e0
79 def randomMAC(): 79 def randomMAC():
80 mac = [ 0x52, 0x54, 0x00, 80 mac = [ 0x52, 0x54, 0x00,
81 random.randint(0x00, 0xff), 81 random.randint(0x00, 0xff),
82 random.randint(0x00, 0xff), 82 random.randint(0x00, 0xff),
83 random.randint(0x00, 0xff) ] 83 random.randint(0x00, 0xff) ]
84 return ':'.join(map(lambda x: "%02x" % x, mac)) 84 return ':'.join(["%02x" % x for x in mac])
85 85
86 # def randomUUID(): 86 # def randomUUID():
87 # u = [random.randint(0, 255) for ignore in range(0, 16)] 87 # u = [random.randint(0, 255) for ignore in range(0, 16)]
88 # u[6] = (u[6] & 0x0F) | (4 << 4) 88 # u[6] = (u[6] & 0x0F) | (4 << 4)
89 # u[8] = (u[8] & 0x3F) | (2 << 6) 89 # u[8] = (u[8] & 0x3F) | (2 << 6)
112 help="Kernel debug mode"); 112 help="Kernel debug mode");
113 113
114 (options, args) = parser.parse_args(); 114 (options, args) = parser.parse_args();
115 115
116 if not options.name or not options.config: 116 if not options.name or not options.config:
117 print "Usage %s -n name -c template_xml [-d debug_port]" % sys.argv[0] 117 print(("Usage %s -n name -c template_xml [-d debug_port]" % sys.argv[0]))
118 sys.exit(1) 118 sys.exit(1)
119 119
120 config = ET.parse(options.config) 120 config = ET.parse(options.config)
121 vm_name = options.name 121 vm_name = options.name
122 print(options.config) 122 print((options.config))
123 if ie_check_name(vm_name): 123 if ie_check_name(vm_name):
124 print "Can't make new vm. Bad vmname %s. Try 01 - 04" % vm_name 124 print(("Can't make new vm. Bad vmname %s. Try 01 - 04" % vm_name))
125 sys.exit(1) 125 sys.exit(1)
126 126
127 ie_mkdir(vm_name) 127 ie_mkdir(vm_name)
128 name = config.find('name') 128 name = config.find('name')
129 129
140 disk_ext = os.path.splitext(disk_old)[1] 140 disk_ext = os.path.splitext(disk_old)[1]
141 disk_image = disk_path + '/' + vm_name + disk_ext 141 disk_image = disk_path + '/' + vm_name + disk_ext
142 disk.attrib['file'] = disk_image 142 disk.attrib['file'] = disk_image
143 vnc = config.find('devices/graphics') 143 vnc = config.find('devices/graphics')
144 vnc.attrib['passwd'] = genPassword() 144 vnc.attrib['passwd'] = genPassword()
145 print "vnc password : %s" % vnc.attrib['passwd'] 145 print(("vnc password : %s" % vnc.attrib['passwd']))
146 146
147 if (options.debug): 147 if (options.debug):
148 # http://wiki.libvirt.org/page/QEMUSwitchToLibvirt 148 # http://wiki.libvirt.org/page/QEMUSwitchToLibvirt
149 domain = config.getroot() 149 domain = config.getroot()
150 domain.attrib['xmlns:qemu'] = "http://libvirt.org/schemas/domain/qemu/1.0" 150 domain.attrib['xmlns:qemu'] = "http://libvirt.org/schemas/domain/qemu/1.0"
153 ET.SubElement(qemu_elem, 'qemu:arg', {'value' : ('-gdb')}) 153 ET.SubElement(qemu_elem, 'qemu:arg', {'value' : ('-gdb')})
154 ET.SubElement(qemu_elem, 'qemu:arg', {'value' : ('tcp::%d' % options.debug)}) 154 ET.SubElement(qemu_elem, 'qemu:arg', {'value' : ('tcp::%d' % options.debug)})
155 domain.append(qemu_elem) 155 domain.append(qemu_elem)
156 156
157 if os.path.exists(vm_name + '.xml'): 157 if os.path.exists(vm_name + '.xml'):
158 print "File %s.xml exists, abort" % vm_name 158 print(("File %s.xml exists, abort" % vm_name))
159 sys.exit(1) 159 sys.exit(1)
160 # config.write('/usr/local/etc/libvirt/qemu/' + vm_name + '.xml') 160 # config.write('/usr/local/etc/libvirt/qemu/' + vm_name + '.xml')
161 print("VM_NAME:" + new_name) 161 print(("VM_NAME:" + new_name))
162 config.write('/etc/libvirt/qemu/' + vm_name + '.xml') 162 config.write('/etc/libvirt/qemu/' + vm_name + '.xml')
163 print "Created vm config file %s.xml" % vm_name 163 print(("Created vm config file %s.xml" % vm_name))
164 print "Use disk image %s" % disk_image 164 print(("Use disk image %s" % disk_image))
165 print "Done!" 165 print("Done!")