xref: /openbmc/qemu/tests/vm/ubuntuvm.py (revision 2e1cacfb)
1#!/usr/bin/env python3
2#
3# Ubuntu VM testing library
4#
5# Copyright 2017 Red Hat Inc.
6# Copyright 2020 Linaro
7#
8# Authors:
9#  Robert Foley <robert.foley@linaro.org>
10#  Originally based on ubuntu.i386 Fam Zheng <famz@redhat.com>
11#
12# This code is licensed under the GPL version 2 or later.  See
13# the COPYING file in the top-level directory.
14
15import os
16import subprocess
17import basevm
18
19class UbuntuVM(basevm.BaseVM):
20
21    def __init__(self, args, config=None):
22        self.login_prompt = "ubuntu-{}-guest login:".format(self.arch)
23        basevm.BaseVM.__init__(self, args, config)
24
25    def build_image(self, img):
26        """Build an Ubuntu VM image.  The child class will
27           define the install_cmds to init the VM."""
28        os_img = self._download_with_cache(self.image_link,
29                                           sha256sum=self.image_sha256)
30        img_tmp = img + ".tmp"
31        subprocess.check_call(["cp", "-f", os_img, img_tmp])
32        self.exec_qemu_img("resize", img_tmp, "+50G")
33        ci_img = self.gen_cloud_init_iso()
34
35        self.boot(img_tmp, extra_args = [ "-device", "VGA", "-cdrom", ci_img, ])
36
37        # First command we issue is fix for slow ssh login.
38        self.wait_ssh(wait_root=True,
39                      cmd="chmod -x /etc/update-motd.d/*")
40        # Wait for cloud init to finish
41        self.wait_ssh(wait_root=True,
42                      cmd="ls /var/lib/cloud/instance/boot-finished")
43        self.ssh_root("touch /etc/cloud/cloud-init.disabled")
44        # Disable auto upgrades.
45        # We want to keep the VM system state stable.
46        self.ssh_root('sed -ie \'s/"1"/"0"/g\' '\
47                      '/etc/apt/apt.conf.d/20auto-upgrades')
48        self.ssh_root("sed -ie s/^#\ deb-src/deb-src/g /etc/apt/sources.list")
49
50        # If the user chooses not to do the install phase,
51        # then we will jump right to the graceful shutdown
52        if self._config['install_cmds'] != "":
53            # Issue the install commands.
54            # This can be overridden by the user in the config .yml.
55            install_cmds = self._config['install_cmds'].split(',')
56            for cmd in install_cmds:
57                self.ssh_root(cmd)
58        self.graceful_shutdown()
59        os.rename(img_tmp, img)
60        return 0
61