-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqubes-update-tt
executable file
·593 lines (543 loc) · 18 KB
/
qubes-update-tt
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
#!/bin/bash
#
# Ali's personal Qubes OS improvements, http://www.mirjamali.com
#
# Copyright (C) 2024 Ali Mirjamai <ali@mirjamali.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
set -o noglob
declare SELF=${0##*/}
### Configs
# Lockfile path
lock_file="/tmp/${SELF}.lck"
# qrexec_agent could be either 'qrexec-client' or 'qvm-run'
[ -t 1 ] && qrexec_agent="qvm-run" || qrexec_agent="qrexec-client"
# Colors for qvm-run
color_output=91
color_stderr=37
### End configs
declare syntax="\
This tool is a package manager helper for Qubes OS templates. It could check for
availability of updates, download updates without installation, perform updates;
search, install or remove individual packages on one or more templates.
It is currently compatible with templates using apt(8), dnf(8), emerge(1) or
pacman(8) as their primary package manager. This tool could be run from dom0 or
on management VMs with sufficient privileges. Use it at your own risk!
usage: ${SELF} [--verbose|--quiet] [--assume-yes|assume-no]
[--refresh] [--skip-broken] [--download-only]
[--dry-run] [--auto-start|--no-autostart]
[--if-updates-available]
[--after-update shutdown|running|revert-state]
[--apt OPTIONS] [--dnf OPTIONS]
[--emerge OPTIONS] [--pacman OPTIONS] [--help]
TEMPLATE|--all COMMAND ...
options:
-v, --verbose verbose operation
-q, --quiet quiet operation
--notify-send send graphical notifications via notify-send(1)
-h, --help show command help
-y, --assume-yes automatically answer yes to all questions
-n, --assume-no automatically answer no to all questions
--refresh update list of available packages within template
--skip-broken resolve depsolve problems by skipping packages or
ignoring conflicts. this is usually a terrible idea,
...unless used with '--download-only' option
--download-only download packages without installation.
--all, '*' perform operation on all templates
--dry-run do not execute the actual command(s) but print them.
this option ignores --quiet to print update command
if the template is not running, package manager is
guessed based on template name
--autostart, --auto option ignored, this is default
--no-autostart, --no-auto do not autostart template(s)
--if-updates-available, --if-updates
only perform update task if template is tagged for
updates availability. better used after 'check-only'
--after-update, --after by default, template status will be reverted to the
situation before update task (revert-state). this
behavior could be changed to 'shutdown' or keep
them 'running|keep-running' after update.
paused or transient templates will not be touched!
--apt OPTIONS Additional custom options for apt(8)
--dnf OPTIONS Additional custom options for dnf(8)
--emerge OPTIONS Additional custom options for emerge(1)
--pacman OPTIONS Additional custom options for pacman(8)
TEMPLATE
At least one template. Comma-separated list is OK. Space separated list of
templates inside quotation marks is accepted. Wildcards inside quotes are OK.
--all implies all templates.
subcommands:
check-only Check if any update is available for the
template(s), then notify DOM0.
salt-update Update template(s) via Salt interface (experimental)
update Download and/or Install updates for the template(s)
This is obsolete and highly discouraged.
...unless used with --download-only option
install <PACKAGE> ... Install package(s) on the template(s)
remove <PACKAGE> ... Remove the specified package(s) from template(s)
search <keywords> Search for packages
repolist List enabled repositories of the template(s)
custom ... Used together with --dnf, --apt, --emerge & --pacman
This option is for advanced users! Check it with
'--dry-run' before running the actual command.
"
declare VERBOSE=false
declare QUIET=false
declare ASSUMEYES=false
declare ASSUMENO=false
declare REFRESH=false
declare SKIPBROKEN=false
declare DOWNLOADONLY=false
declare ALLTEMPLATES=false
declare DRYRUN=false
declare AUTOSTART=true
declare IFUPDATES=false
declare AFTERUPDATE='revert-state'
declare SUBCOMMAND=""
declare NOTIFYSEND=false
declare -a template_patterns=()
declare -a cmd_apt=(apt-get)
declare -a cmd_dnf=(dnf)
declare -a cmd_emerge=(emerge)
declare -a cmd_pacman=(pacman)
declare HOSTNAME=$(hostname)
# Synopsis: print_usage [EXIT_CODE]
print_usage() {
message "${syntax}"
[ ! -z ${1} ] && exit ${1}
}
# Synopsis: message MESSAGE
message() {
if ! $QUIET; then
if ! $NOTIFYSEND; then
echo "${@}"
else
notify-send -u normal -a "Qubes Update Tweak Tool" \
--icon software-update-available \
"Tweak Tool Updater" \
"Performing routine ${SUBCOMMAND} tasks.\n${@}"
fi
fi
}
# Synopsis: abort EXIT_CODE MESSAGE
abort() {
message "error: ${2}" 1>&2
message "Please see --help for more information." 1>&2
exit ${1}
}
# Synopsis: parse_cmd_parameters [OPTIONS] COMMAND ...
parse_cmd_parameters() {
arguments=$(getopt -n "${SELF}" --options vqhyn \
--longoptions verbose,quiet,help,assume-yes,assume-no \
--longoptions refresh,skip-broken,download-only,all,dry-run \
--longoptions autostart,auto,no-autostart,no-auto \
--longoptions if-updates-available,notify-send \
--longoptions after-update:,after: \
--longoptions apt:,dnf:,emerge:,pacman: -- "${@}")
[[ ${?} -ne 0 ]] && abort 22 "Invalid option!"
eval set -- "${arguments}"
unset arguments
while true
do
case ${1} in
-v|--verbose) VERBOSE=true;;
-q|--quiet) QUIET=true;;
--notify-send) NOTIFYSEND=true;;
-h|--help) print_usage 0;;
-y|--assume-yes) ASSUMEYES=true;;
-n|--assume-no) ASSUMENO=true;;
--refresh) REFRESH=true;;
--skip-broken) SKIPBROKEN=true;;
--download-only) DOWNLOADONLY=true;;
--all) ALLTEMPLATES=true;;
--dry-run) DRYRUN=true;;
--autostart|--auto)
AUTOSTART=true;;
--no-autostart|--no-auto)
AUTOSTART=false;;
--if-updates-available|--if-updates)
IFUPDATES=true;;
--after-update|--after)
AFTERUPDATE="${2}"; shift;;
--apt) cmd_apt+=(${2}); shift;;
--dnf) cmd_dnf+=(${2}); shift;;
--emerge) cmd_emerge+=(${2}); shift;;
--pacman) cmd_pacman+=(${2}); shift;;
--) shift; break;;
*) abort 22 "Invalid argument: $1";;
esac
shift
done
if $QUIET && $VERBOSE; then
QUIET=false
abort 22 "Could not be Quiet and Verbose at the same time!"
fi
if $ASSUMEYES && $ASSUMENO; then
QUIET=false
abort 22 "Could not assume YES and NO at the same time!"
fi
if ${ALLTEMPLATES}; then
template_patterns=("*")
else
IFS=", " read -ra template_patterns <<< "${1}"
shift
fi
[ ${AFTERUPDATE} = "keep-running" ] && AFTERUPDATE="running"
if ! [[ ${AFTERUPDATE} =~ ^(shutdown|running|revert-state)$ ]]; then
QUIET=false
abort 22 "Invalid option: ${AFTERUPDATE} for --after-update!"
fi
[[ $# -lt 1 ]] && abort 2 "Subcommand missing!"
synthesize_template_cmds "${@}"
}
# Synopsys synthesize_template_cmds COMMAND
synthesize_template_cmds() {
if ${QUIET}; then
cmd_apt+=(-qqq)
cmd_dnf+=(--quiet)
cmd_emerge+=(--quiet)
cmd_pacman+=(--quiet)
fi
if ${VERBOSE}; then
cmd_dnf+=(--verbose)
cmd_emerge+=(--verbose)
cmd_pacman+=(--verbose)
fi
SUBCOMMAND="$1"
case ${SUBCOMMAND} in
check-only)
# Check-only does not need more arguments
[[ $# -gt 1 ]] && abort 7 "Invalid argument: ${2}"
# Note: Special case! No need to handle per distro
# Qubes OS 'upgrades-status-notify' does a nice job.
cmd_all=("/usr/lib/qubes/upgrades-status-notify")
cmd_apt=("${cmd_all}")
cmd_dnf=("${cmd_all}")
cmd_emerge=("${cmd_all}")
cmd_pacman=("${cmd_all}")
return
;;
salt-update)
# salt-update does not need more arguments
[[ $# -gt 1 ]] && abort 7 "Invalid argument: ${2}"
abort 38 "Salt update functionality is not implemented"
;;
update)
# Update does not need more arguments
[[ $# -gt 1 ]] && abort 7 "Invalid argument: ${2}"
cmd_apt+=(full-upgrade)
cmd_dnf+=(upgrade)
cmd_emerge+=(--update --deep --newuse --changed-use \
\@world)
cmd_pacman+=(--sync --sysupgrade)
;;
install)
# We need a list of one or more packages to install
[[ $# -lt 2 ]] && abort 7 "Arguments missing!"
shift
cmd_apt+=(install "${@}")
cmd_dnf+=(install "${@}")
cmd_emerge+=("--update ${@}")
cmd_pacman+=(--sync --needed "${@}")
;;
remove)
# We need a list of one or more packages to remove
[[ $# -lt 2 ]] && abort 7 "Arguments missing!"
shift
cmd_apt+=(remove "${@}")
cmd_dnf+=(remove "${@}")
cmd_emerge+=(--depclean "${@}")
cmd_pacman+=(--remove "${@}")
;;
search)
# We need a list of one or more packages to search for
[[ $# -lt 2 ]] && abort 7 "Arguments missing!"
shift
cmd_apt[0]="apt-cache"
cmd_apt+=(search "${@}")
cmd_dnf+=(search "${@}")
cmd_emerge+=(--search "${@}")
cmd_pacman+=(--sync --search "${@}")
;;
repolist)
cmd_apt=(apt-cache policy)
cmd_dnf=(dnf -v repolist --enabled \| grep -i \
Repo-baseurl)
cmd_emerge=(portageq repos_config / \| grep \
-E '^.*\(sync-uri\).*$')
cmd_pacman=(cat /etc/pacman.d/{mirrorlist,"*.conf"})
;;
custom)
shift
# Using "apt" instead of "apt-get" for custom query
cmd_apt[0]="apt"
cmd_apt+=("${@}")
cmd_dnf+=("${@}")
cmd_emerge+=("${@}")
cmd_pacman+=("${@}")
;;
*)
abort 22 "Invalid command: ${1}";;
esac
if ${REFRESH}; then
# Special refresh case for apt(9) should be prepended to cmd
if ${QUIET}; then
cmd_apt=("apt-get -qqq update;" "${cmd_apt[@]}")
cmd_emerge=("emerge --sync --quiet;" "${cmd_emerge[@]}")
else
cmd_apt=("apt-get update;" "${cmd_apt[@]}")
cmd_emerge=("emerge --sync;" "${cmd_emerge[@]}")
fi
cmd_dnf+=(--refresh)
cmd_pacman+=(--refresh)
fi
if ${ASSUMEYES}; then
cmd_apt+=(--assume-yes)
cmd_dnf+=(--assumeyes)
cmd_pacman+=(--noconfirm)
fi
if ${ASSUMENO}; then
cmd_apt+=(--assume-no)
cmd_dnf+=(--assumeno)
cmd_emerge+=(--pretend)
cmd_pacman+=(--confirm)
fi
if ${SKIPBROKEN}; then
cmd_apt+=(--ignore-missing)
cmd_dnf+=(--skip-broken)
cmd_pacman+=(--nodeps)
fi
if ${DOWNLOADONLY}; then
cmd_apt+=(--download-only)
cmd_dnf+=(--downloadonly --nobest)
cmd_emerge+=(--fetchonly)
cmd_pacman+=(--downloadonly)
fi
}
# Synopsis: qubesd_admin_api <CALL> <DEST> [ARGUMENTS]
qubesd_admin_api() {
local -a api_message
local -i message_type
local CALL="${1}" DEST="${2}"
shift; shift
if [ $# -le 1 ]; then
readarray -t api_message < <(qubesd-query --empty --null \
${HOSTNAME} ${CALL} ${DEST} ${@} | tr '\0' '\n')
else
local ARGUMENT="${1}"
shift
local INSIDE="${@}"
readarray -t api_message < <(qubesd-query --null \
${HOSTNAME} ${CALL} ${DEST} ${ARGUMENT} <<< "${INSIDE}"\
| tr '\0' '\n')
fi
message_type=${api_message[0]}
unset api_message[0]
printf '%s\n' "${api_message[@]}"
return ${message_type}
}
# Synopsis: power_state VM
power_state() {
local api_response
local -a fields
api_response=($(qubesd_admin_api admin.vm.CurrentState $1))
printf "${api_response[3]##*=}"
}
# Synopsis: qrexec VM USER COMMAND...
qrexec() {
local domain=${1}
local user=${2}
shift; shift
local -a qrexec_cmd
if $DRYRUN; then
local tmp=$QUIET
QUIET=false
message "Dry-running as ${user} on ${domain}: ${@}"
QUIET=$tmp
return
elif $VERBOSE; then
message "Running as ${user} on ${domain}: ${@}"
fi
if [ ${qrexec_agent} = "qrexec-client" ]; then
qrexec_cmd=(qrexec-client -w 60 -d ${domain} ${user}:"${@}")
elif [ ${qrexec_agent} = "qvm-run" ]; then
qrexec_cmd=(qvm-run --user=${user} --no-auto --no-gui \
--pass-io --filter-escape-chars)
if [ -t 1 ]; then
qrexec_cmd+=(--color-output=${color_output})
qrexec_cmd+=(--color-stderr=${color_stderr})
else
qrexec_cmd+=(--no-color-output)
qrexec_cmd+=(--no-color-stderr)
fi
qrexec_cmd+=(${domain} -- "${@}")
else
abort 22 "Invalid qrexec agent: ${qrexec_agent}"
fi
"${qrexec_cmd[@]}"
return ${?}
}
# Synopsis: detect_distro TEMPLATE
detect_distro() {
local template="${1}"
local -l ID
# Qubes OS 4.2 templates provide /etc/os-release
# This was tested in February 2024
ID=$(qrexec-client -d ${template} user:"cat /etc/os-release" | \
awk -F= '/^ID=/ {gsub(/"/, ""); print $2}')
printf "${ID}"
}
# Synopsis: update_template TEMPLATE STATE_BEFORE_UPDATE
update_template() {
local template="${1}" state="${2,,}"
local cmd_update
local -l current_state ID
if ! [[ ${state} =~ ^(running|halted)$ ]]; then
message "Only running or halted templates could be updated!"
message "Ignoring ${template} which is currently ${state}."
return
fi
message "Working on ${template} template."
if [ $AUTOSTART = true ] && [ "${state}" = "halted" ] && \
[ $DRYRUN = false ];
then
$VERBOSE && message "Starting ${template} template."
qubesd_admin_api admin.vm.Start ${template} >/dev/null
[[ ${?} -ne 0 ]] && \
abort 42 "Qubes API call to start ${template} failed!"
fi
current_state=$(power_state ${template})
if [ $current_state = "running" ]; then
ID=$(detect_distro "${template}")
else
ID="${template}"
fi
case "${ID}" in
*alpine*)
message "Alpine update is not yet implemented";;
*arch*|*manjaro*)
cmd_update="${cmd_pacman[@]}";;
*debian*|*kali*|*mint*|pop|*buntu*|*whonix*)
cmd_update="${cmd_apt[@]}";;
*fedora*|*centos*|*rocky*)
cmd_update="${cmd_dnf[@]}";;
*gentoo*)
# Since emerge does not detect qvm-run -p as valid tty,
# Sadly it does not honor --ask option.
if ${ASSUMEYES} || ${DOWNLOADONLY} || [[ ${SUBCOMMAND} \
=~ ^(check-only|search|custom|repolist)$ ]];
then
cmd_update="${cmd_emerge[@]}"
else
message "Gentoo deserves user intervention." \
"Add --download-only to download" \
"without installation or add" \
"--assume-yes to override safety!"
unset cmd_update
fi;;
*nixos*)
message "NixOS update is not yet implemented";;
*opensuse*)
message "openSUSE update is not yet implemented";;
*openwrt*)
message "OpenWrt update is not yet implemented";;
*slackware*)
message "Slackware update is not yet implemented";;
*)
message "Release: ${release} unknown! Skipping";;
esac
[ -z "${cmd_update}" ] && return
qrexec ${template} root "${cmd_update}"
if [ $DRYRUN = false ] && \
{ [ $AFTERUPDATE = "shutdown" ] || \
{ [ $AFTERUPDATE = "revert-state" ] && \
[ "${state}" = "halted" ]; }; };
then
$VERBOSE && message "Powering-off ${template} template."
qubesd_admin_api admin.vm.Shutdown ${template} >/dev/null
[[ ${?} -ne 0 ]] && \
abort 42 "Qubes API call to stop ${template} failed!"
fi
return
}
# Synopsis: update_templates TEMPLATE_PATTERNS
update_templates() {
local -a patterns=(${@}) all_vms=() templates=()
local -A vm_class=() vm_state=()
local api_result records record vm template
api_result=$(qubesd_admin_api admin.vm.List dom0)
[[ ${?} -ne 0 ]] && abort 42 "Qubes API call to read VM List failed!"
readarray -t records <<< "${api_result}"
for record in "${records[@]}"; do
local fields
fields=($record)
all_vms+=(${fields[0]})
vm_class[${fields[0]}]=${fields[1]##*=}
vm_state[${fields[0]}]=${fields[2]##*=}
done
# Iterating through all Qubes VMs, if it is a template and matches the
# - provided pattern, add it to templates array
for vm in ${all_vms[@]}; do
[ ! ${vm_class[${vm}],,} == "templatevm" ] && continue
for pattern in ${patterns[@]}; do
if [[ ${vm} == ${pattern} ]]; then
templates+=(${vm})
fi
done
done
for template in ${templates[@]}; do
template_os=$(qubesd_admin_api \
admin.vm.feature.Get ${template} os)
if [[ ! $? == 0 ]]; then
$VERBOSE && message "Could not determin OS of "\
"${template} via Qubes API! Ignoring."
continue
fi
if [[ ! ${template_os,,} == "linux" ]]; then
$VERBOSE && message "${template} is not Linux based. "\
"Ignoring."
# Windows updates could be implemented via powershell
# and 'wuauclt.exe /updatenow'.
# Only if we had a working qrexec. Work in progress...
continue
fi
template_qrexec=$(qubesd_admin_api \
admin.vm.feature.Get ${template} qrexec)
if [[ ! $? == 0 ]] || [[ ! ${template_qrexec} = "1" ]]; then
message "${template} does not support qrexec or " \
"qrexec status unknown. Ignoring."
continue
fi
if $IFUPDATES; then
if_updates=$(qubesd_admin_api admin.vm.feature.Get \
${template} updates-available)
[ "${if_updates}" != "1" ] && continue
fi
# We have a matching and supported template!
update_template ${template} ${vm_state[${template}]}
done
}
# Synopsis: main ALL_ARGS
main() {
parse_cmd_parameters "${@}"
touch "${lock_file}"
exec {FD}<>"${lock_file}"
if ! flock --nonblock --exclusive $FD; then
abort 114 "Another instance of ${SELF} is probably running."
fi
update_templates ${template_patterns[@]}
}
main "${@}"
exit 0