remove unwated proxy in the script
[samplevnf.git] / tools / samplevnf-img-dpdk-samplevnf-modify
1 #!/bin/bash
2
3 # Copyright (c) 2016-2017 Intel Corporation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # samplevnf-img-dpdk-nsb-modify - download and modify a Ubuntu cloud image
18 #
19 # The actual customization is done by a script passed with an absolute path as
20 # the only single argument. The command needs to be invoked as sudo
21 #
22 # Example invocation:
23 # samplevnf-img-dpdk-nsb-modify /home/samplevnf/tools/ubuntu-server-cloudimg-nsb-modify.sh
24 #
25 # Warning: the script will create files by default in:
26 #   /tmp/workspace/samplevnf
27 # the files will be owned by root!
28 #
29 # TODO: image resize is needed if the base image is too small
30 #
31 set -e
32 set -x
33
34 die() {
35     echo "error: $1" >&2
36     exit 1
37 }
38
39 test $# -eq 1 -o $# -eq 2 || die "no image specific script as argument"
40 test $(id -u) -eq 0 || die "should invoke using sudo"
41
42 cmd=$1
43 RELEASE=$2
44 test -x $cmd
45 mountdir="/mnt/samplevnf"
46 workspace=${WORKSPACE:-"/tmp/workspace/samplevnf"}
47 host=${HOST:-"cloud-images.ubuntu.com"}
48 release=${RELEASE:-"xenial"}
49 boot_mode="disk1"
50 YARD_IMG_ARCH="amd64"
51 if [[ "${YARD_IMG_ARCH}" = "arm64" ]]; then
52     boot_mode="uefi1"
53 fi
54
55 image_path="${release}/current/${release}-server-cloudimg-${YARD_IMG_ARCH}-${boot_mode}.img"
56 image_url=${IMAGE_URL:-"https://${host}/${image_path}"}
57 sha256sums_path="${release}/current/SHA256SUMS"
58 sha256sums_url=${SHA256SUMS_URL:-"https://${host}/${sha256sums_path}"}
59
60 imgfile="${workspace}/samplevnf-image.img"
61 raw_imgfile_basename="samplevnf-${release}-server.raw"
62 raw_imgfile="${workspace}/${raw_imgfile_basename}"
63 filename=$(basename $image_url)
64 YARD_IMG_ARCH="amd64" #Neha added this just for testing
65 apt-get install -y parted
66
67 # download and checksum base image, conditionally if local copy is outdated
68 download() {
69     test -d $workspace || mkdir -p $workspace
70     cd $workspace
71     rm -f SHA256SUMS # always download the checksum file to a detect stale image
72     wget $sha256sums_url
73     test -e $filename || wget -nc --progress=dot:giga $image_url
74     grep $filename SHA256SUMS | sha256sum -c ||
75     if [ $? -ne 0 ]; then
76         rm $filename
77         wget -nc --progress=dot:giga $image_url
78         grep $filename SHA256SUMS | sha256sum -c
79     fi
80
81     for i in $(seq 0 9); do
82         [ -a /dev/loop$i ] || mknod -m 660 /dev/loop$i b 7 $i
83     done
84
85     qemu-img convert $filename $raw_imgfile
86     cd -
87 }
88
89 # mount image
90 setup() {
91 #    qemu-img resize $raw_imgfile +5GB
92     mkdir -p $mountdir
93
94     loopdevice=$(kpartx -l $raw_imgfile | head -1 | cut -f1 -d ' ')
95
96     kpartx -av $raw_imgfile
97
98     # for trouble shooting
99     sleep 2
100     dmsetup ls
101     fuser -c /dev/loop0
102     fuser -f /dev/loop0
103     parted -l /dev/${loopdevice:0:5} || true
104     mount /dev/mapper/$loopdevice $mountdir
105     mount -t proc none $mountdir/proc
106
107     sudo resize2fs /dev/mapper/$loopdevice
108     cp $cmd $mountdir/$(basename $cmd)
109     YARD_IMG_ARCH="amd64" #Neha added this just for testing
110     if [ "${YARD_IMG_ARCH}" = "arm64" ]; then
111         cp /usr/bin/qemu-aarch64-static $mountdir/usr/bin
112     fi
113 }
114
115 # modify image running a script using in a chrooted environment
116 modify() {
117     # resolv.conf does not exist in base image, pass nameserver value from host
118     nameserver_ip=$(grep -m 1 '^nameserver' \
119         /etc/resolv.conf | awk '{ print $2 '})
120
121     # prevent init scripts from running during install
122     echo $'#!/bin/sh\nexit 101' >$mountdir/usr/sbin/policy-rc.d
123     chmod a+x $mountdir/usr/sbin/policy-rc.d
124
125     chroot $mountdir /$(basename $cmd) $nameserver_ip
126
127     rm -rf $mountdir/usr/sbin/policy-rc.d
128
129     umount -f $mountdir/proc
130     umount -l $mountdir
131
132     qemu-img convert -c -o compat=0.10 -O qcow2 $raw_imgfile $imgfile
133
134     if dmsetup table | grep $loopdevice; then
135        dmsetup clear $loopdevice || true
136     fi
137 }
138
139 # cleanup (umount) the image
140 cleanup() {
141     # designed to be idempotent
142     mount | grep $mountdir/proc && umount -l $mountdir/proc
143     mount | grep $mountdir && umount -l $mountdir
144     mount | grep "/mnt/${release}" && umount -l "/mnt/${release}"
145
146     if [ -f "${raw_imgfile}" ]; then
147         #kpartx -dv $raw_imgfile sometimes failed, we should checked it agein.
148         #if [ -z "$(kpartx -l $raw_imgfile | grep 'loop deleted')" ]; then
149         #    kpartx -dv $raw_imgfile
150         #fi
151         kpartx -dv $raw_imgfile || true
152     fi
153
154     rm -f $raw_imgfile
155     rm -rf $mountdir
156 }
157
158 exitcode=""
159 error_trap()
160 {
161     local rc=$?
162
163     set +e
164
165     if [ -z "$exitcode" ]; then
166         exitcode=$rc
167     fi
168
169     dmesg -T | tail -50
170
171     cleanup
172
173     echo "Image build failed with $exitcode"
174
175     exit $exitcode
176 }
177
178 main() {
179     cleanup
180
181     trap "error_trap" EXIT SIGTERM
182
183     download
184     setup
185     modify
186
187     trap - EXIT SIGTERM
188     cleanup
189
190     echo "the modified image is found here: $imgfile"
191 }
192
193 main
194