forked from crowbar/crowbar
-
Notifications
You must be signed in to change notification settings - Fork 6
/
start-up.sh
executable file
·97 lines (84 loc) · 2.62 KB
/
start-up.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
#!/bin/bash
set -x
shopt -s extglob
DHCPDIR=/var/lib/dhclient
RSYSLOGSERVICE=rsyslog
[ -e /etc/SuSE-release ] && {
DHCPDIR=/var/lib/dhcp
RSYSLOGSERVICE=syslog
}
# Figure out where we PXE booted from.
bootif_re='BOOTIF=([^ ]+)'
ip_re='inet ([0-9.]+)/([0-9]+)'
ik_re='crowbar\.install\.key=([^ ]+)'
if [[ $(cat /proc/cmdline) =~ $bootif_re ]]; then
MAC="${BASH_REMATCH[1]//-/:}"
MAC="${MAC#*:}"
elif [[ -d /sys/firmware/efi ]]; then
declare -A boot_entries
bootent_re='^Boot([0-9]{4})'
efimac_re='MAC\(([0-9a-f]+)'
while read line; do
k="${line%% *}"
v="${line#* }"
if [[ $k = BootCurrent:* ]]; then
current_bootent="${line##BootCurrent: }"
elif [[ $k =~ $bootent_re ]]; then
boot_entries["${BASH_REMATCH[1]}"]="$v"
fi
done < <(efibootmgr -v)
if [[ ${boot_entries["$current_bootent"]} =~ $efimac_re ]]; then
MAC=''
for o in 0 2 4 6 8 10; do
MAC+="${BASH_REMATCH[1]:$o:2}:"
done
MAC=${MAC%:}
fi
fi
for nic in /sys/class/net/*; do
[[ -f $nic/address && -f $nic/type && \
$(cat "$nic/type") = 1 && \
$(cat "$nic/address") = $MAC ]] || continue
BOOTDEV="${nic##*/}"
break
done
if [[ ! $BOOTDEV ]]; then
echo "We don't know what the MAC address of our boot NIC was!"
echo "We will assume we booted off eth0 and hope for the best."
BOOTDEV=eth0
MAC=$(cat /sys/class/net/eth0/address)
fi
killall dhclient && sleep 5
# Make sure our PXE interface is up, then fire up DHCP on it.
ip link set "$BOOTDEV" up
dhclient "$BOOTDEV"
if ! [[ $(ip -4 -o addr show dev $BOOTDEV) =~ $ip_re ]]; then
echo "We did not get an address on $BOOTDEV"
echo "Things will end badly."
fi
MYIP="${BASH_REMATCH[1]}"
ADMIN_IP=$(grep dhcp-server $DHCPDIR/dhclient*.leases | \
uniq | cut -d" " -f5 | cut -d";" -f1)
DOMAIN=$(grep "domain-name " $DHCPDIR/dhclient*.leases | \
uniq | cut -d" " -f5 | cut -d";" -f1 | awk -F\" '{ print $2 }')
HOSTNAME_MAC="d${MAC//:/-}.${DOMAIN}"
[[ $(cat /proc/cmdline) =~ $ik_re ]] && \
export CROWBAR_KEY="${BASH_REMATCH[1]}"
HOSTNAME=$(hostname)
[[ $HOSTNAME = localhost.localdomain ]] && {
hostname $HOSTNAME_MAC
HOSTNAME=${HOSTNAME_MAC}
}
# enable remote logging to our admin node.
echo "# Sledgehammer added to log to the admin node" >> /etc/rsyslog.conf
echo "*.* @@${ADMIN_IP}" >> /etc/rsyslog.conf
service $RSYSLOGSERVICE restart
# Setup common dirs
for d in updates install-logs; do
mkdir -p /$d
mount -t nfs $ADMIN_IP:/$d /$d
done
export MAC BOOTDEV ADMIN_IP DOMAIN HOSTNAME_MAC HOSTNAME MYIP
cd /updates
cp /updates/control.sh /tmp
/tmp/control.sh