-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrpm_ostree_utils.sh
More file actions
executable file
·247 lines (216 loc) · 9.55 KB
/
rpm_ostree_utils.sh
File metadata and controls
executable file
·247 lines (216 loc) · 9.55 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
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
#!/usr/bin/env bash
# SCRIPT_DIR="$(dirname "${BASH_SOURCE[0]}")"
source "$(dirname "${BASH_SOURCE[0]}")/lib/bashlib.sh"
rot() { rpm-ostree "$@"; return $?; }
rot.install() { rot install --idempotent --apply-live --assumeyes "$@"; return $?; }
rot.i() { rot.install "$@"; return $?; }
rot.uninstall() { rot uninstall --idempotent --assumeyes "$@"; return $?; }
rot.u() { rot.uninstall "$@"; return $?; }
rot.search() { rot search "$@" | sort -u | perl -ne 'print if /^[^=]/'; return $?; }
rot.s() { rot.search "$@"; return $?; }
rot.booted() {
[[ " $* " =~ ' --help ' ]] && {
echo -e "Usage: ${FUNCNAME[0]} [OPTIONS] [properties]
Get information about the currently booted deployment.
--index print deployment index
"
return 0
}
local json_data=$(rpm-ostree status --json) || { echo "Error: Failed to get rpm-ostree status" >&2; return 1; }
local deployments=$(echo "$json_data" | jq '.deployments')
[[ " $* " =~ ' --index ' ]] && {
local booted_index=$(echo "$deployments" | jq 'to_entries[] | select(.value.booted) | .key') || { echo "Error: Failed to get booted index" >&2; return 1; }
shift 1
echo "$booted_index"
return 0
}
local booted_deployment=$(echo "$deployments" | jq '.[] | select(.booted)') || { echo "Error: Failed to get booted deployment data" >&2; return 1; }
local props=($@)
[[ ${#props[@]} -eq 0 ]] && { echo "$booted_deployment"; return 0; }
for prop in "${props[@]}"; do
echo -e "$prop:\t$(echo "$booted_deployment" | jq --raw-output ".${prop}")"
done
}
rot.booted.version() { rot.booted "$@" | jq -r '.version'; return $?; }
# rot.booted.dir() {
# local depl_booted_serial="$(rpm-ostree status --json | jq --raw-output '.deployments[] | select(.booted) | .serial')"
# local depl_booted_checksum="$(rpm-ostree status --json | jq --raw-output '.deployments[] | select(.booted) | .checksum')"
# local depl_booted_osname="$(rpm-ostree status --json | jq --raw-output '.deployments[] | select(.booted) | .osname')"
# local depl_booted_root_dir_path="/ostree/deploy/${depl_booted_osname}/deploy/${depl_booted_checksum}.${depl_booted_serial}"
# }
rot.repos.list() {
[[ " $* " =~ ' --help ' ]] && {
echo -e "Usage: ${FUNCNAME[0]} [OPTIONS] [DEPLOYMENT_INDEX]
List repos in a deployment (default index: 0).
--help Show this help
"
return 0
}
local json_data=$(rpm-ostree status --json) || { echo -e "Error: Failed to get rpm-ostree status" >&2; return 1; }
local depl="${1:-$(rot.booted --index)}"
[[ ! "${depl}" =~ ^[0-9]+$ ]] && { echo -e "Error: Deployment index must be a number" >&2; return 1; }
local deployment_count=$(echo "$json_data" | jq '.deployments | length')
[[ "${depl}" -ge "${deployment_count}" ]] && { echo -e "Error: Deployment index ${depl} out of range (total: ${deployment_count})" >&2; return 1; }
local json_data_depl=$(echo "$json_data" | jq --argjson idx "${depl}" '.deployments[$idx]')
local repos=$(echo "${json_data_depl}" | jq --raw-output '.["layered-commit-meta"].["rpmostree.rpmmd-repos"][].["id"]' | sort -u)
echo "${repos}"
}
#rot.repo.url() {
#
#}
rot.pl() {
[[ " $* " =~ ' --help ' ]] && {
echo -e "Usage: ${FUNCNAME[0]} [OPTIONS] [DEPLOYMENT_INDEX]
List packages from a specific deployment in rpm-ostree (default: index 0).
--lskeys List top level keys
--lsdeps List deployment indexes
--lsdepsver List deployment indexes with versions
--ulo User layered only
--all All packages
--help Show this help
"
# --blo Base layer only
return 0
}
local json_data=$(rpm-ostree status --json) || { echo -e "Error: Failed to get rpm-ostree status" >&2; return 1; }
[[ " $* " =~ ' --lskeys ' ]] && { shift 1; echo "${json_data}" | jq --raw-output 'keys[]'; return 0; }
[[ " $* " =~ ' --lsdeps ' ]] && { shift 1; echo "${json_data}" | jq --raw-output '.deployments | keys[]'; return 0; }
[[ " $* " =~ ' --lsdepsver ' ]] && { shift 1; echo "${json_data}" | jq --raw-output '.deployments | to_entries[] | "\(.key)\t\(.value.version)"'; return 0; }
local opt_ulo=0
local opt_all=0
[[ " $* " =~ ' --ulo ' ]] && { shift 1; opt_ulo=1; }
[[ " $* " =~ ' --all ' ]] && { shift 1; opt_all=1; }
#(($# > 0)) && shift $(( $# - 1 ))
local depl="${1:-$(rot.booted --index)}"
[[ ! "${depl}" =~ ^[0-9]+$ ]] && { echo -e "Error: Deployment index must be a number" >&2; return 1; }
local deployment_count=$(echo "$json_data" | jq '.deployments | length')
[[ "${depl}" -ge "${deployment_count}" ]] && { echo -e "Error: Deployment index ${depl} out of range (total: ${deployment_count})" >&2; return 1; }
local json_data_depl=$(echo "$json_data" | jq --raw-output --argjson idx "${depl}" '.deployments[$idx]')
local pkg_full_list=""
pkg_full_list+=$(echo "$json_data_depl" | jq --raw-output '.packages[]?');
(( !opt_ulo || opt_all )) && { pkg_full_list+=$(echo "$json_data_depl" | jq --raw-output '.["base-commit-meta"].["ostree.container.image-config"] | fromjson | .config.Labels.["dev.hhd.rechunk.info"] | fromjson | .packages | keys[]'); }
echo "${pkg_full_list}" | sort -u
}
rot.pl.diff() {
local depl_old="${1:-1}"; [[ ! "${depl_old}" =~ ^[0-9]+$ ]] && { echo -e "Old Deployment index must be a number (got ${depl_old})"; return 1; }
local depl_new="${2:-$(rot.booted --index)}"; [[ ! "${depl_new}" =~ ^[0-9]+$ ]] && { echo -e "New Deployment index must be a number (got ${depl_new})"; return 1; }
diff -u <(rot.pl "${depl_old}") <(rot.pl "${depl_new}") | perl -ne 'print if /^[+-]/'
}
rot.pl.diff.add() {
rot.pl.diff "${1}" | grep -E "^\+" | tail -n+2 | sed 's/^[+-]//g'
}
rot.pl.diff.rem() {
rot.pl.diff "${1}" | grep -E "^\-" | tail -n+2 | sed 's/^[+-]//g'
}
# rot.s.inst() {
# # TODO: param --description to include a description.
# local prefix="${1:?"Search prefix required"}"
# grep -f <(rot.pl --all | grep "${prefix}") <(rot.s "${prefix}") | awk --field-separator " : " '{ print $1 }'
# return $?
# }
# rot.s.ninst() {
# # TODO: param --description to include a description.
# local prefix="${1:?"Search prefix required"}"
# grep -v -f <(rot.pl --all | grep "${prefix}") <(rot.s "${prefix}") | awk --field-separator " : " '{ print $1 }'
# return $?
# }
#rot.copr.enable() {
# [[ ((($# == 0))) || (" $* " =~ ' --help ') ]] && {
# echo -e "Usage: ${FUNCNAME[0]} <copr_owner>/<copr_project>
#Add a COPR repo.
# --help Display this help."
# }
# local repo_path="${1:?"Repo path is required"}"
# (($# != 1)) && { echo -e "Command requires 1 argument"; return 0; }
# local owner="$(dirname "${1}")"
# local project="$(basename "${1}")"
# local fedora_version
# fedora_version=$(rpm -E %fedora)
# local repo_url="https://copr.fedorainfracloud.org/coprs/${owner}/${project}/repo/fedora-${fedora_version}/${owner}-${project}-fedora-${fedora_version}.repo"
# echo "${repo_url}"
# local repo_file="/etc/yum.repos.d/_copr:${owner}:${project}.repo"
# echo "${repo_file}"
# #sudo tee "$repo_file" > /dev/null <<EOF
##$(curl -fsSL "$repo_url")
##EOF
# #sudo rpm-ostree refresh-md
#}
# Wrap in $(cmd) for a bash array / space separated list
# To reinstall missing:
#rot install --apply-live -y $(rot_pl_diff | grep -E -v "^\+" | tail -n+2 | sed 's/^[-]//g')
# rot.swap.rm.by.uuid() {
# set -o errexit
# set -o pipefail
# set -o nounset
# local usage="NOT READY YET, DOES NOTHING
# Usage: ${FUNCNAME[0]} [OPTIONS] UUID
# Remove swap safely by UUID.
# -v, --verbose Verbose messages
# -h, --help Show this help
# "
# # -d, --dry-run Show planned actions without changes to the system
# local verbose=0
# local showhelp=0
# # local dryrun=0
# local uuid=""
# # Parse options
# while (( "$#" )); do
# case "$1" in
# --verbose|-V) verbose=1; shift 1 ;;
# --help|-h) showhelp=1; shift 1 ;;
# # --dry-run|-d) dryrun=1; shift 1 ;;
# *) if [[ -z "$uuid" ]]; then uuid="${1}"; shift 1; else echo "Error: Unexpected argument ${1}"; showhelp=1; fi ;;
# esac
# done
# [[ -z "$uuid" ]] && { echo "Error: UUID is required"; showhelp=1; return 1; }
# (( showhelp )) && { echo -e "${usage}"; return 0; }
# # (( verbose )) && echo "Looking up swap device for UUID: $uuid"
# # # Resolve swap device path by UUID
# # local devpath="/dev/disk/by-uuid/$uuid"
# # [[ ! -e $devpath ]] && { echo "Error: No device found with UUID $uuid"; return 1; }
# # local swapdev
# # swapdev=$(readlink -f "$devpath")
# # (( verbose )) && echo "Swap device resolved to: $swapdev"
# # # Disable swap
# # (( verbose )) && echo "Turning off swap on $swapdev"
# # sudo swapoff "$swapdev"
# # # Comment out swap in /etc/fstab
# # if grep -q "UUID=$uuid" /etc/fstab; then
# # (( verbose )) && echo "Commenting out swap entry in /etc/fstab"
# # sudo sed -i.bak "/UUID=$uuid/ s/^/#/" /etc/fstab
# # else
# # (( verbose )) && echo "No matching swap entry in /etc/fstab to comment"
# # fi
# # # Remove resume=UUID= from kernel args using rpm-ostree if exists
# # if command -v rpm-ostree >/dev/null 2>&1; then
# # if rpm-ostree status | grep -q "kernel-args"; then
# # (( verbose )) && echo "Removing resume=UUID=$uuid from kernel arguments"
# # sudo rpm-ostree kargs --delete "resume=UUID=$uuid"
# # (( verbose )) && echo "Reboot required to apply kernel argument changes"
# # fi
# # else
# # (( verbose )) && echo "rpm-ostree not found, skipping kernel args clean up"
# # fi
# # (( verbose )) && echo "Swap removal by UUID $uuid completed"
# return 0
# }
export -f rot
export -f rot.install
export -f rot.i
export -f rot.uninstall
export -f rot.u
export -f rot.search
export -f rot.s
export -f rot.booted
export -f rot.booted.version
export -f rot.pl
export -f rot.pl.diff
export -f rot.pl.diff.add
export -f rot.pl.diff.rem
export -f rot.repos.list
# export -f rot.booted.dir
# export -f rot.copr.enable
# export -f rot.repo.url
# export -f rot.s.inst
# export -f rot.s.ninst
# export -f rot.swap.rm.by.uuid