fixup-fstab-swaps 4.98 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2007-2016 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file 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 Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
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
#
use English;
use Getopt::Std;

# Drag in path stuff so we can find emulab stuff.
BEGIN { require "/etc/emulab/paths.pm"; import emulabpaths; }

sub usage()
{
    print "Usage: fixup-fstab-swaps [-U] [-E]\n";
    exit(1);
}

my $optlist = "UE";

my $FSTAB = "/etc/fstab";
my $SWAPS = "/proc/swaps";
my $FIRST_BOOT_FLAG = "$BOOTDIR/added_swaps";
my $script = $0;

my $noupdate = 0;
my $noenable = 0;

sub removeswaps();
sub findswaps();
sub swapenable();

# Turn off line buffering on output
$| = 1;

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"U"})) {
    $noupdate = 1;
}
if (defined($options{"E"})) {
    $noenable = 1;
}
if (@ARGV) {
    usage();
}

# Remove any existing swap devices from fstab; we can't ensure that they'll
# work on this node.  This only runs the first time the node is booted after
# being imaged.  Then look for linux swap partitions on the root disk and add
# them to fstab.  This way we have sane defaults for swap.  The user is, of
# course, free to change the swap configration later if necessary.
if (! -f $FIRST_BOOT_FLAG && !$noupdate) {

	removeswaps();
	findswaps();

	open FD, ">$FIRST_BOOT_FLAG";
	close FD;
}

#
# Figure out if there is a swap device, add it to fstab and intialize if
# necessary.
#
swapenable()
    if (!$noenable);

exit(0);

sub removeswaps()
{
    my @buffer;

    if (!open(FD, "+<$FSTAB")) {
	print STDERR "*** WARNING: could not open $FSTAB for writing: $!\n";
	return;
    }
    while (<FD>) {
	next if (/^\s*$/ || /^#/);
	my ($fs,$mpt,$type,$opt) = split;
	if ($type eq "swap") {
	    print "Removing old swap device $fs\n";
	    next;
	}

	push @buffer, $_;
    }

    truncate FD, 0;
    seek(FD, 0, 0);
    print FD $_ for (@buffer);
    close(FD);
}

sub findswaps()
{
    #
    # No swap device.
    # Identify the root disk and see if we can locate a Linux swap partition
    # on it.
    #
    my @swapdevs;
    my $rdisk;
    my $rootfs = `df / | grep /dev/`;
129 130 131
    if ($rootfs =~ /^(\/dev\/nvme\d+n\d+)p\d+/) {
	$rdisk = $1;
    } elsif ($rootfs =~ /^(\/dev\/[a-z]+)\d+/) {
132
	$rdisk = $1;
133 134 135 136 137 138 139 140 141 142 143 144 145
    }
    elsif ($rootfs =~ /^(\/dev\/[^\s]+)\s/) {
	$rootfs = `readlink -f $1`;
	if ($rootfs =~ /^(\/dev\/[a-z]+)\d+/) {
	    $rdisk = $1;
	}
	else {
	    print STDERR "*** WARNING: could not identify root disk, ".
		"no swap enabled\n";
	    return;
	}
    }
    else {
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
	print STDERR "*** WARNING: could not identify root disk, ".
	    "no swap enabled\n";
	return;
    }

    @lines = `fdisk -l $rdisk 2>/dev/null | grep 'Linux swap'`;
    if ($? != 0) {
	print STDERR "*** WARNING: could not read MBR of $rdisk, ".
	    "no swap enabled\n";
	return;
    }

    chomp(@lines);
    foreach $line (@lines) {
	if ($line =~ /^(\/dev\/\S+)\s+\d+\s+\d+\s+(\d+)\+?/) {
	    my $dev = $1;
	    my $size = $2;
	    print "Using $dev ($size sectors) for swap\n";
	    push @swapdevs, $dev;
	}
    }

    if (!@swapdevs) {
        print STDERR "*** WARNING: could not locate a suitable swap device, ".
	    "no swap enabled\n";
	return;
    }

    if (!open(FD, ">>$FSTAB")) {
	print STDERR "*** WARNING: could not add swap devices to $FSTAB: $!\n";
	return;
    }

    print FD "# the following swap devices added by $script\n";
    for (@swapdevs) {
	print FD "$_\t\tswap\tswap\tdefaults\t0\t0\n";
    }
    close(FD);
}

sub swapenable()
{
    my %curswaps;

    if (!open(FD, "<$SWAPS")) {
	print STDERR "*** WARNING: could not open $SWAPS for reading: $!\n";
	return;
    }

    <FD>; # Throw away the header
    while(<FD>) {
	    @_ = split;
	    $curswaps{$_[0]} = 1;
    }
    close FD;

    if (!open(FD, "<$FSTAB")) {
	print STDERR "*** WARNING: could not open $FSTAB for reading, ".
	    "no swap enabled\n";
	return;
    }
    while (<FD>) {
	next if (/^\s*$/ || /^#/);
	my ($fs,$mpt,$type,$opt) = split;
	next if ($type ne "swap" || ! -b $fs || exists $curswaps{$fs});
	next if ($opt =~ /\bnoauto\b/);

	if (system("mkswap $fs")) {
		print STDERR "*** WARNING: could not initialize swap on $fs\n";
		next;
	}

	if (system("swapon $fs")) {
	    print STDERR "*** WARNING: could not enable swap on $fs\n";
	    next;
	}
    }
    close(FD);

}