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
|
#!/bin/bash
# FORMAT DEFINITIONS:
# -- formats used to interface with this library --
# $TMP_PARTITIONS
# one line per partition, blockdevice + partioning string for sfdisk. See docs for function partition for more info.
# $TMP_BLOCKDEVICES
# one line per blockdevice, multiple fs'es in 1 'fs-string'
# $TMP_BLOCKDEVICES entry.
# <blockdevice> type label/no_label <FS-string>/no_fs
# FS-string:
# type;recreate(yes/no);mountpoint;mount?(target,runtime,no);opts;label;params[|FS-string|...] where opts/params have _'s instead of whitespace if needed
# NOTE: the 'mount?' for now just matters for the location (if 'target', the target path gets prepended and mounted in the runtime system)
# NOTE: filesystems that span multiple underlying filesystems/devices (eg lvm VG) should specify those in params, separated by colons. \
# the <blockdevice> in the beginning doesn't matter much, it can be pretty much any device, or not existent, i think. But it's probably best to make it one of the devices listed in params
# no '+' characters allowed for devices in $fs_params (eg use the real names)
# -- ADDITIONAL INTERNAL FORMATS --
# $TMP_FILESYSTEMS: each filesystem on a separate line, so block devices can appear multiple times be on multiple lines (eg LVM volumegroups with more lvm LV's)
# part part_type part_label fs_type fs_create fs_mountpoint fs_mount fs_opts fs_label fs_params
modprobe dm-crypt || show_warning modprobe 'Could not modprobe dm-crypt. no support for disk encryption'
modprobe aes-i586 || show_warning modprobe 'Could not modprobe aes-i586. no support for disk encryption'
TMP_DEV_MAP=$RUNTIME_DIR/aif-dev.map
TMP_FSTAB=$RUNTIME_DIR/aif-fstab
TMP_PARTITIONS=$RUNTIME_DIR/aif-partitions
TMP_FILESYSTEMS=$RUNTIME_DIR/aif-filesystems # Only used internally by this library. Do not even think about using this as interface to this library. it won't work
TMP_BLOCKDEVICES=$RUNTIME_DIR/aif-blockdata
# procedural code from quickinst functionized and fixed.
# there were functions like this in the setup script too, with some subtle differences. see below
# NOTE: why were the functions in the setup called CHROOT_mount/umount? this is not chrooting ? ASKDEV
target_special_fs ()
{
[ "$1" = on -o "$1" = off ] || die_error "special_fs needs on/off argument"
if [ "$1" = on ]
then
# mount proc/sysfs first, so mkinitrd can use auto-detection if it wants
! [ -d $var_TARGET_DIR/proc ] && mkdir $var_TARGET_DIR/proc
! [ -d $var_TARGET_DIR/sys ] && mkdir $var_TARGET_DIR/sys
! [ -d $var_TARGET_DIR/dev ] && mkdir $var_TARGET_DIR/dev
#mount, if not mounted yet
mount | grep -q "$var_TARGET_DIR/proc" || mount -t proc none $var_TARGET_DIR/proc || die_error "Could not mount $var_TARGET_DIR/proc" # NOTE: setup script uses mount -t proc proc ? what's best? ASKDEV
mount | grep -q "$var_TARGET_DIR/sys" || mount -t sysfs none $var_TARGET_DIR/sys || die_error "Could not mount $var_TARGET_DIR/sys" # NOTE: setup script uses mount -t sysfs sysfs ? what's best? ASKDEV
mount | grep -q "$var_TARGET_DIR/dev" || mount -o bind /dev $var_TARGET_DIR/dev || die_error "Could not mount $var_TARGET_DIR/dev"
elif [ "$1" = off ]
then
umount $var_TARGET_DIR/proc || die_error "Could not umount $var_TARGET_DIR/proc"
umount $var_TARGET_DIR/sys || die_error "Could not umount $var_TARGET_DIR/sys"
umount $var_TARGET_DIR/dev || die_error "Could not umount $var_TARGET_DIR/dev"
fi
}
# taken from setup #TODO: we should be able to not need this function. although it may still be useful. but maybe we shouldn't tie it to $var_TARGET_DIR, and let the user specify the base point as $1
# Disable swap and umount all mounted filesystems for the target system in the correct order. (eg first $var_TARGET_DIR/a/b/c, then $var_TARGET_DIR/a/b, then $var_TARGET_DIR/a until lastly $var_TARGET_DIR
target_umountall()
{
infofy "Disabling all swapspace..." disks
swapoff -a >/dev/null 2>&1
declare target=${var_TARGET_DIR//\//\\/} # escape all slashes otherwise awk complains
for mountpoint in $(mount | awk "/\/$target/ {print \$3}" | sort | tac )
do
infofy "Unmounting mountpoint $mountpoint" disks
umount $mountpoint >/dev/null 2>$LOG
done
}
# taken from setup script, modified for separator control
# $1 set to 1 to echo a newline after device instead of a space (optional)
# $2 extra things to echo for each device (optional)
# $3 something to append directly after the device (optional)
finddisks() {
workdir="$PWD"
cd /sys/block
# ide devices
for dev in $(ls | egrep '^hd'); do
if [ "$(cat $dev/device/media)" = "disk" ]; then
echo -n "/dev/$dev$3"
[ "$1" = 1 ] && echo || echo -n ' '
[ "$2" ] && echo $2
fi
done
#scsi/sata devices
for dev in $(ls | egrep '^sd'); do
# TODO: what is the significance of 5? ASKDEV
if ! [ "$(cat $dev/device/type)" = "5" ]; then
echo -n "/dev/$dev$3"
[ "$1" = 1 ] && echo || echo -n ' '
[ "$2" ] && echo $2
fi
done
# cciss controllers
if [ -d /dev/cciss ] ; then
cd /dev/cciss
for dev in $(ls | egrep -v 'p'); do
echo -n "/dev/cciss/$dev$3"
[ "$1" = 1 ] && echo || echo -n ' '
[ "$2" ] && echo $2
done
fi
# Smart 2 controllers
if [ -d /dev/ida ] ; then
cd /dev/ida
for dev in $(ls | egrep -v 'p'); do
echo -n"/dev/ida/$dev$3"
[ "$1" = 1 ] && echo || echo -n ' '
[ "$2" ] && echo $2
done
fi
cd "$workdir"
}
# getuuid(). taken and modified from setup. this can probably be more improved. return an exit code, rely on blkid's exit codes etc.
# converts /dev/[hs]d?[0-9] devices to UUIDs
#
# parameters: device file
# outputs: UUID on success
# nothing on failure
# returns: nothing
getuuid()
{
[ -z "$1" ] && die_error "getuuid needs an argument"
[ "${1%%/[hs]d?[0-9]}" != "${1}" ] && echo "$(blkid -s UUID -o value ${1})"
}
# taken from setup script, slightly optimized and modified for separator control
# $1 set to 1 to echo a newline after partition instead of a space (optional)
# $2 extra things to echo for each partition (optional)
# $3 something to append directly after the partition (optional) TODO: refactor code so there's a space in between, merge $2 and $3. use echo -e to print whatever user wants
findpartitions() {
workdir="$PWD"
for devpath in $(finddisks)
do
disk=$(echo $devpath | sed 's|.*/||')
cd /sys/block/$disk
for part in $disk*
do
# check if not already assembled to a raid device. TODO: what is the significance of the 5? ASKDEV
if ! [ "$(grep $part /proc/mdstat 2>/dev/null)" -o "$(fstype 2>/dev/null </dev/$part | grep lvm2)" -o "$(sfdisk -c /dev/$disk $(echo $part | sed -e "s#$disk##g") 2>/dev/null | grep "5")" ]
then
if [ -d $part ]
then
echo -n "/dev/$part$3"
[ "$1" = 1 ] && echo || echo -n ' '
[ "$2" ] && echo $2
fi
fi
done
done
# include any mapped devices
for devpath in $(ls /dev/mapper 2>/dev/null | grep -v control)
do
echo -n "/dev/mapper/$devpath$3"
[ "$1" = 1 ] && echo || echo -n ' '
[ "$2" ] && echo $2
done
# include any raid md devices
for devpath in $(ls -d /dev/md* | grep '[0-9]' 2>/dev/null)
do
if grep -qw $(echo $devpath /proc/mdstat | sed -e 's|/dev/||g')
then
echo -n "$devpath$3"
[ "$1" = 1 ] && echo || echo -n ' '
[ "$2" ] && echo $2
fi
done
# inlcude cciss controllers
if [ -d /dev/cciss ]
then
cd /dev/cciss
for dev in $(ls | egrep 'p')
do
echo -n "/dev/cciss/$dev$3"
[ "$1" = 1 ] && echo || echo -n ' '
[ "$2" ] && echo $2
done
fi
# inlcude Smart 2 controllers
if [ -d /dev/ida ]
then
cd /dev/ida
for dev in $(ls | egrep 'p')
do
echo -n "/dev/ida/$dev$3"
[ "$1" = 1 ] && echo || echo -n ' '
[ "$2" ] && echo $2
done
fi
cd "$workdir"
}
# taken from setup
get_grub_map() {
rm $TMP_DEV_MAP #TODO: this doesn't exist? is this a problem? ASKDEV
infofy "Generating GRUB device map...\nThis could take a while.\n\n Please be patient."
$var_TARGET_DIR/sbin/grub --no-floppy --device-map $TMP_DEV_MAP >/tmp/grub.log 2>&1 <<EOF
quit
EOF
}
# TODO: $1 is what?? ASKDEV
# taken from setup. slightly edited.
mapdev() {
partition_flag=0
device_found=0
devs=$( grep -v fd $TMP_DEV_MAP | sed 's/ *\t/ /' | sed ':a;$!N;$!ba;s/\n/ /g')
linuxdevice=$(echo $1 | cut -b1-8)
if [ "$(echo $1 | egrep '[0-9]$')" ]; then
# /dev/hdXY
pnum=$(echo $1 | cut -b9-)
pnum=$(($pnum-1))
partition_flag=1
fi
for dev in $devs
do
if [ "(" = $(echo $dev | cut -b1) ]; then
grubdevice="$dev"
else
if [ "$dev" = "$linuxdevice" ]; then
device_found=1
break
fi
fi
done
if [ "$device_found" = "1" ]; then
if [ "$partition_flag" = "0" ]; then
echo "$grubdevice"
else
grubdevice_stringlen=${#grubdevice}
grubdevice_stringlen=$(($grubdevice_stringlen - 1))
grubdevice=$(echo $grubdevice | cut -b1-$grubdevice_stringlen)
echo "$grubdevice,$pnum)"
fi
else
echo "DEVICE NOT FOUND" >&2
return 2
fi
}
# auto_fstab(). taken from setup
# preprocess fstab file
# comments out old fields and inserts new ones
# according to partitioning/formatting stage
#
target_configure_fstab()
{
if [ -f $TMP_FSTAB ]
then
# comment out stray /dev entries
sed -i 's/^\/dev/#\/dev/g' $var_TARGET_DIR/etc/fstab
# append entries from new configuration
sort $TMP_FSTAB >>$var_TARGET_DIR/etc/fstab
fi
}
# partitions a disk. heavily altered
# $1 device to partition
# $2 a string of the form: <partsize in MiB>:<fstype>[:+] (the + is bootable flag)
partition()
{
debug 'FS' "Partition called like: partition '$1' '$2'"
[ -z "$1" ] && die_error "partition() requires a device file and a partition string"
[ -z "$2" ] && die_error "partition() requires a partition string"
DEVICE=$1
STRING=$2
# validate DEVICE
if [ ! -b "$DEVICE" ]; then
notify "Device '$DEVICE' is not valid"
return 1
fi
target_umountall
# setup input var for sfdisk
# format: each line=1 part. <start> <size> <id> <bootable>[ <c,h,s> <c,h,s>]
read -r -a fsspecs <<< "$STRING" # split up like this otherwise '*' will be globbed. which usually means an entry containing * is lost
for fsspec in "${fsspecs[@]}"; do
fssize=$(echo $fsspec | tr -d ' ' | cut -f1 -d:)
fssize_spec=",$fssize"
[ "$fssize" = "*" ] && fssize_spec=';'
fstype=$(echo $fsspec | tr -d ' ' | cut -f2 -d:)
fstype_spec=","
[ "$fstype" = "swap" ] && fstype_spec=",S"
bootflag=$(echo $fsspec | tr -d ' ' | cut -f3 -d:)
bootflag_spec=""
[ "$bootflag" = "+" ] && bootflag_spec=",*"
sfdisk_input="${sfdisk_input}${fssize_spec}${fstype_spec}${bootflag_spec}\n"
done
sfdisk_input=$(printf "$sfdisk_input") # convert \n to newlines
# invoke sfdisk
debug 'FS' "Partition calls: sfdisk $DEVICE -uM >$LOG 2>&1 <<< $sfdisk_input"
printk off
sfdisk $DEVICE -uM >$LOG 2>&1 <<EOF
$sfdisk_input
EOF
if [ $? -gt 0 ]; then
notify "Error partitioning $DEVICE (see $LOG for details)"
printk on
return 1
fi
printk on
return 0
}
# go over each disk in $TMP_PARTITIONS and partition it
process_disks ()
{
while read disk scheme
do
process_disk $disk "$scheme" || return $?
done < $TMP_PARTITIONS
}
process_disk ()
{
infofy "Partitioning $1" disks
partition $1 "$2"
}
generate_filesystem_list ()
{
echo -n > $TMP_FILESYSTEMS
while read part part_type part_label fs_string
do
if [ "$fs_string" != no_fs ]
then
for fs in `sed 's/|/ /g' <<< $fs_string` # this splits multiple fs'es up, or just takes the one if there is only one (lvm vg's can have more then one lv)
do
fs_type=` cut -d ';' -f 1 <<< $fs`
fs_create=` cut -d ';' -f 2 <<< $fs`
fs_mountpoint=` cut -d ';' -f 3 <<< $fs`
fs_mount=` cut -d ';' -f 4 <<< $fs`
fs_opts=` cut -d ';' -f 5 <<< $fs`
fs_label=` cut -d ';' -f 6 <<< $fs`
fs_params=` cut -d ';' -f 7 <<< $fs`
echo "$part $part_type $part_label $fs_type $fs_create $fs_mountpoint $fs_mount $fs_opts $fs_label $fs_params" >> $TMP_FILESYSTEMS
done
fi
done < $TMP_BLOCKDEVICES
}
# process all entries in $TMP_BLOCKDEVICES, create all blockdevices and filesystems and mount them correctly
process_filesystems ()
{
debug 'FS' "process_filesystems Called. checking all entries in $TMP_BLOCKDEVICES"
rm -f $TMP_FSTAB
generate_filesystem_list
returncode=0
# phase 1: create all blockdevices and filesystems in the correct order (for each fs, the underlying block/lvm/devicemapper device must be available so dependencies must be resolved. for lvm:first pv's, then vg's, then lv's etc)
# don't let them mount yet. we take care of all that ourselves in the next phase
infofy "Phase 1: Creating filesystems & blockdevices" disks
done_filesystems=
for i in `seq 1 10`
do
open_items=0
while read part part_type part_label fs_type fs_create fs_mountpoint fs_mount fs_opts fs_label fs_params
do
fs_id="$part $fs_type $fs_mountpoint $fs_opts $fs_label $fs_params"
if [ "$fs_create" = yes ]
then
if check_is_in "$fs_id" "${done_filesystems[@]}"
then
debug 'FS' "$fs_id ->Already done"
else
# We can't always do -b on the lvm VG. because the devicefile sometimes doesn't exist for a VG. vgdisplay to the rescue!
if [ "$part_type" = lvm-vg ] && vgdisplay $part | grep -q 'VG Name' # $part is a lvm VG and it exists. note that vgdisplay exists 0 when the requested vg doesn't exist.
then
debug 'FS' "$fs_id ->Still need to do it: Making the filesystem on a vg volume"
infofy "Making $fs_type filesystem on $part" disks
process_filesystem $part $fs_type $fs_create $fs_mountpoint no_mount $fs_opts $fs_label $fs_params && done_filesystems+=("$fs_id") || returncode=1
elif [ "$part_type" != lvm-pv -a -b "$part" ] # $part is not a lvm PV and it exists
then
debug 'FS' "$fs_id ->Still need to do it: Making the filesystem on a non-pv volume"
infofy "Making $fs_type filesystem on $part" disks
process_filesystem $part $fs_type $fs_create $fs_mountpoint no_mount $fs_opts $fs_label $fs_params && done_filesystems+=("$fs_id") || returncode=1
elif [ "$part_type" = lvm-pv ] && pvdisplay ${fs_params//:/ } >/dev/null # $part is a lvm PV. all needed lvm pv's exist. note that pvdisplay exits 5 as long as one of the args doesn't exist
then
debug 'FS' "$fs_id ->Still need to do it: Making the filesystem on a pv volume"
infofy "Making $fs_type filesystem on $part" disks
process_filesystem ${part/+/} $fs_type $fs_create $fs_mountpoint no_mount $fs_opts $fs_label $fs_params && done_filesystems+=("$fs_id") || returncode=1
else
debug 'FS' "$fs_id ->Cannot do right now..."
open_items=1
fi
fi
fi
done < $TMP_FILESYSTEMS
[ $open_items -eq 0 ] && break
done
[ $open_items -eq 1 ] && show_warning "Filesystem/blockdevice processor problem" "Warning: Could not create all needed filesystems. Either the underlying blockdevices didn't became available in 10 iterations, or process_filesystem failed" && returncode=1
# phase 2: mount all filesystems in the vfs in the correct order. (also swapon where appropriate)
infofy "Phase 2: Mounting filesystems" disks
sort -t \ -k 6 $TMP_FILESYSTEMS | while read part part_type part_label fs_type fs_create fs_mountpoint fs_mount fs_opts fs_label fs_params
do
if [ "$fs_mountpoint" != no_mountpoint ]
then
infofy "Mounting $part ($fs_type) on $fs_mountpoint" disks
process_filesystem $part $fs_type no $fs_mountpoint $fs_mount $fs_opts $fs_label $fs_params || returncode=1
elif [ "$fs_type" = swap ]
then
infofy "Swaponning $part" disks
process_filesystem $part $fs_type no $fs_mountpoint $fs_mount $fs_opts $fs_label $fs_params || returncode=1
fi
done
BLOCK_ROLLBACK_USELESS=0
[ $returncode -eq 0 ] && infofy "Done processing filesystems/blockdevices" disks 1 && return 0
return $returncode
}
# Roll back all "filesystems" (normal ones and dm-mapper based stuff) specified in $BLOCK_DATA. Not partitions. Doesn't restore data after you erased it, of course.
rollback_filesystems ()
{
infofy "Rolling back filesystems..." disks
generate_filesystem_list
local warnings=
rm -f $TMP_FSTAB
# phase 1: destruct all mounts in the vfs and swapoff swap volumes who are listed in $BLOCK_DATA
# re-order list so that we umount in the correct order. eg first umount /a/b/c, then /a/b. we sort alphabetically, which has the side-effect of sorting by stringlength, hence by vfs dependencies.
infofy "Phase 1: Umounting all specified mountpoints" disks
done_umounts= # We translate some devices back to their original (eg /dev/sda3+ -> /dev/sda3 for lvm PV's). No need to bother user twice for such devices.
sort -t \ -k 6 $TMP_FILESYSTEMS | tac | while read part part_type part_label fs_type fs_create fs_mountpoint fs_mount fs_opts fs_label fs_params
do
if [ "$fs_type" = swap ]
then
infofy "(Maybe) Swapoffing $part" disks
swapoff $part &>/dev/null # could be that it was not swappedon yet. that's not a problem at all.
elif [ "$fs_mountpoint" != no_mountpoint ]
then
part_real=${part/+/}
if ! check_is_in "$part_real" "${done_umounts[@]}"
then
infofy "(Maybe) Umounting $part_real" disks
if mount | grep -q "^$part_real " # could be that this was not mounted yet. no problem, we can just skip it then.
then
if umount $part_real >$LOG
then
done_umounts+=("$part_real")
else
warnings="$warnings\nCould not umount umount $part_real . Probably device is still busy. See $LOG"
show_warning "Umount failure" "Could not umount umount $part_real . Probably device is still busy. See $LOG" #TODO: fix device busy things
fi
fi
fi
fi
done
# phase 2: destruct blockdevices listed in $BLOCK_DATA if they would exist already, in the correct order (first lvm LV, then VG, then PV etc)
# targets are device-mapper devices such as any lvm things, dm_crypt devices, etc and lvm PV's.
# Possible approach 1 (not implemented): for each target in $TMP_BLOCKDEVICES, check that it has no_fs or has a non-lvm/dm_crypt fs. (egrep -v ' lvm-pv;| lvm-vg;| lvm-lv;| dm_crypt;' ) and clean it
# -> requires updating of underlying block device string when you clean something, on a copy of .block_data etc. too complicated
# Approach 2 : iterate over all targets in $TMP_BLOCKDEVICES as much as needed, until a certain limit, and in each loop check what can be cleared by looking at the real, live usage of / dependencies on the partition.
# -> easier (implemented)
infofy "Phase 2: destructing specified blockdevices" disks
for i in `seq 1 10`
do
open_items=0
while read part part_type part_label fs_string # $fs_string can be ignored
do
real_part=${part/+/}
if [ "$part_type" = dm_crypt ] # Can be in use for: lvm-pv or raw. we don't need to care about raw (it will be unmounted so it can be destroyed)
then
if [ -b $real_part ] && cryptsetup status $real_part &>/dev/null # don't use 'isLuks' it only works for the "underlying" device (eg in /dev/sda1 -> luksOpen -> /dev/mapper/foo, isLuks works only on the former. status works on the latter too)
then
if pvdisplay $real_part &>/dev/null
then
debug 'FS' "$part ->Cannot do right now..."
open_items=1
else
infofy "Attempting destruction of device $part (type $part_type)" disks
if ! cryptsetup luksClose $real_part &>$LOG
then
warnings="$warnings\nCould not cryptsetup luksClose $real_part"
show_warning "process_filesystems blockdevice destruction" "Could not cryptsetup luksClose $real_part"
fi
fi
else
debug 'FS' "Skipping destruction of device $part (type $part_type) because it doesn't exist"
fi
elif [ "$part_type" = lvm-pv ] # Can be in use for: lvm-vg
then
if [ -b $real_part ] && pvdisplay $real_part &>/dev/null
then
if vgdisplay -v 2>/dev/null | grep -q $real_part # check if it's in use
then
debug 'FS' "$part ->Cannot do right now..."
open_items=1
else
infofy "Attempting destruction of device $part (type $part_type)" disks
if ! pvremove $real_part &>$LOG
then
warnings="$warnings\nCould not pvremove $part"
show_warning "process_filesystems blockdevice destruction" "Could not pvremove $part"
fi
fi
else
debug 'FS' "Skipping destruction of device $part (type $part_type) because it doesn't exist"
fi
elif [ "$part_type" = lvm-vg ] #Can be in use for: lvm-lv
then
if vgdisplay $part 2>/dev/null | grep -q 'VG Name' # workaround for non-existing lvm VG device files
then
open_lv=`vgdisplay -c $part 2>/dev/null | cut -d ':' -f6`
if [ $open_lv -gt 0 ]
then
debug 'FS' "$part ->Cannot do right now..."
open_items=1
else
infofy "Attempting destruction of device $part (type $part_type)" disks
if ! vgremove $part &>$LOG # we shouldn't need -f because we clean up the lv's first.
then
warnings="$warnings\nCould not vgremove $part"
show_warning "process_filesystems blockdevice destruction" "Could not vgremove $part"
fi
fi
else
debug 'FS' "Skipping destruction of device $part (type $part_type) because it doesn't exist"
fi
elif [ "$part_type" = lvm-lv ] #Can be in use for: dm_crypt or raw. we don't need to care about raw (it will be unmounted so it can be destroyed)
then
if lvdisplay $part &>/dev/null && ! vgdisplay $part 2>/dev/null | grep -q 'VG Name' # it exists: lvdisplay works, and it's not a volume group (you can do lvdisplay $volumegroup)
then
if cryptsetup isLuks $part &>/dev/null
then
debug 'FS' "$part ->Cannot do right now..."
open_items=1
else
infofy "Attempting destruction of device $part (type $part_type)" disks
if ! lvremove -f $part &>$LOG
then
warnings="$warnings\nCould not lvremove -f $part"
show_warning "process_filesystems blockdevice destruction" "Could not lvremove -f $part"
fi
fi
else
debug 'FS' "Skipping destruction of device $part (type $part_type) because it doesn't exist"
fi
else
die_error "Unrecognised partition type $part_type for partition $part. This should never happen. please report this"
fi
done < <(egrep '\+|mapper' $TMP_BLOCKDEVICES) #TODO: improve regex
[ $open_items -eq 0 ] && break
done
if [ $open_items -eq 1 ]
then
warnings="$warnings\nCould not destruct all filesystems/blockdevices. It appears some depending filesystems/blockdevices could not be cleared in 10 iterations"
show_warning "Filesystem/blockdevice processor problem" "Warning: Could not destruct all filesystems/blockdevices. It appears some depending filesystems/blockdevices could not be cleared in 10 iterations"
fi
[ -n "$warnings" ] && infofy "Rollback failed" disks 1 && show_warning "Rollback problems" "Some problems occurred while rolling back: $warnings.\n Thisk needs to be fixed before retrying disk/filesystem creation or restarting the installer" && return 1
infofy "Rollback succeeded" disks 1
BLOCK_ROLLBACK_USELESS=1
return 0
}
# make a filesystem on a blockdevice and mount if needed.
# $1 partition
# $2 fs_type
# $3 fs_create (optional. defaults to yes)
# $4 fs_mountpoint (optional. defaults to no_mountpoint)
# $5 fs_mount (optional. defaults to no_mount)
# $6 fs_opts (optional. defaults to no_opts)
# $7 fs_label (optional. defaults to no_label or for lvm volumes who need a label (VG's and LV's) vg1,vg2,lv1 etc). Note that if there's no label for a VG you probably did something wrong, because you probably want LV's on it so you need a label for the VG.
# $8 fs_params (optional. defaults to no_params)
process_filesystem ()
{
[ "$2" != lvm-lv ] && [ -z "$1" -o ! -b "$1" ] && die_error "process_filesystem needs a partition as \$1" # Don't do this for lv's. It's a hack to workaround non-existence of VG device files.
[ -z "$2" ] && die_error "process_filesystem needs a filesystem type as \$2"
debug 'FS' "process_filesystem $@"
part=$1
fs_type=$2
fs_create=${3:-yes}
fs_mountpoint=${4:-no_mountpoint}
fs_mount=${5:-no_mount}
fs_opts=${6:-no_opts}
fs_label=${7:-no_label}
fs_params=${8:-no_params}
[ "$fs_mountpoint" = no_mountpoint ] && fs_mountpoint=
[ "$fs_mount" = no_mount ] && fs_mount=
[ "$fs_opts" = no_opts ] && fs_opts=
[ "$fs_label" = no_label ] && fs_label=
[ "$fs_params" = no_params ] && fs_params=
# Create the FS
if [ "$fs_create" = yes ]
then
if ! program=`get_filesystem_program $fs_type`
then
show_warning "process_filesystem error" "Cannot determine filesystem program for $fs_type on $part. Not creating this FS"
return 1
fi
[ -z "$fs_label" ] && [ "$fs_type" = lvm-vg -o "$fs_type" = lvm-pv ] && fs_label=default #TODO. implement the incrementing numbers label for lvm vg's and lv's
ret=0
#TODO: health checks on $fs_params etc
case ${fs_type} in #TODO: implement label, opts etc decently
xfs) mkfs.xfs -f $part $opts >$LOG 2>&1; ret=$? ;;
jfs) yes | mkfs.jfs $part $opts >$LOG 2>&1; ret=$? ;;
reiserfs) yes | mkreiserfs $part $opts >$LOG 2>&1; ret=$? ;;
ext2) mke2fs "$part" $opts >$LOG 2>&1; ret=$? ;;
ext3) mke2fs -j $part $opts >$LOG 2>&1; ret=$? ;;
ext4) mkfs.ext4 $part $opts >$LOG 2>&1; ret=$? ;; #TODO: installer.git uses mke2fs -t ext4 -O dir_index,extent,uninit_bg , which is best?
vfat) mkfs.vfat $part $opts >$LOG 2>&1; ret=$? ;;
swap) mkswap $part $opts >$LOG 2>&1; ret=$? ;;
dm_crypt) [ -z "$fs_params" ] && fs_params='-c aes-xts-plain -y -s 512';
fs_params=${fs_params//_/ }
cryptsetup $fs_params $opts luksFormat -q $part >$LOG 2>&1 < /dev/tty ; ret=$? #hack to give cryptsetup the approriate stdin. keep in mind we're in a loop (see process_filesystems where something else is on stdin)
cryptsetup luksOpen $part $fs_label >$LOG 2>&1 < /dev/tty; ret=$? || ( show_warning 'cryptsetup' "Error luksOpening $part on /dev/mapper/$fs_label" ) ;;
lvm-pv) pvcreate $fs_opts $part >$LOG 2>&1; ret=$? ;;
lvm-vg) # $fs_params: ':'-separated list of PV's
vgcreate $fs_opts $fs_label ${fs_params//:/ } >$LOG 2>&1; ret=$? ;;
lvm-lv) # $fs_params = size string (eg '5G')
lvcreate -L $fs_params $fs_opts -n $fs_label `sed 's#/dev/mapper/##' <<< $part` >$LOG 2>&1; ret=$? ;; #$opts is usually something like -L 10G # Strip '/dev/mapper/' part because device file may not exist. TODO: do i need to activate them?
# don't handle anything else here, we will error later
esac
BLOCK_ROLLBACK_USELESS=0
[ "$ret" -gt 0 ] && ( show_warning "process_filesystem error" "Error creating filesystem $fs_type on $part." ; return 1 )
sleep 2
fi
# Mount it, if requested. Note that it's your responsability to figure out if you want this or not before calling me. This will only work for 'raw' filesystems (ext,reiser,xfs, swap etc. not lvm stuff,dm_crypt etc)
if [ "$fs_mount" = runtime -o "$fs_mount" = target ]
then
BLOCK_ROLLBACK_USELESS=0
if [ "$fs_type" = swap ]
then
debug 'FS' "swaponning $part"
swapon $part >$LOG 2>&1 || ( show_warning 'Swapon' "Error activating swap: swapon $part" ; return 1 )
else
[ "$fs_mount" = runtime ] && dst=$fs_mountpoint
[ "$fs_mount" = target ] && dst=$var_TARGET_DIR$fs_mountpoint
debug 'FS' "mounting $part on $dst"
mkdir -p $dst &>/dev/null # directories may or may not already exist
mount -t $fs_type $part $dst >$LOG 2>&1 || ( show_warning 'Mount' "Error mounting $part on $dst" ; return 1 )
if [ "$fs_mount" = target -a $fs_mountpoint = '/' ]
then
PART_ROOT=$part
fi
fi
fi
# Add to temp fstab, if not already there.
if [ -n "$fs_mountpoint" -a "$fs_mount" = target ]
then
local _uuid="$(getuuid $part)"
if [ -n "${_uuid}" ]; then
_device="UUID=${_uuid}"
fi
if ! grep -q "$part $fs_mountpoint $fs_type defaults 0 " $TMP_FSTAB 2>/dev/null #$TMP_FSTAB may not exist yet
then
echo -n "$part $fs_mountpoint $fs_type defaults 0 " >> $TMP_FSTAB
if [ "$FSTYPE" = "swap" ]; then
echo "0" >>$TMP_FSTAB
else
echo "1" >>$TMP_FSTAB
fi
fi
fi
return 0
#TODO: if target has LVM volumes, copy /etc/lvm/backup to /etc on target (or maybe it can be regenerated with a command, i should look that up)
}
# $1 filesystem type
get_filesystem_program ()
{
[ -z "$1" ] && die_error "get_filesystem_program needs a filesystem id as \$1"
[ $1 = swap ] && echo mkswap && return 0
[ $1 = ext2 ] && echo mkfs.ext2 && return 0
[ $1 = ext3 ] && echo mkfs.ext3 && return 0
[ $1 = ext4 ] && echo mkfs.ext4 && return 0
[ $1 = reiserfs ] && echo mkreiserfs && return 0
[ $1 = xfs ] && echo mkfs.xfs && return 0
[ $1 = jfs ] && echo mkfs.jfs && return 0
[ $1 = vfat ] && echo mkfs.vfat && return 0
[ $1 = lvm-pv ] && echo pvcreate && return 0
[ $1 = lvm-vg ] && echo vgcreate && return 0
[ $1 = lvm-lv ] && echo lvcreate && return 0
[ $1 = dm_crypt ] && echo cryptsetup && return 0
return 1
}
# $1 blockdevice
# $2 unit: B, KiB, kB, MiB, MB, GiB or GB. defaults to B (we follow IEEE 1541-2002 )
# output will be in $BLOCKDEVICE_SIZE
# WARNING: hdparm works - by design - only for ide/sata. not scsi et al
get_blockdevice_size ()
{
[ -b "$1" ] || die_error "get_blockdevice_size needs a blockdevice as \$1 ($1 given)"
unit=${2:-B}
allowed_units=(B KiB kB MiB MB GiB GB)
if ! is_in $unit "${allowed_units[@]}"
then
die_error "Unrecognized unit $unit!"
fi
# NOTES about older, deprecated methods:
# - BLOCKDEVICE_SIZE=$(hdparm -I $1 | grep -F '1000*1000' | sed "s/^.*:[ \t]*\([0-9]*\) MBytes.*$/\1/") # if you do this on a partition, you get the size of the entire disk ! + hdparm only supports sata and ide. not scsi.
# - unreliable method: on some interwebs they say 1 block = 512B, on other internets they say 1 block = 1kiB. 1kiB seemed to work for me.
# blocks=`fdisk -s $1` || show_warning "Fdisk problem" "Something failed when trying to do fdisk -s $1"
# BLOCKDEVICE_SIZE=$(($blocks/1024))
#
bytes=$((`fdisk -l $1 2>/dev/null | sed -n '2p' | cut -d' ' -f5`))
[ $unit = B ] && BLOCKDEVICE_SIZE=$bytes
[ $unit = KiB ] && BLOCKDEVICE_SIZE=$(($bytes/2*10)) # /1024
[ $unit = kB ] && BLOCKDEVICE_SIZE=$(($bytes/10**3)) # /1000
[ $unit = MiB ] && BLOCKDEVICE_SIZE=$(($bytes/2*20)) # ...
[ $unit = MB ] && BLOCKDEVICE_SIZE=$(($bytes/10**6))
[ $unit = GiB ] && BLOCKDEVICE_SIZE=$(($bytes/2*30))
[ $unit = GB ] && BLOCKDEVICE_SIZE=$(($bytes/10**9))
}
|