445807b1f8084d98e9c375393429582943400dec
[nfvbench.git] / nfvbenchvm / dib / build-image.sh
1 #!/bin/bash
2 #
3 # A shell script to build the VPP VM image or NFVbench+TRex VM image using diskinage-builder
4 #
5 # The following packages must be installed prior to using this script:
6 # Ubuntu: sudo apt-get -y install python3 python3-venv qemu-utils kpartx
7 # CentOS: sudo yum install -y python3 qemu-img kpartx
8
9 # Stop on error (see https://wizardzines.com/comics/bash-errors/)
10 set -euo pipefail
11
12 DEBUG=no
13 verify_only=0
14 generator_only=0
15 loopvm_only=0
16 __prefix__=""
17
18 # Artifact URL
19 gs_url=artifacts.opnfv.org/nfvbench/images
20
21 # image version number
22 __version__=0.15
23 loopvm_image_name=nfvbenchvm_centos-$__version__
24 generator_image_name=nfvbenchvm_centos-generator-$__version__
25
26
27 # ----------------------------------------------------------------------------
28 # Parse command line options and configure the script
29 # ----------------------------------------------------------------------------
30
31 usage() {
32     cat <<EOF
33 $(basename $0) - build NFVbench VM images
34 Usage:
35     $(basename $0) [OPTIONS]
36
37 OPTIONS
38     -l: build NFVbench loop VM image
39     -g: build NFVbench generator image
40     -v: verify only (build but do not push to google storage)
41
42     -t: enable debug trace (set -x + DIB_DEBUG_TRACE=1)
43     -d: start debug shell in image chroot in case of build error
44     -h: show this help message
45 EOF
46     exit 1
47 }
48
49 while getopts ":lgvtdh" opt; do
50     case $opt in
51         l)
52             loopvm_only=1
53             ;;
54         g)
55             generator_only=1
56             ;;
57         v)
58             verify_only=1
59             ;;
60         t)
61             set -x
62             export DIB_DEBUG_TRACE=1
63             ;;
64         d)
65             DEBUG=yes
66             ;;
67         h)
68             usage
69             exit 0
70             ;;
71         ?)
72             usage
73             exit 1
74             ;;
75     esac
76 done
77
78
79 # ----------------------------------------------------------------------------
80 # Configure and start the nfvbenchvm image build
81 # ----------------------------------------------------------------------------
82
83 function build_image {
84     # if image exists skip building
85     echo "Checking if image exists in google storage..."
86     if  command -v gsutil >/dev/null; then
87        if gsutil -q stat gs://$gs_url/$1.qcow2; then
88            echo "Image already exists at http://$gs_url/$1.qcow2"
89            echo "Build is skipped"
90            exit 0
91        fi
92        echo "Image does not exist in google storage, starting build..."
93        echo
94     else
95        echo "Cannot check image availability in OPNFV artifact repository (gsutil not available)"
96     fi
97
98     # check if image is already built locally
99     if [ -f $1.qcow2 ]; then
100         echo "Image $1.qcow2 already exists locally"
101     else
102         # install diskimage-builder
103         if [ -d dib-venv ]; then
104            . dib-venv/bin/activate
105         else
106            python3 -m venv dib-venv
107            . dib-venv/bin/activate
108            pip install diskimage-builder==3.16.0
109         fi
110
111         # Add nfvbenchvm_centos elements directory to the DIB elements path
112         export ELEMENTS_PATH=`pwd`/elements
113
114         # canned user/password for direct login
115         export DIB_DEV_USER_USERNAME=nfvbench
116         export DIB_DEV_USER_PASSWORD=nfvbench
117         export DIB_DEV_USER_PWDLESS_SUDO=Y
118
119         # Set the data sources to have ConfigDrive only
120         export DIB_CLOUD_INIT_DATASOURCES="ConfigDrive"
121
122         # Configure VPP REPO
123         export DIB_YUM_REPO_CONF=$ELEMENTS_PATH/nfvbenchvm/fdio-release.repo
124
125         # Use ELRepo to have latest kernel
126         # only for loop vm image
127         if [ $1 = $loopvm_image_name ]; then
128            export DIB_USE_ELREPO_KERNEL=True
129            export DIB_DEV_IMAGE=loopvm
130         else
131            export DIB_USE_ELREPO_KERNEL=False
132            export DIB_DEV_IMAGE=generator
133            # get current git branch to build image with current code
134            export GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
135            # retrieve TREX_VER from Dockerfile
136            export TREX_VER=$(awk '/ENV TREX_VER/ {print $3}' ../../docker/Dockerfile | sed 's/"//g' | sed 's/\r//g')
137         fi
138
139         # Specify CentOS version
140         export DIB_RELEASE=7
141
142         # Debug on error: if an error occurs during the build, disk-image-create
143         # will drop us in a Bash inside the chroot, and we will be able to inspect
144         # the current state of the image.
145         if [[ "${DEBUG}" == "yes" ]]; then
146             export break=after-error
147         fi
148
149         echo "Building $1.qcow2..."
150         time disk-image-create -o $1 centos nfvbenchvm
151     fi
152
153     ls -l $1.qcow2
154
155     if [ $verify_only -eq 1 ]; then
156         echo "Image verification SUCCESS"
157         echo "NO upload to google storage (-v)"
158     else
159         if command -v gsutil >/dev/null; then
160             echo "Uploading $1.qcow2..."
161             gsutil cp $1.qcow2 gs://$gs_url/$1.qcow2
162             echo "You can access to image at http://$gs_url/$1.qcow2"
163         else
164             echo "Cannot upload new image to the OPNFV artifact repository (gsutil not available)"
165             exit 1
166         fi
167     fi
168 }
169
170
171 # ----------------------------------------------------------------------------
172 # Main program
173 # ----------------------------------------------------------------------------
174
175 if [ ! $generator_only -eq 1 ] && [ ! $loopvm_only -eq 1 ]; then
176    echo "Build loop VM image"
177    build_image $loopvm_image_name
178    echo "Build generator image"
179    build_image $generator_image_name
180 else
181     if [ $loopvm_only -eq 1 ]; then
182        echo "Build loop VM image"
183        build_image $loopvm_image_name
184     fi
185     if [ $generator_only -eq 1 ]; then
186        echo "Build generator image"
187        build_image $generator_image_name
188     fi
189 fi