forked from osuosl/ganeti-instance-image
-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.sh.in
538 lines (496 loc) · 15.8 KB
/
common.sh.in
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
#
# Copyright (C) 2007, 2008, 2009 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
AWK="@AWK@"
DUMP="@DUMP@"
LOSETUP="@LOSETUP@"
KPARTX="@KPARTX@"
SFDISK="@SFDISK@"
PARTED="@PARTED@"
QEMU_IMG="@QEMU_IMG@"
MKDIR_P="@MKDIR_P@"
INSTANCE_MEM=
CURL="@CURL@"
SHA1SUM="@SHA1SUM@"
CLEANUP=( )
log_error() {
echo "$@" >&2
}
debug() {
[ "$IMAGE_DEBUG" == "1" -o "$IMAGE_DEBUG" == "yes" ] && $@ || :
}
get_api5_arguments() {
GETOPT_RESULT=$*
# Note the quotes around `$TEMP': they are essential!
eval set -- "$GETOPT_RESULT"
while true; do
case "$1" in
-i|-n) instance=$2; shift 2;;
-o) old_name=$2; shift 2;;
-b) blockdev=$2; shift 2;;
-s) swapdev=$2; shift 2;;
--) shift; break;;
*) log_error "Internal error!" >&2; exit 1;;
esac
done
if [ -z "$instance" -o -z "$blockdev" ]; then
log_error "Missing OS API Argument (-i, -n, or -b)"
exit 1
fi
if [ "$SCRIPT_NAME" != "export" -a -z "$swapdev" ]; then
log_error "Missing OS API Argument -s (swapdev)"
exit 1
fi
if [ "$SCRIPT_NAME" = "rename" -a -z "$old_name" ]; then
log_error "Missing OS API Argument -o (old_name)"
exit 1
fi
}
get_api10_arguments() {
if [ -z "$INSTANCE_NAME" -o -z "$HYPERVISOR" -o -z "$DISK_COUNT" ]; then
log_error "Missing OS API Variable:"
log_error "(INSTANCE_NAME HYPERVISOR or DISK_COUNT)"
exit 1
fi
instance=$INSTANCE_NAME
if [ $DISK_COUNT -lt 1 -o -z "$DISK_0_PATH" ]; then
log_error "At least one disk is needed"
exit 1
fi
if [ "$SCRIPT_NAME" = "export" ]; then
if [ -z "$EXPORT_DEVICE" ]; then
log_error "Missing OS API Variable EXPORT_DEVICE"
fi
blockdev=$EXPORT_DEVICE
elif [ "$SCRIPT_NAME" = "import" ]; then
if [ -z "$IMPORT_DEVICE" ]; then
log_error "Missing OS API Variable IMPORT_DEVICE"
fi
blockdev=$IMPORT_DEVICE
else
blockdev=$DISK_0_PATH
fi
if [ "$SCRIPT_NAME" = "rename" -a -z "$OLD_INSTANCE_NAME" ]; then
log_error "Missing OS API Variable OLD_INSTANCE_NAME"
fi
old_name=$OLD_INSTANCE_NAME
}
get_os_type() {
target=$1
if [ -z "${target}" ] ; then
log_error "target is not set in get_os_type"
exit 1
fi
if [ -e ${target}/etc/redhat-release ] ; then
OS_TYPE="redhat"
elif [ -e ${target}/etc/debian_version ] ; then
OS_TYPE="debian"
elif [ -e ${target}/etc/gentoo-release ] ; then
OS_TYPE="gentoo"
elif [ -e ${target}/etc/SuSE-release ] ; then
OS_TYPE="suse"
fi
}
get_os() {
target=$1
if [ -z "${target}" ] ; then
log_error "target is not set in get_os"
exit 1
fi
lsb="/usr/bin/lsb_release"
if [ -e ${target}/$lsb ] ; then
OPERATING_SYSTEM="$(chroot ${target} ${lsb} -i -s | tr "[:upper:]" "[:lower:]")"
elif [ -e ${target}/etc/debian_version ] ; then
OPERATING_SYSTEM="debian"
elif [ -e ${target}/etc/gentoo-release ] ; then
OPERATING_SYSTEM="gentoo"
elif [ -e ${target}/etc/fedora-release ] ; then
OPERATING_SYSTEM="fedora"
elif [ -e ${target}/etc/redhat-release ] ; then
if [ -n "$(grep -i centos ${target}/etc/redhat-release)" ] ; then
OPERATING_SYSTEM="centos"
else
OPERATING_SYSTEM="redhat"
fi
fi
}
get_os_release() {
target=$1
if [ -z "${target}" ] ; then
log_error "target is not set in get_os_release"
exit 1
fi
lsb="/usr/bin/lsb_release"
if [ -e ${target}/lsb ] ; then
OS_RELEASE="$(chroot ${target} ${lsb} -r -s | tr "[:upper:]" "[:lower:]")"
elif [ -e ${target}/etc/debian_version ] ; then
OS_RELEASE="$(cat ${target}/etc/debian_version)"
elif [ -e ${target}/etc/fedora-release ] ; then
OS_RELEASE="$(cat ${target}/etc/fedora-release | awk '{print $3}')"
elif [ -e ${$target}/etc/redhat-release ] ; then
OS_RELEASE="$(cat ${target}/etc/redhat-release | awk '{print $3}')"
fi
}
format_disk0() {
if [ "${FDISK}" = "parted" ]; then
format_disk0_parted "$1"
elif [ "${FDISK}" = "sfdisk" ]; then
format_disk0_sfdisk "$1"
fi
}
format_disk0_sfdisk() {
local sfdisk_cmd="$SFDISK -uM -H 255 -S 63 --quiet --Linux --DOS $1"
if [ "${SWAP}" = "yes" -a -z "${KERNEL_PATH}" ] ; then
# Create three partitions:
# 1 - $BOOT_SIZE /boot, bootable
# 2 - Size of Memory, swap
# 3 - Rest
$sfdisk_cmd > /dev/null <<EOF
,${BOOT_SIZE},L,*
,${SWAP_SIZE},S
,,L
EOF
elif [ "${SWAP}" = "no" -a -z "${KERNEL_PATH}" ] ; then
# Create two partitions:
# 1 - $BOOT_SIZE /boot, bootable
# 2 - Rest
$sfdisk_cmd > /dev/null <<EOF
,${BOOT_SIZE},L,*
,,L
EOF
elif [ "${SWAP}" = "yes" -a -n "${KERNEL_PATH}" ] ; then
# Create two partitions:
# 1 - Size of Memory, swap
# 2 - Rest
$sfdisk_cmd > /dev/null <<EOF
,$SWAP_SIZE,S
,,L,*
EOF
elif [ "${SWAP}" = "no" -a -n "${KERNEL_PATH}" ] ; then
# Create two partitions:
# 1 - Whole
$sfdisk_cmd > /dev/null <<EOF
,,L,*
EOF
fi
}
format_disk0_parted() {
local parted_cmd="${PARTED} $1 --script --"
if [ "${SWAP}" = "yes" -a -z "${KERNEL_PATH}" ] ; then
# Create three partitions:
# 1 - $BOOT_SIZE /boot, bootable
# 2 - Size of Memory, swap
# 3 - Rest
$parted_cmd mklabel msdos
$parted_cmd mkpart primary 0% ${BOOT_SIZE}M
$parted_cmd mkpart primary ${BOOT_SIZE}M $((${BOOT_SIZE}+$SWAP_SIZE))M
$parted_cmd mkpart primary $((${BOOT_SIZE}+$SWAP_SIZE))M 100%
$parted_cmd set 1 boot on
elif [ "${SWAP}" = "no" -a -z "${KERNEL_PATH}" ] ; then
# Create two partitions:
# 1 - $BOOT_SIZE /boot, bootable
# 2 - Rest
$parted_cmd mklabel msdos
$parted_cmd mkpart primary 0% ${BOOT_SIZE}M
$parted_cmd mkpart primary ${BOOT_SIZE}M 100%
$parted_cmd set 1 boot on
elif [ "${SWAP}" = "yes" -a -n "${KERNEL_PATH}" ] ; then
# Create two partitions:
# 1 - Size of Memory, swap
# 2 - Rest
$parted_cmd mklabel msdos
$parted_cmd mkpart primary 0% ${SWAP_SIZE}M
$parted_cmd mkpart primary ${SWAP_SIZE}M 100%
elif [ "${SWAP}" = "no" -a -n "${KERNEL_PATH}" ] ; then
# Create two partitions:
# 1 - Whole
$parted_cmd mklabel msdos
$parted_cmd mkpart primary 0% 100%
fi
}
mkfs_disk0() {
local mkfs="mkfs.${FILESYSTEM}"
local mkfs_opts=
if [ "${FILESYSTEM}" = "xfs" ] ; then
mkfs_opts="-fq"
else
mkfs_opts="-Fq"
fi
# Format /
$mkfs $mkfs_opts -L / $root_dev > /dev/null
# Format /boot
if [ -n "${boot_dev}" ] ; then
$mkfs $mkfs_opts -L /boot $boot_dev > /dev/null
fi
# Format swap
if [ -n "${swap_dev}" ] ; then
# Format swap
mkswap -f $swap_dev > /dev/null
fi
# During reinstalls, ext4 needs a little time after a mkfs so add it here
# and also run a sync to be sure.
sync
sleep 2
}
mount_disk0() {
local target=$1
mount $root_dev $target
CLEANUP+=("umount $target")
if [ -n "${boot_dev}" ] ; then
$MKDIR_P $target/boot
mount $boot_dev $target/boot
CLEANUP+=("umount $target/boot")
fi
# sync the file systems before unmounting to ensure everything is flushed
# out
CLEANUP+=("sync")
}
map_disk0() {
blockdev="$1"
filesystem_dev_base=`$KPARTX -l -p- $blockdev | \
grep -m 1 -- "-1.*$blockdev" | \
$AWK '{print $1}'`
if [ -z "$filesystem_dev_base" ]; then
log_error "Cannot interpret kpartx output and get partition mapping"
exit 1
fi
$KPARTX -a -p- $blockdev > /dev/null
filesystem_dev="/dev/mapper/${filesystem_dev_base/%-1/}"
if [ ! -b "/dev/mapper/$filesystem_dev_base" ]; then
log_error "Can't find kpartx mapped partition: /dev/mapper/$filesystem_dev_base"
exit 1
fi
echo "$filesystem_dev"
}
map_partition() {
local filesystem_dev="$1"
local partition="$2"
local dev
for dev in "${filesystem_dev}-"*; do
[ ! -b "${dev}" ] || continue
# see https://uapi-group.org/specifications/specs/discoverable_partitions_specification/
case $(blkid -p -s PART_ENTRY_TYPE -o value "${dev}" 2>/dev/null) in
21686148-6449-6e6f-744e-656564454649)
# BIOS boot partition
boot_dev="${dev}" ;;
0fc63daf-8483-4772-8e79-3d69d8477de4)
# first generic data partition
generic_dev="${generic_dev:-${dev}}" ;;
0657fd6d-a4ab-43c4-84e5-0933c84b4f4f)
# swap
swap_dev="${dev}" ;;
69dad710-2ce4-4e3c-b16c-21a1d49abed3)
# arm32
root_dev="${dev}" ;;
b921b045-1df0-41c3-af44-4c6f280d3fae)
# arm64/aarch64
root_dev="${dev}" ;;
44479540-f297-41b2-9af7-d131d5f0458a)
# x86/i386
root_dev="${dev}" ;;
4f68bce3-e8cd-4db1-96e7-fbcaf984b709)
# amd64/x86_64
root_dev="${dev}" ;;
esac
done
root_dev="${root_dev:-${generic_dev}}"
if [ -z "${root_dev}" ]; then
if [ "${SWAP}" = "yes" -a -z "${KERNEL_PATH}" ] ; then
boot_dev="${filesystem_dev}-1"
swap_dev="${filesystem_dev}-2"
root_dev="${filesystem_dev}-3"
elif [ "${SWAP}" = "no" -a -z "${KERNEL_PATH}" ] ; then
boot_dev="${filesystem_dev}-1"
root_dev="${filesystem_dev}-2"
elif [ "${SWAP}" = "yes" -a -n "${KERNEL_PATH}" ] ; then
swap_dev="${filesystem_dev}-1"
root_dev="${filesystem_dev}-2"
elif [ "${SWAP}" = "no" -a -n "${KERNEL_PATH}" ] ; then
root_dev="${filesystem_dev}-1"
fi
fi
echo "$(eval "echo \${$(echo ${partition}_dev)"})"
}
unmap_disk0() {
$KPARTX -d -p- $1
}
setup_fstab() {
local target=$1 fs=${FILESYSTEM}
get_os_type $target
cat > $target/etc/fstab <<EOF
# /etc/fstab: static file system information.
#
# <file system> <mount point> <type> <options> <dump> <pass>
UUID=$root_uuid / $fs defaults 0 1
proc /proc proc defaults 0 0
EOF
if [ -n "$boot_dev" -a -n "$boot_uuid" ] ; then
cat >> $target/etc/fstab <<EOF
UUID=$boot_uuid /boot $fs defaults 1 2
EOF
fi
if [ -n "$swap_dev" -a -n "$swap_uuid" ] ; then
cat >> $target/etc/fstab <<EOF
UUID=$swap_uuid swap swap defaults 0 0
EOF
fi
# OS Specific fstabs
if [ "$OS_TYPE" = "redhat" ] ; then
cat >> $target/etc/fstab <<EOF
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
EOF
fi
if [ "$OS_TYPE" = "gentoo" ] ; then
cat >> $target/etc/fstab <<EOF
shm /dev/shm tmpfs nodev,nosuid,noexec 0 0
EOF
fi
}
setup_console() {
local target=$1
if [ -z "$target" ] ; then
log_error "target not set for setup_console"
exit 1
fi
# Upstart is on this system, so do this instead
if [ -e ${target}/etc/event.d/tty1 ] ; then
cat ${target}/etc/event.d/tty1 | sed -re 's/tty1/ttyS0/' \
> ${target}/etc/event.d/ttyS0
return
fi
# upstart in karmic and newer
if [ -e ${target}/etc/init/tty1.conf ] ; then
cat ${target}/etc/init/tty1.conf | \
sed -re 's/^exec.*/exec \/sbin\/getty -L 115200 ttyS0 vt102/' \
> ${target}/etc/init/ttyS0.conf
sed -ie 's/tty1/ttyS0/g' ${target}/etc/init/ttyS0.conf
return
fi
get_os $target
case $OPERATING_SYSTEM in
gentoo)
sed -i -e 's/.*ttyS0.*/s0:12345:respawn:\/sbin\/agetty 115200 ttyS0 vt100/' \
${target}/etc/inittab
;;
centos|redhat)
echo "s0:12345:respawn:/sbin/agetty 115200 ttyS0 vt100" >> \
${target}/etc/inittab
;;
debian|ubuntu)
sed -i -e 's/.*T0.*/T0:23:respawn:\/sbin\/getty -L ttyS0 115200 vt100/' \
${target}/etc/inittab
;;
*)
echo "No support for your OS in instance-image, skipping..."
;;
esac
}
cleanup() {
if [ ${#CLEANUP[*]} -gt 0 ]; then
LAST_ELEMENT=$((${#CLEANUP[*]}-1))
REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
for i in $REVERSE_INDEXES; do
${CLEANUP[$i]}
done
fi
}
trap cleanup EXIT
DEFAULT_FILE="@defaultdir@/ganeti-instance-image"
if [ -f "$DEFAULT_FILE" ]; then
. "$DEFAULT_FILE"
fi
# Variable name change from 2.5 to 2.6
if [ -n "$INSTANCE_BE_memory" ] ; then
INSTANCE_MEM="$INSTANCE_BE_memory"
else
INSTANCE_MEM="$INSTANCE_BE_maxmem"
fi
: ${CDINSTALL:="no"}
: ${SWAP:="yes"}
: ${SWAP_SIZE:="${INSTANCE_MEM}"}
: ${BOOT_SIZE:="100"}
: ${FILESYSTEM:="ext3"}
: ${KERNEL_ARGS=""}
: ${OVERLAY=""}
: ${IMAGE_NAME:=""}
: ${IMAGE_TYPE:="dump"}
: ${NOMOUNT:="no"}
: ${ARCH:=""}
: ${CUSTOMIZE_DIR:="@sysconfdir@/ganeti/instance-image/hooks"}
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/instance-image/variants"}
: ${NETWORKS_DIR:="@sysconfdir@/ganeti/instance-image/networks"}
: ${OVERLAYS_DIR:="@sysconfdir@/ganeti/instance-image/overlays"}
: ${EXPORT_DIR:="/tmp"}
: ${IMAGE_DIR:="@localstatedir@/cache/ganeti-instance-image"}
: ${IMAGE_DEBUG:="no"}
: ${FDISK:="sfdisk"}
: ${CACHE_DIR:="@localstatedir@/cache/ganeti-instance-image"}
: ${IMAGE_URL:=""}
: ${IMAGE_VERIFY:="yes"}
: ${IMAGE_CLEANUP:="no"}
SCRIPT_NAME=$(basename $0)
KERNEL_PATH="$INSTANCE_HV_kernel_path"
if [ -f /sbin/blkid -a -x /sbin/blkid ]; then
VOL_ID="/sbin/blkid -c /dev/null -o value -s UUID"
VOL_TYPE="/sbin/blkid -c /dev/null -o value -s TYPE"
else
for dir in /lib/udev /sbin; do
if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
VOL_ID="$dir/vol_id -u"
VOL_TYPE="$dir/vol_id -t"
fi
done
fi
if [ -z "$VOL_ID" ]; then
log_error "vol_id or blkid not found, please install udev or util-linux"
exit 1
fi
if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
OS_API_VERSION=5
GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
get_api5_arguments $GETOPT_RESULT
elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then
get_api10_arguments
else
log_error "Unknown OS API VERSION $OS_API_VERSION"
exit 1
fi
if [ -n "$OS_VARIANT" ]; then
if [ ! -d "$VARIANTS_DIR" ]; then
log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
exit 1
fi
VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
if [ -f "$VARIANT_CONFIG" ]; then
. "$VARIANT_CONFIG"
else
if grep -qxF "$OS_VARIANT" variants.list; then
log_error "ERROR: instance-image configuration error"
log_error " Published variant $OS_VARIANT is missing its config file"
log_error " Please create $VARIANT_CONFIG or unpublish the variant"
log_error " (by removing $OS_VARIANT from variants.list)"
else
log_error "Unofficial variant $OS_VARIANT is unsupported"
log_error "Most probably this is a user error, forcing a wrong name"
log_error "To support this variant please create file $VARIANT_CONFIG"
fi
exit 1
fi
fi