/
build_pxe_stick.sh
executable file
·231 lines (194 loc) · 5.69 KB
/
build_pxe_stick.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
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
#!/bin/bash
set -e
shopt -s nullglob
function log() {
echo -e "\e[32m$*\e[0m" >&2
}
function warn() {
echo -e "\e[33m$*\e[0m" >&2
}
function error() {
echo -e "\e[31m$*\e[0m" >&2
}
function happy_grep() {
grep "$@" || :
}
declare -A ENV2PREFIX=(
[LOGEXPORT_HOST]=LOGEXPORT_
[LOGEXPORT_PORT]=LOGEXPORT_
[HEARTBEAT_URL]=HEARTBEAT_
[NODE_HOSTNAME]=NODE_HOSTNAME
)
declare -A PREFIX2SVC=(
[LOGEXPORT_]="Log Export"
[HEARTBEAT_]="Heartbeat"
[NODE_HOSTNAME]="hostname setting"
)
function check_env_vars() {
for e in "${!ENV2PREFIX[@]}"; do
prefix="${ENV2PREFIX[$e]}"
svc="${PREFIX2SVC[$prefix]}"
if [[ -z "${!e}" ]]; then
warn "$e not set. Disabling $svc functionality from resultant system."
unset_many "$prefix"
fi
done
}
function unset_many() {
prefix="${1?}"
log "unsetting $prefix* vars"
mapfile -d '' vars < <(env -0)
for v in "${vars[@]}"; do
varname="${v%%=*}"
if [[ "$varname" =~ "$prefix"* ]]; then
warn "unsetting $varname"
unset "$varname"
fi
done
}
function git_reference() {
repo="$(
git remote show -n origin \
| grep "Fetch URL:" \
| cut -d: -f2,3 \
| tr -d " " \
| sed "s/git@github/github/" \
| sed "s|github.com:|github.com/|" \
| sed "s|^github.com|https://github.com|" \
| sed "s/.git$//"
)"
maybe_dirty=""
if [[ -n "$(cd "$HERE"; git status --short)" ]]; then
maybe_dirty=" (with uncommitted changes)"
fi
commit="$(cd "$HERE"; git rev-parse --short HEAD || echo "???")"
echo "$repo/tree/$commit$maybe_dirty"
}
function repro() {
echo "#!/bin/bash"
echo "# @ $(git_reference)"
env_patterns=(
-e "^K3S_"
-e "^LOGEXPORT_"
-e "^HEARTBEAT_"
-e "^NAMECHEAP_DDNS_"
)
echo "env \\"
for e in $(env | grep "${env_patterns[@]}" | cut -d= -f1); do
printf " %s=%q \\" "$e" "${!e}"
echo # newline
done
echo " $0"
}
function prep_ipxe() {
(
set -e
cd "$OUT"
git clone git://git.ipxe.org/ipxe.git
cd ipxe/src
# enable https because it isn't the 90s anymore
sed -i \
"s/#undef\\s\\+DOWNLOAD_PROTO_HTTPS/#define DOWNLOAD_PROTO_HTTPS/" \
./config/general.h
) >&2
echo "$OUT/ipxe/src"
}
function build_uefi_ipxe() {
(
set -e
cd "$1"
bash "$HERE/mk_ipxe_script.sh" >"$1/generated.ipxe"
make -j "$(nproc)" bin-x86_64-efi/ipxe.efi EMBED="$1/generated.ipxe"
log "Built $PWD/bin-x86_64-efi/ipxe.efi"
) >&2
echo "$1/bin-x86_64-efi/ipxe.efi"
}
function build_bios_ipxe() {
(
set -e
cd "$1"
bash "$HERE/mk_ipxe_script.sh" >"$1/generated.ipxe"
make -j "$(nproc)" bin/ipxe.usb EMBED="$1/generated.ipxe"
log "Built $PWD/bin/ipxe.usb"
) >&2
echo "$1/bin/ipxe.usb"
}
function unmount() {
log "unmounting $DISK (and associated partitions)"
umount -q "$DISK" "$DISK"? || :
}
function usage_and_die() {
error 'Usage: '
error ' [SSH_KEY_PATH=/root/.ssh/id_rsa.pub] \\'
error ' [NODE_HOSTNAME=node0] \\'
error ' [LOGEXPORT_HOST=logsX.papertrailapp.com LOGEXPORT_PORT=XXXXX] \\'
error ' [HEARTBEAT_URL=https://nudge.me/im_alive] \\'
error ' [K3S_ENV_VARS_YOU_WANT_TO_SET=blahblahblah...] \\'
error ' ./build_pxe_stick.sh /dev/sdX'
error ''
error 'If the necessary variables are not populated for a given component,'
error 'then that functionality will be disabled in the resultant system,'
error 'e.g., no LOGEXPORT_PORT, then log exporting will not be enabled.'
exit 1
}
function mount_part() {
root="$1"
device="$2"
name="$3"
mkdir "$root/${name}_mnt"
log "mounting $name volume @ $root/${name}_mnt"
mount "$device" "$root/${name}_mnt"
echo "$root/${name}_mnt"
}
HERE="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
OUT="$(mktemp -d /tmp/pxe_build_ephemeros_XXXXXX)"
log "Using $OUT for temp storage"
DISK="$1"
if [[ -z "$DISK" ]]; then
usage_and_die
fi
check_env_vars
if [[ ! -w "$DISK" ]]; then
error "$DISK is not writeable. Check your privilege, and that it exists."
exit 2
fi
log "$DISK presently looks like this:"
log "$(lsblk "$DISK")"
read -p "We're gonna zap <$DISK>! Type \"i don't want that data\" to continue. >>> "
if [[ "$REPLY" != "i don't want that data" ]]; then
error "Aborting."
exit 2
fi
unmount
IPXE_PATH="$(prep_ipxe)"
UEFI_IPXE="$(build_uefi_ipxe "$IPXE_PATH")"
BIOS_IPXE="$(build_bios_ipxe "$IPXE_PATH")"
log "zeroing the first meg of $DISK"
dd if=/dev/zero of="$DISK" bs=1M count=1
log "Creating disk label for $DISK"
parted "$DISK" mklabel gpt
log "dd'ing bios image to $DISK"
dd if="$BIOS_IPXE" of="$DISK"
log "Adding a partition for EFI boot to $DISK"
parted "$DISK" mkpart primary fat32 4MB 512MB
log "Adding a partition for secrets to $DISK"
parted "$DISK" mkpart primary ext4 512MB 1GB
partprobe
mkfs.fat -F32 -n efi "${DISK}1"
mkfs.ext4 -L secrets "${DISK}2"
EFI="$(mount_part "$OUT" "${DISK}1" efi)"
log "Writing ipxe.efi to efi partition"
mkdir -p "$EFI/efi/boot"
cp "$UEFI_IPXE" "$OUT/efi_mnt/efi/boot/bootx64.efi"
SECRETS="$(mount_part "$OUT" "${DISK}2" secrets)"
log "Writing environment files in $SECRETS..."
(
umask 0077
# NOTE: if you update these, also update the patterns in `repro`
env | happy_grep "^K3S_" > "$SECRETS/k3s_env"
env | happy_grep "^LOGEXPORT_" > "$SECRETS/logexport_env"
env | happy_grep "^HEARTBEAT_" > "$SECRETS/heartbeat_env"
env | happy_grep "^NAMECHEAP_DDNS_" > "$SECRETS/namecheap_ddns_env"
repro > "$SECRETS/reproduce.sh"
)
log "!! DONE !!"