-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnyble-ramboot-init.d
executable file
·352 lines (299 loc) · 10.2 KB
/
nyble-ramboot-init.d
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/bin/bash -x
#
# chkconfig: 235 99 99
# description: nyble post boot config
### BEGIN INIT INFO
# Provides: nyble
# Required-Start: $local_fs $syslog
# Required-Stop: $local_fs $syslog sendsigs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: nyble post boot configuration script
# Description: nyble provides a post boot configuration mechanism
#
### END INIT INFO
### the opening brace below is matched by the closing brace at the
# bottom of the file. DONT REMOVE IT, or you lose logging.
{
#
# nyble provides post boot config
#
# description: Provide post boot config
# Author: joe.landman\@nlytiq.com
#
# Modified by:
# 2017/01/27 - Joe Landman <joe\@nlytiq.com>
# Initial setup
#
# /etc/init.d/nyble
#
RETVAL=0
/sbin/ldconfig
if [ -e /etc/debian_version ]; then
DHCLIENT=/sbin/dhclient
fi
if [ -e /etc/redhat-release ]; then
DHCLIENT=/sbin/dhclient
fi
#. /etc/init.d/functions
# See how we were called.
case "$1" in
start)
echo -n "Starting post boot process configuration: "
# sanity check /proc /sys and /dev
if [ ! -d /proc ]; then
mkdir -p /proc
mount -t procfs procfs /proc
fi
if [ ! -d /sys ]; then
mkdir -p /sys
mount -t sysfs sysfs /sys
fi
if [ ! -d /dev ]; then
mkdir -p /dev
mount -t devtmpfs devtmpfs /dev
fi
# grab rootpw= if it exists
if (grep -q rootpw= /proc/cmdline); then
rootpw=$(/opt/nyble/bin/get_cmdline_key.pl rootpw)
echo "root:$rootpw" | chpasswd
fi
# resize root ramdisk if rootsize= exists and root is on tmpfs
if (grep -q rootsize= /proc/cmdline); then
rootfstype=$(mount | grep `df -h / | tail -1 | cut -d" " -f1` | head -1 | cut -f5 -d" ")
if [[ "$rootfstype" == "tmpfs" ]]; then
rootsize=$(/opt/nyble/bin/get_cmdline_key.pl rootsize)
mount -o remount,size=$rootsize /
fi
fi
# disablettyS0=1 turns off serial console on ttyS0
if (grep -q disablettyS0=1 /proc/cmdline); then
systemctl disable [email protected]
systemctl stop [email protected]
fi
# disablettyS1=1 turns off serial console on ttyS1
if (grep -q disablettyS1=1 /proc/cmdline); then
systemctl disable [email protected]
systemctl stop [email protected]
fi
# disablettyS2=1 turns off serial console on ttyS2
if (grep -q disablettyS2=1 /proc/cmdline); then
systemctl disable [email protected]
systemctl stop [email protected]
fi
# use simplified networking if simplenet=1 is in boot commandline
# basically bring up each ethernet (eth*) and then see whom has a
# carrier. DHCP those interfaces. This breaks if portfast is not
# enabled on your switches (it should be enabled unless there is a
# very good reason to disable it)
# reset network state, loading specific drivers
# bring interfaces up
for net in $(ls /sys/class/net/ | grep -v lo ) ; do
/sbin/ifconfig $net up
done
sleep 3
allports=""
ports=""
for net in $(ls /sys/class/net/ | grep -v lo ); do
allports="$allports $net"
carrier="$( cat /sys/class/net/$net/carrier )"
if [ $carrier -eq 1 ] ; then
ports="$ports $net"
fi
done
# startup openibd if we have Mellanox cards
if [ `lspci | grep Mell | wc -l` -gt 0 ]; then
/etc/init.d/openibd start
fi
if (grep -q simplenet=1 /proc/cmdline); then
sleep 10
# sleep 10 seconds to make sure they establish link, then
# probe for link. dhclient on all interfaces with a link
${DHCLIENT} -x
echo "ports= $ports"
${DHCLIENT} -v $ports
fi
#set specific network IP/mask, DNS, default GW
if (grep -q net_if= /proc/cmdline); then
net_if=$(/opt/nyble/bin/get_cmdline_key.pl net_if)
if (grep -q net_addr= /proc/cmdline); then
net_addr=$(/opt/nyble/bin/get_cmdline_key.pl net_addr)
ifconfig $net_if $net_addr up
else
dhclient -x
sleep 2
dhclient -v $net_if
fi
fi
if (grep -q net_dns= /proc/cmdline); then
net_dns=$(/opt/nyble/bin/get_cmdline_key.pl net_dns)
echo "nameserver $net_dns" > /etc/resolv.conf
fi
if (grep -q net_gw= /proc/cmdline); then
net_gw=$(/opt/nyble/bin/get_cmdline_key.pl net_gw)
route add default gw $net_gw
fi
#bridge control: set specific network IP/mask, DNS, default GW, bridge ports
if (grep -q br_name= /proc/cmdline); then
br_name=$(/opt/nyble/bin/get_cmdline_key.pl br_name)
brctl addbr $br_name
# either specify interface names with br_if ...
if (grep -q br_if= /proc/cmdline); then
br_ifs=$(/opt/nyble/bin/get_cmdline_key.pl br_if)
list=$(echo $br_ifs | sed 's|\,| |g')
for br_if in $list; do
brctl addif $br_name $br_if
ifconfig $br_if up
done
else
# or have the system add interfaces that are marked as
# having a carrier after being brought up, as being in
# the bridge
echo "Bridged interfaces = " $ports
for br_if in $ports; do
brctl addif $br_name $br_if
ifconfig $br_if up
done
fi
ifconfig $br_name up
if (grep -q br_addr= /proc/cmdline); then
net_addr=$(/opt/nyble/bin/get_cmdline_key.pl net_addr)
ifconfig $br_name $net_addr up
else
dhclient -x
sleep 3
dhclient -v $br_name
fi
if (grep -q net_dns= /proc/cmdline); then
net_dns=$(/opt/nyble/bin/get_cmdline_key.pl net_dns)
echo "nameserver $net_dns" > /etc/resolv.conf
fi
if (grep -q net_gw= /proc/cmdline); then
net_gw=$(/opt/nyble/bin/get_cmdline_key.pl net_gw)
route add default gw $net_gw
fi
if (grep -q br_stp= /proc/cmdline); then
br_stp=$(/opt/nyble/bin/get_cmdline_key.pl br_stp)
brctl stp $br_name $br_stp
fi
if (grep -q br_fd= /proc/cmdline); then
br_fd=$(/opt/nyble/bin/get_cmdline_key.pl br_fd)
brctl setfd $br_name $br_fd
fi
fi
# grab rootpw= if it exists
if (grep -q rootpw= /proc/cmdline); then
rootpw=$(/opt/nyble/bin/get_cmdline_key.pl rootpw)
echo "root:$rootpw" | chpasswd
fi
# grab sssdconfig= if it exists
if (grep -q sssdconfig= /proc/cmdline); then
sssdconfig=$(/opt/nyble/bin/get_cmdline_key.pl ssdconfig)
mkdir -p /etc/sssd
cd /etc/sssd ; wget $sssdconfig -O sssd.config
chmod 700 /etc/sssd
chroot 600 /etc/sssd/sssd.config
systemctl enable sssd.service
systemctl start sssd.service
fi
# grab rootauthorizedkeys= if it exists
if (grep -q rootauthorizedkeys= /proc/cmdline); then
rootauthorizedkeys=$(/opt/nyble/bin/get_cmdline_key.pl rootauthorizedkeys)
mkdir -p ~root/.ssh
cd ~root/.ssh ; wget $rootauthorizedkeys -O authorized_keys
chmod 600 ~root/.ssh/authorized_keys
fi
# this is how to do late loading of drivers, so they don't interfere with boot.
# driverprobe=list,of,drivers,to,modprobe,post,boot
if (grep -q driverprobe= /proc/cmdline); then
drivers=$(/opt/nyble/bin/get_cmdline_key.pl driverprobe)
list=$(echo $drivers | sed 's|\,| |g')
for driver in $list; do
modprobe -v $driver
done
fi
# mdadmassemble=0 forces no default assembly of MDRAID devices
mdadm --examine --scan > /tmp/mdadm.conf
mdadm -S /dev/md*
if (grep -q mdadmassemble=0 /proc/cmdline); then
echo "Not assembling MDRAID devices"
else
mdadm -As -c /tmp/mdadm.conf
fi
# grab runscript= if it exists
if (grep -q runscript= /proc/cmdline); then
runscript=$(/opt/nyble/bin/get_cmdline_key.pl runscript)
cd /tmp ; wget $runscript -O runscript
chmod 700 runscript
./runscript
fi
if (grep -q nfsserver= /proc/cmdline); then
mkdir -p /var/lib/nfs/
cd /tmp
# grab the nfsserver option from the command line
nfsserver=$(/opt/nyble/bin/get_cmdline_key.pl nfsserver)
echo "nfsserver=${nfsserver}"
mkdir -p /new_root
mount -t nfs -o soft,rsize=65536,wsize=65536,retry=1,tcp,nolock,intr ${nfsserver} /new_root
cd /new_root
mkdir old_root
pivot_root . old_root
./bin/mount -n --move ./old_root/sys ./sys
./bin/mount -n --move ./old_root/proc ./proc
./bin/mount -n --move ./old_root/dev ./dev
./bin/mount -n --move ./old_root/run ./run
./bin/mount -n --move ./old_root/var ./var
./bin/mount -n --move ./old_root/var/lib/nfs ./var/lib/nfs
./bin/mount -n --move ./old_root/tmp ./tmp
./bin/umount ./old_root/var/tmp
./bin/umount ./old_root/var/lib/nfs
./bin/umount ./old_root/data
exec chroot . /bin/bash -c 'umount -l /old_root ; /sbin/init 3 ' <dev/console >dev/console 2>&1
fi
# enablecloudinit=1 turns on cloud-init
if (grep -q enablecloudinit=1 /proc/cmdline); then
systemctl enable cloud-init
systemctl start cloud-init
fi
# enablepuppet=1 turns on puppet
if (grep -q enablepuppet=1 /proc/cmdline); then
systemctl enable puppet
systemctl start puppet
fi
# enablelldpd=1 turns on lldpd
if (grep -q enablelldpd=1 /proc/cmdline); then
systemctl enable lldpd
systemctl start lldpd
fi
# disablezfs=1 forces zfs modules to not be loaded
if (grep -q disablezfs=1 /proc/cmdline); then
echo "zfs module not loaded"
else
# 2GiB of RAM max for zfs arc
modprobe -v zfs zfs_arc_max=2147483648
# zpoolimport=1 forces a zpool import
if (grep -q zpoolimport= /proc/cmdline); then
pool=$(/opt/nyble/bin/get_cmdline_key.pl zpoolimport)
zpool import -f $pool
fi
fi
# nomad=0 forces nomad not to run
if (grep -q nomad=0 /proc/cmdline); then
echo "nomad will not run automatically"
else
/usr/local/bin/nomad agent -dev
fi
;;
stop)
;;
restart)
echo -n "Restarting post boot process configuration: "
/etc/init.d/nyble stop
/etc/init.d/nyble start
;;
*)
echo "Usage: nyble {start|stop|restart}"
RETVAL=1
esac
} 2>&1 | tee -a /var/log/nyble
exit ${RETVAL}