| 1 |
# shellcheck shell=bash |
| 2 |
# systemd-nspawn(1) completion -*- shell-script -*- |
| 3 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
# |
| 5 |
# This file is part of systemd. |
| 6 |
# |
| 7 |
# systemd is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU Lesser General Public License as published by |
| 9 |
# the Free Software Foundation; either version 2.1 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# systemd is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
# General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU Lesser General Public License |
| 18 |
# along with systemd; If not, see <https://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
__contains_word() { |
| 21 |
local w word=$1; shift |
| 22 |
for w in "$@"; do |
| 23 |
[[ $w = "$word" ]] && return |
| 24 |
done |
| 25 |
} |
| 26 |
|
| 27 |
__get_users() { |
| 28 |
local a b |
| 29 |
loginctl list-users --no-legend --no-pager 2>/dev/null | |
| 30 |
{ while read -r a b; do echo " $b"; done; } |
| 31 |
} |
| 32 |
|
| 33 |
__get_slices() { |
| 34 |
local a b |
| 35 |
systemctl list-units -t slice --no-legend --no-pager --plain 2>/dev/null | |
| 36 |
{ while read -r a b; do echo " $a"; done; } |
| 37 |
} |
| 38 |
|
| 39 |
__get_machines() { |
| 40 |
local a b |
| 41 |
{ machinectl list --full --max-addresses=0 --no-legend --no-pager 2>/dev/null; echo ".host"; } | |
| 42 |
{ while read -r a b; do echo " $a"; done; } | |
| 43 |
sort -u |
| 44 |
} |
| 45 |
|
| 46 |
__get_env() { |
| 47 |
local a |
| 48 |
env | { while read -r a; do [[ $a =~ ^[A-Za-z0-9_]+= ]] && echo " ${a%%=*}"; done; } |
| 49 |
} |
| 50 |
|
| 51 |
__get_interfaces(){ |
| 52 |
local name |
| 53 |
for name in $(cd /sys/class/net && command ls); do |
| 54 |
[[ "$name" != "lo" ]] && echo "$name" |
| 55 |
done |
| 56 |
} |
| 57 |
|
| 58 |
__get_rlimit() { |
| 59 |
local i |
| 60 |
for i in $(systemd-nspawn --rlimit=help 2>/dev/null); do |
| 61 |
echo " ${i}=" |
| 62 |
done |
| 63 |
} |
| 64 |
|
| 65 |
_systemd_nspawn() { |
| 66 |
local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} words cword |
| 67 |
local i verb comps |
| 68 |
|
| 69 |
local -A OPTS=( |
| 70 |
[STANDALONE]='-h --help --version --private-network -b --boot --read-only -q --quiet --share-system |
| 71 |
--keep-unit -n --network-veth -j -x --ephemeral -a --as-pid2 -U --suppress-sync=yes |
| 72 |
--cleanup --user --system' |
| 73 |
[ARG]='-D --directory -u --uid --uuid --capability --drop-capability --link-journal --bind --bind-ro |
| 74 |
-M --machine -S --slice -E --setenv -Z --selinux-context -L --selinux-apifs-context |
| 75 |
--register --network-interface --network-bridge --personality -i --image --image-policy --tmpfs |
| 76 |
--volatile --network-macvlan --kill-signal --template --notify-ready --root-hash --chdir |
| 77 |
--pivot-root --property --private-users --private-users-ownership --network-namespace-path |
| 78 |
--network-ipvlan --network-veth-extra --network-zone -p --port --system-call-filter --overlay |
| 79 |
--overlay-ro --settings --rlimit --hostname --no-new-privileges --oom-score-adjust --cpu-affinity |
| 80 |
--resolv-conf --timezone --root-hash-sig --background --oci-bundle --verity-data |
| 81 |
--restrict-address-families |
| 82 |
--forward-journal --forward-journal-max-use --forward-journal-keep-free |
| 83 |
--forward-journal-max-file-size --forward-journal-max-files' |
| 84 |
) |
| 85 |
|
| 86 |
_init_completion || return |
| 87 |
|
| 88 |
if __contains_word "$prev" ${OPTS[ARG]}; then |
| 89 |
case $prev in |
| 90 |
--directory|-D|--template|--oci-bundle) |
| 91 |
compopt -o nospace |
| 92 |
comps=$(compgen -S/ -A directory -- "$cur" ) |
| 93 |
;; |
| 94 |
--uid|-u) |
| 95 |
comps=$( __get_users ) |
| 96 |
;; |
| 97 |
--uuid|--root-hash) |
| 98 |
comps='' |
| 99 |
;; |
| 100 |
--capability) |
| 101 |
comps='CAP_BLOCK_SUSPEND CAP_IPC_LOCK CAP_MAC_ADMIN CAP_MAC_OVERRIDE CAP_SYS_MODULE CAP_SYS_PACCT CAP_SYS_RAWIO |
| 102 |
CAP_SYS_TIME CAP_SYSLOG CAP_WAKE_ALARM CAP_NET_ADMIN' |
| 103 |
;; |
| 104 |
--drop-capability) |
| 105 |
comps='CAP_AUDIT_CONTROL CAP_AUDIT_WRITE CAP_CHOWN CAP_DAC_OVERRIDE CAP_DAC_READ_SEARCH CAP_FOWNER CAP_FSETID |
| 106 |
CAP_IPC_OWNER CAP_KILL CAP_LEASE CAP_LINUX_IMMUTABLE CAP_MKNOD CAP_NET_ADMIN CAP_NET_BIND_SERVICE |
| 107 |
CAP_NET_BROADCAST CAP_NET_RAW CAP_SETFCAP CAP_SETGID CAP_SETPCAP CAP_SETUID CAP_SYS_ADMIN CAP_SYS_BOOT |
| 108 |
CAP_SYS_CHROOT CAP_SYS_NICE CAP_SYS_PTRACE CAP_SYS_RESOURCE CAP_SYS_TTY_CONFIG' |
| 109 |
;; |
| 110 |
--link-journal) |
| 111 |
comps='no auto guest try-guest host try-host' |
| 112 |
;; |
| 113 |
--bind|--bind-ro) |
| 114 |
compopt -o nospace -o filenames |
| 115 |
comps=$(compgen -f -- "$cur" ) |
| 116 |
;; |
| 117 |
--tmpfs) |
| 118 |
compopt -o nospace |
| 119 |
comps=$(compgen -S/ -A directory -- "$cur" ) |
| 120 |
;; |
| 121 |
--machine|-M) |
| 122 |
comps=$( __get_machines ) |
| 123 |
;; |
| 124 |
--slice|-S) |
| 125 |
comps=$( __get_slices ) |
| 126 |
;; |
| 127 |
--setenv|-E) |
| 128 |
comps=$( __get_env ) |
| 129 |
;; |
| 130 |
--selinux-context|-Z) |
| 131 |
comps='' |
| 132 |
;; |
| 133 |
--selinux-apifs-context|-L) |
| 134 |
comps='' |
| 135 |
;; |
| 136 |
--register) |
| 137 |
comps='yes no auto' |
| 138 |
;; |
| 139 |
--network-interface) |
| 140 |
comps=$(__get_interfaces) |
| 141 |
;; |
| 142 |
--network-bridge) |
| 143 |
comps='' |
| 144 |
;; |
| 145 |
--network-macvlan) |
| 146 |
comps='' |
| 147 |
;; |
| 148 |
--personality) |
| 149 |
comps='x86 x86-64' |
| 150 |
;; |
| 151 |
--volatile) |
| 152 |
comps=$( systemd-nspawn --volatile=help 2>/dev/null ) |
| 153 |
;; |
| 154 |
--image|-i) |
| 155 |
compopt -o nospace |
| 156 |
comps=$( compgen -A file -- "$cur" ) |
| 157 |
;; |
| 158 |
--kill-signal) |
| 159 |
_signals |
| 160 |
return |
| 161 |
;; |
| 162 |
--notify-ready) |
| 163 |
comps='yes no' |
| 164 |
;; |
| 165 |
--private-users) |
| 166 |
comps='yes no pick identity managed' |
| 167 |
;; |
| 168 |
--network-namespace-path) |
| 169 |
comps=$( compgen -A file -- "$cur" ) |
| 170 |
;; |
| 171 |
--settings) |
| 172 |
comps='yes no override trusted' |
| 173 |
;; |
| 174 |
--rlimit) |
| 175 |
comps=$( __get_rlimit ) |
| 176 |
;; |
| 177 |
--hostname) |
| 178 |
comps='' |
| 179 |
;; |
| 180 |
--no-new-privileges) |
| 181 |
comps='yes no' |
| 182 |
;; |
| 183 |
--oom-score-adjust) |
| 184 |
comps='' |
| 185 |
;; |
| 186 |
--cpu-affinity) |
| 187 |
comps='' |
| 188 |
;; |
| 189 |
--resolv-conf) |
| 190 |
comps=$( systemd-nspawn --resolv-conf=help 2>/dev/null ) |
| 191 |
;; |
| 192 |
--timezone) |
| 193 |
comps=$( systemd-nspawn --timezone=help 2>/dev/null ) |
| 194 |
;; |
| 195 |
--root-hash-sig) |
| 196 |
compopt -o nospace |
| 197 |
comps=$( compgen -A file -- "$cur" ) |
| 198 |
;; |
| 199 |
esac |
| 200 |
COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) |
| 201 |
return 0 |
| 202 |
fi |
| 203 |
|
| 204 |
COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") ) |
| 205 |
} |
| 206 |
|
| 207 |
complete -F _systemd_nspawn systemd-nspawn |