xref: /openbmc/qemu/tests/vm/centos (revision c88ee46c)
1*c88ee46cSPhilippe Mathieu-Daudé#!/usr/bin/env python3
21bd26988SFam Zheng#
31bd26988SFam Zheng# CentOS image
41bd26988SFam Zheng#
51bd26988SFam Zheng# Copyright 2018 Red Hat Inc.
61bd26988SFam Zheng#
71bd26988SFam Zheng# Authors:
81bd26988SFam Zheng#  Fam Zheng <famz@redhat.com>
91bd26988SFam Zheng#
101bd26988SFam Zheng# This code is licensed under the GPL version 2 or later.  See
111bd26988SFam Zheng# the COPYING file in the top-level directory.
121bd26988SFam Zheng#
131bd26988SFam Zheng
141bd26988SFam Zhengimport os
151bd26988SFam Zhengimport sys
161bd26988SFam Zhengimport subprocess
171bd26988SFam Zhengimport basevm
181bd26988SFam Zhengimport time
191bd26988SFam Zheng
201bd26988SFam Zhengclass CentosVM(basevm.BaseVM):
211bd26988SFam Zheng    name = "centos"
2231719c37SPhilippe Mathieu-Daudé    arch = "x86_64"
231bd26988SFam Zheng    BUILD_SCRIPT = """
241bd26988SFam Zheng        set -e;
251bd26988SFam Zheng        cd $(mktemp -d);
261bd26988SFam Zheng        export SRC_ARCHIVE=/dev/vdb;
271bd26988SFam Zheng        sudo chmod a+r $SRC_ARCHIVE;
281bd26988SFam Zheng        tar -xf $SRC_ARCHIVE;
29aea43913SWainer dos Santos Moschetta        make docker-test-block@centos7 {verbose} J={jobs} NETWORK=1;
30aea43913SWainer dos Santos Moschetta        make docker-test-quick@centos7 {verbose} J={jobs} NETWORK=1;
31aea43913SWainer dos Santos Moschetta        make docker-test-mingw@fedora  {verbose} J={jobs} NETWORK=1;
321bd26988SFam Zheng    """
331bd26988SFam Zheng
341bd26988SFam Zheng    def _gen_cloud_init_iso(self):
351bd26988SFam Zheng        cidir = self._tmpdir
361bd26988SFam Zheng        mdata = open(os.path.join(cidir, "meta-data"), "w")
371bd26988SFam Zheng        mdata.writelines(["instance-id: centos-vm-0\n",
381bd26988SFam Zheng                          "local-hostname: centos-guest\n"])
391bd26988SFam Zheng        mdata.close()
401bd26988SFam Zheng        udata = open(os.path.join(cidir, "user-data"), "w")
411bd26988SFam Zheng        udata.writelines(["#cloud-config\n",
421bd26988SFam Zheng                          "chpasswd:\n",
431bd26988SFam Zheng                          "  list: |\n",
441bd26988SFam Zheng                          "    root:%s\n" % self.ROOT_PASS,
451bd26988SFam Zheng                          "    %s:%s\n" % (self.GUEST_USER, self.GUEST_PASS),
461bd26988SFam Zheng                          "  expire: False\n",
471bd26988SFam Zheng                          "users:\n",
481bd26988SFam Zheng                          "  - name: %s\n" % self.GUEST_USER,
491bd26988SFam Zheng                          "    sudo: ALL=(ALL) NOPASSWD:ALL\n",
501bd26988SFam Zheng                          "    ssh-authorized-keys:\n",
511bd26988SFam Zheng                          "    - %s\n" % basevm.SSH_PUB_KEY,
521bd26988SFam Zheng                          "  - name: root\n",
531bd26988SFam Zheng                          "    ssh-authorized-keys:\n",
541bd26988SFam Zheng                          "    - %s\n" % basevm.SSH_PUB_KEY,
551bd26988SFam Zheng                          "locale: en_US.UTF-8\n"])
561bd26988SFam Zheng        udata.close()
571bd26988SFam Zheng        subprocess.check_call(["genisoimage", "-output", "cloud-init.iso",
581bd26988SFam Zheng                               "-volid", "cidata", "-joliet", "-rock",
591bd26988SFam Zheng                               "user-data", "meta-data"],
601bd26988SFam Zheng                               cwd=cidir,
611bd26988SFam Zheng                               stdin=self._devnull, stdout=self._stdout,
621bd26988SFam Zheng                               stderr=self._stdout)
631bd26988SFam Zheng        return os.path.join(cidir, "cloud-init.iso")
641bd26988SFam Zheng
651bd26988SFam Zheng    def build_image(self, img):
661bd26988SFam Zheng        cimg = self._download_with_cache("https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud-1802.qcow2.xz")
671bd26988SFam Zheng        img_tmp = img + ".tmp"
68920fff90SPhilippe Mathieu-Daudé        sys.stderr.write("Extracting the image...\n")
69676d1f3eSCleber Rosa        subprocess.check_call(["ln", "-f", cimg, img_tmp + ".xz"])
70676d1f3eSCleber Rosa        subprocess.check_call(["xz", "--keep", "-dvf", img_tmp + ".xz"])
711e48931cSWainer dos Santos Moschetta        self.exec_qemu_img("resize", img_tmp, "50G")
721bd26988SFam Zheng        self.boot(img_tmp, extra_args = ["-cdrom", self._gen_cloud_init_iso()])
731bd26988SFam Zheng        self.wait_ssh()
741bd26988SFam Zheng        self.ssh_root_check("touch /etc/cloud/cloud-init.disabled")
751bd26988SFam Zheng        self.ssh_root_check("yum update -y")
766cd7b608SAlex Bennée        self.ssh_root_check("yum install -y docker make git python3")
771bd26988SFam Zheng        self.ssh_root_check("systemctl enable docker")
781bd26988SFam Zheng        self.ssh_root("poweroff")
791bd26988SFam Zheng        self.wait()
801bd26988SFam Zheng        os.rename(img_tmp, img)
811bd26988SFam Zheng        return 0
821bd26988SFam Zheng
831bd26988SFam Zhengif __name__ == "__main__":
841bd26988SFam Zheng    sys.exit(basevm.main(CentosVM))
85