-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathbuild-image.sh
More file actions
executable file
·180 lines (146 loc) · 5.78 KB
/
build-image.sh
File metadata and controls
executable file
·180 lines (146 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/bin/bash -x
### Build a docker image for ubuntu i386.
# Sources:
# https://wiki.debian.org/Debootstrap
# https://wiki.debian.org/EmDebian/CrossDebootstrap
# https://wiki.debian.org/Arm64Qemu
set -e
### settings
if [ "$IMAGE_OS" ]; then
os=$IMAGE_OS
else
os=ubuntu
fi
if [ "$IMAGE_ARCH" ]; then
arch=$IMAGE_ARCH
else
arch=arm64
fi
if [ "$IMAGE_SUITE" ]; then
suite=$IMAGE_SUITE
else
suite=xenial
fi
chroot_dir="/var/chroot/${os}_${arch}_$suite"
docker_image="osrf/${os}_$arch:$suite"
foreign_arches=(armhf arm64)
if [ $os == 'ubuntu' ]; then
if [ $suite == 'saucy' ] || [ $suite == 'utopic' ] || [ $suite == 'vivid' ] || [ $suite == 'wily' || [ $suite == 'yakkety' || [ $suite == 'zesty' ]; then
apt_mirror='http://old-releases.ubuntu.com/ubuntu'
elif [[ ${foreign_arches[*]} =~ $arch ]]; then
apt_mirror='http://ports.ubuntu.com'
else
apt_mirror='http://archive.ubuntu.com/ubuntu'
fi
elif [ $os == 'debian' ]; then
apt_mirror='http://httpredir.debian.org/debian'
fi
### make sure that the required tools are installed
# apt-get install -y docker.io debootstrap dchroot
### Clear chroot_dir to make sure the rebuild is clean
# This is tp prevent a corrupted chroot dir to break repeated failed
# rebuilds that have been observed at the deboostrap minbase stage
echo Clear chroot before debootstrap
rm -rf $chroot_dir
### install a minbase system with debootstrap
export DEBIAN_FRONTEND=noninteractive
foreign_arg=''
if [[ ${foreign_arches[*]} =~ $arch ]]; then
foreign_arg='--foreign'
fi
debootstrap $foreign_arg --variant=minbase --arch=$arch $suite $chroot_dir $apt_mirror
if [[ ${foreign_arches[*]} =~ $arch ]]; then
if [ $arch == 'armhf' ]; then
cp qemu-arm-static $chroot_dir/usr/bin/
elif [ $arch == 'arm64' ]; then
cp qemu-aarch64-static $chroot_dir/usr/bin/
fi
LC_ALL=C LANGUAGE=C LANG=C chroot $chroot_dir /debootstrap/debootstrap --second-stage
LC_ALL=C LANGUAGE=C LANG=C chroot $chroot_dir dpkg --configure -a
fi
if [ $os == 'ubuntu' ]; then
repositories='main restricted universe multiverse'
else
repositories='main'
additional_components='non-free contrib'
fi
### update the list of package sources
cat <<EOF > $chroot_dir/etc/apt/sources.list
deb $apt_mirror $suite $repositories
EOF
if [ -n "$additional_components" ]; then
for component in $additional_components; do
cat <<EOF >> $chroot_dir/etc/apt/sources.list
deb $apt_mirror $suite $component
EOF
done
fi
if [ $os == 'ubuntu' ]; then
cat <<EOF >> $chroot_dir/etc/apt/sources.list
deb $apt_mirror $suite-updates $repositories
deb $apt_mirror $suite-backports $repositories
EOF
if [ ! [ ${foreign_arches[*]} =~ $arch ] ]; then
cat <<EOF >> $chroot_dir/etc/apt/sources.list
deb http://security.ubuntu.com/ubuntu $suite-security main restricted universe multiverse
EOF
fi
fi
# if [ "$suite" != "vivid" ]; then
# cat <<EOF >> $chroot_dir/etc/apt/sources.list
# deb http://extras.ubuntu.com/ubuntu $suite main
# EOF
# fi
# a few minor docker-specific tweaks
# see https://github.com/docker/docker/blob/master/contrib/mkimage/debootstrap
# prevent init scripts from running during install/update
echo '#!/bin/sh' > $chroot_dir/usr/sbin/policy-rc.d
echo 'exit 101' >> $chroot_dir/usr/sbin/policy-rc.d
chmod +x $chroot_dir/usr/sbin/policy-rc.d
# force dpkg not to call sync() after package extraction (speeding up installs)
echo 'force-unsafe-io' > $chroot_dir/etc/dpkg/dpkg.cfg.d/docker-apt-speedup
# Install humanity-icon-theme on bionic to work around
# https://github.com/ros-infrastructure/buildfarm_deployment/issues/198
if [ $suite == 'bionic' ]; then
chroot $chroot_dir sh -c 'apt-get update && apt-get install -y humanity-icon-theme'
fi
# _keep_ us lean by effectively running "apt-get clean" after every install
echo 'DPkg::Post-Invoke { "rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true"; };' > $chroot_dir/etc/apt/apt.conf.d/docker-clean
echo 'APT::Update::Post-Invoke { "rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true"; };' >> $chroot_dir/etc/apt/apt.conf.d/docker-clean
echo 'Dir::Cache::pkgcache ""; Dir::Cache::srcpkgcache "";' >> $chroot_dir/etc/apt/apt.conf.d/docker-clean
# remove apt-cache translations for fast "apt-get update"
echo 'Acquire::Languages "none";' > $chroot_dir/etc/apt/apt.conf.d/docker-no-languages
# store Apt lists files gzipped on-disk for smaller size
echo 'Acquire::GzipIndexes "true"; Acquire::CompressionTypes::Order:: "gz";' > $chroot_dir/etc/apt/apt.conf.d/docker-gzip-indexes
cp /etc/resolv.conf $chroot_dir/etc/resolv.conf
mount -o bind /proc $chroot_dir/proc
### install ubuntu-minimal
if [ $os == 'ubuntu' ]; then
chroot $chroot_dir apt-get update
chroot $chroot_dir apt-get -y install ubuntu-minimal
elif [ $os == 'debian' ]; then
echo 'TODO debian minimal here'
fi
# https://github.com/docker/docker/issues/1024
chroot $chroot_dir dpkg-divert --local --rename --add /sbin/initctl
chroot $chroot_dir ln -sf /bin/true /sbin/initctl
### Build semop wrapper
cp wrap_semop.c $chroot_dir/tmp/
chroot $chroot_dir apt-get -y install build-essential
chroot $chroot_dir gcc -fPIC -shared -o /opt/libpreload-semop.so /tmp/wrap_semop.c
chroot $chroot_dir echo /opt/libpreload-semop.so > $chroot_dir/etc/ld.so.preload
### cleanup and unmount /proc
chroot $chroot_dir apt-get autoclean
chroot $chroot_dir apt-get clean
chroot $chroot_dir apt-get autoremove
rm $chroot_dir/etc/resolv.conf
umount $chroot_dir/proc
### create a tar archive from the chroot directory
tar cfz $os_$arch_$suite.tgz -C $chroot_dir .
### import this tar archive into a docker image:
cat $os_$arch_$suite.tgz | docker import - $docker_image
# ### cleanup
rm $os_$arch_$suite.tgz
rm -rf $chroot_dir
### push image to Docker Hub
echo "Test the image $docker_image and push it to upstream with 'docker push $docker_image'"