lxc.sh 9.8 KB

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