File: bootcd2disk.lib

package info (click to toggle)
bootcd 6.9.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 520 kB
  • sloc: sh: 1,940; makefile: 49
file content (1550 lines) | stat: -rw-r--r-- 46,891 bytes parent folder | download
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
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
# vim: set filetype=sh :
#        file: /usr/share/bootcd/bootcd2disk.lib
#   copyright: Bernd Schumacher <bernd.schumacher@hpe.com> (2001-2020)
#     license: GNU General Public License, version 3
# description: bootcd2disk.lib - functions needed by bootcd2disk

todo_domount()
{
  eval "$ia_init"
  info "--- mounting DISK ---"
  ia_add "dbg \"EFIBOOT=<$EFIBOOT> COPYDEST=<$COPYDEST>\""
  ia_add "mkdir -p \"$COPYDEST\""
  ia_ignore "^fsck from util-linux \S+$"
  ia_ignore "^fsck.fat [0-9.]+ \([0-9-]+\)$"
  ia_ignore "^/dev/\S+: [0-9]+ files, [0-9/]+ clusters$"
  ia_add "bootcd_mount \"$COPYDEST\""
  ia -c
}

todo_dounmount()
{
  eval "$ia_init"
  info "--- unmounting DISK ---"
  ia_add "bootcd_umount \"$COPYDEST\""
  ia -c
}

# useoriginaldisk - sets DISK0,DISK1,... to ORIG_DISK0,ORIG_DISK1,...
useoriginaldisk()
{
  if [ "$use_originaldiskname" ]; then
    for i in $DISKVARS; do
      eval "DISK$i=\$ORIG_DISK$i"
      dbg "$(eval echo "useoriginaldisk: DISK$i=\$ORIG_DISK$i")"
    done
  fi
}

# getdata - print without empty lines and comments
getdata()
{
  echo "$*" | sed "s/#.*$//" | grep -v "^$"
}

# zero_dsk <dev1> [<dev2> ...]
zero_dsk()
{
  # TODO try wipefs or option -w/-W in sfdisk
  local DELPV
  local dsk

  eval "$ia_init"
  DELPV="$@"

  ia_add "info \"--- Destroy old disk infos $DELPV ---\""
  for dsk in $DELPV; do
    ia_add "info \"zero_dsk \\\"dd if=/dev/zero of=$dsk bs=1k count=1 conv=notrunc\\\"\""
    ia_ignore "^[[:digit:]]+\+[[:digit:]]+ records in$"
    ia_ignore "^[[:digit:]]+\+[[:digit:]]+ records out$"
    ia_ignore "^[[:digit:]]+ bytes\>.*\<copied\>"
    ia_add "dd if=/dev/zero of=$dsk bs=1k count=1 conv=notrunc"
  done
  ia -c
}

# seems not to be used anymore
## run_zerodisk before we have done anything else, if necessarry or requested
#run_zerodisk()
#{
#  local d
#
#  eval "$ia_init"
#  [ "$LVMGRP" -a "$LVMVOL" ] && ia_add "lvmreset # deactivate lvm, delete lvm info on disk"
#
#  if [ "$zerodsk" -o "$(unreadable)" ]; then
#    for i in $DISKVARS; do
#      d="$(eval "echo \"\$DISK$i\"")"
#      ia_nocheck
#      ia_add "zero_dsk <-i> $d"
#    done
#  fi
#
#  ia_nocheck; ia_add "ask_for_reboot # (if neccessary)"
#  ia -c
#}

lvmreset()
{
  eval "$ia_init"

  # delete all pvdisplay | grep "PV Name"
  ALLPV="$(pvdisplay -c 2>/dev/null |
    sed -n "s|[[:blank:]]*\(/dev/[^:]*\):.*|\1|p")"

  ia_add "info \"All physical Volumes ALLPV=<$ALLPV>\""

  for i in $DISKVARS; do
    # Search for old Partitions on reused disks
    DELPV="$(echo "$ALLPV" | grep "^$(eval "echo \"\$DISK$i\"")")"
    # All on one line
    DELPV="$(echo $DELPV|gawk '{printf("%s ",$0)}')"

    if [ "$(echo "$DELPV" | grep "[[:alnum:]]")" ]; then
      # Try to use pvremove first (ignore anything, only write to log)
      ia_ignore ".*"
      ia_add "pvremove -ff -y $DELPV"
    fi

    # Add the disk itself
    DELPV="$DELPV $(eval "echo \"\$DISK$i\"")"

    ia_add "zero_dsk <-i> $DELPV"
  done
  ia -c
}

unreadable()
{
  local err unreadable d
  unreadable=""
  for i in $DISKVARS; do
    d=$(eval "echo \"\$DISK$i\"")
    # Sometimes it just takes longer
    for j in 1 2 3 4 5 6 7; do
      err="$(sfdisk -R $d 2>&1)"
      if [ "$(echo "$err" | grep "Device or resource busy")" ]; then
        if [ $j -eq 7 ]; then
          echo "$d was not readable ($j); giving up" >&2
          unreadable="=== begin output of \"sfdisk -R $d\" ===
$err
=== end output of \"sfdisk -R $d\" ==="
          break 2
        else
          sleep 1
          echo "$d was not readable ($j); retrying" >&2
        fi
      else
        if [ $j -ne 1 ]; then
          echo "$d is now readable ($j); ok" >&2
        fi
        break
      fi
    done
  done
  echo "$unreadable"
}


ask_for_reboot()
{
  local err
  err="$(unreadable)"
  if [ "$err" ]; then
    echo "$err"
    echo "Rereading the partition says \"Device or resource busy\""
    echo "The partition tabel is now deleted."
    echo "After reboot bootcd2disk should work!"
    echo " ------------------------ "
    echo "( Press RETURN to reboot )"
    echo " ------------------------ "
    read dummy
    reboot
    exit
  fi
}

partdisk()
{
  # run_2disk-global DISKVARS_PARTI
  local d1 d2 p s o i
  eval "$ia_init"


  for i in $DISKVARS_PARTI; do
    d1="$(eval "echo \"\$DISK$i\"")" # exp: d1="/dev/sda"
    if [ ! -b "$d1" ]; then
      if [ "$(echo " $ORDER_TODO " | grep " parti ")" ]; then
        continue
      else
        err "no disk <$d1> in the last run of partdisk"
      fi
    fi
    DISKVARS_PARTI="$(echo " $DISKVARS_PARTI " | sed -e "s/ $i //" -e "s/^ //" -e "s/ $//")"
    d2="$(echo "$d1" | sed "s|.*/||")" # exp: d2="sda"
    p="$(cat /proc/partitions |
      sed -E -n -e "s/^.* ($d2\S\S*)$/\/dev\/\1/p" |
      tr "\n" " ")" # exp: p="/dev/sda1 /dev/sda2"
    # make sure we only add partitions, not the full disk
    # if the disk is added: we may get the following error on some hardware
    # raids, if we try to create a filesystem on any partition:
    # "node_size (128) * inodes_count (0) too big for a filesystem with 0 blocks"
    ia_nocheck
    ia_add "zero_dsk <-i> $p"
    ia_ignore "^exit=1$" # next line may exit with 1, if no partitions exist
    ia_add "sfdisk -q --delete --wipe always $d1"

    s=$(eval "echo \"\$SFDISK$i\"")
    o=$(eval "echo \"\$SFDISKOPTS$i\"")
    if [ "$s" ]; then
      ia_add "info \"--- Partitioning DISK$i ($d1) ---\""
      ia_ignore "unrecognized partition table type"
      ia_ignore "Partition .* contains a .* signature.$"
      ia_ignore "^sfdisk: ERROR: sector .* does not have an msdos signature"
      ia_ignore "^Re-reading the partition table failed"
      ia_ignore "^No partitions found"
      ia_ignore "^$"
      ia_add "echo \"$s\" | sfdisk $o --force -uS -q $d1 >/dev/null"
    fi
  done
  # Maybe we have to Reboot now, because of
  #   BLKRRPART: Device or resource busy
  #   The command to re-read the partition table failed
  #   Reboot your system now, before using mkfs
  # so first do a sync
  ia_add "sync"
  [ -x /bin/udevadm ] && ia_add "/bin/udevadm settle # wait until new device files from sfdisk are available"
  ia_nocheck && ia_add "ask_for_reboot2 # (if neccessary)"
  ia -c
}

