lxc.sh 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #!/usr/bin/env bash
  2. # -*- coding: utf-8; mode: sh indent-tabs-mode: nil -*-
  3. # SPDX-License-Identifier: AGPL-3.0-or-later
  4. # shellcheck source=utils/lib.sh
  5. source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
  6. source_dot_config
  7. # ----------------------------------------------------------------------------
  8. # config
  9. # ----------------------------------------------------------------------------
  10. #
  11. # read also:
  12. # - https://lxd.readthedocs.io/en/latest/
  13. # name of https://images.linuxcontainers.org
  14. LINUXCONTAINERS_ORG_NAME="${LINUXCONTAINERS_ORG_NAME:-images}"
  15. HOST_PREFIX="${HOST_PREFIX:-searx}"
  16. TEST_IMAGES=(
  17. "$LINUXCONTAINERS_ORG_NAME:ubuntu/18.04" "ubu1804"
  18. "$LINUXCONTAINERS_ORG_NAME:ubuntu/19.04" "ubu1904"
  19. # TODO: installation of searx & filtron not yet implemented ..
  20. #
  21. #"$LINUXCONTAINERS_ORG_NAME:archlinux" "archlinux"
  22. #"$LINUXCONTAINERS_ORG_NAME:fedora/31" "fedora31"
  23. )
  24. ubu1804_boilerplate="
  25. export DEBIAN_FRONTEND=noninteractive
  26. apt-get install -y git curl wget
  27. "
  28. # shellcheck disable=SC2034
  29. ubu1904_boilerplate="$ubu1804_boilerplate"
  30. REMOTE_IMAGES=()
  31. LOCAL_IMAGES=()
  32. for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do
  33. REMOTE_IMAGES=("${REMOTE_IMAGES[@]}" "${TEST_IMAGES[i]}")
  34. LOCAL_IMAGES=("${LOCAL_IMAGES[@]}" "${HOST_PREFIX}-${TEST_IMAGES[i+1]}")
  35. done
  36. HOST_USER="${SUDO_USER:-$USER}"
  37. HOST_USER_ID=$(id -u "${HOST_USER}")
  38. HOST_GROUP_ID=$(id -g "${HOST_USER}")
  39. # ----------------------------------------------------------------------------
  40. usage() {
  41. # ----------------------------------------------------------------------------
  42. cat <<EOF
  43. usage::
  44. $(basename "$0") build [containers]
  45. $(basename "$0") remove [containers|subordinate]
  46. $(basename "$0") [start|stop] [containers]
  47. $(basename "$0") inspect [info|config]
  48. $(basename "$0") cmd ...
  49. build / remove
  50. :containers: build and remove all LXC containers
  51. add / remove
  52. :subordinate: lxd permission to map ${HOST_USER}'s user/group id through
  53. start/stop
  54. :containers: start/stop of all containers
  55. inspect
  56. :info: show info of all containers
  57. :config: show config of all containers
  58. cmd ...
  59. run commandline ... in all containers
  60. all LXC containers:
  61. ${LOCAL_IMAGES[@]}
  62. EOF
  63. [ -n "${1+x}" ] && err_msg "$1"
  64. }
  65. lxd_info() {
  66. cat <<EOF
  67. LXD is needed, to install run::
  68. snap install lxd
  69. lxd init --auto
  70. EOF
  71. }
  72. main() {
  73. local exit_val
  74. if ! required_commands lxc; then
  75. lxd_info
  76. exit 42
  77. fi
  78. local _usage="unknown or missing $1 command $2"
  79. case $1 in
  80. --source-only) ;;
  81. -h|--help) usage; exit 0;;
  82. build)
  83. sudo_or_exit
  84. case $2 in
  85. containers) build_instances ;;
  86. *) usage "$_usage"; exit 42;;
  87. esac ;;
  88. remove)
  89. sudo_or_exit
  90. case $2 in
  91. containers) remove_instances ;;
  92. subordinate) echo; del_subordinate_ids ;;
  93. *) usage "$_usage"; exit 42;;
  94. esac ;;
  95. add)
  96. sudo_or_exit
  97. case $2 in
  98. subordinate) echo; add_subordinate_ids ;;
  99. *) usage "$_usage"; exit 42;;
  100. esac ;;
  101. start|stop)
  102. sudo_or_exit
  103. case $2 in
  104. containers) lxc_cmd "$1" ;;
  105. *) usage "$_usage"; exit 42;;
  106. esac ;;
  107. inspect)
  108. sudo_or_exit
  109. case $2 in
  110. config) lxc_cmd config show;;
  111. info) lxc_cmd info;;
  112. *) usage "$_usage"; exit 42;;
  113. esac ;;
  114. cmd)
  115. sudo_or_exit
  116. shift
  117. for i in "${LOCAL_IMAGES[@]}"; do
  118. info_msg "call ${_BBlue}${i}${_creset} -- ${_BGreen}${*}${_creset}"
  119. wait_key 3
  120. lxc exec "${i}" -- "$@"
  121. exit_val=$?
  122. if [ $exit_val -ne 0 ]; then
  123. err_msg "$exit_val ${_BBlue}${i}${_creset} -- ${_BGreen}${*}${_creset}"
  124. fi
  125. done
  126. ;;
  127. *)
  128. usage "unknown or missing command $1"; exit 42;;
  129. esac
  130. }
  131. build_instances() {
  132. rst_title "Build LXC instances"
  133. rst_title "copy images" section
  134. echo
  135. lxc_copy_images_localy
  136. lxc image list local: && wait_key
  137. echo
  138. rst_title "build containers" section
  139. echo
  140. lxc_init_containers
  141. lxc_config_containers
  142. lxc_boilerplate_containers
  143. echo
  144. lxc list "$HOST_PREFIX"
  145. }
  146. remove_instances() {
  147. rst_title "Remove LXC instances"
  148. echo -en "\\nLXC containers(s)::\\n\\n ${LOCAL_IMAGES[*]}\\n" | $FMT
  149. if ask_yn "Do you really want to delete all images"; then
  150. lxc_delete_containers
  151. fi
  152. echo
  153. lxc list "$HOST_PREFIX"
  154. # lxc image list local: && wait_key
  155. }
  156. # images
  157. # ------
  158. lxc_copy_images_localy() {
  159. for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do
  160. if lxc image info "local:${TEST_IMAGES[i+1]}" &>/dev/null; then
  161. info_msg "image ${TEST_IMAGES[i]} already copied --> ${TEST_IMAGES[i+1]}"
  162. else
  163. info_msg "copy image locally ${TEST_IMAGES[i]} --> ${TEST_IMAGES[i+1]}"
  164. lxc image copy "${TEST_IMAGES[i]}" local: \
  165. --alias "${TEST_IMAGES[i+1]}" | prefix_stdout
  166. fi
  167. done
  168. }
  169. lxc_delete_images_localy() {
  170. echo
  171. for i in "${LOCAL_IMAGES[@]}"; do
  172. info_msg "delete image 'local:$i'"
  173. lxc image delete "local:$i"
  174. done
  175. #lxc image list local:
  176. }
  177. # container
  178. # ---------
  179. lxc_cmd() {
  180. for i in "${LOCAL_IMAGES[@]}"; do
  181. info_msg "lxc $* $i"
  182. lxc "$@" "$i"
  183. done
  184. }
  185. lxc_init_containers() {
  186. local shortname
  187. for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do
  188. shortname="${TEST_IMAGES[i+1]}"
  189. if lxc info "${HOST_PREFIX}-${shortname}" &>/dev/null; then
  190. info_msg "conatiner '$i' already exists"
  191. else
  192. info_msg "create conatiner instance: $i"
  193. lxc init "local:${shortname}" "${HOST_PREFIX}-${shortname}"
  194. fi
  195. done
  196. }
  197. lxc_config_containers() {
  198. for i in "${LOCAL_IMAGES[@]}"; do
  199. info_msg "configure container: ${_BBlue}${i}${_creset}"
  200. info_msg "map uid/gid from host to container"
  201. # https://lxd.readthedocs.io/en/latest/userns-idmap/#custom-idmaps
  202. echo -e -n "uid $HOST_USER_ID 1000\\ngid $HOST_GROUP_ID 1000"\
  203. | lxc config set "$i" raw.idmap -
  204. info_msg "share ${REPO_ROOT} (repo_share) from HOST into container"
  205. # https://lxd.readthedocs.io/en/latest/instances/#type-disk
  206. lxc config device add "$i" repo_share disk \
  207. source="${REPO_ROOT}" \
  208. path="/share/$(basename "${REPO_ROOT}")" &>/dev/null
  209. # lxc config show "$i" && wait_key
  210. done
  211. }
  212. lxc_boilerplate_containers() {
  213. local shortname
  214. local boilerplate_script
  215. for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do
  216. shortname="${TEST_IMAGES[i+1]}"
  217. info_msg "install boilerplate: ${_BBlue}${HOST_PREFIX}-${shortname}${_creset}"
  218. lxc start -q "${HOST_PREFIX}-${shortname}" &>/dev/null
  219. boilerplate_script="${shortname}_boilerplate"
  220. boilerplate_script="${!boilerplate_script}"
  221. if [[ -n "${boilerplate_script}" ]]; then
  222. echo "$boilerplate_script" \
  223. | lxc exec "${HOST_PREFIX}-${shortname}" -- bash \
  224. | prefix_stdout " ${HOST_PREFIX}-${shortname} | "
  225. else
  226. warn_msg "no boilerplate for instance '$i'"
  227. fi
  228. done
  229. }
  230. lxc_delete_containers() {
  231. for i in "${LOCAL_IMAGES[@]}"; do
  232. if lxc info "$i" &>/dev/null; then
  233. info_msg "stop & delete instance '$i'"
  234. lxc stop "$i" &>/dev/null
  235. lxc delete "$i" | prefix_stdout
  236. else
  237. warn_msg "instance '$i' does not exist / can't delete :o"
  238. fi
  239. done
  240. }
  241. # subordinates
  242. # ------------
  243. #
  244. # see man: subgid(5), subuid(5), https://lxd.readthedocs.io/en/latest/userns-idmap
  245. #
  246. # E.g. in the HOST you have uid=1001(user) and/or gid=1001(user) ::
  247. #
  248. # root:1001:1
  249. #
  250. # in the CONTAINER::
  251. #
  252. # config:
  253. # raw.idmap: |
  254. # uid 1001 1000
  255. # gid 1001 1000
  256. add_subordinate_ids() {
  257. if grep "root:${HOST_USER_ID}:1" /etc/subuid -qs; then
  258. info_msg "lxd already has permission to map ${HOST_USER_ID}'s user/group id through"
  259. else
  260. info_msg "add lxd permission to map ${HOST_USER_ID}'s user/group id through"
  261. usermod --add-subuids "${HOST_USER_ID}-${HOST_USER_ID}" \
  262. --add-subgids "${HOST_GROUP_ID}-${HOST_GROUP_ID}" root
  263. fi
  264. }
  265. del_subordinate_ids() {
  266. local out
  267. local exit_value
  268. if grep "root:${HOST_USER_ID}:1" /etc/subuid -qs; then
  269. # TODO: root user is always in use by process 1, how can we remove subordinates?
  270. info_msg "remove lxd permission to map ${HOST_USER_ID}'s user/group id through"
  271. out=$(usermod --del-subuids "${HOST_USER_ID}-${HOST_USER_ID}" --del-subgids "${HOST_GROUP_ID}-${HOST_GROUP_ID}" root 2>&1)
  272. exit_val=$?
  273. if [ $exit_val -ne 0 ]; then
  274. err_msg "$out"
  275. fi
  276. else
  277. info_msg "lxd does not have permission to map ${HOST_USER_ID}'s user/group id through"
  278. fi
  279. }
  280. # ----------------------------------------------------------------------------
  281. main "$@"
  282. # ----------------------------------------------------------------------------