lxc.sh 10 KB

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