-
Notifications
You must be signed in to change notification settings - Fork 0
/
pxe-add
executable file
·196 lines (184 loc) · 5.46 KB
/
pxe-add
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
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
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
#!/bin/bash
trap '{ stty sane; echo ""; errexit "Aborted"; }' SIGINT SIGTERM
MNTPATH="/tmp/pxe-add-mnt"
errexit()
{
echo ""
echo "$1"
echo ""
if [ "${MNTED}" = "TRUE" ]; then
umount "${BOOTMNT2}/" &> /dev/null
umount "${MNTPATH}/" &> /dev/null
fi
rm -rf "${MNTPATH}/" &> /dev/null
rmloop
echo "Usage: $0 serial_number image_file [client_ip_address] nodename macaddress"
echo ""
exit 1
}
mkloop()
{
LOOP="$(losetup -f --show -P "${IMGFILE}")"
if [ $? -ne 0 ]; then
errexit "Unable to create loop device"
fi
}
rmloop()
{
if [ "${LOOP}" != "" ]; then
losetup -d "${LOOP}"
LOOP=""
fi
}
mntimg()
{
mkloop
if [ ! -d "${MNTPATH}/" ]; then
mkdir "${MNTPATH}/"
if [ $? -ne 0 ]; then
errexit "Unable to make ROOT partition mount point"
fi
fi
mount "${LOOP}p2" "${MNTPATH}/"
if [ $? -ne 0 ]; then
errexit "Unable to mount image ROOT partition"
fi
MNTED=TRUE
BOOTMNT1="$(sed -n 's|^\S\+\s\+\(/boot\S*\)\s\+.*$|\1|p' "${MNTPATH}/etc/fstab")"
BOOTMNT2="${MNTPATH}${BOOTMNT1}"
if [ ! -d "${BOOTMNT2}/" ]; then
mkdir -p "${BOOTMNT2}/"
if [ $? -ne 0 ]; then
errexit "Unable to make BOOT partition mount point"
fi
fi
mount "${LOOP}p1" "${BOOTMNT2}/"
if [ $? -ne 0 ]; then
errexit "Unable to mount image BOOT partition"
fi
}
umntimg()
{
umount "${BOOTMNT2}/"
if [ $? -ne 0 ]; then
errexit "Unable to unmount image BOOT partition"
fi
umount "${MNTPATH}/"
if [ $? -ne 0 ]; then
errexit "Unable to unmount image ROOT partition"
fi
MNTED=FALSE
rmloop
rm -r "${MNTPATH}/"
}
LOOP=""
MNTED=FALSE
if [ $(id -u) -ne 0 ]; then
errexit "Must be run as root user: sudo $0"
fi
PGMNAME="$(basename $0)"
for PID in $(pidof -x -o %PPID "${PGMNAME}"); do
if [ ${PID} -ne $$ ]; then
errexit "${PGMNAME} is already running"
fi
done
SERIAL="$1"
IMGFILE="$2"
CLIENTIP="$3"
NODENAME="$4"
MACADDR="$5"
if [ "${SERIAL}" = "" ]; then
errexit "No serial number specified"
fi
SERIAL="$(tr [A-Z] [a-z] <<< "${SERIAL}")"
if [[ ! "${SERIAL}" =~ ^[[:xdigit:]]{8}$ ]]; then
errexit "Invalid serial number: ${SERIAL}"
fi
if [[ -e "/pxe-boot/${SERIAL}" || -e "/pxe-root/${SERIAL}" || $(grep -c "${SERIAL}" /etc/exports) -ne 0 ]]; then
errexit "Serial number ${SERIAL} already exists"
fi
if [ "${IMGFILE}" = "" ]; then
errexit "No image file specified"
fi
if [ ! -f "${IMGFILE}" ]; then
errexit "${IMGFILE} not found"
fi
if [ "${CLIENTIP}" = "" ]; then
CLIENTIP="*"
echo ""
echo "Access will be granted to ALL"
elif [[ "${CLIENTIP}" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ || \
"${CLIENTIP}" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/[0-9]{1,2}$ || \
"${CLIENTIP}" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
IP="${CLIENTIP}"
OIFS=${IFS}
IFS='./'
IP=(${IP})
IFS=${OIFS}
if [[ ${#IP[@]} -lt 4 || ${IP[0]} -gt 255 || ${IP[1]} -gt 255 || ${IP[2]} -gt 255 || ${IP[3]} -gt 255 || \
(${#IP[@]} -eq 5 && (${IP[4]} -lt 1 || ${IP[4]} -gt 32)) || \
(${#IP[@]} -eq 8 && (${IP[4]} -gt 255 || ${IP[5]} -gt 255 || ${IP[6]} -gt 255 || ${IP[7]} -gt 255)) ]]; then
errexit "Invalid client IP address: ${CLIENTIP}"
fi
echo ""
echo "Access will be granted to ${CLIENTIP}"
else
errexit "Invalid client IP address: ${CLIENTIP}"
fi
if [ "${MACADDR}" = "" ] ; then
errexit "No MAC address specified"
fi
echo ""
echo -n "Add ${SERIAL} (${IMGFILE}) to PXE Network Boot Server (y/n)? "
while read -r -n 1 -s answer; do
if [[ ${answer} = [yYnN] ]]; then
echo "${answer}"
if [[ ${answer} = [yY] ]]; then
break
else
errexit "Aborted"
fi
fi
done
mntimg
mkdir "/pxe-boot/${SERIAL}"
if [ $? -ne 0 ]; then
errexit "Unable to make BOOT files directory"
fi
echo ""
echo "Adding BOOT files"
rsync -ax "${BOOTMNT2}/" "/pxe-boot/${SERIAL}/"
if [ $? -ne 0 ]; then
errexit "Unable to add BOOT files"
fi
mkdir "/pxe-root/${SERIAL}"
if [ $? -ne 0 ]; then
errexit "Unable to make ROOT files directory"
fi
echo ""
echo "Adding ROOT files"
rsync -ax "${MNTPATH}/" "/pxe-root/${SERIAL}/"
if [ $? -ne 0 ]; then
errexit "Unable to add ROOT files"
fi
umntimg
HOSTIP="$(ip -4 addr show dev eth0 | sed -n 's|^\s*inet\s\+\(\S\+\)/\S\+\s.*|\1|p')"
sed -i "s|\(^.*root=\).*$|\1/dev/nfs nfsroot=${HOSTIP}:/pxe-root/${SERIAL},vers=4.1,proto=tcp rw ip=dhcp rootwait|" "/pxe-boot/${SERIAL}/cmdline.txt"
sed -i "/\s\+\/boot\S*\s\+/d" "/pxe-root/${SERIAL}/etc/fstab"
sed -i '/\s\+\/\s\+/d' "/pxe-root/${SERIAL}/etc/fstab"
echo "${HOSTIP}:/pxe-boot/${SERIAL} ${BOOTMNT1} nfs defaults,vers=4.1,proto=tcp 0 0" >> "/pxe-root/${SERIAL}/etc/fstab"
echo "${HOSTIP}:/pxe-root/${SERIAL} / nfs defaults,vers=4.1,proto=tcp 0 0" >> "/pxe-root/${SERIAL}/etc/fstab"
echo "/pxe-boot/${SERIAL} ${CLIENTIP}(rw,sync,no_subtree_check,no_root_squash)" >> /etc/exports
echo "/pxe-root/${SERIAL} ${CLIENTIP}(rw,sync,no_subtree_check,no_root_squash)" >> /etc/exports
echo "/home ${CLIENTIP}(rw,sync,no_subtree_check,no_root_squash)" >> /etc/exports
echo "/modules ${CLIENTIP}(rw,sync,no_subtree_check,no_root_squash)" >> /etc/exports
echo "/sharedfs ${CLIENTIP}(rw,sync,no_subtree_check,no_root_squash)" >> /etc/exports
echo "NodeName=${NODENAME} NodeAddr=${CLIENTIP} CPUs=4 State=UNKNOWN" >> /etc/slurm/slurm.conf
echo "${NODENAME}" > "/pxe-root/${SERIAL}/etc/hostname"
echo "${CLIENTIP} ${NODENAME}" >> "/etc/hosts"
echo "dhcp-host=${MACADDR},${CLIENTIP}" >> "/etc/dnsmasq.conf"
exportfs -ar
sync
echo ""
echo "${SERIAL} \(${IMGFILE}\) added to PXE Network Boot Server"
echo ""