forked from ChimeraOS/frzr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__frzr-deploy
302 lines (247 loc) · 8.3 KB
/
__frzr-deploy
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
#! /bin/bash
set -e
set -o pipefail
get_img_url() {
CHANNEL=$1
result=$(jq '[
sort_by(.created_at) |
reverse |
.[] |
{ name: .name, prerelease: .prerelease, state: .assets[].state, url: .assets[].browser_download_url } |
select(.url|test("img")) |
select(.state=="uploaded")
]')
if [ "$CHANNEL" == "testing" ]; then
result=$(echo $result | jq '[ .[] | select(.name|test("UNSTABLE")|not) ]')
elif [ "$CHANNEL" == "stable" ]; then
result=$(echo $result | jq '[ .[] | select(.prerelease==false) ]')
elif [ "$CHANNEL" != "unstable" ]; then
result=$(echo $result | jq "[ .[] | select(.url|contains(\"-${CHANNEL}_\")) ]")
fi
echo $result | jq 'first | .url' | sed 's/"//g'
}
get_boot_cfg() {
local version=${1}
local amd_ucode=${2}
local intel_ucode=${3}
local additional_arguments=${4}
echo "title ${version}
linux /${version}/vmlinuz-linux
${amd_ucode}
${intel_ucode}
initrd /${version}/initramfs-linux.img
options root=LABEL=frzr_root rw rootflags=subvol=deployments/${version} quiet splash loglevel=3 rd.systemd.show_status=auto rd.udev.log_priority=3 ${additional_arguments}"
}
get_deployment_to_delete() {
local current_version=${1}
local boot_cfg_path=${2}
local deployment_path=${3}
local TO_BOOT=`get_next_boot_deployment ${current_version} ${boot_cfg_path}`
ls -1 ${deployment_path} | grep -v ${current_version} | grep -v ${TO_BOOT} | head -1 || echo
}
get_next_boot_deployment() {
local current_version=${1}
local boot_cfg_path=${2}
local TO_BOOT='this-is-not-a-valid-version-string'
if [ -f "${boot_cfg_path}" ] && grep "^title" "${boot_cfg_path}" > /dev/null; then
TO_BOOT=`grep ^title ${boot_cfg_path} | sed 's/title //'`
fi
echo ${TO_BOOT}
}
clean_progress() {
local scale=$1
local postfix=$2
local last_value=$scale
while IFS= read -r line; do
value=$(( ${line}*${scale}/100 ))
if [ "$last_value" != "$value" ]; then
echo ${value}${postfix}
last_value=$value
fi
done
}
main() {
if [ $EUID -ne 0 ]; then
echo "$(basename $0) must be run as root"
exit 1
fi
FRZR_CHECK_UPDATE=0
FRZR_STEAM_PROGRESS=0
FRZR_SOURCE=""
FRZR_PARAMS=""
while (( "$#" )); do
case $1 in
--check)
FRZR_CHECK_UPDATE=1
shift
;;
--steam-progress)
FRZR_STEAM_PROGRESS=1
shift
;;
-*|--*)
echo "Unknown argument $1"
exit 1
;;
*) # preserve positional arguments
FRZR_PARAMS="${FRZR_PARAMS}$1 " # Use trailing space for the match below
shift
;;
esac
done
if [ ! -d /sys/firmware/efi/efivars ]; then
echo "Legacy BIOS installs are not supported. Aborting."
exit 1
fi
# keep only the first param as source
FRZR_SOURCE="${FRZR_PARAMS%% *}"
MOUNT_PATH=/frzr_root
if ! mountpoint -q ${MOUNT_PATH}; then
MOUNT_PATH=/tmp/frzr_root
fi
if ! mountpoint -q ${MOUNT_PATH}; then
mkdir -p ${MOUNT_PATH}
mount -L frzr_root ${MOUNT_PATH}
sleep 5
fi
if ! mountpoint -q ${MOUNT_PATH}/boot && ls -1 /dev/disk/by-label | grep frzr_efi > /dev/null; then
mkdir -p ${MOUNT_PATH}/boot
mount -L frzr_efi ${MOUNT_PATH}/boot
sleep 5
fi
DEPLOY_PATH=${MOUNT_PATH}/deployments
mkdir -p ${DEPLOY_PATH}
BOOT_CFG="${MOUNT_PATH}/boot/loader/entries/frzr.conf"
mkdir -p ${MOUNT_PATH}/boot/loader/entries
# delete deployments under these conditions:
# - we are currently running inside a frzr deployment (i.e. not during install)
# - the deployment is not currently running
# - the deployment is not configured to be run on next boot
if frzr-release > /dev/null; then
CURRENT=`frzr-release`
TO_DELETE=`get_deployment_to_delete ${CURRENT} ${BOOT_CFG} ${DEPLOY_PATH}`
if [ ! -z ${TO_DELETE} ]; then
echo "deleting ${TO_DELETE}..."
btrfs subvolume delete ${DEPLOY_PATH}/${TO_DELETE} || true
rm -rf ${MOUNT_PATH}/boot/${TO_DELETE}
fi
fi
if [ ! -z "$FRZR_SOURCE" ] && [ "$FRZR_SOURCE" != " " ] && [ $FRZR_CHECK_UPDATE -eq 0 ] && [ $FRZR_STEAM_PROGRESS -eq 0 ]; then
echo "$FRZR_SOURCE" > "${MOUNT_PATH}/source"
fi
if [ -e "${MOUNT_PATH}/source" ]; then
SOURCE=`cat "${MOUNT_PATH}/source" | head -1`
else
echo "WARNING: source wasn't specified"
fi
if [ "${local_install}" == true ]; then
mkdir tmp_source
mount -o rw -L FRZR_UPDATE /root/tmp_source
FILE_NAME=$(basename /root/tmp_source/*.img.tar.xz*)
NAME=$(echo "${FILE_NAME}" | cut -f 1 -d '.')
SUBVOL="${DEPLOY_PATH}/${NAME}"
IMG_FILE="/root/tmp_source/${FILE_NAME}"
elif [[ "$FRZR_SOURCE" == *".img.tar.xz" ]]; then
FILE_NAME=$(basename ${FRZR_SOURCE})
NAME=$(echo "${FILE_NAME}" | cut -f 1 -d '.')
SUBVOL="${DEPLOY_PATH}/${NAME}"
IMG_FILE=${FRZR_SOURCE}
elif [[ "$FRZR_SOURCE" == *".img" ]]; then
FILE_NAME=$(basename ${FRZR_SOURCE})
NAME=$(echo "${FILE_NAME}" | cut -f 1 -d '.')
SUBVOL="${DEPLOY_PATH}/${NAME}"
IMG_FILE=${FRZR_SOURCE}
else
REPO=$(echo "${SOURCE}" | cut -f 1 -d ':')
CHANNEL=$(echo "${SOURCE}" | cut -f 2 -d ':')
RELEASES_URL="https://api.github.com/repos/${REPO}/releases"
IMG_URL=$(curl --http1.1 -L -s "${RELEASES_URL}" | get_img_url "${CHANNEL}")
if [ -z "$IMG_URL" ] || [ "$IMG_URL" == "null" ]; then
echo "No matching source found"
if curl --http1.1 -L -s "${RELEASES_URL}" | grep "rate limit" > /dev/null; then
echo "GitHub API rate limit exceeded"
exit 29
fi
exit 1
fi
FILE_NAME=$(basename ${IMG_URL})
NAME=$(echo "${FILE_NAME}" | cut -f 1 -d '.')
BASE_URL=$(dirname "${IMG_URL}")
CHECKSUM=$(curl --http1.1 -L -s "${BASE_URL}/sha256sum.txt" | cut -f -1 -d ' ')
SUBVOL="${DEPLOY_PATH}/${NAME}"
IMG_FILE="${MOUNT_PATH}/${FILE_NAME}"
if [ -e ${SUBVOL} ]; then
echo "${NAME} already installed; aborting"
exit 7 # let Steam know there is no update available
fi
if [ $FRZR_CHECK_UPDATE -eq 1 ]; then
echo "Update available: ${NAME}"
exit 0 # let Steam know there is an update available
fi
if [ $FRZR_STEAM_PROGRESS -eq 1 ]; then
curl --http1.1 -# -L -o "${IMG_FILE}" -C - "${IMG_URL}" 2>&1 | \
stdbuf -oL tr '\r' '\n' | grep --line-buffered -oP '[0-9]*+(?=.[0-9])' | clean_progress 91 %
elif [ -z ${SHOW_UI} ]; then
echo "downloading ${NAME}..."
curl --http1.1 -L -o "${IMG_FILE}" -C - "${IMG_URL}"
else
curl --http1.1 -# -L -o "${IMG_FILE}" -C - "${IMG_URL}" 2>&1 | \
stdbuf -oL tr '\r' '\n' | grep --line-buffered -oP '[0-9]*+(?=.[0-9])' | clean_progress 100 | \
whiptail --gauge "Downloading system image (${NAME})" 10 50 0
fi
CHECKSUM2=`sha256sum "${IMG_FILE}" | cut -d' ' -f 1`
if [ "$CHECKSUM" != "$CHECKSUM2" ]; then
rm -f "${IMG_FILE}"
echo "checksum does not match; aborting"
exit 1
fi
fi
if [ -z ${SHOW_UI} ]; then
echo "installing ${NAME}..."
else
whiptail --infobox "Extracting and installing system image (${NAME}). This may take some time." 10 50
fi
if [[ "${IMG_FILE##*.}" == "img" ]]; then
btrfs receive --quiet ${DEPLOY_PATH} < ${IMG_FILE}
else
tar xfO ${IMG_FILE} | btrfs receive --quiet ${DEPLOY_PATH}
fi
mkdir -p ${MOUNT_PATH}/boot/${NAME}
cp ${SUBVOL}/boot/vmlinuz-linux ${MOUNT_PATH}/boot/${NAME}
cp ${SUBVOL}/boot/initramfs-linux.img ${MOUNT_PATH}/boot/${NAME}
AMD_UCODE=""
if [ -e ${SUBVOL}/boot/amd-ucode.img ] ; then
cp ${SUBVOL}/boot/amd-ucode.img ${MOUNT_PATH}/boot/${NAME}
AMD_UCODE="initrd /${NAME}/amd-ucode.img"
fi
INTEL_UCODE=""
if [ -e ${SUBVOL}/boot/intel-ucode.img ] ; then
cp ${SUBVOL}/boot/intel-ucode.img ${MOUNT_PATH}/boot/${NAME}
INTEL_UCODE="initrd /${NAME}/intel-ucode.img"
fi
ADDITIONAL_ARGUMENTS=""
if [ -e ${SUBVOL}/usr/lib/frzr.d/bootconfig.conf ] ; then
ADDITIONAL_ARGUMENTS="$ADDITIONAL_ARGUMENTS $(cat ${SUBVOL}/usr/lib/frzr.d/bootconfig.conf)"
fi
get_boot_cfg "${NAME}" "${AMD_UCODE}" "${INTEL_UCODE}" "${ADDITIONAL_ARGUMENTS}" > ${BOOT_CFG}
echo "default frzr.conf" > ${MOUNT_PATH}/boot/loader/loader.conf
rm -f ${MOUNT_PATH}/*.img.*
rm -rf /var/lib/pacman # undo frzr-unlock
# Check if there are migrations available
if compgen -G "${SUBVOL}"/usr/lib/frzr.d/*.migration > /dev/null ; then
for m in "${SUBVOL}"/usr/lib/frzr.d/*.migration ;
do
unset -f post_install
. $m
if [ "$(type -t post_install)" == function ] ; then
post_install "${MOUNT_PATH}" "${SUBVOL}" "${NAME}"
fi
unset -f post_install
done
fi
echo "deployment complete; restart to boot into ${NAME}"
umount -R ${MOUNT_PATH}
}
if [ "$0" = "$BASH_SOURCE" ] ; then
main "$@"
fi