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