Repository navigation
Expand file tree
/
Copy pathbootprep-btrfs
More file actions
executable file
·98 lines (76 loc) · 2.75 KB
/
Copy pathbootprep-btrfs
File metadata and controls
executable file
·98 lines (76 loc) · 2.75 KB
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
#!/usr/bin/env bash
#
# bootprep-btrfs
#
# A btrfs orchestrator
#
# Version: 2.1.0
# License: GPL-3.0-or-later
#
# Copyright (C) 2026 Scott McClain
# SPDX-License-Identifier: GPL-3.0-or-later
set -euo pipefail
# Wrap once so nested installer/engine calls share the same bounded run log.
if [[ $EUID -eq 0 && "${BOOTPREP_LOG_ACTIVE:-}" != 1 && "${1:-}" == activate ]]; then
command -v python3 >/dev/null 2>&1 || { echo "Please install Python 3 for BootPrep logging." >&2; exit 1; }
BOOTPREP_LOG_HELPER="/usr/lib/bootprep/bootprep-log.py"
[[ -f "$BOOTPREP_LOG_HELPER" ]] || { echo "Missing BootPrep logging helper: $BOOTPREP_LOG_HELPER" >&2; exit 1; }
exec python3 "$BOOTPREP_LOG_HELPER" /bin/bash "${BASH_SOURCE[0]}" "$@"
fi
readonly BTRFS="/usr/bin/btrfs"
readonly BOOTPREP="/usr/sbin/bootprep"
die() {
printf '[ERROR] %s\n' "$1" >&2
exit 1
}
require_root() {
[[ ${EUID} -eq 0 ]] || die "Please run with sudo."
}
activate() {
local snapshot_num="${1:-}"
local mount_point="${2:-/}"
local base_mount
local target
local subvol_id
[[ $# -le 2 ]] \
|| die "Usage: bootprep-btrfs activate <snapshot-number> [mount-point]"
[[ -n "${snapshot_num}" && "${snapshot_num}" =~ ^[0-9]+$ ]] \
|| die "Usage: bootprep-btrfs activate <snapshot-number> [mount-point]"
[[ -d "${mount_point}" ]] \
|| die "Mount point does not exist: ${mount_point}"
base_mount="${mount_point%/}/"
target="${base_mount}.snapshots/${snapshot_num}/snapshot"
[[ -d "${target}" ]] \
|| die "Snapshot directory does not exist: ${target}"
"${BTRFS}" subvolume show "${target}" >/dev/null 2>&1 \
|| die "Snapshot path is not a Btrfs subvolume: ${target}"
subvol_id="$("${BTRFS}" inspect-internal rootid "${target}")"
[[ "${subvol_id}" =~ ^[0-9]+$ ]] \
|| die "Unable to resolve Btrfs subvolume ID for: ${target}"
printf '\n'
printf '=========================================\n'
printf ' BootPrep Btrfs\n'
printf '=========================================\n'
printf 'Snapshot : #%s\n' "${snapshot_num}"
printf 'Path : %s\n' "${target}"
printf 'Mount Point : %s\n' "${mount_point}"
printf '\n'
printf '[1/3] Making snapshot writable...\n'
"${BTRFS}" property set "${target}" ro false
printf '[2/3] Setting default Btrfs subvolume...\n'
"${BTRFS}" subvolume set-default "${subvol_id}" "${mount_point}"
printf '[3/3] Preparing snapshot for boot...\n'
"${BOOTPREP}" prepare "${snapshot_num}"
printf '\n'
printf '[ OK ] Snapshot #%s is activated and ready for reboot.\n' "${snapshot_num}"
}
require_root
case "${1:-}" in
activate)
shift
activate "$@"
;;
*)
exec "${BTRFS}" "$@"
;;
esac