3 import sys
, os
, time
, re
4 import optparse
, commands
5 import ConfigParser
, StringIO
7 class ShellConfigParser(ConfigParser
.ConfigParser
):
8 def read(self
, filename
):
10 text
= open(filename
).read()
14 file = StringIO
.StringIO("[shell]\n" + text
)
15 self
.readfp(file, filename
)
17 config
= ShellConfigParser()
18 config
.read('config.mak')
20 external_module
= config
.get('shell', 'want_module')
21 privileged
= os
.getuid() == 0
23 optparser
= optparse
.OptionParser()
25 optparser
.add_option('--no-reload-module',
26 help = 'do not reload kvm module',
27 action
= 'store_false',
32 optparser
.add_option('--install',
33 help = 'start up guest in installer boot cd',
34 action
= 'store_true',
38 optparser
.add_option('-m', '--memory',
39 help = 'guest memory in MB',
45 optparser
.add_option('--debugger',
46 help = 'wait for gdb',
47 action
= 'store_true',
51 optparser
.add_option('--no-tap',
52 help = 'run the guest without tap netif',
53 action
= 'store_true',
55 default
= not privileged
,
58 optparser
.add_option('--mac',
59 help = 'use this specific mac addr',
64 optparser
.add_option('--vnc',
65 help = 'use VNC rather than SDL',
70 optparser
.add_option('--no-kvm',
71 help = 'use standard qemu, without kvm',
72 action
= 'store_false',
76 optparser
.add_option('--image',
77 help = 'select disk image',
79 default
= '/tmp/disk',
81 optparser
.add_option('--cdrom',
82 help = 'select cdrom image',
87 optparser
.add_option('--hdb',
88 help = 'secondary hard disk image',
93 optparser
.add_option('--loadvm',
94 help = 'select saved vm-image',
99 optparser
.add_option('--monitor',
100 help = 'redirect monitor (currently only stdio or tcp)',
105 optparser
.add_option('--stopped',
106 help = 'start image in stopped mode',
107 action
= 'store_true',
111 optparser
.add_option('-s', '--smp',
115 help = 'define number of vcpus',
118 optparser
.add_option('--no-kvm-irqchip',
119 action
= 'store_false',
122 help = 'avoid using in-kernel irqchip',
125 optparser
.add_option('-n', '--dry-run',
126 help = "just print the qemu command line; don't run it",
127 action
= 'store_true',
133 (options
, args
) = optparser
.parse_args()
136 options
.image
= args
[0]
139 options
.cdrom
= args
[1]
141 def remove_module(module
):
142 module
= module
.replace('-', '_')
143 lines
= commands
.getoutput('/sbin/lsmod').split('\n')
145 if x
.startswith(module
+ ' '):
146 if os
.spawnl(os
.P_WAIT
, '/sbin/rmmod', 'rmmod', module
) != 0:
147 raise Exception('failed to remove %s module' % (module
,))
149 def insert_module(module
):
150 if os
.spawnl(os
.P_WAIT
, '/sbin/insmod', 'insmod',
151 'kernel/%s.ko' % (module
,)) != 0:
152 raise Exception('failed to load kvm module')
154 def probe_module(module
):
155 if os
.spawnl(os
.P_WAIT
, '/sbin/modprobe', 'modprobe', module
) != 0:
156 raise Exception('failed to load kvm module')
159 for x
in file('/proc/cpuinfo').readlines():
160 m
= re
.match(r
'vendor_id[ \t]*: *([a-zA-Z]+),*', x
)
166 'GenuineIntel': 'kvm-intel',
167 'AuthenticAMD': 'kvm-amd',
170 if options
.kvm
and options
.reload:
171 for module
in [vendor_module
, 'kvm']:
172 remove_module(module
)
174 insmod
= insert_module
176 insmod
= probe_module
177 for module
in ['kvm', vendor_module
]:
179 commands
.getstatusoutput('/sbin/udevsettle')
180 if not os
.access('/dev/kvm', os
.F_OK
):
181 print '/dev/kvm not present'
185 (status
, output
) = commands
.getstatusoutput(
186 'qemu/qemu-img create -f qcow2 "%s" 30G' % disk
)
188 raise Exception, output
197 cmd
= 'qemu-system-' + arch
201 local_cmd
= 'qemu/' + arch
+ '-softmmu/' + cmd
202 if os
.access(local_cmd
, os
.F_OK
):
207 qemu_args
= (cmd
, '-boot', bootdisk
,
208 '-hda', disk
, '-m', str(options
.memory
),
209 '-serial', 'file:/tmp/serial.log',
210 '-smp', str(options
.vcpus
),
211 #'-usbdevice', 'tablet',
215 qemu_args
+= ('-cdrom', options
.cdrom
,)
218 qemu_args
+= ('-hdb', options
.hdb
,)
221 qemu_args
+= ('-no-kvm',)
226 if not options
.irqchip
:
227 qemu_args
+= ('-no-kvm-irqchip',)
229 if not options
.notap
:
232 for line
in commands
.getoutput('/sbin/ip link show eth0').splitlines():
233 m
= re
.match(r
'.*link/ether (..:..:..:..:..:..).*', line
)
237 raise Exception, 'Unable to determine eth0 mac address'
238 mac_components
= mac
.split(':')
239 mac_components
[0] = 'a0'
240 mac
= ':'.join(mac_components
)
242 qemu_args
+= ('-net', 'nic,macaddr=%s,model=rtl8139' % (mac
,),
243 '-net', 'tap,script=/etc/kvm/qemu-ifup',)
246 qemu_args
+= ('-vnc', str(options
.vnc
))
248 if options
.saved_image
:
249 qemu_args
+= ('-loadvm' , options
.saved_image
, )
252 if options
.monitor
== 'stdio':
253 qemu_args
+= ('-monitor' , 'stdio', )
254 elif options
.monitor
== 'tcp':
255 qemu_args
+= ('-monitor' , 'tcp:0:5555,server,nowait', )
257 raise Exception('illegal monitor option %s' % option
.monitor
)
263 def concat_func(x
,y
): return x
+ ' ' + y
264 print reduce(concat_func
, qemu_args
)
267 os
.execvp(cmd
, qemu_args
)