forked from grnet/ganeti-instance-image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.sh.in
992 lines (911 loc) · 31.6 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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
#
# TODO: improve this more, particularly in using ganeti-provided env-vars:
# refer to:
# http://docs.ganeti.org/ganeti/2.9/html/man-ganeti-os-interface.html
# Copyright (C) 2007, 2008, 2009 Google Inc.
# Copyright (C) 2010, 2011, 2012 Oregon State University
# Copyright (C) 2013, 2014, 2015 GRNET S.A.
#
# 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.
# TODO: quote calls to these, add wget/kpartx/etc args in wrapper functions
AWK="@AWK@"
DUMP="@DUMP@"
LOSETUP="@LOSETUP@"
KPARTX="@KPARTX@"
SFDISK="@SFDISK@"
PARTED="@PARTED@"
QEMU_IMG="@QEMU_IMG@"
MKDIR_P="@MKDIR_P@"
INSTANCE_MEM=''
WGET="@WGET@ --connect-timeout=20 --tries=3"
SHA1SUM="@SHA1SUM@"
BLOCKDEV_CMD="@BLOCKDEV_CMD@"
BLKID="@BLKID@"
CLEANUP=':'
CLEANUP_INITIALISED=0
usage() { :; } # override this
debug() { case "$IMAGE_DEBUG" in 1|yes) "$@" || : ;; esac; }
printout() { printf "$@" | sed -e "s/^/${SCRIPT_NAME}: /"; }
printerr() { printout "$@" >&2; }
verbprint() { test -z "$VERBOSE" || test 0 -eq $VERBOSE || printerr "$@"; }
die() { printerr "$@"; exit 1; }
croak() { usage >&2; die "$@"; }
cleanup() {
_exitval=$?
if test $# -eq 0; then
_cleanup_action='do'
else
_cleanup_action="$1"
shift
fi
case "$_cleanup_action" in
init)
test 1 -eq $CLEANUP_INITIALISED || { CLEANUP=':'; CLEANUP_INITIALISED=1; }
;;
uninit)
test 0 -eq $CLEANUP_INITIALISED || { CLEANUP=':'; CLEANUP_INITIALISED=0; }
;;
*)
if test 1 = "$CLEANUP_INITIALISED"; then
case "$_cleanup_action" in
show)
printf '%s\n' "$CLEANUP"
;;
pop)
CLEANUP="$(printf '%s' "$CLEANUP" | sed -e '1d')"
;;
push)
CLEANUP="{ $1; } && \\
$CLEANUP"
;;
shift)
CLEANUP="$(printf '%s' "$CLEANUP" | sed -n -e '${p;q};1h;1!{x;p;}')"
;;
unshift)
CLEANUP="$(printf '%s' "$CLEANUP" | sed -e '$d')
{ $1; } && \\
:"
;;
do)
test 0 -eq $_exitval || printerr 'CLEANUP MARKER\n'
eval "$CLEANUP"
cleanup uninit
;;
esac
fi
;;
esac
case "$_cleanup_action" in
init|pop|push|shift|unshift)
trap 'cleanup || :' EXIT || :
;;
esac
:
}
mkdir_dumpdir_temp() {
DUMPDIR="`mktemp -d --tmpdir="${EXPORT_DIR:-${TMPDIR:-/tmp}}"`"
cleanup push "rmdir \"$DUMPDIR\""
}
rmdir_disk0_temp() {
test -n "$TARGET" || die 'TARGET is not set in rmdir_disk0_temp\n'
rmdir "$TARGET"
TARGET="$OLD_TARGET"
OLD_TARGET=''
export TARGET
}
mkdir_disk0_temp() {
OLD_TARGET="$TARGET"
TARGET="`mktemp -d --tmpdir="${EXPORT_DIR:-${TMPDIR:-/tmp}}"`"
export TARGET
cleanup push 'rmdir_disk0_temp'
}
unmount_disk0() {
test -n "$TARGET" || die 'TARGET is not set in unmount_disk0\n'
sync
test -z "$boot_dev" || umount "${TARGET}/boot"
# TODO: Unmounting /sys should *not* be needed. Find out what auto-mounts this (systemctl?)...
umount "${TARGET}/sys" 2>/dev/null || :
umount "$TARGET"
}
mount_disk0() { # optional: 'readonly'
test -n "$TARGET" || die 'TARGET is not set in mount_disk0\n'
mount `! test 'readonly' = "$1" || printf -- '-o ro'` "$root_dev" "$TARGET"
if test -n "$boot_dev"; then
$MKDIR_P "${TARGET}/boot"
mount `! test 'readonly' = "$1" || printf -- '-o ro'` "$boot_dev" "${TARGET}/boot"
fi
cleanup push "unmount_disk0"
}
losetup_find() {
OLD_BLOCKDEV=''
if ! test -b "$BLOCKDEV"; then
_new_blockdev="$("$LOSETUP" -j "$BLOCKDEV" | cut -d: -f1 || :)"
test -n "$_new_blockdev" && test -b "$_new_blockdev" || \
die 'Can'\''t find an associated loop device for "%s"\n' "$BLOCKDEV"
OLD_BLOCKDEV="$BLOCKDEV"
BLOCKDEV="$_new_blockdev"
export OLD_BLOCKDEV
export BLOCKDEV
fi
}
unlosetup_blockdev() {
if test -n "$OLD_BLOCKDEV"; then
"$LOSETUP" -d "$BLOCKDEV"
BLOCKDEV="$OLD_BLOCKDEV"
OLD_BLOCKDEV=''
export BLOCKDEV
export OLD_BLOCKDEV
fi
}
losetup_blockdev() {
OLD_BLOCKDEV=''
if ! test -b "$BLOCKDEV"; then
_new_blockdev="$("$LOSETUP" --show -f "$BLOCKDEV")"
test -n "$_new_blockdev" && test -b "$_new_blockdev" || \
die 'Can'\''t create a loop device for "%s"\n' "$BLOCKDEV"
OLD_BLOCKDEV="$BLOCKDEV"
BLOCKDEV="$_new_blockdev"
export OLD_BLOCKDEV
export BLOCKDEV
fi
cleanup push 'unlosetup_blockdev'
}
unmap_disk0() { sync; $KPARTX -d -p- "$BLOCKDEV"; }
map_disk0_nocreate() {
sync
_filesystem_dev1_base="`
$KPARTX -l -p- "$BLOCKDEV" | \
grep -m 1 -- "-1.*$BLOCKDEV" | \
"$AWK" '{print $1}'
`"
test -n "$_filesystem_dev1_base" || die 'Cannot interpret kpartx output and get partition mapping\n'
filesystem_dev="/dev/mapper/${_filesystem_dev1_base%-1}"
}
check_disk0() {
test -b "${filesystem_dev}-1" || \
die 'Can'\''t find kpartx mapped partition: %s\n' "${filesystem_dev}-1"
}
map_disk0() {
map_disk0_nocreate
$KPARTX -a -p- "$BLOCKDEV" >/dev/null
cleanup push 'unmap_disk0'
check_disk0
}
update_map_disk0() { sync; $KPARTX -u -p- "$BLOCKDEV"; }
check_swap_size() {
if test 'yes' = "$SWAP"; then
test -n "$SWAP_SIZE" || die 'SWAP_SIZE not set however SWAP is enabled\n'
test $SWAP_SIZE -le $DISK_SIZE || \
die 'SWAP_SIZE larger than system disk size:\n %sMB swap > %sMB system disk\n' "$SWAP_SIZE" "$DISK_SIZE"
fi
}
map_partition() {
_partition="$1"
_root_dev=''; _boot_dev=''; _swap_dev=''
if test 'no' = "$SWAP"; then
if test -n "$KERNEL_PATH"; then
_root_dev="${filesystem_dev}-1"
else
_boot_dev="${filesystem_dev}-1"
_root_dev="${filesystem_dev}-2"
fi
else
if test -n "$KERNEL_PATH"; then
_swap_dev="${filesystem_dev}-1"
_root_dev="${filesystem_dev}-2"
else
_boot_dev="${filesystem_dev}-1"
_swap_dev="${filesystem_dev}-2"
_root_dev="${filesystem_dev}-3"
fi
fi
eval "${_partition}_dev=\"\$_${_partition}_dev\""
}
unmap_partitions() {
for _partition in 'root' 'boot' 'swap'; do
eval "${_partition}_dev=''"
done
}
map_partitions() {
for _partition in 'root' 'boot' 'swap'; do
map_partition "$_partition"
done
cleanup push 'unmap_partitions'
}
unmap_all() {
unmap_partitions
unmap_disk0
}
map_all() {
map_disk0
map_partitions
cleanup push 'unmap_all'
}
get_uuids() {
root_uuid="$(eval "$VOL_ID \"${root_dev:-$ROOT_DEV}\"")"
test -z "${boot_dev:-$BOOT_DEV}" || boot_uuid="$(eval "$VOL_ID \"${boot_dev:-$BOOT_DEV}\"")"
test -z "${swap_dev:-$SWAP_DEV}" || swap_uuid="$(eval "$VOL_ID \"${swap_dev:-$SWAP_DEV}\"")"
}
disk_size() {
case "$1" in
get)
"$BLOCKDEV_CMD" --getsz "$BLOCKDEV"
;;
set) #TODO - qemu-img resize (numeric)?
:
;;
expand) #TODO - qemu-img resize (+)?
:
;;
shrink) #TODO - qemu-img resize (-)? ...with sanity checking
:
;;
esac
}
get_disk0_size_mbytes() { DISK_SIZE="`expr \`disk_size get\` / 2048 || :`"; }
part_table() {
_part_table_action="$1"
shift
# sfdisk -d seems to always output in 512-byte sectors regardless of -uX
# (using -uS here anyway in case another sfdisk has a different default)
case "$_part_table_action" in
set)
"$SFDISK" -L -uS "$BLOCKDEV" 2>/dev/null
;;
get)
"$SFDISK" -L -uS -d "$BLOCKDEV" 2>/dev/null
;;
esac
}
get_partnum() {
if test 'yes' = "$SWAP" && test -z "$KERNEL_PATH"; then
printf 3
elif { test 'no' = "$SWAP" && test -z "$KERNEL_PATH"; } || { test 'yes' = "$SWAP" && test -n "$KERNEL_PATH"; }; then
printf 2
elif test 'no' = "$SWAP" && test -n "$KERNEL_PATH"; then
printf 1
fi
}
get_part_sizes() {
_partnum="${1:-`get_partnum`}"
part_table get | \
sed -n -e '1,/^$/ b; p' | \
sed -n -e "$(printf '%d q; ' `expr $_partnum + 1 || :`)"'s/^.*size=[ \t]*\([0-9]\+\),.*$/\1/; p' | \
tr '\n' ' '
}
part_size() {
_part_size_action="$1"
_partnum="${2:-`get_partnum`}"
case "$_part_size_action" in
set)
_size="$3"
;;
expand)
_size=''
;;
shrink)
#TODO:
# - capture FS size, convert from sectors (512) to blocks (4k, or..),
# remember to round-up if/when necessary
_size=''
;;
esac
part_table get | \
sed -n -e '1,/^$/ b; p' | \
case "$_part_size_action" in
get)
sed -n -e "$_partnum"'! b; s/^.*size= *\([0-9]\+\),.*$/\1/; p; q'
;;
set|expand|shrink)
sed -e "$_partnum"'! b; s/size= *[0-9]\+,/size=,'"$_size"'/' | \
part_table set
sync
;;
check)
_devicesize="`disk_size get`"
_usedsize=0
for _partsize in `get_part_sizes`; do
_usedsize=`expr $_usedsize + $_partsize || :`
done
if test $_devicesize -lt $_usedsize; then
die 'Device size (%d sectors) is less than size used by partitions (%d sectors).\n' \
"$_devicesize" "$_usedsize"
fi
;;
esac
}
get_fs() { "$BLKID" -p -u 'filesystem' -s 'TYPE' -o 'value' "$1"; }
fs_size() {
_fs_size_action="$1"
_partnum="${2:-`get_partnum`}"
if test 'set' = "$_fs_size_action"; then
_size="$3"
else
_size=''
fi
_unmap_needed=0
if test -z "$filesystem_dev"; then
_unmap_needed=1
map_disk0
cleanup push "unmap_disk0"
fi
_partdev="${filesystem_dev}-$_partnum"
_fstype="`get_fs "$_partdev"`"
case "$_fs_size_action" in
get) #TODO
:
;;
set|expand|shrink)
#TODO: btrfs, xfs, zfs, ntfs, etc...
case "$_fstype" in
ext2|ext3|ext4)
e2fsck -n -f "$_partdev"
resize2fs -f `! test 'shrink' = "$_fs_size_action" || printf -- -M` "$_partdev" $_size
e2fsck -n -f "$_partdev"
;;
esac
;;
esac
test 0 -eq $_unmap_needed || { unmap_disk0; cleanup pop; }
}
get_os_type() {
test -n "$TARGET" || die 'TARGET is not set in get_os_type\n'
if test -e "${TARGET}/etc/redhat-release"; then
OS_TYPE='redhat'
elif test -e "${TARGET}/etc/debian_version"; then
OS_TYPE='debian'
elif test -e "${TARGET}/etc/gentoo-release"; then
OS_TYPE='gentoo'
elif test -e "${TARGET}/etc/SuSE-release"; then
OS_TYPE='suse'
fi
}
get_os() {
test -n "$TARGET" || die 'TARGET is not set in get_os\n'
_lsb='/usr/bin/lsb_release'
if test -e "${TARGET}$_lsb"; then
OPERATING_SYSTEM="$(chroot "$TARGET" "$_lsb" -i -s | tr '[:upper:]' '[:lower:]')"
elif test -e "${TARGET}/etc/debian_version"; then
OPERATING_SYSTEM='debian'
elif test -e "${TARGET}/etc/gentoo-release"; then
OPERATING_SYSTEM='gentoo'
elif test -e "${TARGET}/etc/fedora-release"; then
OPERATING_SYSTEM='fedora'
elif test -e "${TARGET}/etc/redhat-release"; then
if test -n "$(grep -i 'centos' "${TARGET}/etc/redhat-release")"; then
OPERATING_SYSTEM='centos'
else
OPERATING_SYSTEM='redhat'
fi
fi
}
get_os_release() {
test -n "$TARGET" || die 'TARGET is not set in get_os_release\n'
_lsb='/usr/bin/lsb_release'
if test -e "${TARGET}$_lsb"; then
OS_RELEASE="$(chroot "$TARGET" "$_lsb" -r -s | tr '[:upper:]' '[:lower:]')"
elif test -e "${TARGET}/etc/debian_version"; then
OS_RELEASE="$(cat "${TARGET}/etc/debian_version")"
elif test -e "${TARGET}/etc/fedora-release"; then
OS_RELEASE="$(cat "${TARGET}/etc/fedora-release" | awk '{print $3}')"
elif test -e "${TARGET}/etc/redhat-release"; then
OS_RELEASE="$(cat "${TARGET}/etc/redhat-release" | awk '{print $3}')"
fi
}
format_disk0() {
case "$FDISK" in
parted)
format_disk0_parted "$BLOCKDEV"
;;
sfdisk)
format_disk0_sfdisk "$BLOCKDEV"
;;
esac
}
format_disk0_sfdisk() {
_sfdisk_cmd="\"$SFDISK\" -uM -H 255 -S 63 --quiet --Linux --DOS \"$1\""
if test 'yes' = "$SWAP" && test -z "$KERNEL_PATH"; then
# Create three partitions:
# 1 - $BOOT_SIZE /boot, bootable
# 2 - Size of Memory, swap
# 3 - Rest
eval "$_sfdisk_cmd" >/dev/null <<EOF
,${BOOT_SIZE},L,*
,${SWAP_SIZE},S
,,L
EOF
elif test 'no' = "$SWAP" && test -z "$KERNEL_PATH"; then
# Create two partitions:
# 1 - $BOOT_SIZE /boot, bootable
# 2 - Rest
eval "$_sfdisk_cmd" >/dev/null <<EOF
,${BOOT_SIZE},L,*
,,L
EOF
elif test 'yes' = "$SWAP" && test -n "$KERNEL_PATH"; then
# Create two partitions:
# 1 - Size of Memory, swap
# 2 - Rest
eval "$_sfdisk_cmd" >/dev/null <<EOF
,${SWAP_SIZE},S
,,L,*
EOF
elif test 'no' = "$SWAP" && test -n "$KERNEL_PATH"; then
# Create one partition:
# 1 - Whole
eval "$_sfdisk_cmd" >/dev/null <<EOF
,,L,*
EOF
fi
}
_parted_cmd() {
_firstarg="$1"
shift
"$PARTED" "$_firstarg" --script -- "$@"
}
format_disk0_parted() {
_disk0="$1"
_part1=''
_part2=''
_part3=''
if test -n "$KERNEL_PATH"; then
if test 'no' = "$SWAP"; then
# 1 - [all]
_part1="0% 100%"
else
# 1 - swap (size of memory)
# 2 - [the rest]
_part1="0% ${SWAP_SIZE}M"
_part2="${SWAP_SIZE}M 100%"
fi
else
if test 'no' = "$SWAP"; then
# 1 - /boot, bootable ($BOOT_SIZE)
# 2 - [the rest]
_part1="0% ${BOOT_SIZE}M"
_part2="${BOOT_SIZE}M 100%"
else
# 1 - /boot, bootable ($BOOT_SIZE)
# 2 - swap (size of memory)
# 3 - [the rest]
_boot_and_swap=`expr $BOOT_SIZE + $SWAP_SIZE || :`
_part1="0% ${BOOT_SIZE}M"
_part2="${BOOT_SWAP}M ${_boot_and_swap}M"
_part3="${_boot_and_swap}M 100%"
fi
fi
_parted_cmd "$_disk0" 'mklabel' 'msdos'
for _part in _part1 _part2 _part3; do
eval "
if test -n \"\$$_part\"; then
_parted_cmd \"\$_disk0\" 'mkpart' 'primary' \$$_part
else
break
fi
"
done
test -n "$KERNEL_PATH" || _parted_cmd "$_disk0" 'set' 1 'boot' 'on'
}
qcow_create() { "$QEMU_IMG" convert -S 4k -O 'qcow2' -c "$BLOCKDEV" "$1" >/dev/null; }
qcow_extractfile() {
case "$2" in
raw)
_dest="$2"
;;
*)
_dest='host_device'
;;
esac
"$QEMU_IMG" convert -S 4k -O "$_dest" "$1" "$BLOCKDEV" >/dev/null
}
qcow_extract() { qcow_extractfile "$IMAGE_FILE" "$@"; }
dump_create() {
_prefix="${1%root.dump}"
for _devname in 'boot' 'root'; do
eval "_dev=\"\${${_devname}_dev:-`printf '%s' "$_devname" | tr '[a-z]' '[A-Z]'`_DEV}\""
if test -z "$_dev"; then
test 'root' = "$_devname" || continue
die '%s_dev device doesn'\''t exist\n' "$_devname"
fi
_fs="`get_fs "$_dev"`"
case "$_fs" in
ext2|ext3|ext4)
"$DUMP" -0 -q -z9 -f "${_prefix}${_devname}.dump" "$_dev" >/dev/null
;;
'')
die 'Unable to find filesystem type at "%s"\n' "$_dev"
;;
*)
die '%s is not a supported filesystem. Please use ext{2,3,4}\n' "$_fs"
;;
esac
done
}
dump_extractfile() {
test -n "$TARGET" || die 'TARGET is not set in dump_extractfile\n'
_prefix="${1%root.dump}"
rm -rf "${TMPDIR:-/tmp}/rst"* # clean up any previous restore runs
test -f "${_prefix}root.dump" || die 'Can'\''t find image file "%s"\n' "${_prefix}root.dump"
( cd "$TARGET" && restore -r -y -f "${_prefix}root.dump" >/dev/null )
if test -n "$boot_dev"; then
test -f "${_prefix}boot.dump" || die 'Can'\''t find image file "%s"\n' "${_prefix}boot.dump"
( cd "${TARGET}/boot" && restore -r -y -f "${_prefix}boot.dump" >/dev/null )
fi
}
dump_extract() { dump_extractfile "$IMAGE_FILE" "$@"; }
tarball_create() {
test -n "$TARGET" || die 'TARGET is not set in tarball_create\n'
# TODO: put pigz in autoconf as $PIGZ
if test -x /usr/bin/pigz; then
tar -I /usr/bin/pigz --numeric-owner -c -C "$TARGET" -f "$1" .
else
tar -z --numeric-owner -c -C "$TARGET" -f "$1" .
fi >/dev/null
}
tarball_extractfile() {
test -n "$TARGET" || die 'TARGET is not set in tarball_extractfile\n'
# TODO: put pigz in autoconf as $PIGZ
if test -x /usr/bin/pigz; then
tar -I /usr/bin/pigz --numeric-owner -x -p -C "$TARGET" -f "$1" .
else
tar -z --numeric-owner -x -p -C "$TARGET" -f "$1" .
fi >/dev/null
}
tarball_extract() { tarball_extractfile "$IMAGE_FILE" "$@"; }
mkfs_disk0() {
_mkfs="mkfs.$FILESYSTEM"
_mkfs_opts='-Fq'
! test 'xfs' = "$FILESYSTEM" || _mkfs_opts='-fq'
"$_mkfs" $_mkfs_opts -L / "$root_dev" >/dev/null
test -z "$boot_dev" || "$_mkfs" $_mkfs_opts -L /boot "$boot_dev" >/dev/null
test -z "$swap_dev" || mkswap -f "$swap_dev" >/dev/null
# During reinstall ext4 needs time after mkfs so add sync and sleep here.
sync
sleep 2
}
setup_fstab() {
test -n "$TARGET" || die 'TARGET is not set in setup_fstab\n'
get_os_type
cat >"${TARGET}/etc/fstab" <<EOF
# /etc/fstab: static file system information.
#
# <file system> <mount point> <type> <options> <dump> <pass>
UUID=$root_uuid / $FILESYSTEM defaults 0 1
proc /proc proc defaults 0 0
EOF
if test -n "$boot_dev" && test -n "$boot_uuid"; then
cat >>"${TARGET}/etc/fstab" <<EOF
UUID=$boot_uuid /boot $FILESYSTEM defaults 1 2
EOF
fi
if test -n "$swap_dev" && test -n "$swap_uuid"; then
cat >>"${TARGET}/etc/fstab" <<EOF
UUID=$swap_uuid swap swap defaults 0 0
EOF
fi
# OS Specific fstabs
case "$OS_TYPE" in
redhat)
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
;;
gentoo)
cat >>"${TARGET}/etc/fstab" <<EOF
shm /dev/shm tmpfs nodev,nosuid,noexec 0 0
EOF
;;
esac
}
# This is ugly, there must be a better way [sigh]
get_service_manager() {
test -n "$TARGET" || die 'TARGET is not set in get_service_manager\n'
if test -x "${TARGET}/bin/systemctl"; then
printf 'systemd'
else
{ chroot "$TARGET" /sbin/init --version 2>/dev/null || :; } | \
tr '[A-Z\n]' '[a-z ]' | \
sed -e '
s/^.*\(\<upstart\>\).*$/upstart/;
t;
s/^.*\(\<openrc\>\).*$/openrc/;
t;
s/^.*$/sysv/;
'
fi
}
setup_console() {
test -n "$TARGET" || die 'TARGET not set for setup_console\n'
case "`get_service_manager`" in
upstart)
if test -e "${TARGET}/etc/event.d/tty1"; then
cat "${TARGET}/etc/event.d/tty1" | sed -r -e 's/tty1/ttyS0/' >"${TARGET}/etc/event.d/ttyS0"
elif test -e "${TARGET}/etc/init/tty1.conf"; then
# in karmic and newer
cat "${TARGET}/etc/init/tty1.conf" | \
sed -r -e 's/^exec.*/exec \/sbin\/getty -L 115200 ttyS0 vt102/' >"${TARGET}/etc/init/ttyS0.conf"
sed --follow-symlinks -i -e 's/tty1/ttyS0/g' "${TARGET}/etc/init/ttyS0.conf"
fi
;;
systemd)
if ! test -e '/etc/systemd/system/getty.target.wants/[email protected]'; then
chroot "$TARGET" systemctl enable [email protected] >/dev/null 2>&1 || \
ln -sf '/usr/lib/systemd/system/[email protected]' "${TARGET}/etc/systemd/system/getty.target.wants/[email protected]"
fi
;;
*)
get_os
case "$OPERATING_SYSTEM" in
gentoo)
sed --follow-symlinks -i -e 's/.*ttyS0.*/s0:12345:respawn:\/sbin\/agetty 115200 ttyS0 vt100/' "${TARGET}/etc/inittab"
;;
centos|redhat)
printf 's0:12345:respawn:/sbin/agetty 115200 ttyS0 vt100\n' >>"${TARGET}/etc/inittab"
;;
debian|ubuntu)
sed --follow-symlinks -i -e 's/.*T0.*/T0:23:respawn:\/sbin\/getty -L ttyS0 115200 vt100/' "${TARGET}/etc/inittab"
;;
*)
printerr 'No support for your OS in instance-image, skipping...\n'
;;
esac
;;
esac
if test -f "${TARGET}/etc/securetty" && ! grep -q '^ttyS0$' "${TARGET}/etc/securetty"; then
printf '\n\n# Added by ganeti-instance-image\nttyS0\n' >>"${TARGET}/etc/securetty"
fi
}
filesystem_relabel() {
test -n "$TARGET" || die 'TARGET not set for filesystem_relabel\n'
get_os
case "$OPERATING_SYSTEM" in
fedora|centos|redhat)
# we have to force a filesystem relabeling for SELinux after messing
# around with the filesystem in fedora
printerr 'Enforce an automatic relabeling in the initial boot process...\n'
touch "${TARGET}/.autorelabel"
;;
esac
}
get_imagefile() {
case "$IMAGE_TYPE" in
tarball)
IMG_EXT='.tar.gz'
;;
qemu)
IMG_EXT='.img'
;;
dump)
IMG_EXT='-root.dump'
;;
esac
# Allow this to create the empty file (don't use mktemp -u), to avoid race condition (the
# empty file will be overwritten without problem anyway)
#TODO: add $MKTEMP to autoconf, rather than hardcoded mktemp (in other places too)
IMAGE_FILE="$(mktemp --tmpdir="$CACHE_DIR" "${IMAGE_NAME}-${ARCH}-XXXXXX$IMG_EXT")" || \
die 'Unable to create tempfile with template "%s" in the cache-dir "%s".\n' \
"${IMAGE_NAME}-${ARCH}-XXXXXX$IMG_EXT" "$CACHE_DIR"
#TODO: add $BASENAME to autoconf, rather than hardcoded basename (in other places too)
IMAGE_FILENAME="$(basename "$IMAGE_FILE")"
IMAGE_REMOTE_FILENAME="${IMAGE_NAME}-${ARCH}$IMG_EXT"
if test -n "$IMAGE_URL"; then
$WGET -O- "${IMAGE_URL}/$IMAGE_REMOTE_FILENAME" >"$IMAGE_FILE" 2>/dev/null || {
rm -f "$IMAGE_FILE" >/dev/null 2>&1 || :
die 'Failed to download the image from "%s/%s"\n' "$IMAGE_URL" "$IMAGE_REMOTE_FILENAME"
}
if test 'yes' = "$IMG_VERIFY"; then
rm -f "${IMAGE_FILE}.sig" >/dev/null 2>&1 || :
$WGET -O- "${IMAGE_URL}/${IMAGE_REMOTE_FILENAME}.sig" >"${IMAGE_FILE}.sig" 2>/dev/null || {
rm -f "${IMAGE_FILE}.sig" >/dev/null 2>&1 || :
die 'Failed to download the checksum file from "%s/%s.sig"\n' "$IMAGE_URL" "$IMAGE_REMOTE_FILENAME"
}
! test 'yes' = "$IMG_CLEANUP" || cleanup push "rm -f \"${IMAGE_FILE}.sig\""
_pwd="$(pwd)"
cd "$CACHE_DIR" || die 'Couldn'\''t enter cache directory "%s"\n' "$CACHE_DIR"
sed -i -e "s:${IMAGE_REMOTE_FILENAME}\$:${IMAGE_FILENAME}:" "${IMAGE_FILENAME}.sig" || \
die 'Unable to update checksum file "%s.sig" with temporary filename.\n' "$IMAGE_FILE"
"$SHA1SUM" --status --warn --check "${IMAGE_FILENAME}.sig" || \
die 'Image "%s" doesn'\''t match checksum in "%s.sig"!\n' "$IMAGE_FILE" "$IMAGE_FILE"
cd "$_pwd"
fi
elif test -n "$IMAGE_DIR"; then
test -d "$IMAGE_DIR" || die 'Configured image-directory "%s" appears not to be a directory\n' "$IMAGE_DIR"
test -e "${IMAGE_DIR}/$IMAGE_FILENAME" || \
die 'The image "%s" is not in the image-directory "%s"\n' "$IMAGE_FILENAME" "$IMAGE_DIR"
cp -f "${IMAGE_DIR}/$IMAGE_REMOTE_FILENAME" "$IMAGE_FILE" >/dev/null 2>&1 || \
die 'Failed to copy the image "%s" from the image-directory "%s"\n' "$IMAGE_REMOTE_FILENAME" "$IMAGE_DIR"
else
die 'Either $IMAGE_URL or $IMAGE_DIR must be defined in the defaults file.\n'
fi
! test 'yes' = "$IMG_CLEANUP" || cleanup push "rm -f \"$IMAGE_FILE\""
}
use_unique_tmpdir() {
OLD_TMPDIR="$TMPDIR"
TMPDIR="$(mktemp -d --tmpdir "${INSTANCE_NAME}_XXXXXXXX")"
export TMPDIR
cleanup push "rm -fr \"$TMPDIR\"; TMPDIR=\"$OLD_TMPDIR\"; export TMPDIR"
}
source_os_variant() { ! test -f "$VARIANT_CONFIG" || . "$VARIANT_CONFIG"; }
change_hostname() {
test -n "$TARGET" || die 'TARGET is not set in change_hostname\n'
# TODO: systemd needs more changed ("machine-name", etc)
case "$OS_TYPE" in
debian)
HNAME="${TARGET}/etc/hostname"
;;
redhat)
HNAME="${TARGET}/etc/sysconfig/network"
;;
suse)
HNAME="${TARGET}/etc/HOSTNAME"
;;
gentoo)
HNAME="${TARGET}/etc/conf.d/hostname"
# baselayout-2.x support
if test -d "${TARGET}/usr/share/openrc"; then
HOSTNAMEVAR='hostname'
else
HOSTNAMEVAR='HOSTNAME'
fi
;;
esac
case "$OS_TYPE" in
redhat|gentoo)
# syntax-highlighting glitch (at least in emacs) - comment v--THE BELOW LINES--v when editing
OLD_HNAME="$( sed --follow-symlinks -n -e '
s/^[ \t]*'"$HOSTNAMEVAR"'=[ \t]*\(.*[^ \t]\)[ \t]*$/\1/;
t PRINT;
b;
: PRINT;
s/^"\([^"]*\)".*$/\1/;
p;
q;
' "$HNAME" )"
;;
*)
OLD_HNAME="$(cat "$HNAME")"
;;
esac
if test "x$OLD_HNAME" = "x$old_name" || \
test "x${OLD_HNAME}.$`printf '%s' "$old_name" | cut -d. -f2,3`" = "x$old_name"; then
case "$OS_TYPE" in
debian|suse)
printf '%s\n' "$instance" >"$HNAME"
;;
redhat|gentoo)
sed --follow-symlinks -i -e "
s/HOSTNAME=\"$OLD_HNAME\"/${HOSTNAMEVAR}=\"$instance\"/
t;
s/HOSTNAME=${OLD_HNAME}/${HOSTNAMEVAR}=\"$instance\"/;
" "$HNAME"
;;
esac
! test -x "${CUSTOMIZE_DIR}/zz_ddns" || "${CUSTOMIZE_DIR}/zz_ddns" -r
else
die 'Cannot rename from "%s" to "%s":
Instance has a different hostname "%s"\n' "$old_name" "$instance" "$OLD_HNAME"
fi
}
get_api10_parameters() {
instance="$INSTANCE_NAME"
old_name="$OLD_INSTANCE_NAME"
case "$SCRIPT_NAME" in
export)
BLOCKDEV="$EXPORT_DEVICE"
;;
import)
BLOCKDEV="$IMPORT_DEVICE"
;;
*)
BLOCKDEV="$DISK_0_PATH"
;;
esac
}
get_api20_parameters() {
IMG_ID="$OSP_IMG_ID"; IMAGE_NAME="$IMG_ID" # backwards-compatibility, historical naming
IMG_FORMAT="$OSP_IMG_FORMAT"; IMAGE_TYPE="$IMG_FORMAT" # backwards-compatibility, historical naming
IMG_SSH_KEY_URL="$OSP_IMG_SSH_KEY_URL"
IMG_PASSWD="$OSP_IMG_PASSWD"
NOMOUNT="${OSP_IMG_NOMOUNT:-$NOMOUNT}"
}
get_api_parameters() {
get_api10_parameters
get_api20_parameters
}
##
cleanup init
DEFAULT_FILE="@defaultdir@/ganeti-instance-image"
! test -f "$DEFAULT_FILE" || . "$DEFAULT_FILE"
# Variable name change from 2.5 to 2.6
if test -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:=""}
: ${IMAGE_NAME:=""}
: ${IMAGE_TYPE:="dump"}
: ${IMAGE_DIR:="@localstatedir@/cache/ganeti-instance-image"}
: ${NOMOUNT:="no"}
: ${OVERLAY:=""}
: ${EXPORT_DIR:="${TMPDIR:-/tmp}"}
: ${ARCH:=""}
: ${CUSTOMIZE_DIR:="@sysconfdir@/ganeti/instance-image/hooks"}
: ${PRE_CUSTOMIZE_DIR:="@sysconfdir@/ganeti/instance-image/pre-hooks"}
: ${IMAGE_DEBUG:="yes"}
: ${IMAGE_URL:=""}
: ${CACHE_DIR:="@localstatedir@/cache/ganeti-instance-image"}
: ${IMG_VERIFY:="yes"}
: ${IMG_CLEANUP:="yes"}
: ${VARIANTS_DIR:="@sysconfdir@/ganeti/instance-image/variants"}
: ${NETWORKS_DIR:="@sysconfdir@/ganeti/instance-image/networks"}
: ${OVERLAYS_DIR:="@sysconfdir@/ganeti/instance-image/overlays"}
: ${FDISK:="sfdisk"}
SCRIPT_NAME="$(basename "$0")"
KERNEL_PATH="$INSTANCE_HV_kernel_path"
VOL_ID="\"$BLKID\" -c /dev/null -o 'value' -s 'UUID'"
VOL_TYPE="\"$BLKID\" -c /dev/null -o 'value' -s 'TYPE'"
VARIANT_CONFIG="${VARIANTS_DIR}/${OS_VARIANT}.conf"
# NB: as ganeti handles passing more of these variables to "verify" script, move the
# below checks to there if appropriate
check_os_variant() {
if test -n "$OS_VARIANT"; then
test -d "$VARIANTS_DIR" || die 'OS Variants directory "%s" doesn'\''t exist\n' "$VARIANTS_DIR"
if ! test -f "$VARIANT_CONFIG"; then
if grep -qxF "$OS_VARIANT" 'variants.list'; then
die 'ERROR: instance-image configuration error
Published variant "%s" is missing its config file
Please create "%s" or unpublish the variant
(by removing "%s" from variants.list)\n' \
"$OS_VARIANT" "$VARIANT_CONFIG" "$OS_VARIANT"
else
die 'Unofficial variant "%s" is unsupported
Most probably this is a user error, forcing a wrong name
To support this variant please create file "%s" (and add
it to variants.list)\n' "$OS_VARIANT" "$VARIANT_CONFIG"
fi
fi
fi
}
check_sanity() {
test -n "$HYPERVISOR" && test -n "$DISK_COUNT" || \
die 'Missing OS API Variable:\n(HYPERVISOR or DISK_COUNT)\n'
test 1 -le $DISK_COUNT && test -n "$DISK_0_PATH" || die 'At least one disk is needed\n'
case "$SCRIPT_NAME" in
export)
test -n "$EXPORT_DEVICE" || die 'Missing OS API Variable EXPORT_DEVICE\n'
;;
import)
test -n "$IMPORT_DEVICE" || die 'Missing OS API Variable IMPORT_DEVICE\n'
;;
'')
die 'Missing OS API Variable SCRIPT_NAME\n'
;;
esac
! test 'rename' = "$SCRIPT_NAME" || test -n "$OLD_INSTANCE_NAME" || \
die 'Missing OS API Variable OLD_INSTANCE_NAME\n'
! test 20 -eq $OS_API_VERSION || check_os_variant
}
check_sanity
get_api_parameters
source_os_variant