lxc.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. source "${REPO_ROOT}/utils/lxc.env"
  8. # ----------------------------------------------------------------------------
  9. # config
  10. # ----------------------------------------------------------------------------
  11. #
  12. # read also:
  13. # - https://lxd.readthedocs.io/en/latest/
  14. # name of https://images.linuxcontainers.org
  15. LINUXCONTAINERS_ORG_NAME="${LINUXCONTAINERS_ORG_NAME:-images}"
  16. HOST_PREFIX="${HOST_PREFIX:-searx}"
  17. # where all folders from HOST are mounted
  18. LXC_SHARE_FOLDER="/share"
  19. LXC_REPO_ROOT="${LXC_SHARE_FOLDER}/$(basename "${REPO_ROOT}")"
  20. TEST_IMAGES=(
  21. "$LINUXCONTAINERS_ORG_NAME:ubuntu/18.04" "ubu1804"
  22. "$LINUXCONTAINERS_ORG_NAME:ubuntu/19.04" "ubu1904"
  23. "$LINUXCONTAINERS_ORG_NAME:ubuntu/19.10" "ubu1910"
  24. "$LINUXCONTAINERS_ORG_NAME:ubuntu/20.04" "ubu2004"
  25. "$LINUXCONTAINERS_ORG_NAME:archlinux" "archlinux"
  26. "$LINUXCONTAINERS_ORG_NAME:fedora/31" "fedora31"
  27. )
  28. ubu1804_boilerplate="
  29. export DEBIAN_FRONTEND=noninteractive
  30. apt-get update -y
  31. apt-get upgrade -y
  32. apt-get install -y git curl wget
  33. "
  34. ubu1904_boilerplate="$ubu1804_boilerplate"
  35. ubu1910_boilerplate="$ubu1904_boilerplate"
  36. # shellcheck disable=SC2034
  37. ubu2004_boilerplate="$ubu1910_boilerplate"
  38. # shellcheck disable=SC2034
  39. archlinux_boilerplate="
  40. pacman -Syu --noconfirm
  41. pacman -S --noconfirm git curl wget sudo
  42. echo 'Set disable_coredump false' >> /etc/sudo.conf
  43. "
  44. # shellcheck disable=SC2034
  45. fedora31_boilerplate="
  46. dnf update -y
  47. dnf install -y git curl wget hostname
  48. echo 'Set disable_coredump false' >> /etc/sudo.conf
  49. "
  50. REMOTE_IMAGES=()
  51. LOCAL_IMAGES=()
  52. for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do
  53. REMOTE_IMAGES=("${REMOTE_IMAGES[@]}" "${TEST_IMAGES[i]}")
  54. LOCAL_IMAGES=("${LOCAL_IMAGES[@]}" "${HOST_PREFIX}-${TEST_IMAGES[i+1]}")
  55. done
  56. HOST_USER="${SUDO_USER:-$USER}"
  57. HOST_USER_ID=$(id -u "${HOST_USER}")
  58. HOST_GROUP_ID=$(id -g "${HOST_USER}")
  59. # ----------------------------------------------------------------------------
  60. usage() {
  61. # ----------------------------------------------------------------------------
  62. cat <<EOF
  63. usage::
  64. $(basename "$0") build [containers]
  65. $(basename "$0") install [searx-suite]
  66. $(basename "$0") remove [containers|subordinate]
  67. $(basename "$0") [start|stop] [containers|<container-name>]
  68. $(basename "$0") show [info|config|searx-suite]
  69. $(basename "$0") cmd ...
  70. build / remove
  71. :containers: build & launch (or remove) all LXC containers
  72. add / remove
  73. :subordinate: lxd permission to map ${HOST_USER}'s user/group id through
  74. start/stop
  75. :containers: start/stop of all 'containers' or only <container-name>
  76. show
  77. :info: show info of all containers
  78. :config: show config of all containers
  79. :searx-suite: show searx-suite services of all containers
  80. cmd ...
  81. run commandline ... in all containers
  82. install
  83. :searx-suite: install searx suite, includes morty & filtron
  84. all LXC containers:
  85. ${LOCAL_IMAGES[@]}
  86. EOF
  87. [ -n "${1+x}" ] && err_msg "$1"
  88. }
  89. lxd_info() {
  90. cat <<EOF
  91. LXD is needed, to install run::
  92. snap install lxd
  93. lxd init --auto
  94. EOF
  95. }
  96. main() {
  97. local exit_val
  98. local _usage="unknown or missing $1 command $2"
  99. if [[ ! $1 == __* ]] && ! required_commands lxc; then
  100. lxd_info
  101. exit 42
  102. fi
  103. case $1 in
  104. --source-only) ;;
  105. -h|--help) usage; exit 0;;
  106. build)
  107. sudo_or_exit
  108. case $2 in
  109. containers) build_instances ;;
  110. *) usage "$_usage"; exit 42;;
  111. esac
  112. ;;
  113. remove)
  114. sudo_or_exit
  115. case $2 in
  116. containers) remove_instances ;;
  117. subordinate) echo; del_subordinate_ids ;;
  118. *) usage "$_usage"; exit 42;;
  119. esac
  120. ;;
  121. add)
  122. sudo_or_exit
  123. case $2 in
  124. subordinate) echo; add_subordinate_ids ;;
  125. *) usage "$_usage"; exit 42;;
  126. esac
  127. ;;
  128. start|stop)
  129. sudo_or_exit
  130. case $2 in
  131. containers) lxc_cmd "$1" ;;
  132. *)
  133. info_msg "lxc $1 $2"
  134. lxc "$1" "$2" | prefix_stdout "[${_BBlue}${i}${_creset}] "
  135. ;;
  136. esac
  137. ;;
  138. show)
  139. sudo_or_exit
  140. case $2 in
  141. config) lxc_cmd config show;;
  142. info) lxc_cmd info;;
  143. searx-suite)
  144. for i in "${LOCAL_IMAGES[@]}"; do
  145. info_msg "[${_BBlue}${i}${_creset}] ${_BGreen}${LXC_REPO_ROOT}/utils/lxc.sh install $2${_creset}"
  146. lxc exec -t "${i}" -- "${LXC_REPO_ROOT}/utils/lxc.sh" __show "$2" | prefix_stdout "[${i}] "
  147. done
  148. ;;
  149. *) usage "$_usage"; exit 42;;
  150. esac
  151. ;;
  152. __show)
  153. case $2 in
  154. searx-suite) searx_suite_info ;;
  155. esac
  156. ;;
  157. cmd)
  158. sudo_or_exit
  159. shift
  160. for i in "${LOCAL_IMAGES[@]}"; do
  161. exit_val=
  162. info_msg "[${_BBlue}${i}${_creset}] ${_BGreen}${*}${_creset}"
  163. lxc exec "${i}" -- "$@"
  164. exit_val=$?
  165. if [[ $exit_val -ne 0 ]]; then
  166. warn_msg "[${_BBlue}${i}${_creset}] exit code (${_BRed}${exit_val}${_creset}) from ${_BGreen}${*}${_creset}"
  167. else
  168. info_msg "[${_BBlue}${i}${_creset}] exit code (${_BRed}${exit_val}${_creset}) from ${_BGreen}${*}${_creset}"
  169. fi
  170. done
  171. ;;
  172. install)
  173. sudo_or_exit
  174. case $2 in
  175. searx-suite)
  176. for i in "${LOCAL_IMAGES[@]}"; do
  177. info_msg "[${_BBlue}${i}${_creset}] ${_BGreen}${LXC_REPO_ROOT}/utils/lxc.sh install $2${_creset}"
  178. lxc exec -t "${i}" -- "${LXC_REPO_ROOT}/utils/lxc.sh" __install "$2" | prefix_stdout "[${i}] "
  179. done
  180. ;;
  181. *) usage "$_usage"; exit 42;;
  182. esac
  183. ;;
  184. __install)
  185. case $2 in
  186. searx-suite) searx_suite_install ;;
  187. esac
  188. ;;
  189. doc)
  190. echo
  191. echo ".. generic utils/lxc.sh documentation"
  192. ;;
  193. *)
  194. usage "unknown or missing command $1"; exit 42;;
  195. esac
  196. }
  197. searx_suite_install() {
  198. (
  199. searx_suite_set_env
  200. export FORCE_TIMEOUT=0
  201. "${LXC_REPO_ROOT}/utils/searx.sh" install all
  202. "${LXC_REPO_ROOT}/utils/morty.sh" install all
  203. "${LXC_REPO_ROOT}/utils/filtron.sh" install all
  204. rst_title "searx-suite installation finished ($(hostname))" part
  205. searx_suite_info
  206. echo
  207. )
  208. }
  209. searx_suite_info() {
  210. (
  211. searx_suite_set_env
  212. rst_para "Services of the container $(hostname)"
  213. for ip in $(hostname -I); do
  214. echo
  215. if [[ $ip =~ .*:.* ]]; then
  216. :
  217. # IPv6: not yet implemented / tested
  218. # echo " searx (filtron) --> http://[$ip]:4004/"
  219. # echo " morty --> http://[$ip]:3000/"
  220. else
  221. # IPv4:
  222. echo " searx (filtron) --> http://$ip:4004/"
  223. echo " morty --> http://$ip:3000/"
  224. fi
  225. done
  226. )
  227. }
  228. build_instances() {
  229. rst_title "Build LXC instances"
  230. rst_title "copy images" section
  231. echo
  232. lxc_copy_images_localy
  233. # lxc image list local: && wait_key
  234. echo
  235. rst_title "build containers" section
  236. echo
  237. lxc_init_containers
  238. lxc_config_containers
  239. lxc_boilerplate_containers
  240. echo
  241. lxc list "$HOST_PREFIX"
  242. }
  243. remove_instances() {
  244. rst_title "Remove LXC instances"
  245. lxc list "$HOST_PREFIX"
  246. echo -en "\\nLXC containers(s)::\\n\\n ${LOCAL_IMAGES[*]}\\n" | $FMT
  247. if ask_yn "Do you really want to delete all images"; then
  248. lxc_delete_containers
  249. fi
  250. echo
  251. lxc list "$HOST_PREFIX"
  252. # lxc image list local: && wait_key
  253. }
  254. # images
  255. # ------
  256. lxc_copy_images_localy() {
  257. for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do
  258. if lxc image info "local:${TEST_IMAGES[i+1]}" &>/dev/null; then
  259. info_msg "image ${TEST_IMAGES[i]} already copied --> ${TEST_IMAGES[i+1]}"
  260. else
  261. info_msg "copy image locally ${TEST_IMAGES[i]} --> ${TEST_IMAGES[i+1]}"
  262. lxc image copy "${TEST_IMAGES[i]}" local: \
  263. --alias "${TEST_IMAGES[i+1]}" | prefix_stdout
  264. fi
  265. done
  266. }
  267. lxc_delete_images_localy() {
  268. echo
  269. for i in "${LOCAL_IMAGES[@]}"; do
  270. info_msg "delete image 'local:$i'"
  271. lxc image delete "local:$i"
  272. done
  273. #lxc image list local:
  274. }
  275. # container
  276. # ---------
  277. lxc_cmd() {
  278. for i in "${LOCAL_IMAGES[@]}"; do
  279. info_msg "lxc $* $i"
  280. lxc "$@" "$i" | prefix_stdout "[${_BBlue}${i}${_creset}] "
  281. done
  282. }
  283. lxc_init_containers() {
  284. local image_name
  285. local container_name
  286. for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do
  287. image_name="${TEST_IMAGES[i+1]}"
  288. container_name="${HOST_PREFIX}-${image_name}"
  289. if lxc info "${container_name}" &>/dev/null; then
  290. info_msg "container '${container_name}' already exists"
  291. else
  292. info_msg "create conatiner instance: ${container_name}"
  293. lxc init "local:${image_name}" "${container_name}"
  294. fi
  295. done
  296. }
  297. lxc_config_containers() {
  298. for i in "${LOCAL_IMAGES[@]}"; do
  299. info_msg "[${_BBlue}${i}${_creset}] configure container ..."
  300. info_msg "[${_BBlue}${i}${_creset}] map uid/gid from host to container"
  301. # https://lxd.readthedocs.io/en/latest/userns-idmap/#custom-idmaps
  302. echo -e -n "uid $HOST_USER_ID 1000\\ngid $HOST_GROUP_ID 1000"\
  303. | lxc config set "$i" raw.idmap -
  304. info_msg "[${_BBlue}${i}${_creset}] share ${REPO_ROOT} (repo_share) from HOST into container"
  305. # https://lxd.readthedocs.io/en/latest/instances/#type-disk
  306. lxc config device add "$i" repo_share disk \
  307. source="${REPO_ROOT}" \
  308. path="${LXC_REPO_ROOT}" &>/dev/null
  309. # lxc config show "$i" && wait_key
  310. done
  311. }
  312. lxc_boilerplate_containers() {
  313. local image_name
  314. local container_name
  315. local boilerplate_script
  316. for ((i=0; i<${#TEST_IMAGES[@]}; i+=2)); do
  317. image_name="${TEST_IMAGES[i+1]}"
  318. container_name="${HOST_PREFIX}-${image_name}"
  319. boilerplate_script="${image_name}_boilerplate"
  320. boilerplate_script="${!boilerplate_script}"
  321. info_msg "[${_BBlue}${container_name}${_creset}] install boilerplate"
  322. if lxc start -q "${container_name}" &>/dev/null; then
  323. sleep 5 # guest needs some time to come up and get an IP
  324. fi
  325. if [[ -n "${boilerplate_script}" ]]; then
  326. echo "${boilerplate_script}" \
  327. | lxc exec "${container_name}" -- bash \
  328. | prefix_stdout "[${_BBlue}${container_name}${_creset}] "
  329. else
  330. err_msg "[${_BBlue}${container_name}${_creset}] no boilerplate for image '${image_name}'"
  331. fi
  332. done
  333. }
  334. lxc_delete_containers() {
  335. for i in "${LOCAL_IMAGES[@]}"; do
  336. if lxc info "$i" &>/dev/null; then
  337. info_msg "stop & delete instance ${_BBlue}${i}${_creset}"
  338. lxc stop "$i" &>/dev/null
  339. lxc delete "$i" | prefix_stdout
  340. else
  341. warn_msg "instance '$i' does not exist / can't delete :o"
  342. fi
  343. done
  344. }
  345. # subordinates
  346. # ------------
  347. #
  348. # see man: subgid(5), subuid(5), https://lxd.readthedocs.io/en/latest/userns-idmap
  349. #
  350. # E.g. in the HOST you have uid=1001(user) and/or gid=1001(user) ::
  351. #
  352. # root:1001:1
  353. #
  354. # in the CONTAINER::
  355. #
  356. # config:
  357. # raw.idmap: |
  358. # uid 1001 1000
  359. # gid 1001 1000
  360. add_subordinate_ids() {
  361. if grep "root:${HOST_USER_ID}:1" /etc/subuid -qs; then
  362. info_msg "lxd already has permission to map ${HOST_USER_ID}'s user/group id through"
  363. else
  364. info_msg "add lxd permission to map ${HOST_USER_ID}'s user/group id through"
  365. usermod --add-subuids "${HOST_USER_ID}-${HOST_USER_ID}" \
  366. --add-subgids "${HOST_GROUP_ID}-${HOST_GROUP_ID}" root
  367. fi
  368. }
  369. del_subordinate_ids() {
  370. local out
  371. local exit_val
  372. if grep "root:${HOST_USER_ID}:1" /etc/subuid -qs; then
  373. # TODO: root user is always in use by process 1, how can we remove subordinates?
  374. info_msg "remove lxd permission to map ${HOST_USER_ID}'s user/group id through"
  375. out=$(usermod --del-subuids "${HOST_USER_ID}-${HOST_USER_ID}" --del-subgids "${HOST_GROUP_ID}-${HOST_GROUP_ID}" root 2>&1)
  376. exit_val=$?
  377. if [ $exit_val -ne 0 ]; then
  378. err_msg "$out"
  379. fi
  380. else
  381. info_msg "lxd does not have permission to map ${HOST_USER_ID}'s user/group id through"
  382. fi
  383. }
  384. # ----------------------------------------------------------------------------
  385. main "$@"
  386. # ----------------------------------------------------------------------------