source: trunk/debathena/config/recovery-mode-config/debian/athena-renumber @ 24395

Revision 24395, 5.1 KB checked in by jdreed, 14 years ago (diff)
In recovery-mode-config: * Remove "really-reboot", because it doesn't actually workaround kexec, because kexec sucks * Revert to shutting down after updates and renumbers, because kexec sucks * Call athena-auto-update with new (as of 1.15) -n option to ensure it won't reboot and end up back in recovery-mode, because kexec sucks
  • Property svn:executable set to *
Line 
1#!/usr/bin/perl -w
2#
3#
4
5use strict;
6use Socket;
7use File::Copy;
8use Getopt::Std;
9
10my $MASKS="/usr/share/debathena-recovery-mode-config/masks";
11
12if (defined($ARGV[0]) && ($ARGV[0] eq 'test')) {
13    print "Change IP address or hostname\n";
14    exit 0;
15}
16
17sub confirm {
18    my $question = shift || "Continue?";
19    print "${question} (y/n) ";
20    my $ans = <STDIN>;
21    return ($ans =~ m/^y$/i);
22}
23
24sub bail {
25    print shift;
26    print "\n\n(press ENTER to return to the recovery mode menu)\n";
27    my $dummy = <STDIN>;
28    exit 0;
29}
30
31sub mask {
32    my $ip = shift;
33    my $mask = shift;
34    my $mbits = pack("B*", (1 x $mask) . (0 x (32 - $mask)));
35    my $ibits = pack("CCCC", split(/\./, $ip));
36    my $net = $ibits & $mbits;
37    return join('.', unpack("CCCC", $net));
38}
39
40sub cidr2ip {
41    my $cidr = shift;
42    return join('.', unpack("CCCC", pack("B*", (1 x $cidr) . (0 x (32 - $cidr)))));
43}
44
45sub validIP {
46    my $ip = shift;
47    if ($ip =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/) {
48        if (scalar(grep { ($_ <= 255) && ($_ >= 0) } ($1, $2, $3, $4)) == 4) {
49            return 1;
50        }
51    }
52    return 0;
53}
54
55# main
56
57# Sanity checks
58foreach (qw(/etc/network/interfaces /etc/hosts /etc/hostname)) {
59    (-w $_) || bail("Cannot write to $_. (Are you root?)");
60}
61
62(-r $MASKS) || bail("Cannot read $MASKS");
63
64my $machtype = `machtype -L`;
65chomp($machtype);
66if ($machtype ne 'debathena-cluster') {
67    print "WARNING: This script is designed for debathena-cluster machines but\n";
68    print "this machine is running $machtype.\n";
69    confirm("Are you SURE you want to continue?") || bail("OK, cancelled.");
70}
71
72my $ip = '';
73until (validIP($ip)) {
74    if ($ip) {
75        print "Invalid IP address ($ip).  Try again.\n";
76    }
77    print "Enter the new IP address: ";
78    ($ip = <STDIN>) =~ s/\s//g;
79}
80print "Looking up hostname...";
81my $fqdn = gethostbyaddr(inet_aton($ip), AF_INET);
82if ($fqdn) {
83    print " found.\n";
84} else {
85    $fqdn = '';
86    print " not found.\n";
87    until ($fqdn =~ /^[a-z][\w\-\.]*/i) {
88        print "Please enter the machine's new hostname: ";
89        ($fqdn = <STDIN>) =~ s/\s//g;
90    }
91}
92$fqdn = lc($fqdn);
93my $hostname = $fqdn;
94if ($fqdn =~ /\./) {
95    $hostname = (split(/\./, $fqdn))[0];
96} else {
97    print "NOTE: No domain specified, assuming 'mit.edu'";
98    $fqdn = "${hostname}.mit.edu";
99}
100
101my ($subnetmask, $gateway);
102print "Looking up netmask information...\n";
103open(MASKS, $MASKS) || bail("Cannot read $MASKS: $!");
104while (<MASKS>) {
105    next if (/^#/);
106    next unless (/\d/);
107    my ($addr, $sbits, $mbits, $gw) = split(' '); #the ' ' pattern emulates awk
108    if (mask($ip, $sbits) eq $addr) {
109        $subnetmask = cidr2ip($mbits);
110        if (! $gw) {
111            my @octets = split(/\./, mask($ip, $mbits));
112            my $trailing = pop(@octets) + 1;
113            $gateway = join('.', @octets, $trailing);
114        } else {
115            $gateway = $gw;
116        }
117        last;
118    }
119}
120(defined($subnetmask) && defined($gateway)) || bail("Could not find a matching line in masks file for $ip.\nIf you are sure you typed the IP address correctly, please report\nthis error to IS&T, along with the IP address you were trying to use.");
121
122close(MASKS);
123print "IP address:  $ip\n";
124print "Subnet Mask: $subnetmask\n";
125print "Gateway:     $gateway\n";
126print "Hostname:    $hostname\n";
127print "FQDN:        $fqdn\n\n";
128
129confirm("Does that look correct?") || bail("OK, cancelled.");
130
131print "Updating /etc/network/interfaces...\n";
132copy("/etc/network/interfaces", "/etc/network/interfaces.old") || bail("Could not copy /etc/network/interfaces");
133open(INTERFACES, ">/etc/network/interfaces") || bail ("Could not overwrite /etc/network/interfaces: $!");
134print INTERFACES <<"ENDINTERFACES";
135# This file was created by $0
136
137# The loopback interface
138auto lo
139iface lo inet loopback
140
141# The primary network interface
142auto eth0
143iface eth0 inet static
144        address $ip
145        netmask $subnetmask
146        gateway $gateway
147        dns-nameservers 18.70.0.160 18.71.0.151 18.72.0.3
148ENDINTERFACES
149close(INTERFACES);
150
151print "Updating /etc/hosts...\n";
152
153copy("/etc/hosts", "/etc/hosts.old") || bail("Could not copy /etc/hosts");
154open(HOSTS, ">/etc/hosts") || bail ("Could not overwrite /etc/hosts: $!");
155print HOSTS <<"ENDHOSTS";
156# This file was created by $0
157127.0.0.1       localhost
158127.0.1.1       $fqdn $hostname
159
160# The following lines are desirable for IPv6 capable hosts
161::1     localhost ip6-localhost ip6-loopback
162fe00::0 ip6-localnet
163ff00::0 ip6-mcastprefix
164ff02::1 ip6-allnodes
165ff02::2 ip6-allrouters
166ff02::3 ip6-allhosts
167ENDHOSTS
168close(HOSTS);
169                                   
170print "Updating /etc/hostname...\n";
171copy("/etc/hostname", "/etc/hostname.old") || bail("Could not copy /etc/hostname");
172open(HOSTNAME, ">/etc/hostname") || bail ("Could not overwrite /etc/hostname: $!");
173print HOSTNAME "$hostname\n";
174close(HOSTNAME);
175
176print "Setting hostname...\n";
177system("/bin/hostname", $hostname);
178print "\nDone!  You will need to reboot the workstation for the\n";
179print "changes to take effect.\n";
180confirm("Would you like to reboot now?") || bail("Please perform a full shutdown and reboot as soon as possible.");
181print "The workstation will now power down completely.\n";
182print "When you turn it back on, it will use the new IP address.\n\n";
183print "(press ENTER to power down the workstation)";
184getc(STDIN);
185system("/sbin/poweroff");
186exit 0;
Note: See TracBrowser for help on using the repository browser.