rc.freebsd 3.93 KB
Newer Older
Mike Hibler's avatar
Mike Hibler committed
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/>.
# 
# }}}
Mike Hibler's avatar
Mike Hibler committed
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
#
use English;
use Getopt::Std;

my $FSTAB = "/etc/fstab";
my $script = $0;

#
# Figure out if there is a swap device and add it to fstab.
# We used to do this in slicefix as part of the post-frisbee customization,
# but it is difficult to do a good job of it just using sh and sed given
# that the swap partition might be in a different slice than the filesystems.
#
sub swapenable();
sub addswap($$);

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

swapenable();
exit(0);

sub swapenable()
{
    my $rootfs;
    my $nextcheck = 0;

    if (!open(FD, "<$FSTAB")) {
	print STDERR "*** WARNING: could not open $FSTAB for reading, ".
	    "no swap enabled\n";
	return;
    }
    while (<FD>) {
	my ($fs,$mpt,$type,$opt) = split;
	if ($mpt eq "/") {
	    $rootfs = $fs;
	    next;
	}
	if ($type eq "swap") {
	    close(FD);
	    return;
	}
    }
    close(FD);

    #
    # No swap device found, see if the disklabel of the root disk reveals one
    #
    my ($dtype, $dnum, $snum, $part);
    if ($rootfs =~ /([a-zA-Z]+)(\d+)s(\d+)([a-z])/ ||
	$rootfs =~ /([a-zA-Z]+)(\d+)([a-z])/) {
	$dtype = $1;
	$dnum = $2;
	if (!defined($part)) {
	    $snum = "1";
	    $part = $3;
	}
	else {
	    $snum = $3;
	    $part = $4;
	}
    }
    else {
	print STDERR "*** WARNING: could not parse root device $rootfs, ".
	    "no swap enabled\n";
	return;
    }

    my $disk = "${dtype}${dnum}";
    my $slice = "${disk}s${snum}";
    if (-e "/dev/$slice") {
	my @lines = `disklabel $slice 2>/dev/null`;
	if ($? != 0) {
	    print STDERR "*** WARNING: could not read disklabel of /dev/$slice, ".
		"no swap enabled\n";
	    return;
	}

	chomp(@lines);
	foreach $line (@lines) {
	    if ($line =~ /\s*([a-z]):\s+(\d+)\s+\d+\s+(\S+)/) {
		my $part = $1;
		my $size = $2;
		my $fstype = $3;
		if ($fstype eq "swap") {
		    addswap("$slice$part", $size);
		    return;
		}
	    }
	}
    }

    #
    # Still nothing, look at the MBR to see if there is a Linux swap partition
    # we can use.
    #
    while (-e "/dev/$disk") {
	@lines = `fdisk -s /dev/$disk 2>/dev/null`;
	if ($? != 0) {
	    print STDERR "*** WARNING: could not read MBR of /dev/$disk, ".
		"no swap enabled\n";
	    return;
	}

	chomp(@lines);
	foreach $line (@lines) {
	    if ($line =~ /\s*(\d):\s+\d+\s+(\d+)\s+(\S+)/) {
		my $part = $1;
		my $size = $2;
		my $ptype = $3;
		if ($ptype eq "0x82") {
		    addswap("${disk}s${part}", $size);
		    return;
		}
	    }
	}
	#
	# Cycle through all disks looking for one with a linux swap partition.
	#
	$dnum++;
	$disk = "${dtype}${dnum}";
    }

    print STDERR "*** WARNING: could not locate a suitable swap device, ".
	"no swap enabled\n";
}

sub addswap($$)
{
    my ($dev,$size) = @_;

    print "Using /dev/$dev ($size sectors) for swap\n";
    if (system("swapon /dev/$dev")) {
	print STDERR "*** WARNING: could not enable swap on /dev/$dev, ".
	    "no swap enabled\n";
	exit(0);
    }
    if (!open(FD, ">>$FSTAB")) {
	print STDERR "*** WARNING: could not add /dev/$dev to $FSTAB, ".
	    "swap enabled this boot only\n";
    }
164
    print FD "# the following swap devices added by $script\n";
Mike Hibler's avatar
Mike Hibler committed
165
166
167
168
    print FD "/dev/$dev\t\tnone\tswap\tsw\t0\t0\n";
    close(FD);
    print "Swap enabled\n";
}