-
Notifications
You must be signed in to change notification settings - Fork 3
/
build-openstack-debian-image
executable file
·421 lines (365 loc) · 13.4 KB
/
build-openstack-debian-image
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#!/bin/sh
# tab = 4 spaces
set -e
# Parse input parameters
usage() {
echo "Usage: $0 --release|-r <jessie|wheezy> [options]
Options are:
--minimal|-m
--debootstrap-url|-u <debootstrap-mirror> (default: http://http.debian.net/debian)
--sources.list-mirror|-s <source-list-mirror> (default: http://http.debian.net/debian)
--extra-packages|-e <package>,<package>,...
--hook-script|-hs <hook-script>
--image-size|-is <image-size> (default: 2G)
--automatic-resize|-ar
--automatic-resize-space|-ars <suplementary-space> (default: 50M)
--password|-p <root-password> (dangerous option: avoid it if possible)
For more info: man $0"
exit 1
}
EXTRA=yes
for i in $@ ; do
case "${1}" in
"--extra-packages"|"-e")
if [ -z "${2}" ] ; then
echo "No parameter defining the extra packages"
usage
fi
EXTRA_PACKAGES=${2}
shift
shift
;;
"--debootstrap-url"|"-u")
if [ -z "${2}" ] ; then
echo "No parameter defining the debootstrap URL"
usage
fi
DEB_MIRROR=${2}
shift
shift
;;
"--minimal"|"-m")
EXTRA=no
shift
;;
"--automatic-resize"|"-ar")
AUTOMATIC_RESIZE=yes
shift
;;
"--automatic-resize-space"|"-ars")
if [ -z "${2}" ] ; then
echo "No parameter defining the suplementary space"
usage
fi
AUTOMATIC_RESIZE_SPACE=${2}
shift
shift
;;
"--image-size"|"-is")
if [ -z "${2}" ] ; then
echo "No parameter defining the image size"
usage
fi
IMAGE_SIZE=${2}
shift
shift
;;
"--hook-script"|"-hs")
if [ -z "${2}" ] ; then
echo "No parameter defining the hook script"
usage
fi
if ! [ -x "${2}" ] ; then
echo "Hook script not executable"
fi
HOOK_SCRIPT=${2}
shift
shift
;;
"--sources.list-mirror"|"-s")
if [ -z "${2}" ] ; then
echo "No parameter defining the hook script"
usage
fi
SOURCE_LIST_MIRROR=${2}
shift
shift
;;
"--release"|"-r")
if [ "${2}" = "wheezy" ] || [ "${2}" = "jessie" ] ; then
RELEASE=${2}
shift
shift
else
echo "Release not recognized."
usage
fi
;;
"--password"|"-p")
if [ -z "${2}" ] ; then
echo "No parameter defining the root password"
fi
ROOT_PASSWORD=${2}
shift
shift
;;
*)
;;
esac
done
if [ -z "${RELEASE}" ] ; then
echo "Release not recognized: please specify the -r parameter."
usage
fi
if [ -z "${DEB_MIRROR}" ] ; then
DEB_MIRROR=http://ftp.at.debian.org/debian
fi
if [ -z "${EXTRA_PACKAGES}" ] ; then
EXTRA_PACKAGES=bash-completion,joe,most,screen,less,vim,bzip2
fi
if [ -z "${SOURCE_LIST_MIRROR}" ] ; then
SOURCE_LIST_MIRROR=http://ftp.at.debian.org/debian/
fi
if [ -z "${IMAGE_SIZE}" ] ; then
IMAGE_SIZE=2
fi
if [ -z "${AUTOMATIC_RESIZE_SPACE}" ] ; then
AUTOMATIC_RESIZE_SPACE=50
fi
ADDITIONAL_PACKAGES=tcpdump,tcptraceroute,htop,multitail,ifstat
NEEDED_PACKAGES=sudo,adduser,locales,extlinux,openssh-server,linux-image-amd64,euca2ools,file,kbd
if [ "${RELEASE}" = "jessie" ] ; then
NEEDED_PACKAGES=${NEEDED_PACKAGES},${ADDITIONAL_PACKAGES},cloud-init,cloud-utils,cloud-initramfs-growroot
else
# These are needed by cloud-init and friends, and since we don't want backports of them,
# but just normal packages from Wheezy, we resolve dependencies by hand, prior to using
# apt-get -t wheezy-backports install cloud-init cloud-utils cloud-initramfs-growroot
NEEDED_PACKAGES=${NEEDED_PACKAGES},${ADDITIONAL_PACKAGES},python,python-paramiko,python-argparse,python-cheetah,python-configobj,python-oauth,python-software-properties,python-yaml,python-boto,python-prettytable,initramfs-tools,python-requests
fi
if [ ${EXTRA} = "no" ] ; then
PKG_LIST=${NEEDED_PACKAGES}
else
PKG_LIST=${NEEDED_PACKAGES},${EXTRA_PACKAGES}
fi
if ! [ `whoami` = "root" ] ; then
echo "You have to be root to run this script"
exit 1
fi
DATE=`date '+%F_%H%M%S'`
FILE_NAME=debian-${RELEASE}-amd64_${DATE}
AMI_NAME=${FILE_NAME}.raw
QCOW2_NAME=${FILE_NAME}.qcow2
set -x
######################################
### Prepare the HDD (format, ext.) ###
######################################
PARTED=/sbin/parted
rm -f $AMI_NAME
qemu-img create ${AMI_NAME} ${IMAGE_SIZE}G
#dd if=/dev/null bs=1M seek=1024 of=${AMI_NAME}
${PARTED} -s ${AMI_NAME} mktable msdos
${PARTED} -s -a optimal ${AMI_NAME} mkpart primary ext3 1Mi 100%
${PARTED} -s ${AMI_NAME} set 1 boot on
install-mbr ${AMI_NAME}
RESULT_KPARTX=`kpartx -av ${AMI_NAME} 2>&1`
if echo "${RESULT_KPARTX}" | grep "^add map" ; then
LOOP_DEVICE=`echo ${RESULT_KPARTX} | cut -d" " -f3`
echo "kpartx mounted using: ${LOOP_DEVICE}"
else
echo "It seems kpartx didn't mount the image correctly: exiting."
exit 1
fi
cleanup(){
[ ! -d "${MOUNT_DIR}" ] && return
# Save debootsrap log before deleting everything during cleanup
# Probably much more should be saved in case for debug purpose
echo "error occured ... "
echo "saving logs and umounting $MOUNT_DIR"
DEBOOTSRAP_LOG=${MOUNT_DIR}/debootstrap/debootstrap.log
mkdir -p log
cp ${DEBOOTSRAP_LOG} log/`basename $DEBOOTSRAP_LOG`_${DATE}
chroot ${MOUNT_DIR} umount /proc || true
chroot ${MOUNT_DIR} umount /sys || true
umount ${MOUNT_DIR}
rmdir ${MOUNT_DIR}
kpartx -d ${AMI_NAME}
}
trap "cleanup" EXIT TERM INT
# We first use ext2, THEN convert to ext3, because that's so much faster this way.
mkfs.ext2 /dev/mapper/${LOOP_DEVICE}
# No fsck because of X days without checks
tune2fs -i 0 /dev/mapper/${LOOP_DEVICE}
MOUNT_DIR=`mktemp -d -t build-debimg.XXXXXX`
mount -o loop /dev/mapper/${LOOP_DEVICE} ${MOUNT_DIR}
debootstrap --arch=amd64 --verbose \
--include=${PKG_LIST} \
${RELEASE} ${MOUNT_DIR} ${DEB_MIRROR}
############################
### Customize the distro ###
############################
### Customize: access to the VM ###
# Setup default root password to what has been set on the command line
# Password will be overidden by set_root_password unles it fails
# in which case, we have a backdoor
if [ -n "${ROOT_PASSWORD}" ] ; then
chroot ${MOUNT_DIR} sh -c "echo root:${ROOT_PASSWORD} | chpasswd"
else
chroot ${MOUNT_DIR} sh -c "echo root:lol | chpasswd"
fi
# Since we implemented "backdoor" we want to be able to use it :)
# sed -i "s/PermitRootLogin yes/PermitRootLogin without-password/" ${MOUNT_DIR}/etc/ssh/sshd_config
# Add a default "debian" user which is used by cloud-init by default
chroot ${MOUNT_DIR} adduser --gecos Debian-cloud-init-user --disabled-password --quiet debian
# Adds the "debian" user to sudoers, since that is the way
# cloud-init grant access
mkdir -p ${MOUNT_DIR}/etc/sudoers.d
echo "debian ALL = NOPASSWD: ALL" >${MOUNT_DIR}/etc/sudoers.d/debian-cloud-init
chmod 0440 ${MOUNT_DIR}/etc/sudoers.d/debian-cloud-init
### Customize: misc stuff ###
# Setup fstab
echo "# /etc/fstab: static file system information.
proc /proc proc nodev,noexec,nosuid 0 0
/dev/vda1 / ext3 errors=remount-ro 0 1
" > ${MOUNT_DIR}/etc/fstab
chroot ${MOUNT_DIR} mount /proc || true
echo "# disable pc speaker
blacklist pcspkr" >${MOUNT_DIR}/etc/modprobe.d/blacklist.conf
echo "# Required for cinder hotplug
acpiphp
pci_hotplug
" >>${MOUNT_DIR}/etc/modules
# Enable bash-completion by default
if [ ${EXTRA} = "yes" ] ; then
echo "# enable bash completion in interactive shells
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi" >>${MOUNT_DIR}/etc/bash.bashrc
# No clear for the tty1 console
sed -i "s#1:2345:respawn:/sbin/getty 38400 tty1#1:2345:respawn:/sbin/getty --noclear 38400 tty1#" ${MOUNT_DIR}/etc/inittab
chroot ${MOUNT_DIR} apt-get install -y locales-all
fi
# Turn off console blanking which is *very* annoying
# and increase KEYBOARD_DELAY because it can be annoying
# over network.
sed -i s/^BLANK_TIME=.*/BLANK_TIME=0/ ${MOUNT_DIR}/etc/kbd/config
sed -i s/^POWERDOWN_TIME=.*/POWERDOWN_TIME=0/ ${MOUNT_DIR}/etc/kbd/config
sed -i 's/^[ \t#]KEYBOARD_DELAY=.*/KEYBOARD_DELAY=1000/' ${MOUNT_DIR}/etc/kbd/config
rm -f ${MOUNT_DIR}/etc/ssh/ssh_host_*
rm -f ${MOUNT_DIR}/etc/udev/rules.d/70-persistent-net.rules
rm -f ${MOUNT_DIR}/lib/udev/write_net_rules
# Setup networking (eg: DHCP by default)
echo "# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
# The normal eth0
auto eth0
iface eth0 inet dhcp
# Maybe the VM has 2 NICs?
allow-hotplug eth1
iface eth1 inet dhcp
# Maybe the VM has 3 NICs?
allow-hotplug eth2
iface eth2 inet dhcp
# Maybe the VM has 4 NICs?
allow-hotplug eth3
iface eth3 inet dhcp
" >${MOUNT_DIR}/etc/network/interfaces
# Setup the default hostname (will be set by cloud-init
# at boot time anyway)
echo "debian.cloud.testlab" >${MOUNT_DIR}/etc/hostname
# This should be a correct default everywhere
echo "deb ${SOURCE_LIST_MIRROR} ${RELEASE} main contrib non-free
deb-src ${SOURCE_LIST_MIRROR} ${RELEASE} main contrib non-free" >${MOUNT_DIR}/etc/apt/sources.list
if [ "${RELEASE}" = "wheezy" ] ; then
echo "deb ${SOURCE_LIST_MIRROR} wheezy-updates main contrib non-free
deb-src ${SOURCE_LIST_MIRROR} wheezy-updates main contrib non-free
deb http://security.debian.org/ wheezy/updates main contrib non-free
deb-src http://security.debian.org/ wheezy/updates main contrib non-free
deb ${SOURCE_LIST_MIRROR} wheezy-backports main contrib non-free
deb-src ${SOURCE_LIST_MIRROR} wheezy-backports main contrib non-free
" >>${MOUNT_DIR}/etc/apt/sources.list
fi
chroot ${MOUNT_DIR} apt-get update
# Setup cloud-init, cloud-utils and cloud-initramfs-growroot
# These are only available from backports in Wheezy
if [ "${RELEASE}" = "wheezy" ] ; then
chroot ${MOUNT_DIR} apt-get -t wheezy-backports install cloud-init cloud-utils cloud-initramfs-growroot -y
fi
# For OpenStack, we would like to use Ec2 and no other API
echo "# to update this file, run dpkg-reconfigure cloud-init
datasource_list: [ Ec2 ]" >${MOUNT_DIR}/etc/cloud/cloud.cfg.d/90_dpkg.cfg
# Allow SSH in as root, even though it might be unsecure
sed -i 's/disable_root: true/disable_root: false/' ${MOUNT_DIR}/etc/cloud/cloud.cfg
# Set up apt hhtp proxy for use in tssk cloud
echo 'Acquire::http::Proxy "http://10.233.130.190:8080";' >>${MOUNT_DIR}/etc/apt/apt.conf.d/00proxy
# Set timezone to Europe/Bratislava
cp ${MOUNT_DIR}/usr/share/zoneinfo/Europe/Bratislava ${MOUNT_DIR}/etc/localtime
# Temporary workaround for set_root_password
# Script will be moved to github and downloaded on every build
cp ./lib/set_root_password ${MOUNT_DIR}/etc/init.d/set_root_password
chroot ${MOUNT_DIR} chmod +x /etc/init.d/set_root_password
chroot ${MOUNT_DIR} update-rc.d set_root_password defaults
# Setting-up initramfs
chroot ${MOUNT_DIR} update-initramfs -u
rm ${MOUNT_DIR}/var/cache/apt/archives/*.deb
###########################
### Setting-up extlinux ###
###########################
KERNEL=`chroot ${MOUNT_DIR} find boot -name 'vmlinuz-*'`
RAMDISK=`chroot ${MOUNT_DIR} find boot -name 'initrd.img-*'`
UUID=`blkid -o value -s UUID /dev/mapper/${LOOP_DEVICE}`
echo "default linux
timeout 1
label linux
kernel ${KERNEL}
append initrd=${RAMDISK} root=/dev/vda1 console=tty0 console=ttyS0,115200 ro quiet" > ${MOUNT_DIR}/boot/extlinux/extlinux.conf
#append initrd=${RAMDISK} root=/dev/vda1 ro console=ttyS0" > ${MOUNT_DIR}/boot/extlinux/extlinux.conf
cp ${MOUNT_DIR}/boot/extlinux/extlinux.conf ${MOUNT_DIR}/extlinux.conf
extlinux --install ${MOUNT_DIR}
#chroot ${MOUNT_DIR} extlinux-update
###################
### HOOK SCRIPT ###
###################
if [ -x ${HOOK_SCRIPT} ] ; then
export BODI_CHROOT_PATH=${MOUNT_DIR}
export BODI_RELEASE=${RELEASE}
${HOOK_SCRIPT}
fi
##########################
### Unmount everything ###
##########################
cleanup(){
# refine cleanup everything was ok
echo "Finished."
}
chroot ${MOUNT_DIR} umount /proc || true
umount ${MOUNT_DIR}
# Run FSCK so that resize can work
tune2fs -j /dev/mapper/${LOOP_DEVICE}
fsck.ext3 -f /dev/mapper/${LOOP_DEVICE} || true
if [ "${AUTOMATIC_RESIZE}" = "yes" ] ; then
resize2fs -M /dev/mapper/${LOOP_DEVICE}
FS_BLOCKS=`tune2fs -l /dev/mapper/${LOOP_DEVICE} | awk '/Block count/{print $3}'`
WANTED_SIZE=`expr $FS_BLOCKS '*' 4 '/' 1024 + ${AUTOMATIC_RESIZE_SPACE}` # Add ${AUTOMATIC_RESIZE_SPACE}M
resize2fs /dev/mapper/${LOOP_DEVICE} ${WANTED_SIZE}M
FINAL_FS_BLOCKS=`tune2fs -l /dev/mapper/${LOOP_DEVICE} | awk '/Block count/{print $3}'`
FINAL_IMG_SIZE=`expr '(' $FINAL_FS_BLOCKS + 258 ')' '*' 4 '/' 1024` # some blocks for mbr and multiple block size (4k)
fi
kpartx -d ${AMI_NAME}
rmdir ${MOUNT_DIR}
if [ "${AUTOMATIC_RESIZE}" = "yes" ] ; then
# Rebuild a smaller partition table
parted -s ${AMI_NAME} rm 1
parted -s ${AMI_NAME} mkpart primary ext3 1Mi ${FINAL_IMG_SIZE}Mi
parted -s ${AMI_NAME} set 1 boot on
# Add 2M for the 1M at the beginning of the partition and some additionnal space
truncate -s `expr 3 + ${FINAL_IMG_SIZE}`M ${AMI_NAME}
install-mbr ${AMI_NAME}
fi
#qemu-img convert -c -f raw ${AMI_NAME} -O qcow2 ${QCOW2_NAME}