-
Notifications
You must be signed in to change notification settings - Fork 2
/
podman-machine-init.sh
executable file
·310 lines (276 loc) · 8.8 KB
/
podman-machine-init.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
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
#! /usr/bin/env bash
set -eu # x
#declare -i debug=${DEBUG:-}
declare -i trace=${TRACE:-}
declare -r REPO='https://github.com/srcshelton/docker-gentoo-build.git'
declare -r INIT='gentoo-init.docker'
(( trace )) && set -o xtrace
export LANG=C
export LC_ALL=C
declare MACHINE='podman-machine-default'
declare MACHINE_SOCKET='/var/run/podman/podman.sock'
declare MACHINE_KEY="${HOME}/.ssh/id_rsa"
declare MACHINE_KEY_TYPE='ed25519'
declare REMOTE_HOME='/var/home'
declare REMOTE_USER='core'
declare ARCH=''
declare -i init=0 xfer=0 cores=4 local_install=0
type -pf jq || {
echo >&2 "FATAL: 'jq' binary is required on Darwin"
exit 1
}
if [[ "$( uname -s )" == 'Darwin' ]]; then
# Darwin/BSD lacks GNU readlink - either realpath or perl's Cwd module
# will do at a pinch, although both lack the additional options of GNU
# binaries...
#
readlink() {
if command -v realpath >/dev/null 2>&1; then
realpath "${2}"
else
perl -MCwd -le 'print Cwd::abs_path shift' "${2}"
fi
}
export -f readlink
else
case "$( id -un )" in
"${REMOTE_USER}")
: ;;
'mixtile')
REMOTE_USER='mixtile' ;;
*)
REMOTE_USER="$( id -un )"
if ! REMOTE_HOME="$( dirname "$( # <- Syntax
eval "readlink -e ~${REMOTE_USER}"
)" )" || ! [ -d "${REMOTE_HOME}/${REMOTE_USER}" ]
then
echo >&2 "FATAL: Unable to determine home directory" \
"for user '${REMOTE_USER:-}': ${?}"
exit 1
else
echo >&2 "INFO: Using home '${REMOTE_HOME}' for" \
"user '${REMOTE_USER}' ..."
fi
;;
esac
fi
if type -pf portageq >/dev/null 2>&1; then
ARCH="$( portageq envvar ARCH )"
else
echo >&2 "WARN: Cannot locate 'portageq' utility"
fi
if [[ -z "${ARCH:-}" ]]; then
case "$( uname -m )" in
aarch64)
ARCH='arm64' ;;
arm*)
ARCH='arm' ;;
x86_64)
ARCH='amd64' ;;
*)
echo >&2 "FATAL: Unknown architecture '$( uname -m )'"
exit 1
;;
esac
fi
readonly ARCH
if [[ "$( uname -s )" == 'Darwin' ]]; then
declare -i total=${cores}
total=$(( $( sysctl -n machdep.cpu.core_count || sysctl -n machdep.cpu.core_total ) ))
case "$( sysctl -n machdep.cpu.brand_string )" in
'Apple M1'|'Apple M2'|'Apple M3')
# M1: 'Tonga' - 4 Firestorm and 4 Icestorm cores, first
# seen in the A14 Bionic ('Sicily')
# M2: 'Staten' - 4 Avalance and 4 Blizzard cores, first
# seen in the A15 Bionic ('Ellis')
# M3: 'Ibiza' - 4 Everest and 4 Sawtooth cores, first
# seen in the A17 Pro ('Coll')
cores=$(( total - 4 ))
;;
'Apple M4')
# 'Donan' - either 3 or 4 Everest and 6 Sawtooth cores,
# first seen in the A18 Pro ('Tahiti')
cores=$(( total - 6 ))
;;
'Apple M1 Pro'|'Apple M1 Max')
# 'Jade' - either 8 Firestorm and 2 Icestorm or 6
# Firestorm and 8 Icestorm cores
case "${total}" in
14)
cores=$(( total - 2 )) ;;
*)
cores=$(( total - 8 )) ;;
esac
;;
'Apple M2 Pro'|'Apple M2 Max')
# 'Rhodes' - either 8 Avalanche and 4 Blizzard or 6
# Avalanche and 4 Blizzard cores
cores=$(( total - 4 ))
;;
'Apple M3 Pro')
# 'Lobos' - either 5 or 6 Everest and 6 Sawtooth cores
cores=$(( total - 6 ))
;;
'Apple M4 Pro')
# Either 8 or 10 Everest and 4 Sawtooth cores
cores=$(( total - 4 ))
;;
'Apple M3 Max')
# 'Palma' - either 10 or 12 Everest and 4 Sawtooth
# cores
cores=$(( total - 4 ))
;;
'Apple M4 Max')
# Either 10 or 12 Everest and 4 Sawtooth cores
cores=$(( total - 4 ))
;;
'Apple M1 Ultra')
# Two M1 Max joined via UltraFusion Interconnect
# resulting in 16 Firestorm and 4 Icestorm cores
cores=$(( total - 4 ))
;;
'Apple M2 Ultra')
# Two M2 Max joined via UltraFusion Interconnect
# resulting in 16 Avalanche and 8 Blizzard cores
cores=$(( total - 8 ))
;;
'Intel'*)
# Adjust for SMT/HT
cores=$(( total / 2 ))
esac
unset total
else
cores="$( nproc || grep 'cpu cores' /proc/cpuinfo | tail -n 1 | awk -F': ' '{ print $2 }' )"
fi
cd "$( dirname "$( readlink -e "${0}" )" )" || exit 1
declare arg=''
for arg in "${@:-}"; do
case "${arg:-}" in
-h|--help)
echo "Usage: $( basename "${0}" ) <--host>|[--machine=<name>] [--cores=<${cores}>] [--init] [--transfer-cache]"
exit 0
;;
-c|--cores)
cores="${arg#*"-"}"
;;
--host)
if ! [[ "${*:-}" == '--host' ]]; then
echo >&2 "FATAL: '--host' cannot be used with any other options"
exit 1
fi
local_install=1
;;
-i|--init)
init=1
;;
-m=*|--machine=*)
MACHINE="${arg#*"="}"
;;
-t|--xfer|--transfer|--transfer-cache)
xfer=1
;;
esac
done
unset arg
if (( local_install )); then
sudo mkdir -p /var/cache &&
test -s "${REMOTE_HOME}/${REMOTE_USER}/portage-cache.tar" &&
sudo tar -xpf "${REMOTE_HOME}/${REMOTE_USER}/portage-cache.tar" -C /var/cache/
sudo mkdir -p /var/cache/portage &&
sudo chown "${REMOTE_USER}:root" /var/cache/portage &&
sudo chmod ug+rwX /var/cache/portage
sudo mkdir -p "/var/cache/portage/pkg/${ARCH:-"${arch}"}/${PKGHOST:-"docker"}" &&
sudo chown "${REMOTE_USER}:root" "/var/cache/portage/pkg/${ARCH:-"${arch}"}/${PKGHOST:-"docker"}" &&
sudo chmod ug+rwX "/var/cache/portage/pkg/${ARCH:-"${arch}"}/${PKGHOST:-"docker"}"
if ! [[ -x sync-portage.sh ]]; then
echo >&2 "WARN: Cannot locate 'sync-portage.sh' script - please run" \
"this manually in order to populate '/etc/portage'"
else
sudo ./sync-portage.sh &&
sudo cp gentoo-base/etc/portage/make.conf /etc/portage/
echo >&2 "INFO: Please review the settings in '/etc/portage/make.conf'"
fi
else
if ! [[ -f ~/.ssh/id_ed25519 && -s ~/.ssh/id_ed25519 ]]; then
echo >&2 "FATAL: ssh private key '${HOME}/.ssh/id_ed25519' not accessible"
exit 1
fi
if ! podman machine list | grep -q -- "^${MACHINE}"; then
#podman machine init --cpus "${cores}" --disk-size 25 -m $(( 12 * 1024 )) "${MACHINE}"
echo >&2 "FATAL: No 'podman machine' named '${MACHINE}' defined - please create this first"
exit 1
fi
if ! podman machine list | grep -q -- "^${MACHINE}.*Currently running"; then
echo >&2 "FATAL: No 'podman machine' named '${MACHINE}' running - please start this first"
exit 1
fi
# Check that our `podman machine` is actually usable...
echo -n "Waiting for running 'podman machine' named '${MACHINE}' ..."
until [[ "$( # <- Syntax
podman machine list --noheading --format '{{.Name}} {{.LastUp}}' |
grep "^${MACHINE}[* ]"
)" =~ ^${MACHINE}\*?\ Currently\ running$ ]]
do
printf '.'
sleep 0.1
done
echo
echo -n "Waiting for accessible 'podman machine' named '${MACHINE}' ..."
until podman machine ssh "${MACHINE}" 'true'; do
printf '.'
sleep 0.1
done
echo ; echo
MACHINE_SOCKET="$( podman machine inspect | jq -Mr '.[].ConnectionInfo.PodmanSocket.Path' )"
MACHINE_KEY="$( podman machine inspect | jq -Mr '.[].SSHConfig.IdentityPath' )"
if [[ -s ~/.ssh/authorized_keys ]] &&
grep -Fq -- "$( < "${MACHINE_KEY}.pub" )" ~/.ssh/authorized_keys
then
:
else
mkdir -p ~/.ssh
chmod 0700 ~/.ssh
cat "${MACHINE_KEY}.pub" >> ~/.ssh/authorized_keys
chmod 0600 ~/.ssh/authorized_keys
fi
podman-remote system connection add "$( id -un )" --identity ~/.ssh/id_ed25519 "${MACHINE_SOCKET}" || {
echo >&2 "FATAL: Could not update system connection ($?): is there a podman machine running?"
exit 1
}
podman machine ssh "${MACHINE}" "cat - > ~/.ssh/id_${MACHINE_KEY_TYPE} && chmod 0600 ~/.ssh/id_${MACHINE_KEY_TYPE}" < "${MACHINE_KEY}"
podman machine ssh "${MACHINE}" "cat - > ~/.ssh/id_${MACHINE_KEY_TYPE}.pub && chmod 0600 ~/.ssh/id_${MACHINE_KEY_TYPE}.pub" < "${MACHINE_KEY}.pub"
podman machine ssh "${MACHINE}" <<-EOF
test -d src/podman-gentoo-build || {
mkdir -p src &&
cd src &&
git clone "${REPO}" podman-gentoo-build ;
} ;
git config --system --replace-all safe.directory '*' ;
cd ~/src/podman-gentoo-build && git pull --all
EOF
for f in 'make.conf' 'local.sh' 'portage-cache.tar'; do
if [[ -f "${f}" && -s "${f}" ]]; then
case "${f}" in
*.conf)
dest="/etc/portage/" ;;
*.sh)
dest="${REMOTE_HOME}/${REMOTE_USER}/src/podman-gentoo-build/common/" ;;
*.tar)
dest="${REMOTE_HOME}/${REMOTE_USER}/" ;;
esac
podman machine ssh "${MACHINE}" "sudo mkdir -p '${dest}' && sudo scp -i ~/.ssh/id_ed25519 -o StrictHostKeyChecking=no '$( id -nu )@host.containers.internal:$( pwd )/${f}' '${dest}'"
fi
done
podman machine ssh "${MACHINE}" "sudo mkdir -p /var/cache ; test -s ${REMOTE_HOME}/${REMOTE_USER}/portage-cache.tar && sudo tar -xpf ${REMOTE_HOME}/${REMOTE_USER}/portage-cache.tar -C /var/cache/ || sudo mkdir -p /var/cache/portage ; sudo chown ${REMOTE_USER}:root /var/cache/portage && sudo chmod ug+rwX /var/cache/portage"
if ! (( init || xfer )); then
podman machine ssh "${MACHINE}"
else
if (( init )); then
time podman machine ssh "${MACHINE}" "${REMOTE_HOME}/${REMOTE_USER}/src/podman-gentoo-build/${INIT}"
fi
if (( xfer )); then
podman machine ssh "${MACHINE}" 'cd /var/cache && tar -cvpf - portage' > portage-cache.tar
fi
fi
fi
# vi: set syntax=bash: