lxc.sh 10.0 KB

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