diff options
author | cajun-rat <phil@advancedtelematic.com> | 2017-11-15 17:38:11 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-15 17:38:11 +0100 |
commit | f64cbdb8deadacbece998636b7d3bc83e7ac2ec9 (patch) | |
tree | 364c02213fe9d16bdadc24d0e531e137eff1165b /scripts/qemucommand.py | |
parent | 71410bd31ec76e55247807551e68a2061e277b08 (diff) | |
parent | 9411b6039661f50832779d021f3d47b2d8516634 (diff) | |
download | meta-updater-f64cbdb8deadacbece998636b7d3bc83e7ac2ec9.tar.gz |
Merge pull request #174 from advancedtelematic/feat/PRO-4199/oe-selftest-sota-tools
Feat/pro 4199/oe selftest sota tools
Diffstat (limited to 'scripts/qemucommand.py')
-rw-r--r-- | scripts/qemucommand.py | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/scripts/qemucommand.py b/scripts/qemucommand.py new file mode 100644 index 0000000..a75ffb6 --- /dev/null +++ b/scripts/qemucommand.py | |||
@@ -0,0 +1,118 @@ | |||
1 | from os.path import exists, join, realpath, abspath | ||
2 | from os import listdir | ||
3 | import random | ||
4 | import socket | ||
5 | |||
6 | EXTENSIONS = { | ||
7 | 'intel-corei7-64': 'wic', | ||
8 | 'qemux86-64': 'otaimg' | ||
9 | } | ||
10 | |||
11 | |||
12 | def find_local_port(start_port): | ||
13 | """" | ||
14 | Find the next free TCP port after 'start_port'. | ||
15 | """ | ||
16 | |||
17 | for port in range(start_port, start_port + 10): | ||
18 | try: | ||
19 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
20 | s.bind(('', port)) | ||
21 | return port | ||
22 | except socket.error: | ||
23 | print("Skipping port %d" % port) | ||
24 | finally: | ||
25 | s.close() | ||
26 | raise Exception("Could not find a free TCP port") | ||
27 | |||
28 | |||
29 | def random_mac(): | ||
30 | """Return a random Ethernet MAC address | ||
31 | @link https://www.iana.org/assignments/ethernet-numbers/ethernet-numbers.xhtml#ethernet-numbers-2 | ||
32 | """ | ||
33 | head = "ca:fe:" | ||
34 | hex_digits = '0123456789abcdef' | ||
35 | tail = ':'.join([random.choice(hex_digits) + random.choice(hex_digits) for _ in range(4)]) | ||
36 | return head + tail | ||
37 | |||
38 | |||
39 | class QemuCommand(object): | ||
40 | def __init__(self, args): | ||
41 | if args.machine: | ||
42 | self.machine = args.machine | ||
43 | else: | ||
44 | machines = listdir(args.dir) | ||
45 | if len(machines) == 1: | ||
46 | self.machine = machines[0] | ||
47 | else: | ||
48 | raise ValueError("Could not autodetect machine type from %s" % args.dir) | ||
49 | if args.efi: | ||
50 | self.bios = 'OVMF.fd' | ||
51 | else: | ||
52 | uboot = abspath(join(args.dir, self.machine, 'u-boot-qemux86-64.rom')) | ||
53 | if not exists(uboot): | ||
54 | raise ValueError("U-Boot image %s does not exist" % uboot) | ||
55 | self.bios = uboot | ||
56 | if exists(args.imagename): | ||
57 | image = args.imagename | ||
58 | else: | ||
59 | ext = EXTENSIONS.get(self.machine, 'wic') | ||
60 | image = join(args.dir, self.machine, '%s-%s.%s' % (args.imagename, self.machine, ext)) | ||
61 | self.image = realpath(image) | ||
62 | if not exists(self.image): | ||
63 | raise ValueError("OS image %s does not exist" % self.image) | ||
64 | if args.mac: | ||
65 | self.mac_address = args.mac | ||
66 | else: | ||
67 | self.mac_address = random_mac() | ||
68 | self.serial_port = find_local_port(8990) | ||
69 | self.ssh_port = find_local_port(2222) | ||
70 | self.kvm = not args.no_kvm | ||
71 | self.gui = not args.no_gui | ||
72 | self.gdb = args.gdb | ||
73 | self.pcap = args.pcap | ||
74 | self.overlay = args.overlay | ||
75 | |||
76 | def command_line(self): | ||
77 | netuser = 'user,hostfwd=tcp:0.0.0.0:%d-:22,restrict=off' % self.ssh_port | ||
78 | if self.gdb: | ||
79 | netuser += ',hostfwd=tcp:0.0.0.0:2159-:2159' | ||
80 | cmdline = [ | ||
81 | "qemu-system-x86_64", | ||
82 | "-bios", self.bios | ||
83 | ] | ||
84 | if not self.overlay: | ||
85 | cmdline += ["-drive", "file=%s,if=ide,format=raw,snapshot=on" % self.image] | ||
86 | cmdline += [ | ||
87 | "-serial", "tcp:127.0.0.1:%d,server,nowait" % self.serial_port, | ||
88 | "-m", "1G", | ||
89 | "-usb", | ||
90 | "-usbdevice", "tablet", | ||
91 | "-show-cursor", | ||
92 | "-vga", "std", | ||
93 | "-net", netuser, | ||
94 | "-net", "nic,macaddr=%s" % self.mac_address | ||
95 | ] | ||
96 | if self.pcap: | ||
97 | cmdline += ['-net', 'dump,file=' + self.pcap] | ||
98 | if self.gui: | ||
99 | cmdline += ["-serial", "stdio"] | ||
100 | else: | ||
101 | cmdline.append('-nographic') | ||
102 | if self.kvm: | ||
103 | cmdline.append('-enable-kvm') | ||
104 | else: | ||
105 | cmdline += ['-cpu', 'Haswell'] | ||
106 | if self.overlay: | ||
107 | cmdline.append(self.overlay) | ||
108 | return cmdline | ||
109 | |||
110 | def img_command_line(self): | ||
111 | cmdline = [ | ||
112 | "qemu-img", "create", | ||
113 | "-o", "backing_file=%s" % self.image, | ||
114 | "-f", "qcow2", | ||
115 | self.overlay] | ||
116 | return cmdline | ||
117 | |||
118 | |||