[25023] | 1 | #!/bin/sh |
---|
[24348] | 2 | # |
---|
[25023] | 3 | # Change the IP/hostname on cluster machines |
---|
| 4 | # |
---|
[24348] | 5 | |
---|
[25023] | 6 | # Required for recovery-mode scripts |
---|
| 7 | if [ "$1" = "test" ]; then |
---|
| 8 | echo "Change IP address or hostname" |
---|
| 9 | exit 0 |
---|
| 10 | fi |
---|
[24348] | 11 | |
---|
[25023] | 12 | if [ "$(id -u)" != "0" ]; then |
---|
| 13 | echo "This script must be run as root." |
---|
| 14 | exit 0 |
---|
| 15 | fi |
---|
[24398] | 16 | |
---|
[24348] | 17 | |
---|
[25023] | 18 | # Blatantly stolen from the installer |
---|
| 19 | ask() { |
---|
| 20 | answer='' |
---|
| 21 | while [ y != "$answer" -a n != "$answer" ]; do |
---|
| 22 | echo -n "$1" |
---|
| 23 | read answer |
---|
| 24 | [ Y = answer ] && answer=y |
---|
| 25 | [ N = answer ] && answer=n |
---|
| 26 | [ -z "$answer" ] && answer=$2 |
---|
| 27 | done |
---|
[24348] | 28 | } |
---|
| 29 | |
---|
[25023] | 30 | bail() { |
---|
| 31 | echo |
---|
| 32 | echo "Cancelled. Press Enter to the menu." |
---|
| 33 | read dummy |
---|
[24348] | 34 | exit 0; |
---|
| 35 | } |
---|
| 36 | |
---|
[25599] | 37 | # Precise mounts root read-only in recovery mode. |
---|
| 38 | mount -o remount,rw / |
---|
| 39 | |
---|
[25023] | 40 | # sulogin gets upset if it gets a ^C |
---|
| 41 | trap bail INT |
---|
[24348] | 42 | |
---|
[25023] | 43 | PATH="/sbin:/bin:/usr/bin:/usr/sbin:$PATH" |
---|
[24348] | 44 | |
---|
[25023] | 45 | if [ "$DEBATHENA_DEBUG" = "1" ]; then |
---|
| 46 | echo "**********\nDEBUG MODE\n**********" |
---|
| 47 | MASKS="$(pwd)/masks"; |
---|
| 48 | NETPARAMS="$(pwd)/netparams"; |
---|
| 49 | . "$(pwd)/require_network.sh" |
---|
| 50 | else |
---|
| 51 | MASKS="/usr/share/debathena-recovery-mode-config/masks"; |
---|
| 52 | NETPARAMS="/usr/share/debathena-recovery-mode-config/netparams"; |
---|
| 53 | . /usr/share/debathena-recovery-mode-config/require_network.sh |
---|
| 54 | fi |
---|
[24348] | 55 | |
---|
[25023] | 56 | if [ "$DEBATHENA_DEBUG" != "1" ] && \ |
---|
| 57 | [ "$(machtype -L)" != "debathena-cluster" ]; then |
---|
| 58 | echo "WARNING: This script is designed for debathena-cluster machines" |
---|
| 59 | echo "but this machine is running $(machtype -L).\n"; |
---|
| 60 | ask "Are you SURE you want to continue? (y/N) " n |
---|
| 61 | if [ "$answer" = "n" ]; then |
---|
| 62 | exit 0 |
---|
| 63 | fi |
---|
| 64 | fi |
---|
[24348] | 65 | |
---|
[25023] | 66 | echo "Testing networking, please wait..." |
---|
| 67 | if ! require_network; then |
---|
| 68 | echo "Can't verify that networking is available. If you continue," |
---|
| 69 | ask "errors may occur. Continue? (y/N) " n |
---|
| 70 | if [ "$answer" = "n" ]; then |
---|
| 71 | exit 0 |
---|
| 72 | fi |
---|
| 73 | fi |
---|
[24348] | 74 | |
---|
[25023] | 75 | while true; do |
---|
| 76 | while [ -z "$IPADDR" ] ; do |
---|
| 77 | echo -n "Enter IP address: " |
---|
| 78 | read IPADDR |
---|
| 79 | done |
---|
[24348] | 80 | |
---|
[25023] | 81 | NETMASK=`$NETPARAMS -f $MASKS $IPADDR|cut -d\ -f 1` |
---|
| 82 | GATEWAY=`$NETPARAMS -f $MASKS $IPADDR|cut -d\ -f 4` |
---|
[24348] | 83 | |
---|
[25023] | 84 | echo "Address: $IPADDR" |
---|
| 85 | echo |
---|
| 86 | echo "Autoconfigured settings:" |
---|
| 87 | echo " Netmask: $NETMASK" |
---|
| 88 | echo " Gateway: $GATEWAY" |
---|
| 89 | echo |
---|
| 90 | ask "Is this correct? (Y/n) " y |
---|
| 91 | [ "$answer" = "y" ] || continue |
---|
| 92 | echo "Looking up hostname..."; |
---|
| 93 | FQDN="$(dig +short -x $IPADDR 2>/dev/null | sed -e 's/.$//')" |
---|
| 94 | if [ $? != 0 ]; then |
---|
| 95 | echo "Unable to look up hostname. You may enter it below" |
---|
| 96 | echo "but beware that typos may render your machine unusable." |
---|
| 97 | FQDN= |
---|
| 98 | while [ -z "$FQDN" ] ; do |
---|
| 99 | echo -n "Enter hostname: " |
---|
| 100 | read FQDN |
---|
| 101 | FQDN="$(echo $FQDN | tr -d ' ')" |
---|
| 102 | if echo "$FQDN" | egrep -qi "[^a-z0-9_-\.]"; then |
---|
| 103 | echo "Invalid characters in hostname, try again." |
---|
| 104 | FQDN= |
---|
| 105 | fi |
---|
| 106 | done |
---|
| 107 | fi |
---|
| 108 | if ! echo "$FQDN" | grep -qi "MIT.EDU$"; then |
---|
| 109 | FQDN="$FQDN.MIT.EDU" |
---|
| 110 | fi |
---|
| 111 | echo "Hostname: $FQDN" |
---|
| 112 | ask "Is this correct? (Y/n) " y |
---|
| 113 | if [ "$answer" = "y" ]; then |
---|
| 114 | break |
---|
| 115 | fi |
---|
| 116 | done |
---|
| 117 | HOSTNAME="$(echo "$FQDN" | cut -d. -f 1)" |
---|
| 118 | DOMAIN="$(echo "$FQDN" | sed 's/[^\.]*\.//')" |
---|
| 119 | if [ "$DEBATHENA_DEBUG" = "1" ]; then |
---|
| 120 | echo "$IPADDR $NETMASK $GATEWAY $HOSTNAME $DOMAIN $FQDN" |
---|
| 121 | echo "Done!" |
---|
| 122 | exit 0 |
---|
| 123 | fi |
---|
| 124 | echo "Updating /etc/network/interfaces..." |
---|
| 125 | mv -f "/etc/network/interfaces" "/etc/network/interfaces.old" |
---|
| 126 | cat <<EOF >/etc/network/interfaces |
---|
| 127 | # This file was created by athena-renumber |
---|
[24348] | 128 | |
---|
| 129 | # The loopback interface |
---|
| 130 | auto lo |
---|
| 131 | iface lo inet loopback |
---|
| 132 | |
---|
| 133 | # The primary network interface |
---|
| 134 | auto eth0 |
---|
| 135 | iface eth0 inet static |
---|
[25023] | 136 | address $IPADDR |
---|
| 137 | netmask $NETMASK |
---|
| 138 | gateway $GATEWAY |
---|
[24348] | 139 | dns-nameservers 18.70.0.160 18.71.0.151 18.72.0.3 |
---|
[25071] | 140 | dns-search mit.edu |
---|
[25023] | 141 | EOF |
---|
| 142 | echo "Updating /etc/hosts..." |
---|
| 143 | mv -f "/etc/hosts" "/etc/hosts.old" |
---|
| 144 | cat <<EOF >/etc/hosts |
---|
| 145 | # This file was created by athena-renumber |
---|
[24348] | 146 | 127.0.0.1 localhost |
---|
[25627] | 147 | $IPADDR $FQDN $HOSTNAME |
---|
[24348] | 148 | |
---|
| 149 | # The following lines are desirable for IPv6 capable hosts |
---|
| 150 | ::1 localhost ip6-localhost ip6-loopback |
---|
| 151 | fe00::0 ip6-localnet |
---|
| 152 | ff00::0 ip6-mcastprefix |
---|
| 153 | ff02::1 ip6-allnodes |
---|
| 154 | ff02::2 ip6-allrouters |
---|
| 155 | ff02::3 ip6-allhosts |
---|
[25023] | 156 | EOF |
---|
| 157 | echo "Updating /etc/hostname..." |
---|
| 158 | mv -f /etc/hostname /etc/hostname.old |
---|
| 159 | echo $HOSTNAME > /etc/hostname |
---|
| 160 | echo "Setting hostname..."; |
---|
| 161 | hostname $HOSTNAME |
---|
| 162 | echo |
---|
| 163 | echo "Done! You will need to reboot the workstation for the changes to take effect." |
---|
| 164 | ask "Would you like to reboot now? (Y/n) " y |
---|
| 165 | if [ "$answer" = "n" ]; then |
---|
| 166 | echo "Please perform a full shutdown and reboot as soon as possible." |
---|
| 167 | echo "(Press Enter to return to the menu)" |
---|
| 168 | exit 0 |
---|
| 169 | fi |
---|
| 170 | reboot |
---|