| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
#********************************************************************* |
| 4 |
# This program is free software; you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation; either version 2 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, but |
| 10 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
# General Public License for more details. |
| 13 |
# |
| 14 |
# A copy of the GNU General Public License is available as |
| 15 |
# `/usr/share/common-licences/GPL' in the Debian GNU/Linux distribution |
| 16 |
# or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html. You |
| 17 |
# can also obtain it by writing to the Free Software Foundation, Inc., |
| 18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 19 |
#********************************************************************* |
| 20 |
|
| 21 |
use strict; |
| 22 |
|
| 23 |
# treat all warnings about uninitialised values as errors |
| 24 |
use warnings FATAL => qw(uninitialized); |
| 25 |
|
| 26 |
################################################################################ |
| 27 |
# |
| 28 |
# @file setup-storage |
| 29 |
# |
| 30 |
# @brief The main function of setup-storage - the tool to configure the |
| 31 |
# partitioning from within FAI. |
| 32 |
# |
| 33 |
# This is an implementation from scratch to properly support LVM and RAID. The |
| 34 |
# input format is documented in @ref parser.pm |
| 35 |
# |
| 36 |
# Some (developer) documentation may be found on |
| 37 |
# https://wiki.fai-project.org/index.php/Setup-storage |
| 38 |
# |
| 39 |
# Some coding conventions: |
| 40 |
# - no whitespace after ( and before ) |
| 41 |
# - keyword<whitespace>(...) |
| 42 |
# - do not start a new line for { |
| 43 |
# - } goes on a line on its own |
| 44 |
# - one blank line after sub ... |
| 45 |
# |
| 46 |
# @author Christian Kern, Michael Tautschnig, Thomas Lange |
| 47 |
# |
| 48 |
################################################################################ |
| 49 |
|
| 50 |
package FAI; |
| 51 |
|
| 52 |
my $version = "3.0"; |
| 53 |
$|=1; |
| 54 |
|
| 55 |
# command line parameter handling |
| 56 |
use Getopt::Std; |
| 57 |
$main::VERSION = $version; |
| 58 |
$Getopt::Std::STANDARD_HELP_VERSION = 1; |
| 59 |
our ($opt_X, $opt_f, $opt_h, $opt_d, $opt_s, $opt_D, $opt_L,$opt_y,$opt_p); # the variables for getopt |
| 60 |
(&getopts('Xf:hdsD:L:yp') && !$opt_h) || die <<EOF; |
| 61 |
setup-storage version $version |
| 62 |
|
| 63 |
USAGE: [-X] no test, your harddisks will be formated |
| 64 |
default: only test, no real formating |
| 65 |
[-f<config-filename>] default: parse classes |
| 66 |
[-d] enable debug output (equivalent to debug=1) |
| 67 |
[-s] perform syntax check only and exit |
| 68 |
[-D<disks>] override disklist variable by space-separated <disks> |
| 69 |
[-L<logdir>] use <logdir> instead of LOGDIR variable |
| 70 |
[-h] print this help message |
| 71 |
[-p] print the commands that would be executed, then exit. |
| 72 |
[-y] print disk variables as YAML file into disk_var.yml |
| 73 |
EOF |
| 74 |
|
| 75 |
# include all subparts, which are part of the FAI perl package |
| 76 |
use lib "/usr/share/fai/setup-storage/"; |
| 77 |
use Init; |
| 78 |
use Volumes; |
| 79 |
use Parser; |
| 80 |
use Sizes; |
| 81 |
use Commands; |
| 82 |
use Fstab; |
| 83 |
use Exec; |
| 84 |
|
| 85 |
sub prtdebug { |
| 86 |
|
| 87 |
# for debugging purposes to print the hash structures |
| 88 |
use Data::Dumper; |
| 89 |
|
| 90 |
print "Current disk layout in \%current_config\n"; |
| 91 |
print Dumper \%FAI::current_config; |
| 92 |
|
| 93 |
print "Current LVM layout in \%current_lvm_config\n"; |
| 94 |
print Dumper \%FAI::current_lvm_config; |
| 95 |
|
| 96 |
print "Current RAID layout in \%current_raid_config\n"; |
| 97 |
print Dumper \%FAI::current_raid_config; |
| 98 |
|
| 99 |
print "Current device tree in \%current_dev_children\n"; |
| 100 |
print Dumper \%FAI::current_dev_children; |
| 101 |
} |
| 102 |
|
| 103 |
# enable debug mode, if requested using -d |
| 104 |
$opt_d and $FAI::debug = 1; |
| 105 |
|
| 106 |
# Really write any changes to disk |
| 107 |
$opt_X and $FAI::no_dry_run = 1; |
| 108 |
warn "setup-storage is running in test-only mode\n" unless ($FAI::no_dry_run); |
| 109 |
|
| 110 |
# syntactic checks only |
| 111 |
$opt_s and $FAI::check_only = 1; |
| 112 |
|
| 113 |
# Find out whether $LOGDIR is usable or default to /tmp/fai |
| 114 |
$opt_L and $FAI::DATADIR = $opt_L; |
| 115 |
if (! -d $FAI::DATADIR) { |
| 116 |
!defined($opt_L) and defined ($ENV{LOGDIR}) and die |
| 117 |
"Environment variable LOGDIR is set, but $FAI::DATADIR is not a directory\n"; |
| 118 |
mkdir $FAI::DATADIR or die |
| 119 |
"Failed to create directory $FAI::DATADIR\n"; |
| 120 |
warn "Created data directory $FAI::DATADIR\n"; |
| 121 |
} |
| 122 |
|
| 123 |
# $disklist may be provided by the environment |
| 124 |
my $disklist = $ENV{disklist}; |
| 125 |
$opt_D and $disklist = $opt_D; |
| 126 |
if (! defined($disklist)) { |
| 127 |
&FAI::in_path("/usr/lib/fai/fai-disk-info") or die "/usr/lib/fai/fai-disk-info not found\n"; |
| 128 |
$disklist = `/usr/lib/fai/fai-disk-info | sort`; |
| 129 |
} |
| 130 |
|
| 131 |
@FAI::disks = split( /[\n ]/, $disklist); |
| 132 |
if ($FAI::debug) { |
| 133 |
print "disklist: "; |
| 134 |
print "$_\n" foreach(@FAI::disks); |
| 135 |
} |
| 136 |
warn "No disks defined in \$disklist\n" unless (@FAI::disks); |
| 137 |
|
| 138 |
# the config source file |
| 139 |
my $config_file = undef; |
| 140 |
# use the config file, if given |
| 141 |
open($config_file, $opt_f) or die "Failed to open config file $opt_f\n" if ($opt_f); |
| 142 |
unless ($opt_f) { |
| 143 |
defined ($ENV{classes}) or |
| 144 |
die "Environment variable classes is not set and -f was not given\n"; |
| 145 |
# see which class file to use |
| 146 |
foreach my $classfile (reverse split(/\s+/, $ENV{classes})) { |
| 147 |
next unless (-r "$ENV{FAI}/disk_config/$classfile"); |
| 148 |
open($config_file, "$ENV{FAI}/disk_config/$classfile") |
| 149 |
or die "Failed to open $ENV{FAI}/disk_config/$classfile\n"; |
| 150 |
$opt_f = "$ENV{FAI}/disk_config/$classfile"; |
| 151 |
last; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
# if we could not find any matching class file, bail out |
| 156 |
defined ($config_file) or die "No matching disk_config found\n"; |
| 157 |
|
| 158 |
# start the parsing - thereby $FAI::configs is filled |
| 159 |
warn "Starting setup-storage $version\n"; |
| 160 |
print "Using config file: $opt_f\n"; |
| 161 |
&FAI::run_parser($config_file); |
| 162 |
|
| 163 |
# make sure there are no empty disk_config stanzas |
| 164 |
&FAI::check_config; |
| 165 |
|
| 166 |
if ($FAI::check_only) { |
| 167 |
print "Syntax ok\n"; |
| 168 |
exit 0; |
| 169 |
} |
| 170 |
|
| 171 |
# first find the proper way to tell udev to settle |
| 172 |
$FAI::udev_settle = "udevadm settle --timeout=10" if (&FAI::in_path("udevadm")); |
| 173 |
$FAI::udev_settle = "udevsettle --timeout=10" if (&FAI::in_path("udevsettle")); |
| 174 |
defined($FAI::udev_settle) or die "Failed to find determine a proper way to tell udev to settle; is udev installed?"; |
| 175 |
`$FAI::udev_settle`; |
| 176 |
|
| 177 |
# read the sizes and partition tables of all disks listed in $FAI::disks |
| 178 |
&FAI::get_current_disks; |
| 179 |
|
| 180 |
# see whether there are any existing LVMs |
| 181 |
$FAI::uses_lvm and &FAI::in_path("pvdisplay") and &FAI::get_current_lvm; |
| 182 |
|
| 183 |
# see whether there are any existing RAID devices |
| 184 |
$FAI::uses_raid and &FAI::in_path("mdadm") and &FAI::get_current_raid; |
| 185 |
|
| 186 |
# mark devices as preserve, where not all already done so and check that only |
| 187 |
# defined devices are marked preserve |
| 188 |
&FAI::propagate_and_check_preserve; |
| 189 |
|
| 190 |
# debugging only: print the current configuration |
| 191 |
if ($FAI::debug) { |
| 192 |
use Data::Dumper; |
| 193 |
prtdebug; |
| 194 |
} |
| 195 |
|
| 196 |
# compute the new LVM and partition sizes; do the partition sizes first to have |
| 197 |
# them available for the the volume group size estimation |
| 198 |
&FAI::compute_partition_sizes; |
| 199 |
&FAI::compute_lv_sizes; |
| 200 |
|
| 201 |
# print the current contents of $FAI::configs |
| 202 |
$FAI::debug and print "Desired disk layout in %FAI::configs\n"; |
| 203 |
$FAI::debug and print Dumper \%FAI::configs; |
| 204 |
$FAI::debug and print "Desired device tree in %FAI::dev_children\n"; |
| 205 |
$FAI::debug and print Dumper \%FAI::dev_children; |
| 206 |
|
| 207 |
# generate the command script |
| 208 |
&FAI::build_disk_commands; |
| 209 |
$FAI::uses_raid and &FAI::build_raid_commands; |
| 210 |
&FAI::build_btrfs_commands; |
| 211 |
$FAI::uses_lvm and &FAI::build_lvm_commands; |
| 212 |
&FAI::build_cryptsetup_commands; |
| 213 |
&FAI::order_commands; |
| 214 |
|
| 215 |
# run all commands |
| 216 |
# debugging only: print the command script |
| 217 |
if ($FAI::debug) { |
| 218 |
foreach (&numsort(keys %FAI::commands)) { |
| 219 |
defined($FAI::commands{$_}{cmd}) or &FAI::internal_error("Missing command entry for $_"); |
| 220 |
print "$_:" . $FAI::commands{$_}{cmd} . "\n"; |
| 221 |
defined($FAI::commands{$_}{pre}) and print "\tpre: " . $FAI::commands{$_}{pre} . "\n"; |
| 222 |
defined($FAI::commands{$_}{post}) and print "\tpost: " . $FAI::commands{$_}{post} . "\n"; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
# only print the commands that would be executed |
| 227 |
if ($opt_p) { |
| 228 |
print "These commands would be executed:\n"; |
| 229 |
foreach (&numsort(keys %FAI::commands)) { |
| 230 |
next if ($FAI::commands{$_}{cmd} eq "true"); |
| 231 |
print "$FAI::commands{$_}{cmd}\n"; |
| 232 |
} |
| 233 |
exit 0; |
| 234 |
} |
| 235 |
|
| 236 |
# run the commands (if $FAI::no_dry_run is set) |
| 237 |
foreach (&numsort(keys %FAI::commands)) { |
| 238 |
`$FAI::udev_settle`; |
| 239 |
next if ($FAI::commands{$_}{cmd} eq "true"); |
| 240 |
&FAI::execute_command($FAI::commands{$_}{cmd}); |
| 241 |
} |
| 242 |
|
| 243 |
# clean crypt key files |
| 244 |
unless (defined ($ENV{'FAI_KEEP_CRYPTKEYFILE'})) { |
| 245 |
foreach (@FAI::crypttab) { |
| 246 |
my $fname = (split)[0]; |
| 247 |
unlink "$FAI::DATADIR/$fname"; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
# generate the proposed fstab contents |
| 252 |
# wait for udev to set up all devices |
| 253 |
`$FAI::udev_settle`; |
| 254 |
my @fstab = &FAI::generate_fstab(\%FAI::configs); |
| 255 |
|
| 256 |
# print fstab |
| 257 |
$FAI::debug and print "$_\n" foreach (@fstab); |
| 258 |
|
| 259 |
# write the proposed contents of fstab to $FAI::DATADIR/fstab |
| 260 |
if ($FAI::no_dry_run) { |
| 261 |
open(FSTAB, ">$FAI::DATADIR/fstab") |
| 262 |
or die "Failed to open $FAI::DATADIR/fstab for writing\n"; |
| 263 |
print FSTAB "$_\n" foreach (@fstab); |
| 264 |
close FSTAB; |
| 265 |
} |
| 266 |
|
| 267 |
# write variables to $FAI::DATADIR/disk_var.sh |
| 268 |
# debugging |
| 269 |
$FAI::debug and print "$_=\${$_:-$FAI::disk_var{$_}}\n" |
| 270 |
foreach (keys %FAI::disk_var); |
| 271 |
|
| 272 |
if ($FAI::no_dry_run) { |
| 273 |
# List all bootable devices. Can be used by later tasks to install a |
| 274 |
# boot loader. Using Variable PHYSICAL_BOOT_DEVICES. |
| 275 |
my @boot_devices; |
| 276 |
foreach (keys %FAI::configs) { |
| 277 |
$_ =~ /PHY_(.+)/; |
| 278 |
if ( $1 && defined $FAI::configs{$_}{'bootable'} |
| 279 |
&& $FAI::configs{$_}{'bootable'} > 0 ) { |
| 280 |
push @boot_devices, $1; |
| 281 |
} |
| 282 |
} |
| 283 |
@boot_devices = sort @boot_devices; |
| 284 |
|
| 285 |
open(DISK_VAR, ">$FAI::DATADIR/disk_var.sh") |
| 286 |
or die "Unable to write to file $FAI::DATADIR/disk_var.sh\n"; |
| 287 |
print DISK_VAR "$_=\${$_:-$FAI::disk_var{$_}}\n" foreach (keys %FAI::disk_var); |
| 288 |
print DISK_VAR "PHYSICAL_BOOT_DEVICES=\"", join(" ", @boot_devices), "\"\n"; |
| 289 |
close DISK_VAR; |
| 290 |
|
| 291 |
if ($opt_y) { |
| 292 |
open(DISK_YML, ">$FAI::DATADIR/disk_var.yml") |
| 293 |
or die "Unable to write to file $FAI::DATADIR/disk_var.yml\n"; |
| 294 |
|
| 295 |
print DISK_YML "---\n# disk_var.sh as YAML\n"; |
| 296 |
foreach (keys %FAI::disk_var) { |
| 297 |
$FAI::disk_var{$_} =~ s/"//g; |
| 298 |
if ( $FAI::disk_var{$_} =~ m/\s/ ) { |
| 299 |
# print an array |
| 300 |
print DISK_YML "$_:\n"; |
| 301 |
my $a = join("\n - ", split(/ /,$FAI::disk_var{$_})); |
| 302 |
print DISK_YML " - $a\n"; |
| 303 |
} else { |
| 304 |
print DISK_YML "$_: $FAI::disk_var{$_}\n"; |
| 305 |
} |
| 306 |
} |
| 307 |
print DISK_YML "PHYSICAL_BOOT_DEVICES:\n"; |
| 308 |
my $a = join("\n - ", @boot_devices); |
| 309 |
print DISK_YML " - $a\n"; |
| 310 |
close DISK_YML; |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
# print crypttab |
| 315 |
$FAI::debug and print "$_\n" foreach (@FAI::crypttab); |
| 316 |
|
| 317 |
# write the proposed contents of fstab to $FAI::DATADIR/fstab |
| 318 |
if ($FAI::no_dry_run && scalar(@FAI::crypttab)) { |
| 319 |
open(CRYPTTAB, ">$FAI::DATADIR/crypttab") |
| 320 |
or die "Failed to open $FAI::DATADIR/crypttab for writing\n"; |
| 321 |
print CRYPTTAB "$_\n" foreach (@FAI::crypttab); |
| 322 |
close CRYPTTAB; |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
# at the end print the whole config again |
| 327 |
if ($FAI::debug) { |
| 328 |
print "==========================================\n"; |
| 329 |
print "= Variables at the end of setup-storage =\n"; |
| 330 |
print "==========================================\n"; |
| 331 |
prtdebug; |
| 332 |
} |