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
|
#!/usr/bin/perl -w
#*********************************************************************
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# A copy of the GNU General Public License is available as
# `/usr/share/common-licences/GPL' in the Debian GNU/Linux distribution
# or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html. You
# can also obtain it by writing to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#*********************************************************************
use strict;
################################################################################
#
# @file commands.pm
#
# @brief Build the required commands in @FAI::commands using the config stored
# in %FAI::configs
#
# @author Christian Kern, Michael Tautschnig, Sebastian Hetze, Andreas Schuldei, Thomas Lange
#
################################################################################
package FAI;
my %partition_table_deps;
################################################################################
#
# @brief Build the mkfs commands for the partition pointed to by $partition
#
# @param $device Device name of the target partition
# @param $partition Reference to partition in the config hash
#
################################################################################
my @preserved_raid = ();
sub build_mkfs_commands {
my ($device, $partition) = @_;
# check for old-style encryption requests
&FAI::handle_oldstyle_encrypt_device($device, $partition);
defined ($partition->{filesystem})
or &FAI::internal_error("filesystem is undefined");
my $fs = $partition->{filesystem};
my $journal = $partition->{journal_dev};
return if ($fs eq "-" or $fs eq "btrfs");
my ($create_options) = $partition->{createopts};
my ($tune_options) = $partition->{tuneopts};
# prevent warnings of uninitialized variables
$create_options = '' unless $create_options;
$tune_options = '' unless $tune_options;
print "$partition->{mountpoint} FS create_options: $create_options\n" if ($FAI::debug && $create_options);
print "$partition->{mountpoint} FS tune_options: $tune_options\n" if ($FAI::debug && $tune_options);
my $prereqs = "exist_$device";
my $provides;
my $create_tool;
# create filesystem journal
if ($fs =~ m/.*_journal$/) {
$provides = "journal_preped_$device";
undef($tune_options);
if ($fs =~ /ext[34]_journal/) {
$create_tool = "mke2fs";
$create_options = "-O journal_dev";
} elsif ($fs eq "xfs_journal") {
$create_tool = "/bin/true";
$create_options = "";
} else {
&FAI::internal_error("unsupported journal type $fs");
}
} else {
# create regular filesystem
$provides = "has_fs_$device";
$create_tool = "mkfs.$fs";
($fs eq "swap") and $create_tool = "mkswap";
($fs eq "xfs") and $create_options = "$create_options -f" unless ($create_options =~ m/-f/);
($fs eq "reiserfs") and $create_options = "$create_options -q" unless ($create_options =~ m/-(f|q|y)/);
# adjust options for filesystem with external journal
if (defined($journal)) {
$journal =~ s/^journal=//;
$prereqs = "$prereqs,journal_preped_$journal";
($fs eq "xfs") and $create_options = "$create_options -l logdev=$journal";
($fs eq "ext3") and $create_options = "$create_options -J device=$journal";
($fs eq "ext4") and $create_options = "$create_options -J device=$journal";
}
}
&FAI::push_command( "$create_tool $create_options $device", $prereqs, $provides);
# possibly tune the file system - this depends on whether the file system
# supports tuning at all
return unless $tune_options;
my $tune_tool;
($fs eq "ext2" || $fs eq "ext3" || $fs eq "ext4") and $tune_tool = "tune2fs";
($fs eq "reiserfs") and $tune_tool = "reiserfstune";
die "Don't know how to tune $fs\n" unless $tune_tool;
# add the tune command
&FAI::push_command( "$tune_tool $tune_options $device", "has_fs_$device",
"has_fs_$device" );
}
################################################################################
#
# @brief Check for encrypt option and prepare corresponding CRYPT entry
#
# If encrypt is set, a corresponding CRYPT entry will be created and filesystem
# and mountpoint get set to -
#
# @param $device Original device name of the target partition
# @param $partition Reference to partition in the config hash
#
################################################################################
sub handle_oldstyle_encrypt_device {
my ($device, $partition) = @_;
return unless ($partition->{encrypt});
if (!defined($FAI::configs{CRYPT}{randinit})) {
$FAI::configs{CRYPT}{fstabkey} = "device";
$FAI::configs{CRYPT}{randinit} = 0;
$FAI::configs{CRYPT}{volumes} = {};
}
$FAI::configs{CRYPT}{randinit} = 1 if ($partition->{encrypt} > 1);
my $vol_id = scalar(keys %{ $FAI::configs{CRYPT}{volumes} });
$FAI::configs{CRYPT}{volumes}{$vol_id} = {
device => $device,
mode => "luks",
preserve => (defined($partition->{size}) ?
$partition->{size}->{preserve} : $partition->{preserve}),
mountpoint => $partition->{mountpoint},
mount_options => $partition->{mount_options},
filesystem => $partition->{filesystem},
createopts => $partition->{createopts},
lukscreateopts => $partition->{lukscreateopts},
tuneopts => $partition->{tuneopts}
};
$partition->{mountpoint} = "-";
$partition->{filesystem} = "-";
}
################################################################################
#
# @brief Set the partition flag $t on a device $d. This is a no-op if $d is not
# a physical device
#
# @param $d Device name
# @param $t Flag (e.g., lvm or raid)
#
################################################################################
sub set_partition_flag_on_phys_dev {
my ($d, $t) = @_;
my ($i_p_d, $disk, $part_no) = &FAI::phys_dev($d);
return 0 unless $i_p_d;
# make sure this device really exists (we can't check for the partition
# as that may be created later on
(-b $disk) or die "Specified disk $disk does not exist in this system!\n";
# set the raid/lvm unless this is an entire disk flag or a virtual disk
return 0 if ($part_no == -1 ||
(defined($FAI::configs{"PHY_$disk"}) && $FAI::configs{"PHY_$disk"}{virtual}));
my $pre = "exist_$d";
$pre .= ",cleared2_$disk" if (defined($FAI::configs{"PHY_$disk"}));
&FAI::push_command( "parted -s $disk set $part_no $t on", $pre, "flag_${t}_$d" );
if (defined($partition_table_deps{$disk}) &&
$partition_table_deps{$disk} ne "") {
$partition_table_deps{$disk} .= ",flag_${t}_$d";
} else {
$partition_table_deps{$disk} = "flag_${t}_$d";
}
return 1;
}
################################################################################
#
# @brief Using the configurations from %FAI::configs, a list of commands is
# built to create any encrypted devices
#
################################################################################
sub build_cryptsetup_commands {
foreach my $config (keys %FAI::configs) { # loop through all configs
# no LVM or physical devices here
next if ($config ne "CRYPT");
# create all encrypted devices
foreach my $id (&numsort(keys %{ $FAI::configs{$config}{volumes} })) {
# keep a reference to the current volume
my $vol = (\%FAI::configs)->{$config}->{volumes}->{$id};
# the desired encryption mode
my $mode = $vol->{mode};
warn "cryptsetup support is incomplete - preserve is not supported\n"
if ($vol->{preserve});
# rewrite the device name
my $real_dev = $vol->{device};
my $enc_dev_name = &FAI::enc_name($real_dev);
my $enc_dev_short_name = $enc_dev_name;
$enc_dev_short_name =~ s#^/dev/mapper/##;
my $pre_dep = "exist_$real_dev";
if ($FAI::configs{$config}{randinit}) {
# ignore exit 1 caused by reaching the end of $real_dev
&FAI::push_command(
"dd if=/dev/urandom of=$real_dev || true",
$pre_dep, "random_init_$real_dev");
$pre_dep = "random_init_$real_dev";
}
if ($mode =~ /^luks(:"([^"]+)")?$/) {
my $keyfile = "$FAI::DATADIR/$enc_dev_short_name";
my $luksoption = $1;
my $passphrase = $2;
# generate a key for encryption
&FAI::push_command(
"head -c 2048 /dev/urandom | od | tee $keyfile",
"", "keyfile_$real_dev" );
my $lukscreateopts = $vol->{lukscreateopts} // "";
# encrypt
&FAI::push_command(
"yes YES | cryptsetup luksFormat $real_dev $keyfile $lukscreateopts",
"$pre_dep,keyfile_$real_dev", "crypt_format_$real_dev" );
&FAI::push_command(
"cryptsetup luksOpen $real_dev $enc_dev_short_name --key-file $keyfile",
"crypt_format_$real_dev", "exist_$enc_dev_name" );
if (defined($luksoption)) {
# add user-defined key
&FAI::push_command(
"yes '$passphrase' | cryptsetup luksAddKey --key-file $keyfile $real_dev",
"exist_$enc_dev_name", "newkey_$enc_dev_name");
# remove previous key
&FAI::push_command(
"yes '$passphrase' | cryptsetup luksRemoveKey $real_dev $keyfile",
"newkey_$enc_dev_name", "removed_key_$enc_dev_name");
$keyfile = "none";
}
# add entries to crypttab
push @FAI::crypttab, "$enc_dev_short_name\t$real_dev\t$keyfile\tluks,discard";
} elsif ($mode eq "tmp" || $mode eq "swap") {
&FAI::push_command(
"cryptsetup --key-file=/dev/urandom create $enc_dev_short_name $real_dev",
$pre_dep, "exist_$enc_dev_name");
# add entries to crypttab
push @FAI::crypttab, "$enc_dev_short_name\t$real_dev\t/dev/urandom\t$mode";
}
# create the filesystem on the volume
&FAI::build_mkfs_commands($enc_dev_name,
\%{ $FAI::configs{$config}{volumes}{$id} });
}
}
}
################################################################################
#
# @brief Using the configurations from %FAI::configs, a list of commands is
# built to create all BTRFS volumes/raids
#
################################################################################
sub build_btrfs_commands {
# following monstrosity takes care of single device/partition btrfs disk configs
foreach my $c (keys %FAI::configs) {
next unless ($c =~ /^PHY_(.+)$/);
my $device = $1;
my $single_vol_index = 0;
my $config = 'BTRFS';
my $volume;
my $this_fs;
foreach my $p (keys %{ $FAI::configs{$c}{partitions} }) {
$this_fs = $FAI::configs{$c}{partitions}{$p}{filesystem};
next unless defined($this_fs);
next unless ($this_fs eq 'btrfs');
$volume = 'single_' . $single_vol_index;
$FAI::configs{$config}{volumes}{$volume}{preserve} = $FAI::configs{$c}{partitions}{$p}{size}{preserve};
$FAI::configs{$config}{volumes}{$volume}{encrypt} = $FAI::configs{$c}{partitions}{$p}{encrypt};
$FAI::configs{$config}{volumes}{$volume}{raidlevel} = 'single';
$FAI::configs{$config}{volumes}{$volume}{filesystem} = $this_fs;
$FAI::configs{$c}{partitions}{$p}{filesystem} = '-';
$FAI::configs{$config}{volumes}{$volume}{mountpoint} = $FAI::configs{$c}{partitions}{$p}{mountpoint};
$FAI::configs{$c}{partitions}{$p}{mountpoint} = '-';
$FAI::configs{$config}{volumes}{$volume}{mount_options} = $FAI::configs{$c}{partitions}{$p}{mount_options};
$FAI::configs{$c}{partitions}{$p}{mount_options} = '-';
$FAI::configs{$config}{volumes}{$volume}{fstabkey} = $FAI::configs{$c}{fstabkey};
if ($device =~ m:^/dev/nvme|^/dev/loop|^/dev/mmcblk:) {
$FAI::configs{$config}{volumes}{$volume}{devices}{$device . "p" . $p} = {};
} else {
$FAI::configs{$config}{volumes}{$volume}{devices}{$device . $p} = {};
}
$FAI::configs{$config}{opts_all} = {};
$single_vol_index++;
}
}
my %mkfs_done;
foreach my $config (keys %FAI::configs) { # loop through all configs
next unless ($config eq "BTRFS");
#create BTRFS RAIDs
foreach my $id (numsort(keys %{ $FAI::configs{$config}{volumes} })) {
#reference to current btrfs volume
my $vol = (\%FAI::configs)->{$config}->{volumes}->{$id};
#the list of BTRFS-RAID devices
my @devs = keys %{ $vol->{devices} };
my $raidlevel = $vol->{raidlevel};
my $mountpoint = $vol->{mountpoint};
my $mountoptions = $vol->{mount_options};
($mountoptions =~ m/subvol=([^,\s]+)/ and my $initial_subvolume= $1) or die "You must define an initial subvolume for your BTRFS RAID";
my $btrfscreateopts = $vol->{btrfscreateopts} // "" ;
my $createopts = $vol->{createopts} // "";
$createopts .= " -f";
my $pre_req = "";
my $btrfs_tool = "";
# creates the proper prerequisites for later command ordering
foreach (@devs) {
my $tmp = $_;
# special handling for nvme devices
unless ($tmp =~ s/(nvme.+)p\d+/$1/) {
$tmp =~ s/\d//;
}
$pre_req = "${pre_req}pt_complete_${tmp}," unless ($pre_req =~ m/pt_complete_$tmp/);
}
if (scalar @devs == 1) {
$pre_req = "exist_" . $devs[0];
}
# creates the BTRFS volume/RAID
if ($raidlevel eq 'single') {
if (exists $mkfs_done{join(" ", @devs)}) {
$btrfs_tool = "true";
} else {
print "Adding mkfs command for '", join(", ", @devs), "'.\n";
$btrfs_tool = "mkfs.btrfs -d single $createopts ".join(" ",@devs);
$mkfs_done{join(" ", @devs)} = '1';
}
} else {
if (exists $mkfs_done{join(" ", sort @devs)}) {
$btrfs_tool = "true";
} else {
$btrfs_tool = "mkfs.btrfs -d raid$raidlevel $createopts ".join(" ", sort @devs);
$mkfs_done{join(" ", sort @devs)} = '1';
}
}
# nothing more to do if we need to proserve this volume. No mkfs, no subvolume
next if ($vol->{preserve});
# add mkfs.btrfs if needed (otherwise add true)
&FAI::push_command("$btrfs_tool", "$pre_req", "btrfs_built_raid_$id");
# initial mount, required to create the initial subvolume
&FAI::push_command("mount $devs[0] /mnt",
"btrfs_built_raid_$id",
"btrfs_mounted_$id");
# creating initial subvolume
&FAI::push_command("btrfs subvolume create $btrfscreateopts /mnt/$initial_subvolume",
"btrfs_mounted_$id",
"btrfs_created_$initial_subvolume");
# unmounting the device itself
&FAI::push_command("umount /mnt",
"btrfs_created_$initial_subvolume",
"");
}
}
}
################################################################################
#
# @brief Using the configurations from %FAI::configs, a list of commands is
# built to create any RAID devices
#
################################################################################
sub build_raid_commands {
# check RAID arrays if there are pre-existing ones
if (scalar(keys %FAI::current_raid_config))
{
&FAI::push_command("mdadm --stop --scan", "", "stop_for_assemble");
&FAI::push_command("mdadm --assemble --scan --config=$FAI::DATADIR/mdadm-from-examine.conf",
"stop_for_assemble", "mdadm_startall_examined");
}
foreach my $id (keys %FAI::current_raid_config) {
my $md = "/dev/md$id";
my $pre_deps_cl = "mdadm_startall_examined";
$pre_deps_cl .= ",self_cleared_" .
join(",self_cleared_", @{ $FAI::current_dev_children{$md} })
if (defined($FAI::current_dev_children{$md}) &&
scalar(@{ $FAI::current_dev_children{$md} }));
&FAI::push_command( "mdadm -W --stop $md", "$pre_deps_cl", "self_cleared_$md");
}
foreach my $config (keys %FAI::configs) { # loop through all configs
# no encrypted, tmpfs, LVM or physical devices here
next if ($config eq "BTRFS" || $config eq "CRYPT" || $config eq "TMPFS" || $config eq "NFS" || $config =~ /^VG_./ || $config =~ /^PHY_./);
($config eq "RAID") or &FAI::internal_error("Invalid config $config");
# create all raid devices
foreach my $id (&numsort(keys %{ $FAI::configs{$config}{volumes} })) {
# keep a reference to the current volume
my $vol = (\%FAI::configs)->{$config}->{volumes}->{$id};
# the list of RAID devices
my @devs = keys %{ $vol->{devices} };
my @eff_devs = ();
my @spares = ();
my $pre_req = "";
# set proper partition types for RAID
foreach my $d (@devs) {
if ($vol->{devices}->{$d}->{missing}) {
if ($vol->{devices}->{$d}->{spare}) {
push @spares, "missing";
} else {
push @eff_devs, "missing";
}
# skip devices marked missing
next;
} else {
if ($vol->{devices}->{$d}->{spare}) {
push @spares, &FAI::enc_name($d);
} else {
push @eff_devs, &FAI::enc_name($d);
}
}
$d = &FAI::enc_name($d);
my ($i_p_d, $disk, $part_no) = &FAI::phys_dev($d);
if ($vol->{preserve}) {
$pre_req .= ($i_p_d && defined($FAI::configs{"PHY_$disk"})) ?
",pt_complete_$disk" :
",exist_$d";
} elsif (&FAI::set_partition_flag_on_phys_dev($d, "raid")) {
$pre_req .= defined($FAI::configs{"PHY_$disk"}) ?
",pt_complete_$disk" :
",exist_$d";
} else {
$pre_req .= ",exist_$d";
}
}
# if it is a volume that has to be preserved, there is not much to be
# done; its existance has been checked in propagate_and_check_preserve
if ($vol->{preserve}) {
$pre_req =~ s/^,//;
# Assemble the array
&FAI::push_command(
"mdadm --assemble /dev/md$id " . join(" ", grep(!/^missing$/, @eff_devs)),
"$pre_req", "exist_/dev/md$id");
push(@preserved_raid, "/dev/md$id");
# create the filesystem on the volume, if requested
&FAI::build_mkfs_commands("/dev/md$id",
\%{ $FAI::configs{$config}{volumes}{$id} })
if (1 == $vol->{always_format});
next;
}
# the desired RAID level
my $level = $vol->{mode};
# prepend "raid", if the mode is numeric-only
$level = "raid$level" if ($level =~ /^\d+$/);
my ($create_options) = $FAI::configs{$config}{volumes}{$id}{mdcreateopts};
# prevent warnings of uninitialized variables
$create_options = '' unless $create_options;
print "/dev/md$id MD create_options: $create_options\n" if ($FAI::debug && $create_options);
# create the command
$pre_req = "exist_/dev/md" . ( $id - 1 ) . $pre_req if (0 != $id);
$pre_req =~ s/^,//;
&FAI::push_command(
"yes | mdadm --create /dev/md$id --level=$level --force --run $create_options --raid-devices="
. scalar(@eff_devs) . (scalar(@spares) !=0 ? " --spare-devices=" . scalar(@spares) : "") . " "
. join(" ", @eff_devs) . " " . join(" ", @spares),
"$pre_req", "exist_/dev/md$id" );
# do not sync soft raid during installation
# do not remove ; from echo line. Otherwise it will not work
if ($FAI::do_init_tasks && $level ne 'raid0') { # no sync when initial installation
&FAI::push_command(
"echo frozen | tee /sys/block/md$id/md/sync_action",
"exist_/dev/md$id", "nosync_md$id" );
}
# create the filesystem on the volume
&FAI::build_mkfs_commands("/dev/md$id",
\%{ $FAI::configs{$config}{volumes}{$id} });
}
}
}
################################################################################
#
# @brief Create the volume group $config, unless it exists already; if the
# latter is the case, only add/remove the physical devices
#
# @param $config Config entry
#
################################################################################
sub create_volume_group {
my ($config) = @_;
($config =~ /^VG_(.+)$/) and ($1 ne "--ANY--") or &FAI::internal_error("Invalid config $config");
my $vg = $1; # the actual volume group
my ($pv_create_options) = $FAI::configs{$config}{pvcreateopts};
my ($vg_create_options) = $FAI::configs{$config}{vgcreateopts};
# prevent warnings of uninitialized variables
$pv_create_options = '' unless $pv_create_options;
$vg_create_options = '' unless $vg_create_options;
print "/dev/$vg PV create_options: $pv_create_options\n" if ($FAI::debug && $pv_create_options);
print "/dev/$vg VG create_options: $vg_create_options\n" if ($FAI::debug && $vg_create_options);
# create the volume group, if it doesn't exist already
if (!defined($FAI::configs{"VG_$vg"}{exists})) {
my $pre_dev = "";
my $devs = "";
# create all the devices
foreach my $d (keys %{ $FAI::configs{$config}{devices} }) {
$d = &FAI::enc_name($d);
my $pre = "exist_$d";
my ($i_p_d, $disk, $part_no) = &FAI::phys_dev($d);
$pre .= ",pt_complete_$disk"
if (&FAI::set_partition_flag_on_phys_dev($d, "lvm") &&
defined($FAI::configs{"PHY_$disk"}));
&FAI::push_command( "pvcreate -ff -y $pv_create_options $d",
"$pre", "pv_done_$d");
$devs .= " $d";
$pre_dev .= ",pv_done_$d";
}
$pre_dev =~ s/^,//;
# create the volume group
&FAI::push_command( "vgcreate $vg_create_options $vg $devs",
"$pre_dev", "vg_created_$vg" );
# we are done
return;
}
# otherwise add or remove the devices for the volume group, run pvcreate
# where needed
# the devices to be removed later on
my %rm_devs = ();
@rm_devs{ @{ $FAI::current_lvm_config{$vg}{"physical_volumes"} } } = ();
# all devices of this VG
my @all_devices = ();
# the list of devices to be created
my @new_devices = ();
# create an undefined entry for each device
my $pre_dev = "vg_exists_$vg";
foreach my $d (keys %{ $FAI::configs{$config}{devices} }) {
my $denc = &FAI::enc_name($d);
push @all_devices, $denc;
if (exists($rm_devs{$denc})) {
my ($i_p_d, $disk, $part_no) = &FAI::phys_dev($denc);
$pre_dev .= ($i_p_d && defined($FAI::configs{"PHY_$disk"})) ?
",pt_complete_$disk" : ",exist_$denc";
} else {
push @new_devices, $denc;
}
}
# remove remaining devices from the list
delete $rm_devs{$_} foreach (@all_devices);
# create all the devices
foreach my $dev (@new_devices) {
my $pre = "exist_$dev";
my ($i_p_d, $disk, $part_no) = &FAI::phys_dev($dev);
$pre .= ",pt_complete_$disk"
if (&FAI::set_partition_flag_on_phys_dev($dev, "lvm") &&
defined($FAI::configs{"PHY_$disk"}));
&FAI::push_command( "pvcreate -ff -y $pv_create_options $dev",
"$pre", "pv_done_$dev");
$pre_dev .= ",pv_done_$dev";
}
$pre_dev =~ s/^,//;
# extend the volume group by the new devices
if (scalar (@new_devices)) {
&FAI::push_command( "vgextend $vg " . join (" ", @new_devices), "$pre_dev",
"vg_extended_$vg" );
} else {
&FAI::push_command( "true", "self_cleared_VG_$vg,$pre_dev", "vg_extended_$vg" );
}
# run vgreduce to get them removed
if (scalar (keys %rm_devs)) {
$pre_dev = "";
$pre_dev .= ",exist_$_" foreach (keys %rm_devs);
&FAI::push_command( "vgreduce $vg " . join (" ", keys %rm_devs),
"vg_extended_$vg$pre_dev", "vg_created_$vg" );
} else {
&FAI::push_command( "true", "vg_extended_$vg", "vg_created_$vg" );
}
}
################################################################################
#
# @brief Create the volume group $config, unless it exists already; if the
# latter is the case, only add/remove the physical devices
#
# @param $config Config entry
#
################################################################################
sub setup_logical_volumes {
my ($config) = @_;
($config =~ /^VG_(.+)$/) and ($1 ne "--ANY--") or &FAI::internal_error("Invalid config $config");
my $vg = $1; # the actual volume group
# now create or resize the configured logical volumes
foreach my $lv (@{ $FAI::configs{$config}{ordered_lv_list} }) {
# reference to the size of the current logical volume
my $lv_size = (\%FAI::configs)->{$config}->{volumes}->{$lv}->{size};
# skip preserved partitions, but ensure that they exist
if ($lv_size->{preserve}) {
defined ($FAI::current_lvm_config{$vg}{volumes}{$lv})
or die "Preserved volume $vg/$lv does not exist\n";
warn "$vg/$lv will be preserved\n";
# create the filesystem on the volume, if requested
&FAI::build_mkfs_commands("/dev/$vg/$lv",
\%{ $FAI::configs{$config}{volumes}{$lv} })
if (1 == $lv_size->{always_format});
next;
}
# resize the volume
if ($lv_size->{resize}) {
defined ($FAI::current_lvm_config{$vg}{volumes}{$lv})
or die "Resized volume $vg/$lv does not exist\n";
warn "$vg/$lv will be resized\n";
use POSIX qw(floor);
my $lvsize_mib = &FAI::convert_unit($lv_size->{eff_size} . "B");
if ($lvsize_mib < $FAI::current_lvm_config{$vg}{volumes}{$lv}{size})
{
if (($FAI::configs{$config}{volumes}{$lv}{filesystem} =~
/^ext[23]$/) && &FAI::in_path("resize2fs")) {
my $block_count = POSIX::floor($lv_size->{eff_size} / 512);
&FAI::push_command( "e2fsck -p -f /dev/$vg/$lv",
"vg_enabled_$vg,exist_/dev/$vg/$lv", "e2fsck_f_resize_$vg/$lv" );
&FAI::push_command( "resize2fs /dev/$vg/$lv ${block_count}s",
"e2fsck_f_resize_$vg/$lv", "lv_shrink_$vg/$lv" );
} else {
&FAI::push_command( "parted -s /dev/$vg/$lv resizepart 1 " . $lv_size->{eff_size} . "B",
"vg_enabled_$vg", "lv_shrink_$vg/$lv" );
}
&FAI::push_command( "lvresize -L $lvsize_mib $vg/$lv",
"vg_enabled_$vg,lv_shrink_$vg/$lv", "lv_created_$vg/$lv" );
} else {
&FAI::push_command( "lvresize -L $lvsize_mib $vg/$lv",
"vg_enabled_$vg,exist_/dev/$vg/$lv", "lv_grow_$vg/$lv" );
if (($FAI::configs{$config}{volumes}{$lv}{filesystem} =~
/^ext[23]$/) && &FAI::in_path("resize2fs")) {
my $block_count = POSIX::floor($lv_size->{eff_size} / 512);
&FAI::push_command( "e2fsck -p -f /dev/$vg/$lv",
"vg_enabled_$vg,lv_grow_$vg/$lv", "e2fsck_f_resize_$vg/$lv" );
&FAI::push_command( "resize2fs /dev/$vg/$lv ${block_count}s",
"e2fsck_f_resize_$vg/$lv", "exist_/dev/$vg/$lv" );
} else {
&FAI::push_command( "parted -s /dev/$vg/$lv resizepart 1 " . $lv_size->{eff_size} . "B",
"vg_enabled_$vg,lv_grow_$vg/$lv", "exist_/dev/$vg/$lv" );
}
}
# create the filesystem on the volume, if requested
&FAI::build_mkfs_commands("/dev/$vg/$lv",
\%{ $FAI::configs{$config}{volumes}{$lv} })
if (1 == $lv_size->{always_format});
next;
}
my ($create_options) = $FAI::configs{$config}{volumes}{$lv}{lvcreateopts};
# prevent warnings of uninitialized variables
$create_options = '' unless $create_options;
$create_options .= " --yes";
print "/dev/$vg/$lv LV create_options: $create_options\n" if ($FAI::debug && $create_options);
# create a new volume
&FAI::push_command( "lvcreate $create_options -n $lv -L " .
&FAI::convert_unit($lv_size->{eff_size} . "B") . " $vg", "vg_enabled_$vg",
"exist_/dev/$vg/$lv" );
# create the filesystem on the volume
&FAI::build_mkfs_commands("/dev/$vg/$lv",
\%{ $FAI::configs{$config}{volumes}{$lv} });
}
}
################################################################################
#
# @brief Remove existing volume group if underlying devices will be modified,
# otherwise add proper exist_ preconditions
#
################################################################################
sub cleanup_vg {
my ($vg) = @_;
my $clear_vg = 0;
foreach my $dev (@{ $FAI::current_lvm_config{$vg}{"physical_volumes"} }) {
my ($i_p_d, $disk, $part_no) = &FAI::phys_dev($dev);
if ($i_p_d) {
defined ($FAI::configs{"PHY_$disk"}) or next;
defined ($FAI::configs{"PHY_$disk"}{partitions}{$part_no}) and
($FAI::configs{"PHY_$disk"}{partitions}{$part_no}{size}{preserve}) and
next;
} elsif ($dev =~ m{^/dev/md[\/]?(\d+)$}) {
my $vol = $1;
defined ($FAI::configs{RAID}) or next;
defined ($FAI::configs{RAID}{volumes}{$vol}) or next;
next if (1 == $FAI::configs{RAID}{volumes}{$vol}{preserve});
} elsif ($dev =~ m{^/dev/([^/\s]+)/([^/\s]+)$}) {
my $ivg = $1;
my $lv = $2;
defined($FAI::configs{"VG_$ivg"}) or next;
defined($FAI::configs{"VG_$ivg"}{volumes}{$lv}) or next;
next if (1 == $FAI::configs{"VG_$ivg"}{volumes}{$lv}{size}{preserve});
} else {
warn "Don't know how to check preservation of $dev\n";
next;
}
$clear_vg = 1;
last;
}
#following block responsible for lv/vg preservation
if (0 == $clear_vg) {
my $vg_setup_pre = "vgchange_a_n_VG_$vg";
if (defined($FAI::configs{"VG_$vg"})) {
$FAI::configs{"VG_$vg"}{exists} = 1;
# remove all volumes that do not exist anymore or need not be preserved
foreach my $lv (keys %{ $FAI::current_lvm_config{$vg}{volumes} }) {
my $pre_deps_cl = "";
$pre_deps_cl = "self_cleared_" .
join(",self_cleared_", @{ $FAI::current_dev_children{"/dev/$vg/$lv"} })
if (defined($FAI::current_dev_children{"/dev/$vg/$lv"}) &&
scalar(@{ $FAI::current_dev_children{"/dev/$vg/$lv"} }));
# skip preserved/resized volumes
if (defined ( $FAI::configs{"VG_$vg"}{volumes}{$lv})) {
if ($FAI::configs{"VG_$vg"}{volumes}{$lv}{size}{preserve} == 1 ||
$FAI::configs{"VG_$vg"}{volumes}{$lv}{size}{resize} == 1) {
&FAI::push_command("true", "vgchange_a_n_VG_$vg,$pre_deps_cl",
"exist_/dev/$vg/$lv,self_cleared_/dev/$vg/$lv");
next;
}
}
&FAI::push_command( "vgchange -a y $vg",
"",
"pre_wipe_$vg");
&FAI::push_command( "wipefs -a /dev/$vg/$lv",
"pre_wipe_$vg,$pre_deps_cl",
"wipefs_$vg/$lv");
&FAI::push_command( "lvremove -f /dev/$vg/$lv",
"wipefs_$vg/$lv",
"lv_rm_$vg/$lv,self_cleared_/dev/$vg/$lv");
$vg_setup_pre .= ",lv_rm_$vg/$lv";
}
} else {
&FAI::push_command("true", "vgchange_a_n_VG_$vg",
"exist_/dev/$vg/$_,self_cleared_/dev/$vg/$_") foreach
(keys %{ $FAI::current_lvm_config{$vg}{volumes} });
}
&FAI::push_command("true", $vg_setup_pre, "vg_exists_$vg");
&FAI::push_command( "vgchange -a n $vg",
"",
"$vg_setup_pre");
return 0;
}
my $vg_destroy_pre = "vgchange_a_n_VG_$vg";
foreach my $lv (keys %{ $FAI::current_lvm_config{$vg}{volumes} }) {
my $pre_deps_cl = "";
$pre_deps_cl = "self_cleared_" .
join(",self_cleared_", @{ $FAI::current_dev_children{"/dev/$vg/$lv"} })
if (defined($FAI::current_dev_children{"/dev/$vg/$lv"}) &&
scalar(@{ $FAI::current_dev_children{"/dev/$vg/$lv"} }));
&FAI::push_command( "vgchange -a y $vg",
"",
"pre_wipe_$vg");
&FAI::push_command( "wipefs -a /dev/$vg/$lv",
"pre_wipe_$vg,$pre_deps_cl",
"wipefs_$vg/$lv");
&FAI::push_command( "lvremove -f /dev/$vg/$lv",
"wipefs_$vg/$lv",
"lv_rm_$vg/$lv,self_cleared_/dev/$vg/$lv");
$vg_destroy_pre .= ",lv_rm_$vg/$lv";
}
&FAI::push_command( "vgremove $vg", "$vg_destroy_pre", "vg_removed_$vg");
# clear all the devices
my $devices = "";
$devices .= " " . &FAI::enc_name($_) foreach
(@{ $FAI::current_lvm_config{$vg}{physical_volumes} });
($devices =~ /^\s*$/) and &FAI::internal_error("Empty PV device set");
$FAI::debug and print "Erased devices:$devices\n";
&FAI::push_command( "pvremove $devices", "vg_removed_$vg", "pvremove_$vg");
my $post_wipe = "pvremove_$vg";
foreach my $d (split (" ", $devices)) {
$post_wipe .= ",pv_sigs_removed_wipe_${d}_$vg";
&FAI::push_command( "wipefs -af $d", "pvremove_$vg", "pv_sigs_removed_wipe_${d}_$vg");
}
&FAI::push_command( "true", $post_wipe, "pv_sigs_removed_$vg" );
return 1;
}
################################################################################
#
# @brief Using the configurations from %FAI::configs, a list of commands is
# built to setup the LVM
# creates the volume groups, the logical volumes and the filesystems
#
################################################################################
sub build_lvm_commands {
# disable volumes if there are pre-existing ones
foreach my $d (keys %FAI::current_dev_children) {
next unless ($d =~ /^VG_(.+)$/);
my $vg = $1;
my $vg_pre = "vgchange_a_n_VG_$vg";
my $pre_deps_vgc = "";
my $preserved = 0;
$pre_deps_vgc = ",self_cleared_" .
join(",self_cleared_", @{ $FAI::current_dev_children{$d} })
if (defined($FAI::current_dev_children{$d}) &&
scalar(@{ $FAI::current_dev_children{$d} }));
$pre_deps_vgc =~ s/^,//;
foreach my $raid (@preserved_raid) {
my $tmp_vg = `pvdisplay $raid | grep "VG Name"`;
chomp $tmp_vg;
$tmp_vg = $1 if $tmp_vg =~ /(\S+)$/;
$preserved = 1 if ($tmp_vg eq $vg);
}
# prevent error due to different VG name of existing VG
if (defined($FAI::configs{$d})) {
# don't deactivate preserved VGs to prevent blkid error later on
foreach my $v (keys %{ $FAI::configs{$d}->{volumes} }) {
$preserved = 1 if ($FAI::configs{$d}{volumes}{$v}{size}{preserve});
}
}
&FAI::push_command("vgchange -a n $vg", "$pre_deps_vgc", $vg_pre)
unless $preserved;
$vg_pre .= ",pv_sigs_removed_$vg" if (&FAI::cleanup_vg($vg));
my $pre_deps_cl = "";
if (defined($FAI::current_dev_children{$d})) {
$pre_deps_cl = ",self_cleared_" .
join(",self_cleared_", @{ $FAI::current_dev_children{$d} })
if (scalar(@{ $FAI::current_dev_children{$d} }));
}
&FAI::push_command("true", "$vg_pre$pre_deps_cl", "self_cleared_VG_$vg");
}
# loop through all configs
foreach my $config (keys %FAI::configs) {
# no physical devices, RAID, encrypted or tmpfs here
next if ($config eq "BTRFS" || $config =~ /^PHY_./ || $config eq "RAID" || $config eq "CRYPT" || $config eq "TMPFS" || $config eq "NFS");
($config =~ /^VG_(.+)$/) or &FAI::internal_error("Invalid config $config");
next if ($1 eq "--ANY--");
my $vg = $1; # the volume group
# create the volume group or add/remove devices
&FAI::create_volume_group($config);
# enable the volume group
&FAI::push_command( "vgchange -a y $vg",
"vg_created_$vg", "vg_enabled_$vg" );
# perform all necessary operations on the underlying logical volumes
&FAI::setup_logical_volumes($config);
}
}
################################################################################
#
# @brief Return an ordered list of partitions that must be preserved
#
# @param $config Config entry
#
################################################################################
sub get_preserved_partitions {
my ($config) = @_;
($config =~ /^PHY_(.+)$/) or &FAI::internal_error("Invalid config $config");
my $disk = $1; # the device to be configured
# the list of partitions that must be preserved
my @to_preserve = ();
# find partitions that should be preserved or resized
foreach my $part_id (&numsort(keys %{ $FAI::configs{$config}{partitions} })) {
# reference to the current partition
my $part = (\%FAI::configs)->{$config}->{partitions}->{$part_id};
next unless ($part->{size}->{preserve} || $part->{size}->{resize});
# preserved or resized partitions must exist already
defined( $FAI::current_config{$disk}{partitions}{$part_id} )
or die "$part_id can't be preserved, it does not exist.\n";
# add a mapping from the configured partition to the existing one
# (identical here, may change for extended partitions below)
$part->{maps_to_existing} = $part_id;
# add $part_id to the list of preserved partitions
push @to_preserve, $part_id;
}
# sort the list of preserved partitions
@to_preserve = &numsort(@to_preserve);
# add the extended partition as well, if logical partitions must be
# preserved; and mark it as resize
if ($FAI::configs{$config}{disklabel} eq "msdos") {
# we assume there are no logical partitions
my $has_logical = 0;
my $extended = -1;
# now check all entries; the array is sorted
foreach my $part_id (@to_preserve) {
# the extended partition may already be listed; then, the id of the
# extended partition must not change
if ($FAI::current_config{$disk}{partitions}{$part_id}{is_extended}) {
(defined ($FAI::configs{$config}{partitions}{$extended}{size}{extended})
&& defined ($FAI::current_config{$disk}{partitions}{$extended}{is_extended})
&& $FAI::configs{$config}{partitions}{$extended}{size}{extended}
&& $FAI::current_config{$disk}{partitions}{$extended}{is_extended})
or die "ID of extended partition changes\n";
# make sure resize is set
$FAI::configs{$config}{partitions}{$part_id}{size}{resize} = 1;
$extended = $part_id;
last;
}
# there is some logical partition
if ($part_id > 4) {
$has_logical = 1;
last;
}
}
# if the extended partition is not listed yet, find and add it now; note
# that we need to add the existing one
if ($has_logical && -1 == $extended) {
foreach my $part_id (&numsort(keys %{ $FAI::current_config{$disk}{partitions} })) {
# no extended partition
next unless
$FAI::current_config{$disk}{partitions}{$part_id}{is_extended};
# find the configured extended partition to set the mapping
foreach my $p (&numsort(keys %{ $FAI::configs{$config}{partitions} })) {
# reference to the current partition
my $part = (\%FAI::configs)->{$config}->{partitions}->{$p};
next unless $part->{size}->{extended};
# make sure resize is set
$part->{size}->{resize} = 1;
# store the id for further checks
$extended = $p;
# add a mapping entry to the existing extended partition
$part->{maps_to_existing} = $part_id;
# add it to the preserved partitions
push @to_preserve, $p;
last;
}
# sort the list of preserved partitions (again)
@to_preserve = &numsort(@to_preserve);
last;
}
}
# a sanity check: if there are logical partitions, the extended must
# have been added
(0 == $has_logical || -1 != $extended)
or &FAI::internal_error("Required extended partition not detected for preserve");
}
return @to_preserve;
}
################################################################################
#
# @brief Recreate the preserved partitions once the partition table has been
# flushed
#
# @param $config Config entry
# @param $to_preserve Reference to list of preserved/resized partitions
#
################################################################################
sub rebuild_preserved_partitions {
my ($config, $to_preserve) = @_;
($config =~ /^PHY_(.+)$/) or &FAI::internal_error("Invalid config $config");
my $disk = $1; # the device to be configured
# once we rebuild partitions, their ids are likely to change; this counter
# helps keeping track of this
my $part_nr = 0;
# now rebuild all preserved partitions
foreach my $part_id (@{$to_preserve}) {
# get the existing id
my $mapped_id =
$FAI::configs{$config}{partitions}{$part_id}{maps_to_existing};
# get the original starts and ends
my $start =
$FAI::current_config{$disk}{partitions}{$mapped_id}{begin_byte};
my $end =
$FAI::current_config{$disk}{partitions}{$mapped_id}{end_byte};
# the type of the partition defaults to primary
my $part_type = "primary";
if ( $FAI::configs{$config}{disklabel} eq "msdos" ) {
# change the partition type to extended or logical as appropriate
if ( $FAI::configs{$config}{partitions}{$part_id}{size}{extended} == 1 ) {
$part_type = "extended";
} elsif ( $part_id > 4 ) {
$part_type = "logical";
$part_nr = 4 if ( $part_nr < 4 );
}
}
# set partition label if it exists
if ($FAI::configs{$config}{disklabel} =~ /gpt(-bios)?/) {
$part_type = (defined($FAI::configs{$config}{partitions}{$mapped_id}{ptlabel}) ) ?
$FAI::configs{$config}{partitions}{$mapped_id}{ptlabel} : "'\" \"'"; # or empty ptlabel
}
# restore the partition type, if any
my $fs =
$FAI::current_config{$disk}{partitions}{$mapped_id}{filesystem};
# increase the partition counter for the partition created next and
# write it to the configuration
$part_nr++;
$FAI::current_config{$disk}{partitions}{$mapped_id}{new_id} = $part_nr;
# build a parted command to create the partition
my $dn = &FAI::make_device_name($disk, $part_nr);
&FAI::push_command( "parted -s $disk mkpart $part_type \"$fs\" ${start}B ${end}B",
"cleared1_$disk", "prep1_$dn" );
my $post = "exist_$dn";
$post .= ",rebuilt_$dn" if
$FAI::configs{$config}{partitions}{$part_id}{size}{resize};
my $cmd = "true";
&FAI::push_command($cmd, "prep1_$dn", $post);
}
}
################################################################################
#
# @brief Set up physical partitions
#
# @param $config Config entry
#
################################################################################
sub setup_partitions {
my ($config) = @_;
($config =~ /^PHY_(.+)$/) or &FAI::internal_error("Invalid config $config");
my $disk = $1; # the device to be configured
# the list of partitions that must be preserved
my @to_preserve = &FAI::get_preserved_partitions($config);
# resize needed?
my $needs_resize = 0;
foreach my $part_id (@to_preserve) {
$needs_resize = 1 if ($FAI::configs{$config}{partitions}{$part_id}{size}{resize});
last if ($needs_resize);
}
my $label = $FAI::configs{$config}{disklabel};
$label = "gpt" if ($label eq "gpt-bios");
# A new disk label may only be written if no partitions need to be
# preserved
(($label eq $FAI::current_config{$disk}{disklabel})
|| (scalar (@to_preserve) == 0))
or die "Can't change disklabel, partitions are to be preserved\n";
# write the disklabel to drop the previous partition table
my $pre_deps = "";
foreach my $c (@{ $FAI::current_dev_children{$disk} }) {
$pre_deps .= ",self_cleared_" .
join(",self_cleared_", @{ $FAI::current_dev_children{$c} })
if (defined($FAI::current_dev_children{$c}) &&
scalar(@{ $FAI::current_dev_children{$c} }));
my ($i_p_d, $d, $part_no) = &FAI::phys_dev($c);
($i_p_d && $d eq $disk) or &FAI::internal_error("Invalid dev children entry");
my $wipe_cmd = "wipefs -af $c";
foreach my $part_id (@to_preserve) {
# get the existing id
my $mapped_id = $FAI::configs{$config}{partitions}{$part_id}{maps_to_existing};
$wipe_cmd = "true" if ($mapped_id == $part_no);
}
$wipe_cmd = "true" if
($FAI::current_config{$disk}{partitions}{$part_no}{is_extended});
&FAI::push_command($wipe_cmd, "exist_$disk$pre_deps", "wipefs_$c");
$pre_deps .= ",wipefs_$c";
}
&FAI::push_command( ($needs_resize ? "parted -s $disk mklabel $label" : "true"),
"exist_$disk$pre_deps", "cleared1_$disk" );
&FAI::rebuild_preserved_partitions($config, \@to_preserve) if ($needs_resize);
my $pre_all_resize = "";
# resize partitions while checking for dependencies
foreach my $part_id (reverse &numsort(@to_preserve)) {
# reference to the current partition
my $part = (\%FAI::configs)->{$config}->{partitions}->{$part_id};
# get the existing id
my $mapped_id = $part->{maps_to_existing};
# get the intermediate partition id; only available if
# rebuild_preserved_partitions was done
my $p = undef;
if ($needs_resize) {
$p = $FAI::current_config{$disk}{partitions}{$mapped_id}{new_id};
# anything to be done?
$pre_all_resize .= ",exist_" . &FAI::make_device_name($disk, $p) unless
$part->{size}->{resize};
}
if ($part->{size}->{resize}) {
warn &FAI::make_device_name($disk, $mapped_id) . " will be resized\n";
} else {
warn &FAI::make_device_name($disk, $mapped_id) . " will be preserved\n";
next;
}
$pre_all_resize .= ",resized_" . &FAI::make_device_name($disk, $p);
my $deps = "";
# now walk all other partitions requiring a resize to check for overlaps
foreach my $part_other (reverse &numsort(@to_preserve)) {
# don't compare to self
next if ($part_id == $part_other);
# reference to the current partition
my $part_other_ref = (\%FAI::configs)->{$config}->{partitions}->{$part_other};
# anything to be done?
next unless $part_other_ref->{size}->{resize};
# get the existing id
my $mapped_id_other = $part_other_ref->{maps_to_existing};
# get the intermediate partition id
my $p_other = $FAI::current_config{$disk}{partitions}{$mapped_id_other}{new_id};
# check for overlap
next if($part->{start_byte} >
$FAI::current_config{$disk}{partitions}{$mapped_id_other}{end_byte});
next if($part->{end_byte} <
$FAI::current_config{$disk}{partitions}{$mapped_id_other}{begin_byte});
# overlap detected - add dependency, but handle extended<->logical with
# special care, even though this does not catch all cases (sometimes it
# will fail nevertheless
if ($part->{size}->{extended} && $part_other > 4) {
if($part->{start_byte} >
$FAI::current_config{$disk}{partitions}{$mapped_id_other}{begin_byte}) {
$deps .= ",resized_" . &FAI::make_device_name($disk, $p_other);
}
elsif($part->{end_byte} <
$FAI::current_config{$disk}{partitions}{$mapped_id_other}{end_byte}) {
$deps .= ",resized_" . &FAI::make_device_name($disk, $p_other);
}
}
elsif ($part_id > 4 && $part_other_ref->{size}->{extended}) {
if($part->{start_byte} <
$FAI::current_config{$disk}{partitions}{$mapped_id_other}{begin_byte}) {
$deps .= ",resized_" . &FAI::make_device_name($disk, $p_other);
}
elsif($part->{end_byte} >
$FAI::current_config{$disk}{partitions}{$mapped_id_other}{end_byte}) {
$deps .= ",resized_" . &FAI::make_device_name($disk, $p_other);
}
} else {
$deps .= ",resized_" . &FAI::make_device_name($disk, $p_other);
}
}
# get the new starts and ends
my $start = $part->{start_byte};
my $end = $part->{end_byte};
# ntfs/ext2,3 partition can't be moved
($start == $FAI::current_config{$disk}{partitions}{$mapped_id}{begin_byte})
or &FAI::internal_error(
$FAI::current_config{$disk}{partitions}{$mapped_id}{filesystem}
. " partition start supposed to move, which is not allowed") if
($FAI::current_config{$disk}{partitions}{$mapped_id}{filesystem} =~
/^(ntfs|ext[23])$/);
# build an appropriate command
# ntfs requires specific care
if ($FAI::current_config{$disk}{partitions}{$mapped_id}{filesystem} eq
"ntfs") {
# check, whether ntfsresize is available
&FAI::in_path("ntfsresize") or die "ntfsresize not found in PATH\n";
&FAI::push_command( "yes | ntfsresize -s " . $part->{size}->{eff_size} . " " .
&FAI::make_device_name($disk, $p), "rebuilt_" .
&FAI::make_device_name($disk, $p) . $deps, "ntfs_ready_for_rm_" .
&FAI::make_device_name($disk, $p) );
# TODO this is just a hack, we would really need support for resize
# without data resize in parted, which will be added in some parted
# version > 2.1
&FAI::push_command( "parted -s $disk rm $p", "ntfs_ready_for_rm_" .
&FAI::make_device_name($disk, $p), "resized_" .
&FAI::make_device_name($disk, $p) );
## } elsif (($FAI::current_config{$disk}{partitions}{$mapped_id}{filesystem} =~
## /^ext[23]$/) && &FAI::in_path("resize2fs")) {
## TODO: BROKEN needs more checks, enlarge partition table before resize, just as
## NTFS case
## my $block_count = $part->{size}->{eff_size} / 512;
## &FAI::push_command( "e2fsck -p -f " . &FAI::make_device_name($disk, $p),
## "rebuilt_" . &FAI::make_device_name($disk, $p) . $deps,
## "e2fsck_f_resize_" . &FAI::make_device_name($disk, $p) );
## &FAI::push_command( "resize2fs " . &FAI::make_device_name($disk, $p) .
## " ${block_count}s", "e2fsck_f_resize_" . &FAI::make_device_name($disk, $p),
## "resized_" . &FAI::make_device_name($disk, $p) );
} else {
&FAI::push_command( "parted -s $disk resizepart $p ${end}B",
"rebuilt_" . &FAI::make_device_name($disk, $p) . $deps, "resized_" .
&FAI::make_device_name($disk, $p) );
}
}
# write the disklabel again to drop the partition table and create a new one
# that has the proper ids
&FAI::push_command( "parted -s $disk mklabel $label",
"cleared1_$disk$pre_all_resize", "cleared2_$disk" );
my $boot_disk;
my $prev_id = -1;
# generate the commands for creating all partitions
foreach my $part_id (&numsort(keys %{ $FAI::configs{$config}{partitions} })) {
# reference to the current partition
my $part = (\%FAI::configs)->{$config}->{partitions}->{$part_id};
# get the existing id
my $mapped_id = $part->{maps_to_existing};
# get the new starts and ends
my $start = $part->{start_byte};
my $end = $part->{end_byte};
# if /boot exists, set $boot_disk
if (defined $part->{mountpoint} && $part->{mountpoint} eq "/boot") {
$boot_disk=$disk;
}
# override $boot_disk if /boot/efi exists
if (defined $part->{mountpoint} && $part->{mountpoint} eq "/boot/efi") {
$boot_disk=$disk;
}
# the type of the partition defaults to primary
my $part_type = "primary";
if ($FAI::configs{$config}{disklabel} eq "msdos") {
# change the partition type to extended or logical as appropriate
if ($part->{size}->{extended} == 1) {
$part_type = "extended";
} elsif ($part_id > 4) {
$part_type = "logical";
}
}
# set partition label if it exists
if ($FAI::configs{$config}{disklabel} =~ /gpt(-bios)?/) {
$part_type = (defined($part->{ptlabel}) ) ?
$part->{ptlabel} : "'\" \"'"; # or empty ptlabel
}
my $fs = (defined($part->{filesystem}) && $part->{filesystem} =~ /\S+/) ?
$part->{filesystem} : "-";
($fs) = split(/:/, $fs);
$fs = "linux-swap" if ($fs eq "swap");
$fs = "fat32" if ($fs eq "vfat");
$fs = "fat16" if ($fs eq "msdos");
$fs = "ext3" if ($fs eq "ext4");
$fs = "" if ($fs =~ m/.*_journal$/);
$fs = $FAI::current_config{$disk}{partitions}{$mapped_id}{filesystem}
if ($part->{size}->{preserve} || $part->{size}->{resize});
$fs = "" if ($fs eq "-");
my $pre = "cleared2_$disk";
$pre .= ",exist_" . &FAI::make_device_name($disk, $prev_id) if ($prev_id > -1);
# build a parted command to create the partition
my $dn = &FAI::make_device_name($disk, $part_id);
&FAI::push_command( "parted -s $disk mkpart $part_type \"$fs\" ${start}B ${end}B",
$pre, "prep2_$dn");
my $cmd = "true";
&FAI::push_command($cmd, "prep2_$dn", "exist_$dn");
# (re-)set all flags
my $flags = "";
$flags = $FAI::current_config{$disk}{partitions}{$mapped_id}{flags}
if ($part->{size}->{preserve} || $part->{size}->{resize});
# set the bootable flag, if requested at all
$flags .= ",boot" if($FAI::configs{$config}{bootable} == $part_id);
# set the bios_grub flag on BIOS compatible GPT tables
$flags .= ",bios_grub" if($FAI::configs{$config}{disklabel} eq "gpt-bios"
&& $FAI::configs{$config}{gpt_bios_part} == $part_id);
$flags =~ s/^,//;
&FAI::set_partition_flag_on_phys_dev($dn, $_)
foreach (split(',', $flags));
$prev_id = $part_id;
}
# set bootable flag for gpt-bios and gpt
&FAI::push_command("parted $boot_disk set 1 boot on",
"pt_complete_$disk", "gpt_bios_fake_bootable")
if($FAI::configs{$config}{disklabel} =~ /^gpt/ and $boot_disk);
($prev_id > -1) or &FAI::internal_error("No partitions created");
$partition_table_deps{$disk} = "cleared2_$disk,exist_"
. &FAI::make_device_name($disk, $prev_id);
}
################################################################################
#
# @brief Using the configurations from %FAI::configs, a list of commands is
# built to setup the partitions
#
################################################################################
sub build_disk_commands {
# loop through all configs
foreach my $config ( keys %FAI::configs ) {
# no RAID, encrypted, tmpfs or LVM devices here
next if ($config eq "BTRFS" || $config eq "RAID" || $config eq "CRYPT" || $config eq "TMPFS" || $config eq "NFS" || $config =~ /^VG_./ || $FAI::configs{$config}{vg});
($config =~ /^PHY_(.+)$/) or &FAI::internal_error("Invalid config $config");
my $disk = $1; # the device to be configured
if ($FAI::configs{$config}{virtual}) {
foreach my $part_id (&numsort(keys %{ $FAI::configs{$config}{partitions} })) {
# virtual disks always exist
&FAI::push_command( "true", "",
"exist_" . &FAI::make_device_name($disk, $part_id) );
# no partition table operations
$partition_table_deps{$disk} = "";
}
} elsif (defined($FAI::configs{$config}{partitions}{0})) {
# no partition table operations
$partition_table_deps{$disk} = "";
} elsif (defined($FAI::configs{$config}{opts_all}{preserve})) {
foreach my $part_id (&numsort(keys %{ $FAI::configs{$config}{partitions} })) {
# all partitions exist
&FAI::push_command( "true", "",
"exist_" . &FAI::make_device_name($disk, $part_id) );
# no partition table operations
$partition_table_deps{$disk} = "";
}
# no changes on this disk
$partition_table_deps{$disk} = "";
} else {
# create partitions on non-virtual configs
&FAI::setup_partitions($config);
}
# generate the commands for creating all filesystems
foreach my $part_id (&numsort(keys %{ $FAI::configs{$config}{partitions} })) {
# reference to the current partition
my $part = (\%FAI::configs)->{$config}->{partitions}->{$part_id};
# skip preserved/resized/extended partitions
next if (($part->{size}->{always_format} == 0 &&
($part->{size}->{preserve} == 1 || $part->{size}->{resize} == 1))
|| $part->{size}->{extended} == 1);
# create the filesystem on the device
&FAI::build_mkfs_commands( 0 == $part_id ? $disk : &FAI::make_device_name($disk, $part_id), $part );
}
}
}
################################################################################
#
# @brief Whatever happened, write the previous partition table to the disk again
#
################################################################################
sub restore_partition_table {
# loop through all existing configs
foreach my $disk (keys %FAI::current_config) {
# write the disklabel again to drop the partition table
&FAI::execute_command("parted -s $disk mklabel "
. $FAI::current_config{$disk}{disklabel}, 0, 0);
# generate the commands for creating all partitions
foreach my $part_id (&numsort(keys %{ $FAI::current_config{$disk}{partitions} })) {
# reference to the current partition
my $curr_part = (\%FAI::current_config)->{$disk}->{partitions}->{$part_id};
# get the starts and ends
my $start = $curr_part->{begin_byte};
my $end = $curr_part->{end_byte};
# the type of the partition defaults to primary
my $part_type = "primary";
if ($FAI::current_config{$disk}{disklabel} eq "msdos") {
# change the partition type to extended or logical as appropriate
if ($curr_part->{is_extended}) {
$part_type = "extended";
} elsif ($part_id > 4) {
$part_type = "logical";
}
}
# set partition label if it exists
if ($FAI::current_config{$disk}{disklabel} =~ /gpt(-bios)?/) {
$part_type = (defined($curr_part->{ptlabel}) ) ?
$curr_part->{ptlabel} : "'\" \"'"; # or empty ptlabel
}
# restore the partition type, if any
my $fs = $curr_part->{filesystem};
# build a parted command to create the partition
&FAI::execute_command("parted -s $disk mkpart $part_type \"$fs\" ${start}B ${end}B");
# re-set all flags
&FAI::execute_command("parted -s $disk set $part_id $_ on")
foreach (split(',', $curr_part->{flags}));
}
warn "Partition table of disk $disk has been restored\n";
}
die "setup-storage failed, but the partition tables have been restored\n";
}
################################################################################
#
# @brief Try to order the queued commands to satisfy all dependencies
#
################################################################################
sub order_commands {
# first add partition-table-is-complete
&FAI::push_command("true", $partition_table_deps{$_}, "pt_complete_$_")
foreach (keys %partition_table_deps);
my @pre_deps = ();
my $i = 1;
my $pushed = -1;
while ($i < $FAI::n_c_i) {
if ($FAI::debug) {
print "Trying to add CMD: " . $FAI::commands{$i}{cmd} . "\n";
defined($FAI::commands{$i}{pre}) and print "PRE: " . $FAI::commands{$i}{pre} . "\n";
defined($FAI::commands{$i}{post}) and print "POST: " . $FAI::commands{$i}{post} . "\n";
}
my $all_matched = 1;
if (defined($FAI::commands{$i}{pre})) {
foreach (split(/,/, $FAI::commands{$i}{pre})) {
my $cur = $_;
next if scalar(grep(m{^\Q$cur\E$}, @pre_deps));
$all_matched = 0;
last;
}
}
if ($all_matched) {
defined($FAI::commands{$i}{post}) and push @pre_deps, split(/,/, $FAI::commands{$i}{post});
$pushed = -1;
$i++;
next;
}
if (-1 == $pushed) {
$pushed = $FAI::n_c_i;
}
elsif ($i == $pushed) {
die "Cannot satisfy pre-depends for " . $FAI::commands{$i}{cmd} . ": " .
$FAI::commands{$i}{pre} . " -- system left untouched.\n";
}
&FAI::push_command( $FAI::commands{$i}{cmd}, $FAI::commands{$i}{pre},
$FAI::commands{$i}{post} );
delete $FAI::commands{$i};
$i++;
}
}
1;
|