| 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 |
# http://faiwiki.debian.net/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 |
# $Id$
|
| 47 |
#
|
| 48 |
# @author Christian Kern, Michael Tautschnig
|
| 49 |
# @date Sun Jul 23 16:09:36 CEST 2006
|
| 50 |
#
|
| 51 |
################################################################################
|
| 52 |
|
| 53 |
package FAI;
|
| 54 |
|
| 55 |
# command line parameter handling
|
| 56 |
use Getopt::Std;
|
| 57 |
our ($opt_X, $opt_f); # the variables for getopt
|
| 58 |
&getopts('Xf:') || die <<EOF;
|
| 59 |
USAGE: [-X] no test, your harddisks will be formated
|
| 60 |
default: only test, no real formating
|
| 61 |
[-f<config-filename>] default: parse classes
|
| 62 |
EOF
|
| 63 |
|
| 64 |
# $disklist must be provided by the environment
|
| 65 |
defined ($ENV{disklist}) or die "Environment variable disklist is not set\n";
|
| 66 |
|
| 67 |
################################################################################
|
| 68 |
#
|
| 69 |
# @brief Really write any changes to disk
|
| 70 |
#
|
| 71 |
################################################################################
|
| 72 |
$FAI::no_dry_run = 0;
|
| 73 |
$opt_X and $FAI::no_dry_run = 1;
|
| 74 |
$opt_X or warn "setup-harddisks is running in test-only mode\n";
|
| 75 |
|
| 76 |
# include all subparts, which are part of the FAI perl package
|
| 77 |
use lib "/usr/share/fai/setup-storage/";
|
| 78 |
use init;
|
| 79 |
use volumes;
|
| 80 |
use parser;
|
| 81 |
use sizes;
|
| 82 |
use commands;
|
| 83 |
use fstab;
|
| 84 |
use exec;
|
| 85 |
|
| 86 |
# the config source file
|
| 87 |
my $config_file = undef;
|
| 88 |
# use the config file, if given
|
| 89 |
open($config_file, $opt_f) or die "Failed to open config file $opt_f\n" if ($opt_f);
|
| 90 |
unless ($opt_f) {
|
| 91 |
# see which class file to use
|
| 92 |
foreach my $classfile (reverse split(/\s+/, $ENV{classes})) {
|
| 93 |
next unless (-r "$ENV{FAI}/disk_config/$classfile");
|
| 94 |
open($config_file, "$ENV{FAI}/disk_config/$classfile")
|
| 95 |
or die "Failed to open $ENV{FAI}/disk_config/$classfile\n";
|
| 96 |
last;
|
| 97 |
}
|
| 98 |
}
|
| 99 |
|
| 100 |
# if we could not find any matching class file, bail out
|
| 101 |
defined ($config_file) or die "No matching disk_config found\n";
|
| 102 |
|
| 103 |
# start the parsing - thereby $FAI::configs is filled
|
| 104 |
&FAI::run_parser($config_file);
|
| 105 |
|
| 106 |
# read the sizes and partition tables of all disks listed in $FAI::disks
|
| 107 |
&FAI::get_current_disks;
|
| 108 |
|
| 109 |
# see whether there are any existing LVMs
|
| 110 |
# load the dm-mod module first, otherwise the LVM tools won't work
|
| 111 |
`modprobe dm-mod`;
|
| 112 |
&FAI::get_current_lvm;
|
| 113 |
|
| 114 |
# see whether there are any existing RAID devices
|
| 115 |
# load the md-mod module first, otherwise there is nothing that can be detected
|
| 116 |
`modprobe md-mod`;
|
| 117 |
&FAI::get_current_raid;
|
| 118 |
|
| 119 |
# debugging only: print the current configuration
|
| 120 |
if ($FAI::debug) {
|
| 121 |
# for debugging purposes to print the hash structures
|
| 122 |
use Data::Dumper;
|
| 123 |
|
| 124 |
print "Current disk layout\n";
|
| 125 |
|
| 126 |
# make sure perl doesn't warn about it being used only once, same below
|
| 127 |
our %current_config;
|
| 128 |
print Dumper \%current_config;
|
| 129 |
|
| 130 |
print "Current LVM layout\n";
|
| 131 |
|
| 132 |
our %current_lvm_config;
|
| 133 |
print Dumper \%current_lvm_config;
|
| 134 |
|
| 135 |
print "Current RAID layout\n";
|
| 136 |
|
| 137 |
our %current_raid_config;
|
| 138 |
print Dumper \%current_raid_config;
|
| 139 |
}
|
| 140 |
|
| 141 |
# compute the new LVM and partition sizes; do the partition sizes first to have
|
| 142 |
# them available for the the volume group size estimation
|
| 143 |
&FAI::compute_partition_sizes;
|
| 144 |
&FAI::compute_lv_sizes;
|
| 145 |
|
| 146 |
# print the current contents of $FAI::configs
|
| 147 |
$FAI::debug and print "Desired disk layout\n";
|
| 148 |
$FAI::debug and print Dumper \%FAI::configs;
|
| 149 |
|
| 150 |
# generate the command script
|
| 151 |
&FAI::build_disk_commands;
|
| 152 |
&FAI::build_raid_commands;
|
| 153 |
&FAI::build_lvm_commands;
|
| 154 |
|
| 155 |
# run all commands
|
| 156 |
# debugging only: print the command script
|
| 157 |
$FAI::debug and print "$_\n" foreach (@FAI::commands);
|
| 158 |
|
| 159 |
# run the commands (if $FAI::no_dry_run is set)
|
| 160 |
&FAI::execute_command($_) foreach (@FAI::commands);
|
| 161 |
|
| 162 |
# generate the proposed fstab contents
|
| 163 |
my @fstab = &FAI::generate_fstab(\%FAI::configs);
|
| 164 |
|
| 165 |
# print fstab
|
| 166 |
$FAI::debug and print "$_\n" foreach (@fstab);
|
| 167 |
|
| 168 |
# write the proposed contents of fstab to $LOGDIR/fstab
|
| 169 |
if ($FAI::no_dry_run) {
|
| 170 |
open(FSTAB, ">$ENV{LOGDIR}/fstab")
|
| 171 |
or die "Failed to open $ENV{LOGDIR}/fstab for writing\n";
|
| 172 |
print FSTAB "$_\n" foreach (@fstab);
|
| 173 |
close FSTAB;
|
| 174 |
}
|
| 175 |
|
| 176 |
# write variables to $LOGDIR/disk_var.sh
|
| 177 |
# debugging
|
| 178 |
$FAI::debug and print "$_=$FAI::disk_var{$_}\n"
|
| 179 |
foreach (keys %FAI::disk_var);
|
| 180 |
|
| 181 |
if ($FAI::no_dry_run)
|
| 182 |
{
|
| 183 |
open(DISK_VAR, ">$ENV{LOGDIR}/disk_var.sh")
|
| 184 |
or die "Unable to write to file $ENV{LOGDIR}/disk_var.sh\n";
|
| 185 |
print DISK_VAR "$_=$FAI::disk_var{$_}\n" foreach (keys %FAI::disk_var);
|
| 186 |
close DISK_VAR;
|
| 187 |
}
|
| 188 |
|