# We have systems even at this point where reboot was needed
# But this should be rarely needed
ask_for_reboot2()
{
  local err
  err="$(unreadable)"
  if [ "$err" ]; then
    echo "$err"
    echo "Rereading the partition after partitioning says \"Device or resource busy\""
    echo "The partition tabel is now created as needed."
    echo "After reboot bootcd2disk should work!"
    echo " ------------------------ "
    echo "( Press RETURN to reboot )"
    echo " ------------------------ "
    read dummy
    reboot
    exit
  fi
}

mddisk()
{
  local mddev
  local level
  local dsks
  local cmd

  eval "$ia_init"
  ia_add "info \"--- mddisk ---\""

  # while ... gives a subshell, to set vars we have to eval the set commands
  eval "$(
    getdata "$MD" |
    while read mddev level dsks; do
      dsks="$(echo "$dsks" |sed "s/mdadm.*//")"
      cat <<END
ia_nocheck
ia_add "zero_dsk <-i> $dsks"
END
    done
  )"

  # while ... gives a subshell, to set vars we have to eval the set commands
  eval "$(
    getdata "$MD" |
    while read mddev level dsks; do
      dsks="$(echo "$dsks" |sed "s/mdadm.*//")"
      nr="$(echo "$dsks"|wc -w)"
      cmd="$(echo "$dsks"|sed -n "s/.*\(mdadm.*\)/\1/p")"
      [ "$cmd" ] || cmd="echo y | mdadm --create $mddev --name=$mddev --homehost=any --level=$level --raid-devices=$nr --quiet --force $dsks"
      cat <<END
ia_ignore "mdadm: array .* started"
ia_ignore "^mdadm: .* appears to contain an ext2fs file system"
ia_ignore "^[[:space:]]+ size="
ia_ignore "^mdadm: .* appears to be part of a raid array"
ia_ignore "^[[:space:]]+ level="
ia_ignore "^Continue creating array?"
ia_add "$cmd"
END
    done
  )"
  ia -c
}

lvm_pvcreate()
{
  local grp
  local dsks
  local dsk
  eval "$ia_init"
  ia_add "info \"--- pvcreate ---\""
  ia_add "dbg \"LVMGRP=<$<LVMGRP>>\""

  # while ... gives a subshell, to set vars we have to eval the set commands
  eval "$(
    getdata "$LVMGRP" | sed "s/vgcreate.*//" | while read grp dsks; do
      for dsk in $dsks; do
        # After Disk Partitioning, there could be new places with old
        # LVM Info, so we delete it with pvremove and dd before running pvcreate
        cat <<END
ia_ignore ".*"
ia_add "pvremove -ff -y $dsk"

ia_ignore "WARNING: Forcing physical volume creation on .* of volume group \".*\""
ia_ignore "^[[:digit:]]+\+[[:digit:]]+ records in$"
ia_ignore "^[[:digit:]]+\+[[:digit:]]+ records out$"
ia_ignore "^[[:digit:]]+ bytes\>.*\<copied\>"
ia_add "dd if=/dev/zero of=$dsk bs=1k count=1 conv=notrunc"

ia_add "dbg \"pvcreate --quiet -d -ff -y $dsk\""
ia_ignore "WARNING: Forcing physical volume creation on .* of volume group \".*\""
ia_ignore "Physical volume \".*\" successfully created"
ia_ignore "Writing physical volume data to disk \".*\""
ia_ignore "^File descriptor [0-9]+ \(\S+\) leaked on"
ia_add "pvcreate --quiet -d -ff -y $dsk"
END
      done
    done
  )"
  ia -c
}

lvm_vgcreate()
{
  local cmd
  local grp
  local dsks
  eval "$ia_init"
  ia_add "info \"--- vgcreate ---\""

  # while ... gives a subshell, to set vars we have to eval the set commands
  eval "$(
    getdata "$LVMGRP" |
    while read grp dsks; do
      cmd="$(echo "$dsks"|sed "s/.*\(vgcreate.*\)/\1/"|grep vgcreate)"
      [ "$cmd" ] || cmd="vgcreate --quiet $grp $dsks"
        cat <<END
ia_ignore "^File descriptor [0-9]+ \(\S+\) leaked on"
ia_ignore "Volume group \".*\" successfully created"
ia_add "$cmd"

ia_ignore "^File descriptor [0-9]+ \(\S+\) leaked on"
ia_ignore "^\s*0 logical volume\(s\) in volume group \".*\" now active"
ia_add "vgchange --quiet -aey $grp"
END
    done
  )"

  ia -c
}

lvm_lvcreate()
{
  local cmd
  local vol
  local s
  local grp
  eval "$ia_init"
  ia_add "info \"--- lvcreate ---\""

  # while ... gives a subshell, to set vars we have to eval the set commands
  eval "$(
    getdata "$LVMVOL" |
    while read vol s grp cmd; do
      [ "$cmd" ] || cmd="lvcreate --yes -n $vol -L $s $grp"

      cat <<END
ia_ignore "^File descriptor [0-9]+ \(\S+\) leaked on"
ia_ignore "Logical volume \".*\" created"
ia_ignore "/dev/cdrom: open failed: Read-only file system$"
ia_stdout "Rounding up size to full physical extent"
ia_add "$cmd"
END
    done
  )"

  ia -c
}

lvm_vgchange()
{
  eval "$ia_init"
  ia_add "info \"--- Activate LVM ---\""

  ia_ignore "^File descriptor [0-9]+ \(\S+\) leaked on"
  ia_ignore "Reading all physical volumes.  This may take a while..."
  ia_ignore "Found volume group \".*\" using metadata type lvm2"
  ia_ignore "/dev/cdrom: open failed: Read-only file system"
  ia_ignore "Attempt to close device '/dev/cdrom' which is not open."
  ia_add "vgscan --quiet"

  ia_ignore "^File descriptor [0-9]+ \(\S+\) leaked on"
  ia_ignore "^\s*[1-9][0-9]* logical volume\(s\) in volume group \".*\" now active"
  ia_add "vgchange --quiet -ay"

  ia -c
}

mkfs()
{
  local i
  local l
  eval "$ia_init"

  ia_add "[ \"$EXT2FS$EXT3FS$EXT4FS$VFAT$XFS\" ] || return 0"

  for i in $EXT2FS; do
    l="$(echo "$PARTITIONLABEL" | grep "^$i:" | sed "s|.*:||")"
    [ "$l" ] && l="-L $l"
    info "--- Building Filesystem $i with mke2fs -F -t ext2 $l ---"
    ia_stdout "^mke2fs "
    ia_stdout "^Found a \S+ partition table"
    ia_add "mke2fs -F -t ext2 $l -q $i"
  done

  for i in $EXT3FS; do
    l="$(echo "$PARTITIONLABEL" | grep "^$i:" | sed "s|.*:||")"
    [ "$l" ] && l="-L $l"
    info "--- Building Filesystem $i with mke2fs -F -t ext3 -j $l ---"
    ia_stdout "^mke2fs "
    ia_stdout "^Found a \S+ partition table"
    ia_add "mke2fs -F -t ext3 -j $l -q $i"
  done

  for i in $EXT4FS; do
    l="$(echo "$PARTITIONLABEL" | grep "^$i:" | sed "s|.*:||")"
    [ "$l" ] && l="-L $l"
    ia_add "info \"--- Building Filesystem $i with mke2fs -F -t ext4 $l ---\""
    ia_stdout "^mke2fs "
    ia_stdout "^Found a \S+ partition table"
    ia_add "mke2fs -F -t ext4 $l -q $i"
  done

  for i in $VFAT; do
    info "--- Building FAT Filesystem $i ---"
    ia_ignore "^mkfs.fat [0-9.]+ \([0-9-]+\)$"
    ia_stdout "^Found a \S+ partition table"
    ia_add "mkfs.fat -F32 $i"
  done

  for i in $XFS; do
    l="$(echo "$PARTITIONLABEL" | grep "^$i:" | sed "s|.*:||")"
    [ "$l" ] && l="-L $l"
    info "--- Building XFS Filesystem $i with mkfs.xfs $l ---"
    ia_add "mkfs.xfs $l -q $i"
  done

  ia -c
}


