summaryrefslogtreecommitdiffstats
path: root/scripts/run-qemu-ota
diff options
context:
space:
mode:
authorcajun-rat <phil@advancedtelematic.com>2017-11-17 17:18:41 +0100
committerGitHub <noreply@github.com>2017-11-17 17:18:41 +0100
commit397fb34ea22088a697f58c6d0ad8604a23160972 (patch)
tree00fe8451514fd899703e727db4c5c6da6337d040 /scripts/run-qemu-ota
parent71410bd31ec76e55247807551e68a2061e277b08 (diff)
parenteab456117ad96a43104cef95c68d9bddf4c65872 (diff)
downloadmeta-updater-397fb34ea22088a697f58c6d0ad8604a23160972.tar.gz
Merge pull request #184 from advancedtelematic/feat/PRO-4252/rocko
Feat/pro 4252/rocko
Diffstat (limited to 'scripts/run-qemu-ota')
-rwxr-xr-xscripts/run-qemu-ota129
1 files changed, 11 insertions, 118 deletions
diff --git a/scripts/run-qemu-ota b/scripts/run-qemu-ota
index 641296c..56e4fbc 100755
--- a/scripts/run-qemu-ota
+++ b/scripts/run-qemu-ota
@@ -2,126 +2,12 @@
2 2
3from argparse import ArgumentParser 3from argparse import ArgumentParser
4from subprocess import Popen 4from subprocess import Popen
5from os.path import exists, join, realpath 5from os.path import exists
6from os import listdir
7import random
8import sys 6import sys
9import socket 7from qemucommand import QemuCommand
10 8
11DEFAULT_DIR = 'tmp/deploy/images' 9DEFAULT_DIR = 'tmp/deploy/images'
12 10
13EXTENSIONS = {
14 'intel-corei7-64': 'wic',
15 'qemux86-64': 'otaimg'
16}
17
18
19def find_local_port(start_port):
20 """"
21 Find the next free TCP port after 'start_port'.
22 """
23
24 for port in range(start_port, start_port + 10):
25 try:
26 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
27 s.bind(('', port))
28 return port
29 except socket.error:
30 print("Skipping port %d" % port)
31 finally:
32 s.close()
33 raise Exception("Could not find a free TCP port")
34
35
36def random_mac():
37 """Return a random Ethernet MAC address
38 @link https://www.iana.org/assignments/ethernet-numbers/ethernet-numbers.xhtml#ethernet-numbers-2
39 """
40 head = "ca:fe:"
41 hex_digits = '0123456789abcdef'
42 tail = ':'.join([random.choice(hex_digits) + random.choice(hex_digits) for _ in range(4)])
43 return head + tail
44
45
46class QemuCommand(object):
47 def __init__(self, args):
48 if args.machine:
49 self.machine = args.machine
50 else:
51 machines = listdir(args.dir)
52 if len(machines) == 1:
53 self.machine = machines[0]
54 else:
55 raise ValueError("Could not autodetect machine type from %s" % args.dir)
56 if args.efi:
57 self.bios = 'OVMF.fd'
58 else:
59 uboot = join(args.dir, self.machine, 'u-boot-qemux86-64.rom')
60 if not exists(uboot):
61 raise ValueError("U-Boot image %s does not exist" % uboot)
62 self.bios = uboot
63 if exists(args.imagename):
64 image = args.imagename
65 else:
66 ext = EXTENSIONS.get(self.machine, 'wic')
67 image = join(args.dir, self.machine, '%s-%s.%s' % (args.imagename, self.machine, ext))
68 self.image = realpath(image)
69 if not exists(self.image):
70 raise ValueError("OS image %s does not exist" % self.image)
71 if args.mac:
72 self.mac_address = args.mac
73 else:
74 self.mac_address = random_mac()
75 self.serial_port = find_local_port(8990)
76 self.ssh_port = find_local_port(2222)
77 self.kvm = not args.no_kvm
78 self.gui = not args.no_gui
79 self.gdb = args.gdb
80 self.pcap = args.pcap
81 self.overlay = args.overlay
82
83 def command_line(self):
84 netuser = 'user,hostfwd=tcp:0.0.0.0:%d-:22,restrict=off' % self.ssh_port
85 if self.gdb:
86 netuser += ',hostfwd=tcp:0.0.0.0:2159-:2159'
87 cmdline = [
88 "qemu-system-x86_64",
89 "-bios", self.bios
90 ]
91 if not self.overlay:
92 cmdline += ["-drive", "file=%s,if=ide,format=raw,snapshot=on" % self.image]
93 cmdline += [
94 "-serial", "tcp:127.0.0.1:%d,server,nowait" % self.serial_port,
95 "-m", "1G",
96 "-usb",
97 "-usbdevice", "tablet",
98 "-show-cursor",
99 "-vga", "std",
100 "-net", netuser,
101 "-net", "nic,macaddr=%s" % self.mac_address
102 ]
103 if self.pcap:
104 cmdline += ['-net', 'dump,file=' + self.pcap]
105 if self.gui:
106 cmdline += ["-serial", "stdio"]
107 else:
108 cmdline.append('-nographic')
109 if self.kvm:
110 cmdline.append('-enable-kvm')
111 else:
112 cmdline += ['-cpu', 'Haswell']
113 if self.overlay:
114 cmdline.append(self.overlay)
115 return cmdline
116
117 def img_command_line(self):
118 cmdline = [
119 "qemu-img", "create",
120 "-o", "backing_file=%s" % self.image,
121 "-f", "qcow2",
122 self.overlay]
123 return cmdline
124
125 11
126def main(): 12def main():
127 parser = ArgumentParser(description='Run meta-updater image in qemu') 13 parser = ArgumentParser(description='Run meta-updater image in qemu')
@@ -135,11 +21,18 @@ def main():
135 'OSTREE_BOOTLOADER = "grub" and OVMF.fd firmware to be installed (try "apt install ovmf")', 21 'OSTREE_BOOTLOADER = "grub" and OVMF.fd firmware to be installed (try "apt install ovmf")',
136 action='store_true') 22 action='store_true')
137 parser.add_argument('--machine', default=None, help="Target MACHINE") 23 parser.add_argument('--machine', default=None, help="Target MACHINE")
138 parser.add_argument('--no-kvm', help='Disable KVM in QEMU', action='store_true') 24 kvm_group = parser.add_argument_group()
25 kvm_group.add_argument('--force-kvm', help='Force use of KVM (default is to autodetect)',
26 dest='kvm', action='store_true', default=None)
27 kvm_group.add_argument('--no-kvm', help='Disable KVM in QEMU',
28 dest='kvm', action='store_false')
139 parser.add_argument('--no-gui', help='Disable GUI', action='store_true') 29 parser.add_argument('--no-gui', help='Disable GUI', action='store_true')
140 parser.add_argument('--gdb', help='Export gdbserver port 2159 from the image', action='store_true') 30 parser.add_argument('--gdb', help='Export gdbserver port 2159 from the image', action='store_true')
141 parser.add_argument('--pcap', default=None, help='Dump all network traffic') 31 parser.add_argument('--pcap', default=None, help='Dump all network traffic')
142 parser.add_argument('-o', '--overlay', type=str, metavar='file.cow', help='Use an overlay storage image file. Will be created if it does not exist. This option lets you have a persistent image without modifying the underlying image file, permitting multiple different persistent machines.') 32 parser.add_argument('-o', '--overlay', type=str, metavar='file.cow',
33 help='Use an overlay storage image file. Will be created if it does not exist. ' +
34 'This option lets you have a persistent image without modifying the underlying image ' +
35 'file, permitting multiple different persistent machines.')
143 parser.add_argument('-n', '--dry-run', help='Print qemu command line rather then run it', action='store_true') 36 parser.add_argument('-n', '--dry-run', help='Print qemu command line rather then run it', action='store_true')
144 args = parser.parse_args() 37 args = parser.parse_args()
145 try: 38 try: