This repository was archived by the owner on Oct 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCtrlInstall
executable file
·479 lines (428 loc) · 12.9 KB
/
CtrlInstall
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#!/bin/bash
cd /root/tools
sleep 10
exec 2>&1 >$(pwd)/install.log
MOBO=$(sudo dmidecode -t 2 | awk '/Product Name:/{print $3}')
MOBO=${MOBO:-QEMU}
source /run/net-*.conf
# PXE created environment
eval $(cat /proc/cmdline) &>/dev/null
OURFILESERVER=${fullroot:-LOCAL}
udevadm settle
# if called as ctrlinstall, source cmdline, source config
if [ ! "$install" = "" ]; then
if [ "${OURFILESERVER}" = "LOCAL" ]; then
if [ -d "/root/etc" ]; then
source /root/etc/${install}.conf
elif [ "${INSTALL}" = "manual" ]; then
install=${1}
mkdir -p /root/USB
mount -r -L GIGINSTALL /root/USB
source /root/USB/etc/${install}
else
mkdir -p /root/USB
mount -r -L GIGINSTALL /root/USB
source /root/USB/etc/${install}
fi
fi
fi
AYSHOSTNAME=${HOSTNAME}.${DNSDOMAIN:-$DOMAIN}
NETLSB=${MGMTIP##*.}
OSIMAGE="ubuimage-16.04.3.tgz"
case "$MOBO" in
G1SCN-B) # Gooxi Controller node
BONDIFACES=('enp4s0')
MGMT=('enp3s0')
DISKS=(sda sdb)
;;
VirtualBox) # Playground for geert
BONDIFACES=('enp0s3')
MGMT=('enp0s8')
DISKS=(sda sdb)
;;
QEMU) # Playground for Jan
BONDIFACES=('ens3')
MGMT=('ens4')
DISKS=(vda vdb)
;;
*)
# some reasonable defaults ;-)
BONDIFACES=($DEVICE)
MGMT=('ovs' '2311')
DISKS=(sda sdb)
;;
esac
function zappit() {
## zappitall
## first, zero out evt fs headers from partitions
parts=$(ls /dev/disk/by-id/ | awk '/part/&&!/wwn/&&!/usb/{print}')
for i in $parts; do
dd if=/dev/zero of=/dev/disk/by-id/${i} bs=1M count=100 &
done
wait
sync
# same for md devices that could contain an fs
parts=$(ls /dev/disk/by-id/ | awk '/part/&&!/wwn/&&!/usb/{print}')
mds=$(mdadm --detail --scan | awk '/ARRAY/{print $2}')
for i in $mds; do
dd if=/dev/zero of=$i bs=1M count=100 &
done
wait
sync
# now stop evt raids
mdadm --stop --scan
# erase evt superblocks from md partitions
echo $parts
for i in $parts; do
mdadm --zero-superblock --force /dev/disk/by-id/${i}
done
# we should need :apt-get install gdisk but it's on the rescue of OVH
# Now we can drop all partition tables
disks=$(ls /dev/disk/by-id/ | awk '/scsi|ata/{gsub(/\-part.*/,"");print}' | uniq)
for i in $disks; do
# destruct, destroy, obliterate
SIZE=$(blockdev --getsz /dev/disk/by-id/${i})
dd if=/dev/zero seek=$(($SIZE - 1024)) bs=512 count=1024 of=/dev/disk/by-id/${i}
dd if=/dev/zero of=/dev/disk/by-id/${i} bs=1M count=10 && sync
echo -e 'x\nz\ny\ny\n' | gdisk /dev/disk/by-id/${i}
udevadm settle
partprobe /dev/disk/by-id/${i}
done
##
mdadm --stop --scan
if ! [ -z "$(ls /dev/disk/by-id/ | grep part | grep -v usb)" ]; then
echo not everything is cleared
exit 1
fi
}
function genpartbios() {
mdadm --stop --scan
if [ ${#DISKSS[@]} -eq 1 ]; then
parted /dev/${DISKS[0]} -s mklabel gpt \
mkpart bios ext2 1 8M \
mkpart boot ext2 8M 2G \
mkpart root ext2 2G 100% \
set 1 bios_grub on set 2 raid on set 3 raid on
else
parted /dev/${DISKS[0]} -s mklabel gpt \
mkpart bios ext2 1 8M \
mkpart boot ext2 8M 2G \
mkpart root ext2 2G 100% \
set 1 bios_grub on set 2 raid on set 3 raid on
parted /dev/${DISKS[1]} -s mklabel gpt \
mkpart bios ext2 1 8M \
mkpart boot ext2 8M 2G \
mkpart root ext2 2G 100% \
set 1 bios_grub on set 2 raid on set 3 raid on
fi
# need to sleep here a bit for udev to settle
udevadm settle
sleep 2
mdadm --stop --scan
}
function prepmounts() {
# prepare /boot
yes | mdadm --create /dev/md0 -l1 -n2 /dev/${DISKS[0]}2 /dev/${DISKS[1]}2 --force
sysctl -w dev.raid.speed_limit_max=10
sysctl -w dev.raid.speed_limit_min=10
mkfs.ext4 -L BOOT /dev/md0
# prepare root partition
yes | mdadm --create /dev/md1 -l1 -n2 /dev/${DISKS[0]}3 /dev/${DISKS[1]}3 --force
mkfs.ext4 /dev/md1 -E lazy_itable_init=1
# mount it
mount /dev/md1 /mnt
mkdir /mnt/boot
mount /dev/md0 /mnt/boot
}
function prepmountssingle() {
# prepare /boot
yes | mdadm --create /dev/md0 -l1 -n2 /dev/${DISKS[0]}2 missing --force
sysctl -w dev.raid.speed_limit_max=10
sysctl -w dev.raid.speed_limit_min=10
mkfs.ext4 -L BOOT /dev/md0
# prepare root partition
yes | mdadm --create /dev/md1 -l1 -n2 /dev/${DISKS[0]}3 missing --force
mkfs.ext4 /dev/md1 -E lazy_itable_init=1
# mount it
mount /dev/md1 /mnt
mkdir /mnt/boot
mount /dev/md0 /mnt/boot
}
# TODO errcheck here
function untarit() {
if [ "$OURFILESERVER" = "LOCAL" ]; then
# we're on our own, no network
mkdir -p /root/USB
mount -r -L GIGINSTALL /root/USB
# and untar it
tar --numeric-owner -zxf /root/USB/${OSIMAGE} -C /mnt
[ $? -ne 0 ] && echo "OOPS: No USB and no fullroot, bailing" && exit 1
else
OURFILESERVER="${OURFILESERVER:-$fullroot}"
# INSTALL
URL="${OURFILESERVER}/${OSIMAGE}"
# get the 'image' and pour it in the mountpoint
wget -qO - $URL | tar --numeric-owner -zxf - -C /mnt
[ $? -ne 0 ] && echo "OOPS: can't get \"$URL\"" && exit 1
fi
}
function findXEifaces() {
# we're looking for fast nics
IFACES=""
for i in /sys/class/net/*; do
if ethtool ${i##*/} 2>/dev/null | grep -E '10000|40000' &>/dev/null; then
IFACES=${IFACES}" ${i##*/}"
fi
done
echo ${IFACES:-eth0 eth1}
}
function findGEifaces() {
# we're looking for fast nics
IFACES=""
for i in /sys/class/net/*; do
if ethtool ${i##*/} 2>/dev/null | grep -E '1000 ' &>/dev/null; then
IFACES=${IFACES}" ${i##*/}"
fi
done
echo ${IFACES:-eth0 eth1}
}
function mgmtiface() {
local IFC ADDR GATEWAY MGMTNETWORK
IFC=$1
shift
ADDR=$1
MGMTNETWORK=$(ipcalc "${ADDR}/${MASK}" | grep Network | awk '{print $2}')
[ ${NETLSB} -ne 1 ] && GATEWAY=" gateway ${NETBASE}.${MGMT}.1"
cat <<EOF >/mnt/etc/network/interfaces.d/mgmt.conf
### ${IFC}
auto mgmt
iface mgmt inet static
address ${ADDR}/${MASK}
${GATEWAY}
pre-up ovs-vsctl --may-exist add-br mgmt
pre-up ovs-vsctl --may-exist add-port mgmt ${IFC}
pre-up ip l set ${IFC} up
post-up ip address add ${IPMIIP}/${MASK} dev mgmt
EOF
cat <<EOF > /mnt/etc/iptables/rules.v4
# Generated by iptables-save v1.6.0 on Sun Apr 15 07:18:43 2018
*nat
:PREROUTING ACCEPT [3:1116]
:INPUT ACCEPT [1:576]
:OUTPUT ACCEPT [6:360]
:POSTROUTING ACCEPT [6:360]
-I POSTROUTING -s ${MGMTNETWORK} -j MASQUERADE
COMMIT
# Completed on Sun Apr 15 07:18:43 2018
# Generated by iptables-save v1.6.0 on Sun Apr 15 07:18:43 2018
*filter
:INPUT ACCEPT [2540:747736]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [2494:1292932]
-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A OUTPUT -m conntrack --ctstate ESTABLISHED -j ACCEPT
-A INPUT -i public -p tcp -m multiport --dports 3080,3023,7022,80,443 -j ACCEPT
-A INPUT -i public -j REJECT --reject-with icmp-port-unreachable
-A FORWARD -j ACCEPT
COMMIT
# Completed on Sun Apr 15 07:18:43 2018
EOF
}
function storbr() {
cat <<EOF >/mnt/etc/network/interfaces.d/storage.conf
### storage
auto storage
iface storage inet manual
address ${STORIP}/${MASK}
pre-up ovs-vsctl --may-exist add-br storage
pre-up ovs-vsctl --may-exist add-br backplane1
pre-up ovs-vsctl --may-exist add-port backplane1 bkpln2stor tag=${STORVLAN} -- set Interface bkpln2stor type=patch options:peer=stor2bkpln
pre-up ovs-vsctl --may-exist add-port storage stor2bkpln -- set Interface stor2bkpln type=patch options:peer=bkpln2stor
EOF
}
function pubiface() {
cat <<EOF >/mnt/etc/network/interfaces.d/public.conf
### public
auto public
iface public inet static
address ${PUBIP}/${PUBMASK}
gateway ${PUBGW}
dns-nameserver 8.8.8.8 8.8.4.4
pre-up ovs-vsctl --may-exist add-br backplane1
pre-up ovs-vsctl --may-exist add-port backplane1 public tag=${PUBVLAN} -- set Interface public type=internal
EOF
}
function prepsys() {
echo $AYSHOSTNAME >/mnt/etc/hostname
echo "nameserver 8.8.8.8" >/mnt/etc/resolv.conf
cat <<EOF >/mnt/etc/network/interfaces
# The loopback interface
# Interfaces that comes with Debian Potato does not like to see
# "auto" option before "iface" for the first device specified.
iface lo inet loopback
auto lo
source /etc/network/interfaces.d/*conf
EOF
if [ ${#BONDIFACES[@]} -gt 1 ]; then
cat <<EOF >/mnt/etc/network/interfaces.d/Backplane1.conf
### backplane1
auto backplane1
iface backplane1 inet static
address ${UNTAGIP}/${MASK}
EOF
for iface in ${BONDIFACES[@]}; do
echo " pre-up ip l set ${iface} up " >>/mnt/etc/network/interfaces.d/Backplane1.conf
echo " pre-up sysctl -w net.ipv6.conf.${iface}.disable_ipv6=1" >>/mnt/etc/network/interfaces.d/Backplane1.conf
done
echo " pre-up ovs-vsctl --may-exist add-br backplane1" >>/mnt/etc/network/interfaces.d/Backplane1.conf
echo " pre-up ovs-vsctl --may-exist add-bond backplane1 bond-backplane1 ${BONDIFACES[@]} bond_mode=balance-tcp lacp=active" >>/mnt/etc/network/interfaces.d/Backplane1.conf
# and mgmt iface =
mgmtiface ${MGMT[0]} ${MGMTIP}
else
cat <<EOF >/mnt/etc/network/interfaces.d/Backplane1.conf
### backplane1
auto backplane1
iface backplane1 inet static
address ${UNTAGIP}/${MASK}
pre-up ovs-vsctl --may-exist add-br backplane1
pre-up ovs-vsctl --may-exist add-port backplane1 ${BONDIFACES[0]}
pre-up ip l set ${BONDIFACES[0]} up
pre-up sysctl -w net.ipv6.conf.${BONDIFACES[0]}.disable_ipv6=1
EOF
mgmtiface ${MGMT[0]} ${MGMTIP}
pubiface
storbr
fi
# Also, to make glibc happy, add hostname to /etc/hosts
cat <<EOF >/mnt/etc/hosts
127.0.0.1 localhost $AYSHOSTNAME
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback $AYSHOSTNAME
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
EOF
# Also remove OpenVSwitch initial db
rm -f /mnt/etc/openvswitch/*
# same for persitent net-rules
cat <<EOF >/mnt/etc/udev/rules.d/70-persistent-net.rules
# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.
EOF
echo "DefaultDependencies=False" >> /mnt/lib/systemd/system/network.target
sed -i '/Unit/a DefaultDependencies=False' /mnt/lib/systemd/system/openvswitch-switch.service
}
function installgrub() {
cat <<EOF >/mnt/rungrub
#!/bin/bash
echo "" > /etc/mdadm/mdadm.conf
update-initramfs -c -k all
rm -f /boot/grub/grub.cfg
grub-install --force /dev/${DISKS[0]}
grub-install --force /dev/${DISKS[1]} &> /dev/null
echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
echo "gig:${GIGPWD}" | chpasswd
update-grub
rm -f rungrub
EOF
chmod 755 /mnt/rungrub
./init-chroot /mnt dpkg-reconfigure openssh-server
./init-chroot /mnt /rungrub
}
function installteleport() {
if [ ! -e /root/USB/teleport/teleport.tar.gz ]; then
return
fi
echo Installing teleport
tar xf /root/USB/teleport/teleport.tar.gz -C /tmp
cp /tmp/teleport/{tctl,tsh,teleport} /mnt/usr/local/bin/
cp /tmp/teleport/examples/systemd/teleport.service /mnt/etc/systemd/system/
cp -a /root/USB/teleport/certs/* /mnt/etc/ssl/
cp -a /root/USB/teleport/teleport.yaml /mnt/etc/
cp -a /root/USB/teleport/github.yaml /mnt/tmp/
mkdir -p /mnt/var/lib/teleport
./init-chroot /mnt /usr/local/bin/teleport start &
while [ ! -e /mnt/var/lib/teleport/host_uuid ]; do
echo "Waiting for teleport"
sleep 5
done
echo "Add github auth"
./init-chroot /mnt /usr/local/bin/tctl create /tmp/github.yaml
./init-chroot /mnt systemctl enable teleport
}
function mkfstab() {
# generate fstab
BOOTUUID=$(blkid -o value /dev/md0 -s UUID)
ROOTUUID=$(blkid -o value /dev/md1 -s UUID)
cat <<EOF >/mnt/etc/fstab
# <file system> <mount point> <type> <options> <dump> <pass>
proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
devtmpfs /dev devtmpfs rw 0 0
UUID=${ROOTUUID} / ext4 defaults,discard,data=ordered 0 0
UUID=${BOOTUUID} /boot ext4 defaults 0 0
EOF
}
# to use findifaces, you need to create an empty BONDIFACES array
if [ ${#BONDIFACES[@]} -eq 0 ]; then
BONDIFACES=($(findXEifaces))
fi
# set hostname to some default
hostname ${AYSHOSTNAME}
export HOSTNAME=${AYSHOSTNAME}
# run functions
if [ ${#DISKS[@]} -eq 0 ]; then
# findssds
:
fi
zappit
genpartbios
if [ ${#DISKS[@]} -eq 1 ]; then
prepmountssingle
else
prepmounts
fi
untarit
prepsys
mkfstab
installgrub
if [ ! "$install" = "" ]; then
if [ "${OURFILESERVER}" = "LOCAL" ]; then
if [ -d "/root/etc" ]; then
[ -f "/root/etc/id_rsa.pub" ] &&
cat /root/etc/id_rsa.pub >/mnt/root/.ssh/authorized_keys
else
mkdir -p /root/USB
mount -r -L GIGINSTALL /root/USB
[ -f "/root/USB/etc/id_rsa.pub" ] &&
cat /root/USB/etc/id_rsa.pub >/mnt/root/.ssh/authorized_keys
fi
fi
fi
installteleport
# remove rsa keys that linger from image
rm -f /mnt/root/.ssh/id_rsa* /mnt/root/.ssh/known_hosts
rm -f /mnt/home/gig/.ssh/id_rsa* /mnt/home/gig/.ssh/known_hosts
# get pubkey autorized_keys
if [ ! "${fullroot}" = "" ]; then
wget ${fullroot}/pubkey -O - >>/mnt/root/.ssh/authorized_keys
fi
# umount the stuff
umount /mnt/boot /mnt
# that should do it
echo "All done"
cnt=0
while :; do
(
date
echo -e "Finished installing ${install}.\nPlease reboot without usb stick!!\n\n"
) >/dev/console
sleep 5
if [ $cnt -gt 5 ]; then
exit 0
fi
let cnt++
done
exit 0