xref: /openbmc/qemu/tests/vm/centos (revision 1bd26988)
1*1bd26988SFam Zheng#!/usr/bin/env python
2*1bd26988SFam Zheng#
3*1bd26988SFam Zheng# CentOS image
4*1bd26988SFam Zheng#
5*1bd26988SFam Zheng# Copyright 2018 Red Hat Inc.
6*1bd26988SFam Zheng#
7*1bd26988SFam Zheng# Authors:
8*1bd26988SFam Zheng#  Fam Zheng <famz@redhat.com>
9*1bd26988SFam Zheng#
10*1bd26988SFam Zheng# This code is licensed under the GPL version 2 or later.  See
11*1bd26988SFam Zheng# the COPYING file in the top-level directory.
12*1bd26988SFam Zheng#
13*1bd26988SFam Zheng
14*1bd26988SFam Zhengimport os
15*1bd26988SFam Zhengimport sys
16*1bd26988SFam Zhengimport subprocess
17*1bd26988SFam Zhengimport basevm
18*1bd26988SFam Zhengimport time
19*1bd26988SFam Zheng
20*1bd26988SFam Zhengclass CentosVM(basevm.BaseVM):
21*1bd26988SFam Zheng    name = "centos"
22*1bd26988SFam Zheng    BUILD_SCRIPT = """
23*1bd26988SFam Zheng        set -e;
24*1bd26988SFam Zheng        cd $(mktemp -d);
25*1bd26988SFam Zheng        export SRC_ARCHIVE=/dev/vdb;
26*1bd26988SFam Zheng        sudo chmod a+r $SRC_ARCHIVE;
27*1bd26988SFam Zheng        tar -xf $SRC_ARCHIVE;
28*1bd26988SFam Zheng        make docker-test-block@centos7 V={verbose} J={jobs};
29*1bd26988SFam Zheng        make docker-test-quick@centos7 V={verbose} J={jobs};
30*1bd26988SFam Zheng        make docker-test-mingw@fedora V={verbose} J={jobs};
31*1bd26988SFam Zheng    """
32*1bd26988SFam Zheng
33*1bd26988SFam Zheng    def _gen_cloud_init_iso(self):
34*1bd26988SFam Zheng        cidir = self._tmpdir
35*1bd26988SFam Zheng        mdata = open(os.path.join(cidir, "meta-data"), "w")
36*1bd26988SFam Zheng        mdata.writelines(["instance-id: centos-vm-0\n",
37*1bd26988SFam Zheng                          "local-hostname: centos-guest\n"])
38*1bd26988SFam Zheng        mdata.close()
39*1bd26988SFam Zheng        udata = open(os.path.join(cidir, "user-data"), "w")
40*1bd26988SFam Zheng        udata.writelines(["#cloud-config\n",
41*1bd26988SFam Zheng                          "chpasswd:\n",
42*1bd26988SFam Zheng                          "  list: |\n",
43*1bd26988SFam Zheng                          "    root:%s\n" % self.ROOT_PASS,
44*1bd26988SFam Zheng                          "    %s:%s\n" % (self.GUEST_USER, self.GUEST_PASS),
45*1bd26988SFam Zheng                          "  expire: False\n",
46*1bd26988SFam Zheng                          "users:\n",
47*1bd26988SFam Zheng                          "  - name: %s\n" % self.GUEST_USER,
48*1bd26988SFam Zheng                          "    sudo: ALL=(ALL) NOPASSWD:ALL\n",
49*1bd26988SFam Zheng                          "    ssh-authorized-keys:\n",
50*1bd26988SFam Zheng                          "    - %s\n" % basevm.SSH_PUB_KEY,
51*1bd26988SFam Zheng                          "  - name: root\n",
52*1bd26988SFam Zheng                          "    ssh-authorized-keys:\n",
53*1bd26988SFam Zheng                          "    - %s\n" % basevm.SSH_PUB_KEY,
54*1bd26988SFam Zheng                          "locale: en_US.UTF-8\n"])
55*1bd26988SFam Zheng        udata.close()
56*1bd26988SFam Zheng        subprocess.check_call(["genisoimage", "-output", "cloud-init.iso",
57*1bd26988SFam Zheng                               "-volid", "cidata", "-joliet", "-rock",
58*1bd26988SFam Zheng                               "user-data", "meta-data"],
59*1bd26988SFam Zheng                               cwd=cidir,
60*1bd26988SFam Zheng                               stdin=self._devnull, stdout=self._stdout,
61*1bd26988SFam Zheng                               stderr=self._stdout)
62*1bd26988SFam Zheng        return os.path.join(cidir, "cloud-init.iso")
63*1bd26988SFam Zheng
64*1bd26988SFam Zheng    def build_image(self, img):
65*1bd26988SFam Zheng        cimg = self._download_with_cache("https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud-1802.qcow2.xz")
66*1bd26988SFam Zheng        img_tmp = img + ".tmp"
67*1bd26988SFam Zheng        subprocess.check_call(["cp", "-f", cimg, img_tmp + ".xz"])
68*1bd26988SFam Zheng        subprocess.check_call(["xz", "-df", img_tmp + ".xz"])
69*1bd26988SFam Zheng        subprocess.check_call(["qemu-img", "resize", img_tmp, "50G"])
70*1bd26988SFam Zheng        self.boot(img_tmp, extra_args = ["-cdrom", self._gen_cloud_init_iso()])
71*1bd26988SFam Zheng        self.wait_ssh()
72*1bd26988SFam Zheng        self.ssh_root_check("touch /etc/cloud/cloud-init.disabled")
73*1bd26988SFam Zheng        self.ssh_root_check("yum update -y")
74*1bd26988SFam Zheng        self.ssh_root_check("yum install -y docker make git")
75*1bd26988SFam Zheng        self.ssh_root_check("systemctl enable docker")
76*1bd26988SFam Zheng        self.ssh_root("poweroff")
77*1bd26988SFam Zheng        self.wait()
78*1bd26988SFam Zheng        if os.path.exists(img):
79*1bd26988SFam Zheng            os.remove(img)
80*1bd26988SFam Zheng        os.rename(img_tmp, img)
81*1bd26988SFam Zheng        return 0
82*1bd26988SFam Zheng
83*1bd26988SFam Zhengif __name__ == "__main__":
84*1bd26988SFam Zheng    sys.exit(basevm.main(CentosVM))
85