-
Notifications
You must be signed in to change notification settings - Fork 1
/
initial-server-setup.sh
executable file
·160 lines (125 loc) · 5.17 KB
/
initial-server-setup.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
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
#!/bin/bash
set -euo pipefail
# Get system configuration information
HOSTNAME=$(hostname -s);
IPADDRESS=$(ifconfig | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v 127.0.0.1 | awk '{print $2}')
NETMASK=$(ifconfig | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v 127.0.0.1 | awk '{print $4}')
SCRIPTDIR=$(pwd)
# Set apt to not expect keyboard input
export DEBIAN_FRONTEND=noninteractive
# Write function will echogreen colored text
green=$(tput setaf 2)
bold=$(tput bold)
normal=$(tput sgr0)
red=$(tput setaf 1)
function echogreen() {
echo "${green}${bold}$1${normal}"
}
########################################################################################################################
#### BEGIN SETUP FUNCTIONS ###
########################################################################################################################
function _installpackages() {
echogreen "Installing packages for dnsmasq and nfs server..."
apt-get -y -f install dnsmasq nfs-kernel-server nfs-common unzip wget kpartx
# Don't install these automatically - chrootpi script will check for them and install if needed.
#apt-get install qemu qemu-user-static binfmt-support
}
function _makedirectories() {
echogreen "Making directories in /srv"
if [[ ! -d /srv/tftp ]]; then mkdir /srv/tftp; fi
if [[ ! -d /srv/nfs ]]; then mkdir /srv/nfs; fi
if [[ ! -d /srv/images ]]; then mkdir /srv/images; fi
}
function _configurednsmasq() {
echogreen "Configuring Dnsmasq to respond only to Raspberry Pis"
sed -e "s/{IPADDRESS}/${IPADDRESS}/" -e "s/{NETMASK}/${NETMASK}/" ${SCRIPTDIR}/config/rpiboot > /etc/dnsmasq.d/rpiboot
echogreen "Disabling DNS features of dnsmasq to prevent conflicts with systemd-resolved"
echo "DNSMASQ_OPTS=\"-p0\"" >> /etc/default/dnsmasq
}
function _downloadraspbian() {
echogreen "Downloading latest Raspbian Lite image (this may take a few minutes)"
wget https://downloads.raspberrypi.org/raspbian_lite_latest -O /tmp/raspbian_lite_latest.zip
#cp /root/raspbian_lite_latest /tmp/raspbian_lite_latest.zip
echogreen "Unzipping image to /srv/images"
unzip -uo /tmp/raspbian_lite_latest.zip -d /srv/images
rm /tmp/raspbian_lite_latest.zip
}
function _extractimage() {
# Find the newest image file
IMAGEFILE=$(ls -t /srv/images/*.img | head -1)
echogreen "Latest image file found at ${IMAGEFILE}"
IMAGEDIR="/srv/images/$(basename ${IMAGEFILE} .img)"
if [[ -d "${IMAGEDIR}" ]]; then
echo "${IMAGEDIR} already exists. Skipping extracting image."
else
mkdir "${IMAGEDIR}"
echogreen "Mounting and extracting ${IMAGEFILE} to ${IMAGEDIR}"
if [[ ! -d /tmp/root ]]; then mkdir /tmp/root; fi
if [[ ! -d /tmp/boot ]]; then mkdir /tmp/boot; fi
kpartx -s -a -r -v "${IMAGEFILE}"
LOOPDEV=$(losetup -j "${IMAGEFILE}" | sed -n 's/.*\(loop[0-9]\).*/\1/p')
mount "/dev/mapper/${LOOPDEV}p1" /tmp/boot
mount "/dev/mapper/${LOOPDEV}p2" /tmp/root
echogreen "Copying image files to ${IMAGEDIR}"
rsync -aAX --numeric-ids /tmp/root/ "${IMAGEDIR}"
rsync -aAX --numeric-ids /tmp/boot/ "${IMAGEDIR}/boot"
echogreen "Unmounting ${IMAGEFILE}"
umount /tmp/boot
umount /tmp/root
rmdir /tmp/boot
rmdir /tmp/root
kpartx -s -d "${IMAGEFILE}"
fi
echogreen "Copying bootcode.bin from ${IMAGEDIR}/boot to tftp root"
cp "${IMAGEDIR}/boot/bootcode.bin" /srv/tftp/
}
function _configurenfs() {
echogreen "Exporting /srv/nfs as an NFS share"
if grep -Fxq "/srv/nfs *(rw,sync,no_subtree_check,no_root_squash)" /etc/exports
then
echo "/srv/nfs is already exported"
else
echo "/srv/nfs *(rw,sync,no_subtree_check,no_root_squash)" >> /etc/exports
fi
}
function _restartservices() {
echogreen "Restarting Dnsmasq"
service dnsmasq restart
echogreen "Updating NFS exports"
exportfs -a -r -v
}
function _copyscripts() {
echogreen "Copying scripts to /usr/local/bin"
cp $SCRIPTDIR/scripts/* /usr/local/bin/
}
###############
# Main script #
###############
# Make sure running as root
if [[ $EUID != 0 ]]; then
echo "${red}This script must be run with root privileges.${normal}"
echo "Exiting..."
exit 1
fi
# Allow calling a specific function from the command line
if [ $# -eq 1 ]; then
echo "Argument supplied. Running function..."
"$@"
else
_installpackages
_makedirectories
_configurednsmasq
_downloadraspbian
_extractimage
_configurenfs
_restartservices
_copyscripts
echo
echo
echo "Server is ready to start network booting Raspberry Pis."
echo
echo "Use 'extract-image <image file> <name>' to extract SD card images to /srv/images"
echo "Use 'new-nfsroot' to copy an extracted image to /srv/nfs to create the filesystem for a specific Pi."
echo "Use 'chrootpi' to enter an image or NFSroot and do things like change passwords or run raspi-config"
echo "Use 'assign-nfsroot' to assign that images to a Pi. You must attempt network booting the Pi first so the serial number can be detected."
fi