3 import sys
, os
, time
, re
4 import optparse
, commands
6 optparser
= optparse
.OptionParser()
8 optparser
.add_option('--no-reload-module',
9 help = 'do not reload kvm module',
10 action
= 'store_false',
15 optparser
.add_option('--install',
16 help = 'start up guest in installer boot cd',
17 action
= 'store_true',
21 optparser
.add_option('--debugger',
22 help = 'wait for gdb',
23 action
= 'store_true',
27 optparser
.add_option('--no-tap',
28 help = 'run the guest without tap netif',
29 action
= 'store_true',
34 optparser
.add_option('--mac',
35 help = 'use this specific mac addr',
40 optparser
.add_option('--vnc',
41 help = 'use VNC rather than SDL',
46 optparser
.add_option('--no-kvm',
47 help = 'use standard qemu, without kvm',
48 action
= 'store_false',
52 optparser
.add_option('--image',
53 help = 'select disk image',
55 default
= '/tmp/disk',
57 optparser
.add_option('--cdrom',
58 help = 'select cdrom image',
62 optparser
.add_option('--loadvm',
63 help = 'select saved vm-image',
68 optparser
.add_option('--monitor',
69 help = 'redirect monitor (currently only stdio or tcp)',
74 optparser
.add_option('--stopped',
75 help = 'start image in stopped mode',
76 action
= 'store_true',
80 optparser
.add_option('-n', '--dry-run',
81 help = "just print the qemu command line; don't run it",
82 action
= 'store_true',
88 (options
, args
) = optparser
.parse_args()
91 options
.image
= args
[0]
94 options
.cdrom
= args
[1]
96 def remove_module(module
):
97 module
= module
.replace('-', '_')
98 lines
= commands
.getoutput('/sbin/lsmod').split('\n')
100 if x
.startswith(module
+ ' '):
101 if os
.spawnl(os
.P_WAIT
, '/sbin/rmmod', 'rmmod', module
) != 0:
102 raise Exception('failed to remove %s module' % (module
,))
104 def insert_module(module
):
105 if os
.spawnl(os
.P_WAIT
, '/sbin/insmod', 'insmod',
106 'kernel/%s.ko' % (module
,)) != 0:
107 if os
.spawnl(os
.P_WAIT
, '/sbin/modprobe', 'modprobe', module
) != 0:
108 raise Exception('failed to load kvm module')
111 for x
in file('/proc/cpuinfo').readlines():
112 m
= re
.match(r
'vendor_id[ \t]*: *([a-zA-Z]+),*', x
)
118 'GenuineIntel': 'kvm-intel',
119 'AuthenticAMD': 'kvm-amd',
122 if options
.kvm
and options
.reload:
123 for module
in [vendor_module
, 'kvm']:
124 remove_module(module
)
125 for module
in ['kvm', vendor_module
]:
126 insert_module(module
)
128 if os
.access('/dev/kvm', os
.F_OK
):
130 time
.sleep(0.1 + 0.2 * i
)
131 if not os
.access('/dev/kvm', os
.F_OK
):
132 print '/dev/kvm not present'
138 fd
.seek(10*1024*1024*1024-1)
147 arch
= platform
.machine()
148 # check 32-bit userspace on 64-bit kernel
149 if platform
.architecture()[0] == '32bit':
153 cmd
= 'qemu-system-' + arch
157 local_cmd
= 'qemu/' + arch
+ '-softmmu/' + cmd
158 if os
.access(local_cmd
, os
.F_OK
):
163 qemu_args
= (cmd
, '-boot', bootdisk
,
164 '-L', '/usr/share/qemu', '-hda', disk
, '-m', '384',
165 '-serial', 'file:/tmp/serial.log',
166 '-usbdevice', 'tablet'
170 qemu_args
+= ('-cdrom', options
.cdrom
,)
173 qemu_args
+= ('-no-kvm',)
178 if not options
.notap
:
181 for line
in commands
.getoutput('ip link show eth0').splitlines():
182 m
= re
.match(r
'.*link/ether (..:..:..:..:..:..).*', line
)
186 raise Exception, 'Unable to determine eth0 mac address'
187 mac_components
= mac
.split(':')
188 mac_components
[0] = 'a0'
189 mac
= ':'.join(mac_components
)
191 qemu_args
+= ('-net', 'nic,macaddr=%s,model=rtl8139' % (mac
,),
192 '-net', 'tap,script=/etc/kvm/qemu-ifup',)
195 qemu_args
+= ('-vnc', str(options
.vnc
))
197 if options
.saved_image
:
198 qemu_args
+= ('-loadvm' , options
.saved_image
, )
201 if options
.monitor
== 'stdio':
202 qemu_args
+= ('-monitor' , 'stdio', )
203 elif options
.monitor
== 'tcp':
204 qemu_args
+= ('-monitor' , 'tcp:0:5555,server,nowait', )
206 raise Exception('illegal monitor option %s' % option
.monitor
)
212 def concat_func(x
,y
): return x
+ ' ' + y
213 print reduce(concat_func
, qemu_args
)
216 os
.execvp(cmd
, qemu_args
)