# resume_fix <root>
resume_fix()
{
  local root
  local f
  root="$1"

  RESUME="$(real2uuid "$RESUME")" || exit $?
  dbglog "resume_fix RESUME=<$RESUME>"
  for f in \
    $root/etc/initramfs-tools/conf.d/resume
  do
    [ -e "$f" ] && echo "RESUME=$RESUME" > $f && break
  done
  return 0
}

# use image from a image server and copy it to the later swap-partion
# the partition must be large enough to hold the whole disk image
copyfromserver_before()
{
  eval "$ia_init"
  # timeout for dns-query and connection timeout (seconds)
  local timeout=30
  local url

  ia_add "info \"--- copying image from image server ---\""
  ia_stdout "^mke2fs "
  ia_stdout "^Found a \S+ partition table"
  ia_add "mke2fs -F -q $SWAP"

  ia_add "mkdir -p /tmp/swapfs"

  ia_add "mount $SWAP /tmp/swapfs"

  if [ "$(echo "$IMAGEURL" | grep "^file://")" ]; then
    url="$(echo "$IMAGEURL" | sed "s|^file://\(.*\)$|\1|")"
    if [ -b "$url" ]; then
      ia_stdout "records in$"
      ia_stdout "records out$"
      ia_stdout "bytes .* copied"
      ia_add "dd if=$url of=/tmp/swapfs/cdimage.iso"
    elif [ -f "$url" ]; then
      ia_add "cp $url /tmp/swapfs/cdimage.iso"
    else
      ia_add "info \"No file <$url>\""
      exit 3
    fi
  else
    ia_stdout "^--[[:digit:]:]*--.*"
    ia_stdout "^[[:space:]]*=> .*"
    ia_stdout "^Resolving .*"
    ia_stdout "^Connecting .*"
    ia_stdout "^Logging .*"
    ia_stdout "^==> SYST .*"
    ia_stdout "^==> TYPE .*"
    ia_stdout "^==> PASV .*"
    ia_stdout "^Length: .*"
    ia_stdout "^[[:space:]]*OK.*"
    ia_stdout "^Length .*"
    ia_stdout "^$"
    ia_stdout "^[[:digit:]:]* .*"
    ia_stdout "^[[:space:]]*[[:digit:]:K]* .*"
    ia_stdout "^HTTP request sent, .* 200 OK[[:space:]]*$"
    ia_stdout "^==> SIZE "
    ia_stdout "^Saving to: ./tmp/swapfs/cdimage.iso.*"
    ia_stdout "^.*/tmp/swapfs/cdimage.iso. saved.*"
    ia_stdout "^--....-..-.. ..:..:..-- .*$IMAGEURL" # IMAGEURL without http:// can be prefixed with http://
    ia_stdout "Reusing existing connection"
    ia_add "wget --no-check-certificate --dns-timeout=$timeout --connect-timeout=$timeout -O /tmp/swapfs/cdimage.iso $IMAGEURL"
  fi

  ia_add "mkdir -p $ISOLOOPBACK"

  ia_stdout "^mount: warning: /tmp/isoloopback seems to be mounted read-only\.$"
  ia_stdout "^mount: .* is write-protected, mounting read-only$"
  ia_add "mount -o loop /tmp/swapfs/cdimage.iso $ISOLOOPBACK"

  ia -c
}

handling_udev()
{
  eval "$ia_init"
  echo "--- handling udev ---"
  # -- udev --
  # The filesystem has static device files in /dev.
  # But tmpfs is mounted over /dev and hides the static device files.
  # The static device files are mounted again in /dev/.static/dev/.
  # This means bootcd has only to copy /dev/.static/dev.
  ia_add "cp -a -x /ram1/dev/.static/dev $COPYDEST"
  ia_add "mkdir -p $COPYDEST/dev/.static"
  ia_add "chown root:root $COPYDEST/dev/.static"
  ia_add "chmod 700 $COPYDEST/dev/.static"
  ia -c
}

# calc_rsync_exclude <EXCLUDE_SYSTEM> <EXCLUDE>
#   INPUT: EXCLUDE_SYSTEM EXCLUDE
calc_rsync_exclude()
{
  local EX i
  eval "set $EXCLUDE_SYSTEM $EXCLUDE"
  EX=""
  while [ $# -gt 0 ]; do
    i="$(/bin/echo "$1" | ia_easy_backslash)"
    shift
    [ "$EX" = "" ] && EX="--exclude \"$i\"" || EX="$EX --exclude \"$i\""
  done
  /bin/echo "$EX"
}
#lib_dev_test() # tests calc_rsync_exclude
#{
#  local res exp0 exp f
#  EXCLUDE_SYSTEM="/dev/ /mnt/ /proc/ /run/ /sys/ /tmp/ /var/tmp/"
#  exp0="--exclude \"/dev/\" --exclude \"/mnt/\" --exclude \"/proc/\" --exclude \"/run/\" --exclude \"/sys/\" --exclude \"/tmp/\" --exclude \"/var/tmp/\""
#
#  f="calc_rsync_exclude"
#  EXCLUDE=""; res="$($f)"
#  exp="$exp0"
#  [ "$res" = "$exp" ] && echo "OK $f-1" || echo "ERR $f-1 exp=<$exp> res=<$res>"
#  EXCLUDE="/home/u1 /srv" ; res="$($f)"
#  exp="$exp0 --exclude \"/home/u1\" --exclude \"/srv\""
#  [ "$res" = "$exp" ] && echo "OK $f-2" || echo "ERR $f-2 exp=<$exp> res=<$res>"
#  EXCLUDE="\"/home/a b/c\" /srv/x\ y /s1/\* \"/s2/*\"" ; res="$($f)"
#  exp="$exp0 --exclude \"/home/a b/c\" --exclude \"/srv/x y\" --exclude \"/s1/*\" --exclude \"/s2/*\""
#  [ "$res" = "$exp" ] && echo "OK $f-3" || echo "ERR $f-3 exp=<$exp> res=<$res>"
#}

# calc_slashed_dirs - copy excluded dirs ending with slash, without files
calc_slashed_dirs()
{
  local i
  eval "set $EXCLUDE_SYSTEM $EXCLUDE"
  while [ $# -gt 0 ]; do
    if [ "$(/bin/echo "$1" | grep "/$")" ]; then # file ends with /
      i="$(/bin/echo "$1" | ia_easy_backslash)"
      if [ -d "${ISOLOOPBACK}$i" ]; then # file is a directory
        echo "ia_add \"$(echo "install -d $(stat -c "-o %u -g %d -m %a" \
          "${ISOLOOPBACK}$i") \"${COPYDEST}$i\"" | ia_easy_backslash)\""
      fi
    fi
    shift
  done
}

# retry_rsync <rsync-options>
retry_rsync()
{
  local ex
  for i in $(seq 3); do
    rsync "$@"
    ex=$?
    [ $ex -eq 0 ] && break
    echo "retry_rsync: rsync-exit-code=<$ex> => trying again"
  done
  return $ex
}

# create_target_mountpoints <target>
#   Because rsync is used with option --delete,
#   rsync may also try to delete mounted directories on <target>.
#   But if such a directory has a mounted filesystem, this will fail.
#   This function returns a tmp directory with mountpoints, found on <target>.
#   e.g: boot/efi
create_target_mountpoints()
{
  local target i mp
  target="$(echo "$1" | sed "s|/*$||g")" # whithout ending /
  mp="$(mktemp -d)"
  for i in $(mount | sed -E -n -e "s|\S+ \S+ $target/(\S+) .*|\1|p"); do
    mkdir -p $mp/$i
  done
  echo "$mp"
}

copycdram()
{
  local rtm
  eval "$ia_init"
  ia_add "info \"--- copying CD and RAM to $COPYDEST ---\""
  ia_add "target_mp=\$(create_target_mountpoints \"$COPYDEST\")"
  ia_stdout "^file has vanished:"
  ia_stdout "^rsync warning:"
  ia_stdout "^retry_rsync: .* trying again$"
  ia_add "retry_rsync -a --xattrs --sparse --hard-links --delete --delete-excluded \
    --ignore-errors $(calc_rsync_exclude) \"$ISOLOOPBACK/\" \"$<target_mp>/\" \"$COPYDEST\""
  ia_add "rm -rf $<target_mp>"
  eval "$(calc_slashed_dirs)"
  ia_add "rm -rf $COPYDEST/rr_moved $COPYDEST/isolinux $COPYDEST/EFI"
  ia_add "rm -f $COPYDEST/usr/bin/bootcdflopcp \
    $COPYDEST/etc/bootcd/thisbootcd.conf"
  # restore /etc/securetty
  ia_add "if [ -f  $COPYDEST/etc/securetty ]; then
    cat $COPYDEST/etc/securetty | gawk 'BEGIN {cut=0}
      /added by bootcdwrite BEGIN/ { cut=1 }
      {if (cut==0) {print}}
      /added by bootcdwrite END/ {cut=0}' >$COPYDEST/etc/securetty.tmp
    mv $COPYDEST/etc/securetty.tmp $COPYDEST/etc/securetty
  fi"
  ia -c
}

# configure the swap-partition back to swap
give_swappartition_free()
{
  eval "$ia_init"
  # timeout for dns-query and connection timeout (seconds)
  local timeout=30

  ia_add "info \"--- copying image from image server ---\""

  # cleanup again
  ia_ignore "loop: can.t delete device /dev/loop"
  ia_add "umount -d $ISOLOOPBACK"
# ia_add "losetup -d /dev/loop0 # free loopdevice"
  ia_add "umount /tmp/swapfs"

  # rebuild swap
  ia_nocheck
  ia_add swap

  ia -c
}

swap()
{
  eval "$ia_init"
  for i in $SWAP; do
    l="$(echo "$PARTITIONLABEL" | grep "^$i:" | sed "s|.*:||")"
    [ "$l" ] && l="-L $l"
    ia_add "info \"--- making SWAP $i with mkswap $l ---\""
    ia_ignore "^Setting up swapspace"
    ia_ignore "^no label, "
    ia_ignore "^LABEL="
    ia_ignore "^mkswap: .* wiping old \S+ signature\.$"
    # we need option -f for mkswap on logical volumes, to prevent the warning
    #    mkswap: /dev/mapper/vg_rh6-lv_swap: warning:
    #            don't erase bootbits sectors
    #            on whole disk. Use -f to force.
    ia_add "mkswap -f $l $i"
  done

  ia -c
}

do_fstab()
{
  if [ "$FSTAB" = "unchanged" ]; then
    info "--- Will let fstab unchanged ---"
  else
    info "--- Building fstab ---"
    dbglog "do_fstab: Input: FSTAB=<$FSTAB>"
    FSTAB="$(real2uuid "$FSTAB")" || exit $?
    dbglog "do_fstab: Output: FSTAB=<$FSTAB>"
    echo "$FSTAB" > $COPYDEST/etc/fstab
  fi
}

bootloadercfg()
{
  if [ "$GRUB2" = "unchanged" ]; then
    dbglog "bootloadercfg GRUB2=<$GRUB2>"
    info "--- Will let grub.cfg unchanged ---"
  elif [ "$GRUB2" = "auto" ]; then
    info "--- Running update-grub ---"
    chroot $COPYDEST update-grub
    # Next line fixes a problem seen in sid
    chroot $COPYDEST umount grub-mount >/dev/null 2>&1;:
  elif [ "$GRUB2" ]; then
    GRUB2="$(real2uuid "$GRUB2")" || exit $?
    dbglog "bootloadercfg GRUB2=<$GRUB2>"

    info "--- Building $grubdir/grub.cfg ---"
    mkdir -p $COPYDEST/$grubdir
    echo "$GRUB2" > $COPYDEST/$grubdir/grub.cfg
  fi

  if [ "$GRUB" = "unchanged" ]; then
    dbglog "bootloadercfg GRUB=<$GRUB>"
    info "--- Will let grub/menu.lst unchanged ---"
  elif [ "$GRUB" ]; then
    GRUB="$(real2uuid "$GRUB")" || exit $?
    dbglog "bootloadercfg GRUB=<$GRUB>"
    info "--- Building grub/menu.lst ---"
    mkdir -p $COPYDEST/$grubdir
    echo "$GRUB" > $COPYDEST/$grubdir/menu.lst
  fi

  if [ "$LILO" = "unchanged" ]; then
    info "--- Will let lilo.conf unchanged ---"
  elif [ "$LILO" ]; then
    info "--- Building lilo.conf ---"
    echo "$LILO" > $COPYDEST/etc/lilo.conf
  fi
}

run_bootloadercfg()
{
  local grubdir
  local grubcmd

  grubdir=boot/grub
  for i in boot/grub boot/grub2; do
    if [ -d $COPYDEST/$i ]; then
      grubdir=$i
      break
    fi
  done

  grubcmd=grub-install
  for i in grub-install grub2-install; do
    if [ "$(chroot $COPYDEST which $i 2>/dev/null)" ]; then
      grubcmd=$i
      break
    fi
  done

  eval "$ia_init"
  # Adding boot menu entry for EFI firmware configuration"
  # Adding boot menu entry for UEFI Firmware Settings ...
  ia_add "dbg \"run_bootloadercfg: grubdir=<$grubdir> grubcmd=<$grubcmd>\""
  ia_ignore "^Generating grub"
  ia_ignore "^Found "
  ia_ignore "^File descriptor [0-9]+ \(\S+\) leaked on"
  ia_ignore "^  /dev\S+: open failed: No medium found$"
  ia_ignore "^  WARNING: Failed to connect to lvmetad\. Falling back to device scanning\.$"
  ia_ignore "^done$"
  ia_ignore "^Adding boot menu entry for "
  ia_ignore "^  WARNING: Device \S+ not initialized in udev database even after waiting \S+ microseconds\.$"
  ia_stdout "cannot find a GRUB drive for \S+\.\s+Check your device\.map\."

  # lines found in sid June 2022:
  # Warning: os-prober will not be executed to detect other bootable partitions.
  # Systems on them will not be added to the GRUB boot configuration.
  # Check GRUB_DISABLE_OS_PROBER documentation entry.
  # Warning: os-prober will be executed to detect other bootable partitions.
  # Its output will be used to detect bootable binaries on them and create new boot entries.
  ia_ignore "^Warning: os-prober"
  ia_ignore "will not be added to the GRUB boot configuration"
  ia_ignore "^Check GRUB_DISABLE_OS_PROBER documentation"
  ia_ignore "output will be used"

  ia_add bootloadercfg

  if [ ! "$only_copydata" ]; then
    if [ "$GRUB2" ]; then
      ia_nocheck && ia_add "dogrub2 <-i>"
    fi

    if [ "$GRUB" ]; then
      # To many text
      ia_stdout ".*"
      ia_add "dogrub1 # root ($GRUBBOOTDISK,$GRUBBOOTDIR)"
    fi

    if [ "$LILO" ]; then
      ia_stdout "^Added "
      ia_stdout "^--- .* ---$"
      ia_add dolilo
    fi
  fi

  ia -c
}

createsshkeys()
{
  eval "$ia_init"
  if [ "$SSHHOSTKEY" = "no" ]; then
    ia_add "info \"--- Will let ssh keys unchanged ---\""
  elif [ "$SSHHOSTKEY" = "yes" ]; then
    # each installed PC gets a unique hostkey (only if hostkey already existed)
    ia_nocheck
    ia_add "create_host_keys <-i> $COPYDEST/etc/ssh"
  else
    err "SSHHOSTKEY=<$SSHOSTKEY> has to be defined as \"yes\" or \"no\"."
  fi
  ia -c
}

enablecron()
{
  info "--- Enabling turned off stuff ---"
  eval "set -- $DISABLE_CRON"
  for f in "$@"; do
    if [ -f "$COPYDEST/$f.no_run_on_bootcd" ]; then
      rm "$COPYDEST/$f"
      mv "$COPYDEST/$f.no_run_on_bootcd" "$COPYDEST/$f"
    fi
  done
}

dogrub2_ignore()
{
  ia_ignore "^Installing for (i386-pc|x86_64-efi) platform\.$"
  ia_ignore "^Installation finished\. No error reported\.$"
  ia_ignore "^GUID Partition Table Header signature is wrong: [a-f0-9]+ != [a-f0-9]+$"
  ia_ignore "^grub-install: error: unable to identify a filesystem in \S+; safety check can't be performed\.$"
  ia_ignore "^Installation finished\. No error reported\.$"
  ia_ignore "^grub-install: warning: EFI variables are not supported on this system"
}

dogrub2()
{
  eval "$ia_init"

  if [ "$GRUBDEVICEMAP" = "auto" -o "$GRUBDEVICEMAP" = "" ]; then
    # Automatic handling.
    ia_add "rm -f $COPYDEST/$grubdir/device.map"
    ia_add "grub-mkdevicemap -m $COPYDEST/$grubdir/device.map"
  elif [ "$GRUBDEVICEMAP" = "no" ]; then
    # Do nothing.
    # We do not change device.map
    # This should work if a bootcd is installed on the original hardware
    :
  elif [ "$GRUBDEVICEMAP" ]; then
    # We explicit set a value to device.map
    # This is needed if nothing else helps
    ia_add "echo \"$GRUBDEVICEMAP\" > $COPYDEST/$grubdir/device.map"
  fi

  ia_add "[ \"$<GRUBBOOTDISK>\" ] || GRUBBOOTDISK=\"\$(sed -n \"s|^(\(\S\+\))\s\+$DISK0$|\1|p\" $COPYDEST/$grubdir/device.map)\""
  ia_add "[ \"$<GRUBBOOTDISK>\" ] || GRUBBOOTDISK=\"hd0\""

  # $grubcmd's option --force is needed to overwrite warnings.
  # For example the following not correct warning occured: "grub-install:
  # warning: Attempting to install GRUB to a disk with multiple partition
  # labels. This is not supported yet.."

  if [ "$EFIBOOT" = "bios" -o "$EFIBOOT" = "bios+efi" ]; then
    dogrub2_ignore
    ia_add "chroot $COPYDEST $grubcmd $GRUB_INSTALL_OPTS_BIOS $DISK0"
  fi

  if [ "$EFIBOOT" = "efi" -o "$EFIBOOT" = "bios+efi" ]; then
    # option --bootloader-id=debian makes sure /EFI/debian including
    # /EFI/debian/grub.cfg is created. And this path is hardcoded in
    # grubx64.efi (from /EFI/debian/grubx64.efi and from
    # /EFI/BOOT/grubx64.efi)
    ia_add "rm -rf $COPYDEST/boot/efi/EFI/debian $COPYDEST/boot/efi/EFI/BOOT"
    dogrub2_ignore
    ia_add "chroot $COPYDEST $grubcmd --bootloader-id=debian $GRUB_INSTALL_OPTS_EFI $DISK0"
    # option --removable makes sure /EFI/BOOT is recreated
    dogrub2_ignore
    ia_add "chroot $COPYDEST $grubcmd $GRUB_INSTALL_OPTS_EFI --removable $DISK0"
    # without EFI/BOOT/ it could be neccessarry to use EFI Shell to run grub:
    # Shell> map                     => ... FS0: ...
    # Shell> FS0:
    # FS0:\> ls                      => EFI
    # FSO:\> cd EFI
    # FS0:\EFI\> ls                  => debian
    # FS0:\EFI\> cd debian
    # FS0:\EFI\debian\> ls           => ... grubx64.efi ...
    # FS0:\EFI\debian\> grubx64.efi
  fi

  ia -c
}

dogrub1_chroot()
{
  info "--- Running grub1 chroot ---"
  [ -z "$<GRUBBOOTDIR>" ] && GRUBBOOTDIR=0
  [ -z "$<GRUBBOOTDISK>" ] && GRUBBOOTDISK="hd0"

  if [ "$GRUBDEVICEMAP" = "auto" -o "$GRUBDEVICEMAP" = "" ]; then
    # Automatic handling.
    rm -f $COPYDEST/$grubdir/device.map
    # grub-mkdevicemap does not work always
    # grub-mkdevicemap -m $COPYDEST/$grubdir/device.map
  elif [ "$GRUBDEVICEMAP" = "no" ]; then
    # Do nothing.
    # We do not change device.map
    # This should work if a bootcd is installed on the original hardware
    :
  elif [ "$GRUBDEVICEMAP" ]; then
    # We explicit set a value to device.map
    # This is needed if nothing else helps
    echo "$GRUBDEVICEMAP" > $COPYDEST/$grubdir/device.map
  fi

  chroot $COPYDEST grub-install --no-floppy "($GRUBBOOTDISK)"
}

prepare_grub_stage_files()
{
  info "--- Preparing grub stage files ---"
  # We need the corresponding grub files of the running kernel
  # This files should already be installed.
  # On a restored system the original files can normally be installed again,
  # with grub-install.
# if [ ! -d $COPYDEST/boot/grub.tmp ]; then
#   mkdir $COPYDEST/boot/grub.tmp
#   mv $COPYDEST/boot/grub/stage1 $COPYDEST/boot/grub/stage2 \
#      $COPYDEST/boot/grub/*stage1_5 $COPYDEST/boot/grub.tmp

    cp /usr/lib/grub/*/stage1 /usr/lib/grub/*/stage2 \
       /usr/lib/grub/*/*stage1_5 $COPYDEST/$grubdir/
# fi
}

dogrub1_manual()
{
  [ -z "$GRUBBOOTDIR" ] && GRUBBOOTDIR=0
  [ -z "$GRUBBOOTDISK" ] && GRUBBOOTDISK="hd0"

  if [ "$GRUBDEVICEMAP" = "auto" -o "$GRUBDEVICEMAP" = "" ]; then
    # Automatic handling.
    # We delete device.map. So it will then be auto-created by grub.
    # This should work on different target hardware
    rm -f $COPYDEST/$grubdir/device.map
  elif [ "$GRUBDEVICEMAP" = "no" ]; then
    # Do nothing.
    # We do not change device.map
    # This should work if a bootcd is installed on the original hardware
    :
  elif [ "$GRUBDEVICEMAP" ]; then
    # We explicit set a value to device.map
    # This is needed if nothing else helps
    echo "$GRUBDEVICEMAP" > $COPYDEST/$grubdir/device.map
  fi

  info "--- Running grub1 manual ---"
  (
    for i in $GRUBBOOTDISK; do
      echo "root ($i,$GRUBBOOTDIR)
setup ($i)"
    done
    echo "quit"
  ) | grub --no-floppy --batch --device-map=$COPYDEST/$grubdir/device.map
}

dogrub1()
{
  dogrub1_chroot
  g1_cr_err=$?

  if [ $g1_cr_err -ne 0 ]; then
    info "--- Retry manual ---"
    prepare_grub_stage_files
    dogrub1_manual
  fi
}

dolilo()
{
  info "--- Running lilo ---"
  chroot $COPYDEST lilo -w
}

# is_function_defined <name>
# if is_function_defined fun; then echo yes; else echo no; fi
# is_function_defined fun && echo yes || echo no
is_function_defined()
{
  v=""
  if [ "$1" = "-v" ]; then
    v="$1"
    shift
  fi

  ret="$(type "$1" 2>/dev/null)"

  echo "$ret" | head -1 | grep "function" >/dev/null
  err=$?

  if [ "$v" ]; then
    if [ $err -ne 0 ]; then
      echo "$1 is not a shell function"
    elif [ "$(echo "$ret" | head -1)" = "$ret" ]; then
      echo "$ret"
    else
      echo "$ret"  | tail +2
    fi
  fi

  return $err
}

needs()
{
  # global NEEDS_DONE
  # global NEEDS_WAIT
  local i
  NEEDS_WAIT=""
  if [ "$1" = "--init" ]; then
    shift
    NEEDS_DONE=""
  elif [ "$1" = "--done" ]; then
    shift
    [ "$NEEDS_DONE" ] && NEEDS_DONE="$NEEDS_DONE $*" || NEEDS_DONE="$*"
  else
    for i in $*; do
      if [ ! "$(echo " $NEEDS_DONE " | grep " $i ")" ]; then
        NEEDS_WAIT="$i"
        return 159
      fi
    done
  fi
}

# calc_options [<varsiables or functions separated by space>]
# Input:
#
# In bootcd2disk.conf <options> (variables or functions) can be created, to
# change a default <option>.
#
# It is also possible to define functions named define_<option> to do this
# later. define_<option> can call the function need to create dependencies to
# other <options>, that have to be set first.
#
# calc_options calls define_<options> in the order given by function need.
calc_options()
{
  local def_todo

  eval "$ia_init"
  if [ $# -eq 0 ]; then
     ia_add "def_all=\"\$(list_bootcd2diskconf) \$(list_thisbootcdconf)\""
  else
     ia_add "def_all=\"$*\""
  fi
  ia_add "def_todo=\"$<def_all>\""
  ia_add "needs --init"
  ia_nocheck && ia_add "while [ \"\$def_todo\" ]; do calc_options_try_all <-i> ; done"
  ia_add "useoriginaldisk # with -o: sets DISK0,DISK1,... to ORIG_DISK0,ORIG_DISK1,..."
  ia -c
}

calc_options_try_all()
{
  local i fun def_todo_old NEEDS_WAIT

  eval "$ia_init"
  def_todo_old="$def_todo"
  for i in $def_todo; do
    ia_add "calc_options_try_one <-i> \"$i\""
  done
  ia_nocheck && ia_add "[ \"$<def_todo>\" != \"$<def_todo_old>\" ] ||
    err \"calc_options_try_all: Needs $<NEEDS_WAIT> are not possible.\""
  ia -c
}

# calc_option3 <option>
calc_options_try_one()
{
  # global define_todo
  local option optionname

  eval "$ia_init"
  option="$1"
  optionname="$(echo "$option" | sed -E -e "s/(\(\)|#)$//")"
  optiontype="$(echo "$option" | sed -E -e "s/.*(\(\)|#)$/\1/")"

  if is_function_defined "define_$optionname"; then
    ia_add "define_$optionname; calc_options_result \$?; RETURN_CODE=\$?"
    ia_nocheck && ia_add "chk_RETURN_ERR"
  else
    ia_add "calc_options_result -; RETURN_CODE=\$?"
    ia_nocheck && ia_add "chk_RETURN_ERR"
  fi
  ia -c
}

# calc_options_result -|0|159|<errcode>
calc_options_result()
{
  if [ "$1" = "-" -o "$1" = "0" ]; then
    needs --done "$option"
    def_todo="$(echo " $def_todo " | sed -e "s/ $option / /" -e "s/^ //" -e "s/ $//")"
    if [ "$1" = "-" ]; then
      dbg "calc_option_result: $option done (no define_$optionname)"
    else # "$1" = "0"
      unset -f define_$optionname
      dbg "calc_option_result: $option done. (successful define_$optionname)"
    fi
    [ "$optiontype" = "()" ] || check_$optionname
  elif [ "$1" = "159" ]; then
    dbg "calc_option_result: $option waits for $NEEDS_WAIT."
  else
    echo "calc_options_result: define_$optionname returned errorcode $1."
  fi
}

#
#lib_dev_test() # tests calc_options
#{
#  list_bootcd2diskconf() { echo "f1() f2() f3()"; }
#  unset -f define_f3 f1 f2
#  define_f1() { needs "f2() f3()" || return $?; f1() { echo "f1 called with $*"; }; }
#  define_f2() { needs "f3()" || return $?; f2() { echo "f2 called with $*"; }; }
#  f3() { echo "f3 called with $*"; }
#  calc_options
#  res="f1=<$(f1 one)> f2=<$(f2 two)> f3=<$(f3 three)>"
#  exp="f1=<f1 called with one> f2=<f2 called with two> f3=<f3 called with three>"
#  [ "$res" = "$exp" ] && echo "OK calc_options-1" || echo "ERR calc_options-1 exp=<$exp> res=<$res>"
#
#  list_bootcd2diskconf() { echo "f1() v1 h1# f2() v2 h2# f3() v3 h3#"; }
#  unset -f define_f1 define_h1 define_f2 define_h2 define_f3 define_v3 define_h3 f1 f2 f3
#  unset h1 h2 h3
#  v1=""; v2=""; v3="yes"
#  define_f2() { needs "v1 v3" || return $?; [ "$v1" = "$v3" ] && f2() { echo "v1=v3=<$v1>"; } || f2() { echo "v1=<$v2> v3=<$v3>"; }; }
#  define_v1() { needs "v2" || return $?; v1="$v2"; }
#  define_v2() { needs "v3" || return $?; v2="$v3"; }
#  calc_options
#  res="f2=<$(f2)>"
#  exp="f2=<v1=v3=<yes>>"
#  [ "$res" = "$exp" ] && echo "OK calc_options-2" || echo "ERR calc_options-2 exp=<$exp> res=<$res>"
#
#  list_bootcd2diskconf() { echo "v1 v2 v3"; }
#  unset -f define_v1 define_v3
#  v1=""; v2=""; v3="";
#  define_v2() { needs "v7" || return $?; v2="$v7"; }
#  echo "expect exit because v7 is not resolvable"
#  calc_options
#
#}



run_do_first()
{
  if [ "$(typefun do_first)" ]; then
    info "--- do function do_first ---"
    do_first
  fi
}

run_do_last()
{
  if [ "$(typefun do_last)" ]; then
    info "--- do function do_last ---"
    do_last
  fi
}


# addln <var> <text>
addln()
{
  eval "[ \"\$$1\" ] && $1=\"$(echo "\$$1"; echo "$2")\" || $1=\"$2\""
}

# warn_2disk_part2 <err> <msg>
warn_2disk_part2()
{
  local msg
  local err
  local A

  err="$1"
  msg="$2"

  if [ "$err" ]; then
    addln msg "$err"
    err "$msg"
    exit 1
  elif [ "$yes" ]; then
    log "$msg"
    A="y"
  else
    A="$(ia_ask "$msg" "" "n" "(y/n) " "^(y|n)$")"
  fi
  if [ "$A" = "n" ]; then
    exit 1
  fi
}

realdisk()
{
  { [ $# -gt 0 ] && echo "$1" || cat; } | sed -E -e "s|^UUID=(\"\|)([^\" ]+)(\"\|)$|/dev/disk/by-uuid/\2|" | xargs -r realpath -m
}
#lib_dev_test() # tests realdisk
#{
#  f="realdisk"
#  u="$(/bin/ls /dev/disk/by-uuid | head -1)"; d="$(realpath /dev/disk/by-uuid/$u)"
#  exp="$d"; res="$(realdisk "UUID=$u")"
#  [ "$res" = "$exp" ] && echo "OK $f-1" || echo "ERR $f-1 exp=<$exp> res=<$res>"
#  exp="$d"; res="$(realdisk "UUID=\"$u\"")"
#  [ "$res" = "$exp" ] && echo "OK $f-2" || echo "ERR $f-2 exp=<$exp> res=<$res>"
#  exp="$d"; res="$(realdisk "/dev/disk/by-uuid/$u")"
#  [ "$res" = "$exp" ] && echo "OK $f-3" || echo "ERR $f-3 exp=<$exp> res=<$res>"
#  exp="$(/bin/echo -e "$d\n$d\n$d")"; res="$(realdisk "$(/bin/echo -e \
#    "UUID=$u\nUUID=\"$u\"\n/dev/disk/by-uuid/$u")")"
#  [ "$res" = "$exp" ] && echo "OK $f-4" || echo "ERR $f-4 exp=<$exp> res=<$res>"
#  exp=""; res="$(realdisk "")"
#  [ "$res" = "$exp" ] && echo "OK $f-5" || echo "ERR $f-5 exp=<$exp> res=<$res>"
#  exp="/a_/_b/c_/_d/e"; res="$(realdisk "/a_/_b/c_/_d/e")"
#  [ "$res" = "$exp" ] && echo "OK $f-6" || echo "ERR $f-6 exp=<$exp> res=<$res>"
#  exp="$(/bin/echo -e "/a\n/b\n/c")"; res="$(/bin/echo -e "/a\n/b\n/c" | realdisk)"
#  [ "$res" = "$exp" ] && echo "OK $f-7" || echo "ERR $f-7 exp=<$exp> res=<$res>"
#}

warn_2disk()
{
  local new cmd used msg err i j
  eval "$ia_init"
  ia_add "err=\"\""
  ia_add "msg=\"\""
  if [ "$only_copydata" ]; then
    ia_add "addln msg \"Directory $COPYDEST will be deleted and created again!\""
    [ "$(typefun before_copy)" ] && ia_add "addln msg \"before_copy is defined.\""
    [ "$(typefun after_copy)" ] && ia_add "addln msg \"after_copy is defined.\""
  else
    ia_add "cmd=\"\$(find_bootcd_file /usr/bin/bootcdmk2diskconf)\""
    ia_add "used=\"$(echo "$CIDISK" | sed "s/:.*//" | realdisk | oneline)\""
    for i in $DISKVARS; do
      ia_add "new=\"\$(eval \"echo \\\"$<DISK$i>\\\"\")\""
      ia_add "addln msg \"Harddisk $i ($<new>) will be erased!!!\""
      ia_add "for j in $<used>; do
        [ \"\$j\" != \"$<new>\" ] || addln err \"ERROR: Harddisk $<new> is already used\"
      done"
    done
    [ "$SWAP" ] && ia_add "addln msg \"Partition $SWAP will be newly created as SWAP !!!\""
    [ "$EXT2FS" ] && ia_add "addln msg \"Partition $EXT2FS will be newly created as ext2 !!!\""
    [ "$EXT3FS" ] && ia_add "addln msg \"Partition $EXT3FS will be newly created as ext3 !!!\""
    [ "$EXT4FS" ] && ia_add "addln msg \"Partition $EXT4FS will be newly created as ext4 !!!\""
    [ "$VFAT" ] && ia_add "addln msg \"Partition $VFAT will be newly created as vfat !!!\""
  fi
  ia_nocheck && ia_add "warn_2disk_part2 \"$<err>\" \"$<msg>\""
  ia -c
}

setup_lvm()
{
  eval "$ia_init"
  ia_nocheck && ia_add "lvm_pvcreate <-i>"
  ia_nocheck && ia_add "lvm_vgcreate <-i>"
  ia_nocheck && ia_add "lvm_lvcreate <-i>"
  ia_nocheck && ia_add "lvm_vgchange <-i>"
  ia -c
}

get_LUKS_PASSWD()
{
  # global LUKS_PASSWD
  eval "$ia_init"
  ia_add "[ ! \"\${LUKS_PASSWD:-}\" ] || return 0"
  ia_add "LUKS_PASSWD=\$(ia_ask -p -2 -f \"LUKS password\" \"\" \"\" \"? \" \"^\S\")"
  ia -c
}

# LUKS="target source_dev key_file options\n..."
#   bkuproot /dev/xxx none luks
#   bkupswap /dev/zzz /dev/urandom swap,cipher=aes-xts-plain64,size=256
setup_luks()
{
  local target source_dev key_file options
  eval "$ia_init"
  ia_nocheck && ia_add "get_LUKS_PASSWD <-i>"
  eval "$(
    getdata "$LUKS" |
    while read target source_dev key_file options; do
      if [ "$(echo ",$options," | grep -e ",swap," -e ",luks,")" ]; then
        cat <<END
ia_ignore "^Can't do passphrase verification on non-tty inputs\.$"
ia_add "echo \"\\\$LUKS_PASSWD\" | cryptsetup -q -y luksFormat $source_dev"
END
      else
        err "setup_luks: options=<$options> does not contain <luks> or <swap>"
      fi
    done
  )"
  ia -c
}

# open_luks [--no-swap]
#   --no-swap: do not open luks swap partitions
open_luks()
{
  local no_swap
  eval "$ia_init"
  no_swap=""
  while [ $# -gt 0 ]; do
    if [ "$1" = "--no-swap" ]; then
       no_swap="$1"
       shift
    else
      err "open_luks: unknown argument: <$1>"
    fi
  done

  ia_add "get_LUKS_PASSWD <-i>"
  eval "$(
    getdata "$LUKS" |
    while read target source_dev key_file options; do
      if [ "$(echo ",$options," | grep -e ",swap," -e ",luks,")" ]; then
        if [ ! "$no_swap" -o ! "$(echo ",$options," | grep -e ",swap,")" ]; then
          cat <<END
ia_add "echo \"\\\$LUKS_PASSWD\" | cryptsetup luksOpen $source_dev $target"
END
        fi
      else
        err "open_luks: options=<$options> does not contain <luks> or <swap>"
      fi
    done
  )"
  ia -c
}

# close_luks [-f] [--no-swap]
#   -f close without warnings if it does not exists
#   --no-swap: do not close luks swap partitions
close_luks()
{
  local force no_swap
  eval "$ia_init"

  no_swap=""
  force=""
  while [ $# -gt 0 ]; do
    if [ "$1" = "-f" ]; then
       force="$1"
       shift
    elif [ "$1" = "--no-swap" ]; then
       no_swap="$1"
       shift
    else
      err "close_luks: unknown argument: <$1>"
    fi
  done

  eval "$(
    getdata "$LUKS" |
    while read target source_dev key_file options; do
      if [ ! "$no_swap" -o ! "$(echo ",$options," | grep -e ",swap,")" ]; then
        [ "$force" ] && cat <<END
ia_ignore "^Device \S+ is not active\.$"
ia_ignore "^exit=4$"
END
        cat <<END
ia_add "cryptsetup luksClose $target"
END
      fi
    done
  )"
  ia -c
}

# run_task
run_task()
{
  # run_2disk-global ORDER_TODO
  local task
  eval "$ia_init"

  task="$(echo "$ORDER_TODO" | sed -E -e "s/(\S+).*/\1/")"
  ORDER_TODO="$(echo "$ORDER_TODO" | sed -E -e "s/\S+\s*//")"

  dbg "ORDER=<$ORDER> task=<$task> ORDER_TODO=<$ORDER_TODO>"

  if [ "$task" = "parti" ]; then
    ia_nocheck && ia_add "partdisk <-i>"
  elif [ "$task" = "md" ]; then
    [ "$MD" ] && ia_nocheck && ia_add "mddisk <-i>"
  elif [ "$task" = "lvm" ]; then
    [ "$LVMGRP" -a "$LVMVOL" ] && ia_nocheck && ia_add "setup_lvm <-i>"
  elif [ "$task" = "luks" ]; then
    [ "$LUKS" ] && ia_nocheck && ia_add "setup_luks <-i>"
    [ "$LUKS" ] && ia_nocheck && ia_add "open_luks <-i>"
  else
    ia_add "info \"WARNING run_task: ignoring non-existing task=<$task>\""
  fi
  ia -c
}

write_mdadm_conf()
{
  eval "$ia_init"
  # Exp: mdadm --detail --scan /dev/md/raid5 =>
  #   ARRAY /dev/md/raid5 metadata=1.2 name=any:raid5 UUID=316de24b:aed927d3:6cb85e94:4c471048
  ia_add "sed -i \"/^\s*ARRAY / d\" \"$COPYDEST/etc/mdadm/mdadm.conf\""
  eval "$(
    getdata "$MD" |
    while read mddev level source_dev; do
      cat <<END
ia_add "mdadm --detail --scan /dev/md/$mddev >>$COPYDEST/etc/mdadm/mdadm.conf"
END
    done
  )"
  ia -c
}

# write_crypttab <crypttab>
write_crypttab()
{
  local CRYPTTAB target source_dev pass1 pass2
  eval "$ia_init"
  CRYPTTAB="$1"
  ia_add "echo \"# <target> <source_device> <key_file> <options>\" >$CRYPTTAB"
  eval "$(
    getdata "$LUKS" |
    while read target source_dev key_file options; do
      cat <<END
ia_add "echo \"$(real2uuid "$target UUID=UUID!$source_dev $key_file $options")\" >>$CRYPTTAB"
END
    done
  )"
  ia -c
}

fixinitrd()
{
  # give copy of mdadm.conf to initramfs
  # add luks config to initramfs
  eval "$ia_init"
  ia_ignore "^update-initramfs: Generating \S+$"
  ia_stdout "^WARNING: Unknown X keysym \"dead_belowmacron\"$"
  ia_stdout "^W: Possible missing firmware"
  ia_stdout "cp: warning: behavior of -n is non-portable and may change in future"
  ia_add "chroot $COPYDEST update-initramfs -u # give copy of mdadm.conf to initramfs"
  ia -c
}

run_2disk()
{
  local only_format
  local parti # do format, mount and umount
  local copy
  local ORDER_TODO
  local DISKVARS_PARTI # disks that need to be partitioned
  local mount

  only_format="" # TODO
  ORDER_TODO="$ORDER"
  [ ! "$only_copydata" ]  && parti="1" || parti=""
  [ ! "$only_format" ] && copy="1" || copy=""
  [ "$parti" ] && mount="1" || mount=""
  [ "$only_sync" ] && parti=""

  eval "$ia_init"
  [ "$parti" ] && DISKVARS_PARTI="$DISKVARS"
  [ "$parti" ] && ia_nocheck && ia_add "while [ \"\$ORDER_TODO\" ]; do run_task <-i>; done"
  [ "$parti" ] && ia_nocheck && ia_add "mkfs <-i>"
  [ "$only_sync" ] && [ "$LUKS" ] && ia_nocheck && ia_add "open_luks <-i> --no-swap"
  [ "$mount" ] && ia_nocheck && ia_add "todo_domount <-i>"
  [ "$parti" ] && [ "$SWAP" ] && ia_nocheck && ia_add "swap # mkswap $SWAP"

  [ "$copy" -a ! "$parti" -a ! "$only_sync" ] && [ "$COPYDEST" != "/" -a "$COPYDEST" != "." ] && ia_add "rm -rf $COPYDEST; mkdir -p $COPYDEST"

  [ "$copy" ] && [ "$IMAGEURL" ] && ia_nocheck && ia_add "copyfromserver_before <-i>"
  [ "$copy" ] && [ "$(typefun before_copy)" ] && ia_nocheck && ia_add "before_copy <-i> # run function before_copy defined in bootcd2disk.conf"
  [ "$copy" ] && ia_nocheck && ia_add "copycdram <-i> # copy cd and ram to $COPYDEST, delete unneeded stuff"
  [ "$copy" ] && [ "$UDEV_FIXNET" = "yes" ] && ia_add "disable_persistent_net_rules \"$COPYDEST\" \"$COPYDEST\""
  [ "$copy" ] && [ "$IMAGEURL" ] && ia_nocheck && ia_add "give_swappartition_free <-i>"
  [ "$copy" ] && [ "$RESUME" ] && ia_add "resume_fix $COPYDEST"
  [ "$copy" ] && ia_nocheck && ia_add "prepare_chroot <-i> $COPYDEST"
  [ "$copy" ] && [ "$(typefun after_copy)" ] && ia_add "after_copy # run function after_copy defined in bootcd2disk.conf"
  [ "$copy" ] && ia_add "do_fstab # create $COPYDEST/etc/fstab"
  [ "$copy" ] && ia_nocheck && ia_add "createsshkeys <-i>"
  [ "$copy" ] && [ "$DISABLE_CRON" ] && ia_add enablecron
  [ "$copy" ] && [ "$MD" ] && ia_add "write_mdadm_conf <-i>"
  [ "$copy" ] && [ "$LUKS" ] && ia_nocheck && ia_add "write_crypttab <-i> \"$COPYDEST/etc/crypttab\""
  [ "$copy" ] && [ "$LUKS" -o "$MD" ] && ia_nocheck && ia_add "fixinitrd <-i>"
  [ "$parti" -o "$only_sync" ] && ia_nocheck && ia_add "run_bootloadercfg <-i>"
  [ "$copy" ] && ia_nocheck && ia_add "unprepare_chroot <-i> $COPYDEST"
  [ "$mount" ] && ia_nocheck && ia_add "todo_dounmount <-i>"
  [ "$parti" ] && [ "$LUKS" ] && ia_nocheck && ia_add "close_luks <-i>"
  [ "$only_sync" ] && [ "$LUKS" ] && ia_nocheck && ia_add "close_luks <-i> --no-swap"

  ia -c
}

todo_print_options()
{
  eval "$ia_init"
  ia_stdout "^"
  ia_add "print_bootcd_vars bootcd2diskconf"
  ia -c
}

bootcd2disk()
{
  local cmd
  eval "$ia_init"
  [ "$only_copydata" ] || { ia_nocheck && ia_add "calc_options <-i>"; } # TODO which vars have to be checked instead
  [ "$only_print_options" ] && ia_nocheck && ia_add "todo_print_options <-i>; exit \$?"
  [ "$only_copydata" -o "$only_mount" -o "$only_unmount" ] || ia_add "run_do_first"
  [ "$only_mount" ] && ia_nocheck && ia_add "[ ! \"$<LUKS>\" ] || open_luks <-i> --no-swap"
  [ "$only_mount" ] && ia_nocheck && ia_add "todo_domount <-i>"
  [ "$only_mount" ] && ia_nocheck && ia_add "prepare_chroot <-i> $COPYDEST"
  [ "$only_unmount" ] && ia_nocheck && ia_add "unprepare_chroot <-i> $COPYDEST"
  [ "$only_unmount" ] && ia_nocheck && ia_add "todo_dounmount <-i>"
  [ "$only_unmount" ] && ia_nocheck && ia_add "[ ! \"$<LUKS>\" ] || close_luks <-i> --no-swap"
  [ "$only_mount" -o "$only_unmount" ] && ia_nocheck && ia_add "exit"
  ia_add "dbglog \"bootcd2disk: <\$(print_bootcd_vars)>\""
  ia_add "cmd=\"\$(find_bootcd_file /usr/bin/bootcdmk2diskconf)\""
# ia_nocheck && ia_add "eval \"\$($<cmd> <-i> -- --only_vars CIDISK,CIFS)\" # read info from disk"
  ia_nocheck && ia_add "eval \"\$($<cmd> <-i> -- --only_vars CIDISK)\" # read info from disk"
# ia_add "SRC=\"\$(echo \"$<CIFS>\" | gawk -F: '{print \$2}' | realdisk)\""
# ia_add "[ \"$<SRC>\" ] || SRC=\"/\""
  ia_nocheck && ia_add "warn_2disk <-i>"
  ia_add "trap trapfunc 2"
  ia_nocheck && ia_add "run_2disk <-i>"
  [ "$only_copydata" -o "$only_mount" -o "$only_unmount" ] || ia_add "run_do_last"
  [ "$only_copydata" ] || ia_add "info \"Please Reboot now !\""
  ia -c
}