forked from molior-dbs/molior-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmolior-deploy.sh.inc
677 lines (621 loc) · 16.2 KB
/
molior-deploy.sh.inc
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
# vim: syntax=sh
PARTSEP=""
MAXPART=16
fs_has_partitions=0
fs_has_lvm=0
fs_has_luks=0
exit_on_error()
{
if [ "$?" -ne 0 ]; then
log_error "$@"
fi
}
create_fstab()
{
if [ -z "$target" ]; then
log_error "Error: \$target not set"
fi
echo "# $PROJECT fstab" > $target/etc/fstab
if [ -n "$ROOTDEVICE" ]; then
if [ -z "$ROOTFS" ]; then
ROOTFS=auto
fi
if [ -z "$ROOTFSOPTS" ]; then
o="defaults,errors=remount-ro,noatime"
else
o=$ROOTFSOPTS
fi
echo "$ROOTDEVICE / $ROOTFS $o 0 1" >> $target/etc/fstab
else
for p in `seq 1 $MAXPART`
do
if [ -n "$SFDISK" ]; then
eval t=\$PART${p}_MNT
if [ "$t" = "/" ]; then
eval s=\$PART${p}_FS
if [ -z "$s" ]; then
break
fi
eval o=\$PART${p}_OPTS
if [ -z "$o" ]; then
o="defaults,errors=remount-ro,noatime"
fi
part=${disk}$PARTSEP$p
uuid=`blkid -o value -s UUID $part`
if [ -z "$uuid" ]; then
log_error "Error rootfs uuid is empty for '$part'"
fi
echo "UUID=$uuid $t $s $o 0 1" >> $target/etc/fstab
break
fi
fi
if [ -n "$LVM_VG" ]; then
eval t=\$LVM_LV${p}_MNT
if [ "$t" = "/" ]; then
eval s=\$LVM_LV${p}_FS
if [ -z "$s" ]; then
break
fi
eval o=\$LVM_LV${p}_OPTS
if [ -z "$o" ]; then
o="defaults,errors=remount-ro,noatime"
fi
eval u=\$LVM_LV${p}_NAME
if [ -z "$u" ]; then
log_error "LVM no NAME"
fi
part=/dev/mapper/$(echo ${LVM_VG} | sed -r 's/-/--/g')-$u
uuid=`blkid -o value -s UUID $part`
if [ -z "$uuid" ]; then
log_error "Error rootfs uuid is empty for '$part'"
fi
echo "UUID=$uuid $t $s $o 0 1" >> $target/etc/fstab
break
fi
fi
done
fi
if [ -n "$SFDISK" ]; then
for p in `seq 1 $MAXPART`
do
eval t=\$PART${p}_MNT
if [ "$t" = "/" ]; then
continue
fi
eval s=\$PART${p}_FS
if [ -z "$s" ]; then
break
fi
eval o=\$PART${p}_OPTS
if [ -z "$o" ]; then
o="defaults"
fi
part=${disk}$PARTSEP$p
uuid=`blkid -o value -s UUID $part`
if [ -z "$uuid" ]; then
log_error "Error: empty uuid for '$part'"
fi
if [ "$s" = "swap" ]; then
echo "UUID=$uuid none swap sw 0 0" >> $target/etc/fstab
elif [ "$s" != "lvm" ]; then
echo "UUID=$uuid $t $s $o 0 2" >> $target/etc/fstab
fi
done
fi
if [ -n "$LVM_VG" ]; then
for p in `seq 1 $MAXPART`
do
eval t=\$LVM_LV${p}_MNT
if [ "$t" = "/" ]; then
continue
fi
eval s=\$LVM_LV${p}_FS
if [ -z "$s" ]; then
break
fi
eval o=\$LVM_LV${p}_OPTS
if [ -z "$o" ]; then
o="defaults"
fi
eval u=\$LVM_LV${p}_NAME
if [ -z "$u" ]; then
log_error "LVM no FS"
fi
part=/dev/mapper/$(echo ${LVM_VG} | sed -r 's/-/--/g')-$u
if [ "$s" != "encrypted-swap" ]; then
uuid=`blkid -o value -s UUID $part`
if [ -z "$uuid" ]; then
log_error "Error: empty uuid for '$part'"
fi
if [ "$s" = "swap" ]; then
echo "UUID=$uuid none swap sw 0 0" >> $target/etc/fstab
else
echo "UUID=$uuid $t $s $o 0 2" >> $target/etc/fstab
fi
else
echo "/dev/mapper/${u}-open none swap sw 0 0" >> $target/etc/fstab
fi
eval c=\$LVM_LV${p}_CRYPTTAB
if [ -n "$c" ]; then
echo "${u}-open ${part} $c" >> $target/etc/crypttab
fi
done
fi
if [ -n "$BIND_MOUNTS" ]; then
echo >> $target/etc/fstab
old_IFS=$IFS
IFS=","
for m in $BIND_MOUNTS
do
from=`echo $m | cut -d: -f1`
to=`echo $m | cut -d: -f2`
echo "$to $from none rw,noexec,nosuid,nodev,bind 0 0" >>$target/etc/fstab
done
IFS=$old_IFS
fi
if [ -n "$EXTRA_BIND_MOUNTS" ]; then
echo >> $target/etc/fstab
old_IFS=$IFS
IFS=","
for m in $EXTRA_BIND_MOUNTS
do
from=`echo $m | cut -d: -f1`
to=`echo $m | cut -d: -f2`
echo "$to $from none rw,noexec,nosuid,nodev,bind 0 0" >>$target/etc/fstab
done
IFS=$old_IFS
fi
}
create_partitions()
{
if [ -z "$disk" ]; then
log_error "Error: \$disk not set"
fi
if [ -n "$SFDISK" ]; then
echo " * creating partitions"
sfdiskversion=`dpkg -s util-linux | grep ^Version: | sed 's/^Version: //'`
wipearg=""
if dpkg --compare-versions "$sfdiskversion" gt "2.27.1"; then
wipearg="--wipe=always"
fi
echo "$SFDISK" | sfdisk --force $wiparg $disk >/dev/null
exit_on_error "Error creating partitions"
fi
}
create_lvm()
{
part=$1
if [ -z "$LVM_VG" ]; then
log_error "Error: no LVM_VG specified"
fi
if vgs ${LVM_VG} >/dev/null 2>&1; then
log_error "Error: LVM volume group $LVM_VG already exists. Please remove manually if it's not needed or choose different name"
fi
# 8MByte is good for SSDs and SD cards, and the current max
pvcreate -f -y --dataalignment 8m $part >&2
exit_on_error "Error creating LVM PV on $part"
vgcreate -f -y ${LVM_VG} $part >&2 # FIXME: add PV if VG exists
exit_on_error "Error creating LVM VG $LVM_VG on $part"
for p in `seq 1 $MAXPART`
do
eval t=\$LVM_LV${p}_NAME
if [ -z "$t" ]; then
continue
fi
eval s=\$LVM_LV${p}_SIZE
if [ -z "$s" ]; then
log_error "LVM no size"
fi
if [ -n "${LVM_THINPOOL}" ]; then
lvdisplay $LVM_VG/lv_thin >/dev/null 2>&1
if [ $? -ne 0 ]; then
lvcreate -y --extents "100%FREE" --thinpool lv_thin $LVM_VG
exit_on_error "LVM thin pool create"
fi
fi
echo " * creating LVM volume ${LVM_VG}-$t ($s)"
if [ -n "${LVM_THINPOOL}" ]; then
lvcreate -y --name $t --virtualsize $s --thin $LVM_VG/lv_thin >&2
else
lvcreate -y --name $t --size $s $LVM_VG >&2
fi
exit_on_error "Error creating LVM LV $t ($s) on $LVM_VG"
done
}
create_luks()
{
part=$1
name=$2
echo " * cryptsetup $part"
if [ -z "$name" ]; then
log_error "Error: LUKS partitions need a name. Set PART*_LUKSNAME or LVM_LV*_NAME"
fi
eval cipher=\$LUKS_${name}_CIPHER
if [ -z "$cipher" ]; then
cipher=aes-cbc-null
fi
eval keysize=\$LUKS_${name}_KEYSIZE
if [ -z "$keysize" ]; then
keysize=128
fi
eval passphrase=\$LUKS_${name}_PASSPHRASE
if [ -z "$passphrase" ]; then
passphrase="install"
fi
dd if=/dev/zero of=$part bs=4K count=1
echo -n $passphrase | cryptsetup --cipher $cipher --key-size $keysize luksFormat $part -
exit_on_error "Error formatting LUKS partition $part"
echo -n $passphrase | cryptsetup luksOpen $part $name-decrypted
exit_on_error "Error opening LUKS partition $part"
}
make_fs()
{
s=$1
part=$2
name=$3
opts=$4
if [ "$s" = "encrypted-swap" ]; then
# do nothing - will be created on startup
true
elif [ "$s" = "swap" ]; then
echo " * mkswap $part"
mkswap $part >/dev/null
exit_on_error "Error formatting partition $part as $s"
elif [ "$s" = "lvm" ]; then
create_lvm $part
elif [ "$s" = "luks" ]; then
create_luks $part $name
else # normal filesystems
echo " * mkfs $part"
eval mkfs $opts -t $s $part >/dev/null
exit_on_error "Error formatting partition $part as $s"
fi
}
create_fs()
{
if [ -n "$SFDISK" ]; then
for p in `seq 1 $MAXPART`
do
part=${disk}$PARTSEP$p
eval s=\$PART${p}_FS
if [ -z "$s" ]; then
break
fi
eval t=\$PART${p}_MNT
if [ "$t" = "/" ]; then
rootpart=$part
fi
eval u=\$PART${p}_LUKSNAME
eval opts=\$PART${p}_MKFSOPTS
make_fs "$s" "$part" "$u" "$opts"
done
fi
if [ -n "$LVM_VG" ]; then
if [ "$LVM_PV" = "disk" ]; then
create_lvm $disk
fi
for p in `seq 1 $MAXPART`
do
eval t=\$LVM_LV${p}_NAME
if [ -z "$t" ]; then
continue
fi
eval s=\$LVM_LV${p}_FS
if [ -z "$s" ]; then
break
fi
if [ "$s" = "encrypted-swap" ]; then
continue
fi
# lvm replaces hyphens with double hypents in lvm name
part=/dev/mapper/$(echo ${LVM_VG} | sed -r 's/-/--/g')-$t
eval u=\$LVM_LV${p}_MNT
if [ "$u" = "/" ]; then
rootpart=$part
fi
eval opts=\$LVM_LV${p}_MKFSOPTS
make_fs "$s" "$part" "$t" "$opts"
if [ "$s" = "luks" ]; then
eval v=\$LUKS_${t}_FS
if [ -z "$v" ]; then
continue
fi
make_fs "$v" "/dev/mapper/$t-decrypted" "" ""
fi
done
fi
}
get_fsinfo()
{
if [ -n "$SFDISK" ]; then
fs_has_partitions=1
for p in `seq 1 $MAXPART`
do
eval s=\$PART${p}_FS
if [ "$s" = "luks" ]; then
fs_has_luks=1
break
fi
done
fi
if [ -n "$LVM_VG" ]; then
fs_has_lvm=1
for p in `seq 1 $MAXPART`
do
eval s=\$LVM_LV${p}_FS
if [ "$s" = "luks" ]; then
fs_has_luks=1
break
fi
done
fi
}
mount_fs()
{
if [ -z "$target" ]; then
log_error "Error: \$target not set"
fi
if [ -n "$rootpart" ]; then
part=$rootpart
echo " * mount /"
mount $part $target
exit_on_error "Error mounting filesystem $part to $target"
fi
if [ -n "$SFDISK" ]; then
for p in `seq 1 $MAXPART`
do
eval t=\$PART${p}_MNT
if [ -z "$t" ]; then
continue
fi
if [ "$t" = "/" ]; then
continue
fi
part=${disk}$PARTSEP$p
echo " * mount $t"
mkdir -p $target/$t
mount $part $target/$t
exit_on_error "Error mounting filesystem $part to $target$t"
# FIXME: mount luks
done
fi
if [ -n "$LVM_VG" ]; then
for p in `seq 1 $MAXPART`
do
eval s=\$LVM_LV${p}_NAME
if [ -z "$s" ]; then
continue
fi
eval t=\$LVM_LV${p}_MNT
if [ -z "$t" ]; then
eval u=\$LVM_LV${p}_FS
if [ "$u" = "encrypted-swap" ]; then
continue
elif [ "$u" = "luks" ]; then
eval v=\$LUKS_${s}_MNT
if [ -z "$v" ]; then
continue
fi
echo " * mount $v"
mkdir -p $target/$v
mount /dev/mapper/$s-decrypted $target/$v
fi
continue
fi
if [ "$t" = "/" ]; then
continue
fi
part=/dev/mapper/$(echo ${LVM_VG} | sed -r 's/-/--/g')-$s
echo " * mount $t"
mkdir -p $target/$t
mount $part $target/$t
done
fi
}
unmount()
{
if mountpoint -q "$1"; then
echo " * umount $1"
umount "$1"
exit_on_error "Error umounting $1"
fi
}
umount_fs()
{
if [ -z "$target" ]; then
return
fi
sync
mountpoints=""
if [ -n "$SFDISK" ]; then
for p in `seq 1 $MAXPART`
do
eval s=\$PART${p}_MNT
if [ -n "$s" ]; then
mountpoints="$mountpoints$target$s\n"
fi
# FIXME: umount luks
done
fi
if [ -n "$LVM_VG" ]; then
for p in `seq 1 $MAXPART`
do
eval s=\$LVM_LV${p}_MNT
if [ -n "$s" ]; then
mountpoints="$mountpoints$target$s\n"
fi
eval t=\$LVM_LV${p}_FS
if [ "$t" = "encrypted-swap" ]; then
continue
elif [ "$t" = "luks" ]; then
eval u=\$LVM_LV${p}_NAME
if [ -n "$u" ]; then
eval v=\$LUKS_${u}_MNT
if [ -z "$v" ]; then
continue
fi
mountpoints="$mountpoints$target$v\n"
fi
fi
done
fi
for m in `/bin/echo -e "$mountpoints" | sort -r`
do
umount $m
done
# close LUKS devices
if [ -n "$SFDISK" ]; then
for p in `seq 1 $MAXPART`
do
eval t=\$PART${p}_FS
if [ -z "$t" ]; then
break
fi
if [ "$t" = "luks" ]; then
eval u=\$PART${p}_LUKSNAME
if [ -z "$u" ]; then
continue
fi
cryptsetup luksClose /dev/mapper/$u-decrypted
fi
done
fi
if [ -n "$LVM_VG" ]; then
for p in `seq 1 $MAXPART`
do
eval s=\$LVM_LV${p}_FS
if [ -z "$s" ]; then
break
fi
if [ "$s" = "encrypted-swap" ]; then
continue
elif [ "$s" = "luks" ]; then
eval t=\$LVM_LV${p}_NAME
if [ -z "$t" ]; then
continue
fi
cryptsetup luksClose /dev/mapper/$t-decrypted
fi
done
fi
}
configure_networking()
{
# dhcp is done by network card
# assing the IP from /proc/cmdline to the correct interface (BOOTIF)
# copied from /usr/share/initramfs-tools/scripts/functions:
# strip off the leading "01-", which isn't part of the mac
# address
temp_mac=${BOOTIF#*-}
# convert to typical mac address format by replacing "-" with ":"
bootif_mac=""
IFS='-'
for x in $temp_mac ; do
if [ -z "$bootif_mac" ]; then
bootif_mac="$x"
else
bootif_mac="$bootif_mac:$x"
fi
done
unset IFS
# look for devices with matching mac address, and set DEVICE to
# appropriate value if match is found.
for device in /sys/class/net/* ; do
if [ -f "$device/address" ]; then
current_mac=$(cat "$device/address")
if [ "$bootif_mac" = "$current_mac" ]; then
DEVICE=${device##*/}
break
fi
fi
done
# end of copied
if [ -z "$DEVICE" ]; then
echo No network interface found for $BOOTIF
ls /sys/class/net/ -l
cat /sys/class/net/*/address
busybox sh
fi
CLIENT_IP=$(echo $IP | cut -d: -f1)
ROOTSERVER=$(echo $IP | cut -d: -f2)
GATEWAY=$(echo $IP | cut -d: -f3)
NETMASK=$(echo $IP | cut -d: -f4)
ip addr add $CLIENT_IP/$NETMASK dev $DEVICE
ip link set dev $DEVICE up
ip route add default via $GATEWAY dev $DEVICE
}
get_deploy_config()
{
echo "MOLIOR_TOOLS_VERSION=\"$MOLIOR_TOOLS_VERSION\""
echo "DEPLOYMENT_TIMESTAMP=\"$DEPLOYMENT_TIMESTAMP\""
echo "PROJECT=\"$PROJECT\""
echo "VERSION=\"$VERSION\""
echo "REVISION=\"$REVISION\""
echo "VARIANT=\"$VARIANT\""
echo "SUITE=\"$SUITE\""
echo "SFDISK=\"$SFDISK\""
echo "LVM_VG=\"$LVM_VG\""
echo "ROOTDEVICE=\"$ROOTDEVICE\""
echo "ROOTFS=\"$ROOTFS\""
echo "ROOTFSOPTS=\"$ROOTFSOPTS\""
for p in `seq 1 $MAXPART`
do
eval s=\$PART${p}_FS
test -n "$s" && echo "PART${p}_FS=\"$s\""
eval s=\$PART${p}_MNT
test -n "$s" && echo "PART${p}_MNT=\"$s\""
eval s=\$PART${p}_OPTS
test -n "$s" && echo "PART${p}_OPTS=\"$s\""
eval s=\$PART${p}_MKFSOPTS
test -n "$s" && echo "PART${p}_MKFSOPTS=\"$s\""
eval s=\$PART${p}_LUKSNAME
if [ -n "$s" ]; then
echo "PART${p}_LUKSNAME=\"$s\""
luks_name=$s
eval s=\$LUKS_${luks_name}_FS
test -n "$s" && echo "LUKS_${luks_name}_FS=\"$s\""
eval s=\$LUKS_${luks_name}_MNT
test -n "$s" && echo "LUKS_${luks_name}_MNT=\"$s\""
# FIXME: LUKS_${luks_name}_MKFSOPTS
fi
eval s=\$LVM_LV${p}_FS
test -n "$s" && echo "LVM_LV${p}_FS=\"$s\""
lvm_fs=$s
eval s=\$LVM_LV${p}_MNT
test -n "$s" && echo "LVM_LV${p}_MNT=\"$s\""
eval s=\$LVM_LV${p}_NAME
test -n "$s" && echo "LVM_LV${p}_NAME=\"$s\""
eval s=\$LVM_LV${p}_OPTS
test -n "$s" && echo "LVM_LV${p}_OPTS=\"$s\""
eval s=\$LVM_LV${p}_CRYPTTAB
test -n "$s" && echo "LVM_LV${p}_CRYPTTAB=\"$s\""
if [ "$lvm_fs" = "luks" ]; then
luks_name=$s
eval s=\$LUKS_${luks_name}_FS
test -n "$s" && echo "LUKS_${luks_name}_FS=\"$s\""
eval s=\$LUKS_${luks_name}_MNT
test -n "$s" && echo "LUKS_${luks_name}_MNT=\"$s\""
fi
eval s=\$LVM_LV${p}_SIZE
test -n "$s" && echo "LVM_LV${p}_SIZE=\"$s\""
eval s=\$LVM_LV${p}_MKFSOPTS
test -n "$s" && echo "LVM_LV${p}_MKFSOPTS=\"$s\""
done
echo "BIND_MOUNTS=\"${BIND_MOUNTS}\""
echo "EXTRA_BIND_MOUNTS=\"${EXTRA_BIND_MOUNTS}\""
if [ -n "$TARGET_HOSTNAME" ]; then
echo "HOSTNAME=\"$TARGET_HOSTNAME\""
fi
echo "LINUX_CMDLINE=\"$LINUX_CMDLINE\""
echo "INSTALLER_PASSWD_USERS=\"$INSTALLER_PASSWD_USERS\""
echo "INSTALLER_ASK_HOSTNAME=\"$INSTALLER_ASK_HOSTNAME\""
echo "INSTALLER_ASK_CONFIRM=\"$INSTALLER_ASK_CONFIRM\""
echo "INSTALLER_ASK_REBOOT=\"$INSTALLER_ASK_REBOOT\""
echo "INSTALLER_USE_PXE=\"$INSTALLER_USE_PXE\""
echo "INSTALLER_EXTRA_INITRD=\"$INSTALLER_EXTRA_INITRD\""
echo "INSTALLER_BOOTTYPE=\"$INSTALLER_BOOTTYPE\""
echo "INSTALLER_COMPRESSION=\"$INSTALLER_COMPRESSION\""
echo "INSTALLER_COMPRESSION_EXT=\"$INSTALLER_COMPRESSION_EXT\""
echo "INSTALLER_DISK_MIN=\"$INSTALLER_DISK_MIN\""
echo "INSTALLER_PXE_IMAGE_URL=\"$INSTALLER_PXE_IMAGE_URL\""
}