Add interrupt support:
[qemu-kvm/fedora.git] / kvm / kvm
blob9fc4771d85d66c774bb18fab95d48712d862885e
1 #!/usr/bin/python
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',
11 dest = 'reload',
12 default = True,
15 optparser.add_option('--install',
16 help = 'start up guest in installer boot cd',
17 action = 'store_true',
18 default = False,
21 optparser.add_option('--debugger',
22 help = 'wait for gdb',
23 action = 'store_true',
24 default = False,
27 optparser.add_option('--no-tap',
28 help = 'run the guest without tap netif',
29 action = 'store_true',
30 dest = 'notap',
31 default = False,
34 optparser.add_option('--mac',
35 help = 'use this specific mac addr',
36 dest = 'mac',
37 default = None,
40 optparser.add_option('--vnc',
41 help = 'use VNC rather than SDL',
42 dest = 'vnc',
43 default = None,
46 optparser.add_option('--no-kvm',
47 help = 'use standard qemu, without kvm',
48 action = 'store_false',
49 dest = 'kvm',
50 default = True,
52 optparser.add_option('--image',
53 help = 'select disk image',
54 dest = 'image',
55 default = '/tmp/disk',
57 optparser.add_option('--cdrom',
58 help = 'select cdrom image',
59 dest = 'cdrom',
60 default = None,
62 optparser.add_option('--loadvm',
63 help = 'select saved vm-image',
64 dest = 'saved_image',
65 default = None,
68 optparser.add_option('--monitor',
69 help = 'redirect monitor (currently only stdio or tcp)',
70 dest = 'monitor',
71 default = None,
74 optparser.add_option('--stopped',
75 help = 'start image in stopped mode',
76 action = 'store_true',
77 default = False,
80 optparser.add_option('-n', '--dry-run',
81 help = "just print the qemu command line; don't run it",
82 action = 'store_true',
83 dest = 'dry_run',
84 default = False,
88 (options, args) = optparser.parse_args()
90 if len(args) > 0:
91 options.image = args[0]
93 if len(args) > 1:
94 options.cdrom = args[1]
96 def remove_module(module):
97 module = module.replace('-', '_')
98 lines = commands.getoutput('/sbin/lsmod').split('\n')
99 for x in lines:
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')
110 def vendor():
111 for x in file('/proc/cpuinfo').readlines():
112 m = re.match(r'vendor_id[ \t]*: *([a-zA-Z]+),*', x)
113 if m:
114 return m.group(1)
115 return unknown
117 vendor_module = {
118 'GenuineIntel': 'kvm-intel',
119 'AuthenticAMD': 'kvm-amd',
120 }[vendor()]
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)
127 for i in range(5):
128 if os.access('/dev/kvm', os.F_OK):
129 break
130 time.sleep(0.1 + 0.2 * i)
131 if not os.access('/dev/kvm', os.F_OK):
132 print '/dev/kvm not present'
134 disk = options.image
135 if options.install:
136 fd = file(disk, 'w')
137 fd.truncate()
138 fd.seek(10*1024*1024*1024-1)
139 fd.write('\0')
140 fd.close()
142 bootdisk = 'c'
143 if options.install:
144 bootdisk = 'd'
146 import platform
147 arch = platform.machine()
148 # check 32-bit userspace on 64-bit kernel
149 if platform.architecture()[0] == '32bit':
150 arch = 'i386'
152 if arch == 'x86_64':
153 cmd = 'qemu-system-' + arch
154 else:
155 cmd = 'qemu'
157 local_cmd = 'qemu/' + arch + '-softmmu/' + cmd
158 if os.access(local_cmd, os.F_OK):
159 cmd = local_cmd
160 else:
161 cmd = '/usr/bin/kvm'
163 qemu_args = (cmd, '-boot', bootdisk,
164 '-L', '/usr/share/qemu', '-hda', disk, '-m', '384',
165 '-serial', 'file:/tmp/serial.log',
166 '-usbdevice', 'tablet'
169 if options.cdrom:
170 qemu_args += ('-cdrom', options.cdrom,)
172 if not options.kvm:
173 qemu_args += ('-no-kvm',)
175 if options.debugger:
176 qemu_args += ('-s',)
178 if not options.notap:
179 mac = options.mac
180 if not mac:
181 for line in commands.getoutput('ip link show eth0').splitlines():
182 m = re.match(r'.*link/ether (..:..:..:..:..:..).*', line)
183 if m:
184 mac = m.group(1)
185 if not mac:
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',)
194 if options.vnc:
195 qemu_args += ('-vnc', str(options.vnc))
197 if options.saved_image:
198 qemu_args += ('-loadvm' , options.saved_image, )
200 if options.monitor:
201 if options.monitor == 'stdio':
202 qemu_args += ('-monitor' , 'stdio', )
203 elif options.monitor == 'tcp':
204 qemu_args += ('-monitor' , 'tcp:0:5555,server,nowait', )
205 else:
206 raise Exception('illegal monitor option %s' % option.monitor)
208 if options.stopped:
209 qemu_args += ('-S',)
211 if options.dry_run:
212 def concat_func(x,y): return x + ' ' + y
213 print reduce(concat_func, qemu_args)
214 sys.exit(0)
216 os.execvp(cmd, qemu_args)