lxc.sh 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. # shellcheck source=utils/brand.env
  8. source "${REPO_ROOT}/utils/brand.env"
  9. # load environment of the LXC suite
  10. LXC_ENV="${LXC_ENV:-${REPO_ROOT}/utils/lxc-searx.env}"
  11. source "$LXC_ENV"
  12. lxc_set_suite_env
  13. # ----------------------------------------------------------------------------
  14. # config
  15. # ----------------------------------------------------------------------------
  16. #
  17. # read also:
  18. # - https://lxd.readthedocs.io/en/latest/
  19. LXC_HOST_PREFIX="${LXC_HOST_PREFIX:-test}"
  20. # Location in the container where all folders from HOST are mounted
  21. LXC_SHARE_FOLDER="/share"
  22. LXC_REPO_ROOT="${LXC_SHARE_FOLDER}/$(basename "${REPO_ROOT}")"
  23. ubu1804_boilerplate="
  24. export DEBIAN_FRONTEND=noninteractive
  25. apt-get update -y
  26. apt-get upgrade -y
  27. apt-get install -y git curl wget
  28. "
  29. ubu1904_boilerplate="$ubu1804_boilerplate"
  30. # shellcheck disable=SC2034
  31. ubu2004_boilerplate="
  32. $ubu1904_boilerplate
  33. echo 'Set disable_coredump false' >> /etc/sudo.conf
  34. "
  35. # shellcheck disable=SC2034
  36. ubu2110_boilerplate="$ubu1904_boilerplate"
  37. # shellcheck disable=SC2034
  38. archlinux_boilerplate="
  39. pacman --noprogressbar -Syu --noconfirm
  40. pacman --noprogressbar -S --noconfirm inetutils git curl wget sudo
  41. echo 'Set disable_coredump false' >> /etc/sudo.conf
  42. "
  43. # shellcheck disable=SC2034
  44. fedora35_boilerplate="
  45. dnf update -y
  46. dnf install -y git curl wget hostname
  47. echo 'Set disable_coredump false' >> /etc/sudo.conf
  48. "
  49. # shellcheck disable=SC2034
  50. centos7_boilerplate="
  51. yum update -y
  52. yum install -y git curl wget hostname sudo which
  53. echo 'Set disable_coredump false' >> /etc/sudo.conf
  54. "
  55. REMOTE_IMAGES=()
  56. CONTAINERS=()
  57. LOCAL_IMAGES=()
  58. for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
  59. REMOTE_IMAGES=("${REMOTE_IMAGES[@]}" "${LXC_SUITE[i]}")
  60. CONTAINERS=("${CONTAINERS[@]}" "${LXC_HOST_PREFIX}-${LXC_SUITE[i+1]}")
  61. LOCAL_IMAGES=("${LOCAL_IMAGES[@]}" "${LXC_SUITE[i+1]}")
  62. done
  63. HOST_USER="${SUDO_USER:-$USER}"
  64. HOST_USER_ID=$(id -u "${HOST_USER}")
  65. HOST_GROUP_ID=$(id -g "${HOST_USER}")
  66. # ----------------------------------------------------------------------------
  67. usage() {
  68. # ----------------------------------------------------------------------------
  69. _cmd="$(basename "$0")"
  70. cat <<EOF
  71. usage::
  72. $_cmd build [containers|<name>]
  73. $_cmd copy [images]
  74. $_cmd remove [containers|<name>|images]
  75. $_cmd [start|stop] [containers|<name>]
  76. $_cmd show [images|suite|info|config [<name>]]
  77. $_cmd cmd [--|<name>] '...'
  78. $_cmd install [suite|base [<name>]]
  79. build
  80. :containers: build, launch all containers and 'install base' packages
  81. :<name>: build, launch container <name> and 'install base' packages
  82. copy:
  83. :images: copy remote images of the suite into local storage
  84. remove
  85. :containers: delete all 'containers' or only <container-name>
  86. :images: delete local images of the suite
  87. start/stop
  88. :containers: start/stop all 'containers' from the suite
  89. :<name>: start/stop container <name> from suite
  90. show
  91. :info: show info of all (or <name>) containers from LXC suite
  92. :config: show config of all (or <name>) containers from the LXC suite
  93. :suite: show services of all (or <name>) containers from the LXC suite
  94. :images: show information of local images
  95. cmd
  96. use single qoutes to evaluate in container's bash, e.g.: 'echo \$(hostname)'
  97. -- run command '...' in all containers of the LXC suite
  98. :<name>: run command '...' in container <name>
  99. install
  100. :base: prepare LXC; install basic packages
  101. :suite: install LXC ${LXC_SUITE_NAME} suite into all (or <name>) containers
  102. EOF
  103. usage_containers
  104. [ -n "${1+x}" ] && err_msg "$1"
  105. }
  106. usage_containers() {
  107. lxc_suite_install_info
  108. [ -n "${1+x}" ] && err_msg "$1"
  109. }
  110. lxd_info() {
  111. cat <<EOF
  112. LXD is needed, to install run::
  113. snap install lxd
  114. lxd init --auto
  115. EOF
  116. }
  117. main() {
  118. local exit_val
  119. local _usage="unknown or missing $1 command $2"
  120. # don't check prerequisite when in recursion
  121. if [[ ! $1 == __* ]] && [[ ! $1 == --help ]]; then
  122. if ! in_container; then
  123. ! required_commands lxc && lxd_info && exit 42
  124. fi
  125. [[ -z $LXC_SUITE ]] && err_msg "missing LXC_SUITE" && exit 42
  126. fi
  127. case $1 in
  128. --getenv) var="$2"; echo "${!var}"; exit 0;;
  129. -h|--help) usage; exit 0;;
  130. build)
  131. sudo_or_exit
  132. case $2 in
  133. ${LXC_HOST_PREFIX}-*) build_container "$2" ;;
  134. ''|--|containers) build_all_containers ;;
  135. *) usage "$_usage"; exit 42;;
  136. esac
  137. ;;
  138. copy)
  139. case $2 in
  140. ''|images) lxc_copy_images_localy;;
  141. *) usage "$_usage"; exit 42;;
  142. esac
  143. ;;
  144. remove)
  145. sudo_or_exit
  146. case $2 in
  147. ''|--|containers) remove_containers ;;
  148. images) lxc_delete_images_localy ;;
  149. ${LXC_HOST_PREFIX}-*)
  150. ! lxc_exists "$2" && warn_msg "container not yet exists: $2" && exit 0
  151. if ask_yn "Do you really want to delete container $2"; then
  152. lxc_delete_container "$2"
  153. fi
  154. ;;
  155. *) usage "uknown or missing container <name> $2"; exit 42;;
  156. esac
  157. ;;
  158. start|stop)
  159. sudo_or_exit
  160. case $2 in
  161. ''|--|containers) lxc_cmd "$1" ;;
  162. ${LXC_HOST_PREFIX}-*)
  163. ! lxc_exists "$2" && usage_containers "unknown container: $2" && exit 42
  164. info_msg "lxc $1 $2"
  165. lxc "$1" "$2" | prefix_stdout "[${_BBlue}${i}${_creset}] "
  166. ;;
  167. *) usage "uknown or missing container <name> $2"; exit 42;;
  168. esac
  169. ;;
  170. show)
  171. sudo_or_exit
  172. case $2 in
  173. suite)
  174. case $3 in
  175. ${LXC_HOST_PREFIX}-*)
  176. lxc exec -t "$3" -- "${LXC_REPO_ROOT}/utils/lxc.sh" __show suite \
  177. | prefix_stdout "[${_BBlue}$3${_creset}] "
  178. ;;
  179. *) show_suite;;
  180. esac
  181. ;;
  182. images) show_images ;;
  183. config)
  184. case $3 in
  185. ${LXC_HOST_PREFIX}-*)
  186. ! lxc_exists "$3" && usage_containers "unknown container: $3" && exit 42
  187. lxc config show "$3" | prefix_stdout "[${_BBlue}${3}${_creset}] "
  188. ;;
  189. *)
  190. rst_title "container configurations"
  191. echo
  192. lxc list "$LXC_HOST_PREFIX-"
  193. echo
  194. lxc_cmd config show
  195. ;;
  196. esac
  197. ;;
  198. info)
  199. case $3 in
  200. ${LXC_HOST_PREFIX}-*)
  201. ! lxc_exists "$3" && usage_containers "unknown container: $3" && exit 42
  202. lxc info "$3" | prefix_stdout "[${_BBlue}${3}${_creset}] "
  203. ;;
  204. *)
  205. rst_title "container info"
  206. echo
  207. lxc_cmd info
  208. ;;
  209. esac
  210. ;;
  211. *) usage "$_usage"; exit 42;;
  212. esac
  213. ;;
  214. __show)
  215. # wrapped show commands, called once in each container
  216. case $2 in
  217. suite) lxc_suite_info ;;
  218. esac
  219. ;;
  220. cmd)
  221. sudo_or_exit
  222. shift
  223. case $1 in
  224. --) shift; lxc_exec "$@" ;;
  225. ${LXC_HOST_PREFIX}-*)
  226. ! lxc_exists "$1" && usage_containers "unknown container: $1" && exit 42
  227. local name=$1
  228. shift
  229. lxc_exec_cmd "${name}" "$@"
  230. ;;
  231. *) usage_containers "unknown container: $1" && exit 42
  232. esac
  233. ;;
  234. install)
  235. sudo_or_exit
  236. case $2 in
  237. suite|base)
  238. case $3 in
  239. ${LXC_HOST_PREFIX}-*)
  240. ! lxc_exists "$3" && usage_containers "unknown container: $3" && exit 42
  241. lxc_exec_cmd "$3" "${LXC_REPO_ROOT}/utils/lxc.sh" __install "$2"
  242. ;;
  243. ''|--) lxc_exec "${LXC_REPO_ROOT}/utils/lxc.sh" __install "$2" ;;
  244. *) usage_containers "unknown container: $3" && exit 42
  245. esac
  246. ;;
  247. *) usage "$_usage"; exit 42 ;;
  248. esac
  249. ;;
  250. __install)
  251. # wrapped install commands, called once in each container
  252. # shellcheck disable=SC2119
  253. case $2 in
  254. suite) lxc_suite_install ;;
  255. base) FORCE_TIMEOUT=0 lxc_install_base_packages ;;
  256. esac
  257. ;;
  258. doc)
  259. echo
  260. echo ".. generic utils/lxc.sh documentation"
  261. ;;
  262. -*) usage "unknown option $1"; exit 42;;
  263. *) usage "unknown or missing command $1"; exit 42;;
  264. esac
  265. }
  266. build_all_containers() {
  267. rst_title "Build all LXC containers of suite"
  268. echo
  269. usage_containers
  270. lxc_copy_images_localy
  271. lxc_init_all_containers
  272. lxc_config_all_containers
  273. lxc_boilerplate_all_containers
  274. rst_title "install LXC base packages" section
  275. echo
  276. lxc_exec "${LXC_REPO_ROOT}/utils/lxc.sh" __install base
  277. echo
  278. lxc list "$LXC_HOST_PREFIX"
  279. }
  280. build_container() {
  281. rst_title "Build container $1"
  282. local remote_image
  283. local container
  284. local image
  285. local boilerplate_script
  286. for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
  287. if [ "${LXC_HOST_PREFIX}-${LXC_SUITE[i+1]}" = "$1" ]; then
  288. remote_image="${LXC_SUITE[i]}"
  289. container="${LXC_HOST_PREFIX}-${LXC_SUITE[i+1]}"
  290. image="${LXC_SUITE[i+1]}"
  291. boilerplate_script="${image}_boilerplate"
  292. boilerplate_script="${!boilerplate_script}"
  293. break
  294. fi
  295. done
  296. echo
  297. if [ -z "$container" ]; then
  298. err_msg "container $1 unknown"
  299. usage_containers
  300. return 42
  301. fi
  302. lxc_image_copy "${remote_image}" "${image}"
  303. rst_title "init container" section
  304. lxc_init_container "${image}" "${container}"
  305. rst_title "configure container" section
  306. lxc_config_container "${container}"
  307. rst_title "run LXC boilerplate scripts" section
  308. lxc_install_boilerplate "${container}" "$boilerplate_script"
  309. echo
  310. rst_title "install LXC base packages" section
  311. lxc_exec_cmd "${container}" "${LXC_REPO_ROOT}/utils/lxc.sh" __install base \
  312. | prefix_stdout "[${_BBlue}${container}${_creset}] "
  313. echo
  314. lxc list "$container"
  315. }
  316. remove_containers() {
  317. rst_title "Remove all LXC containers of suite"
  318. rst_para "existing containers matching ${_BGreen}$LXC_HOST_PREFIX-*${_creset}"
  319. echo
  320. lxc list "$LXC_HOST_PREFIX-"
  321. echo -en "\\n${_BRed}LXC containers to delete::${_creset}\\n\\n ${CONTAINERS[*]}\\n" | $FMT
  322. local default=Ny
  323. [[ $FORCE_TIMEOUT = 0 ]] && default=Yn
  324. if ask_yn "Do you really want to delete these containers" $default; then
  325. for i in "${CONTAINERS[@]}"; do
  326. lxc_delete_container "$i"
  327. done
  328. fi
  329. echo
  330. lxc list "$LXC_HOST_PREFIX-"
  331. }
  332. # images
  333. # ------
  334. lxc_copy_images_localy() {
  335. rst_title "copy images" section
  336. for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
  337. lxc_image_copy "${LXC_SUITE[i]}" "${LXC_SUITE[i+1]}"
  338. done
  339. # lxc image list local: && wait_key
  340. }
  341. lxc_delete_images_localy() {
  342. rst_title "Delete LXC images"
  343. rst_para "local existing images"
  344. echo
  345. lxc image list local:
  346. echo -en "\\n${_BRed}LXC images to delete::${_creset}\\n\\n ${LOCAL_IMAGES[*]}\\n"
  347. if ask_yn "Do you really want to delete these images"; then
  348. for i in "${LOCAL_IMAGES[@]}"; do
  349. lxc_delete_local_image "$i"
  350. done
  351. fi
  352. for i in $(lxc image list --format csv | grep '^,' | sed 's/,\([^,]*\).*$/\1/'); do
  353. if ask_yn "Image $i has no alias, do you want to delete the image?" Yn; then
  354. lxc_delete_local_image "$i"
  355. fi
  356. done
  357. echo
  358. lxc image list local:
  359. }
  360. show_images(){
  361. rst_title "local images"
  362. echo
  363. lxc image list local:
  364. echo -en "\\n${_Green}LXC suite images::${_creset}\\n\\n ${LOCAL_IMAGES[*]}\\n"
  365. wait_key
  366. for i in "${LOCAL_IMAGES[@]}"; do
  367. if lxc_image_exists "$i"; then
  368. info_msg "lxc image info ${_BBlue}${i}${_creset}"
  369. lxc image info "$i" | prefix_stdout "[${_BBlue}${i}${_creset}] "
  370. else
  371. warn_msg "image ${_BBlue}$i${_creset} does not yet exists"
  372. fi
  373. done
  374. }
  375. # container
  376. # ---------
  377. show_suite(){
  378. rst_title "LXC suite ($LXC_HOST_PREFIX-*)"
  379. echo
  380. lxc list "$LXC_HOST_PREFIX-"
  381. echo
  382. for i in "${CONTAINERS[@]}"; do
  383. if ! lxc_exists "$i"; then
  384. warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
  385. else
  386. lxc exec -t "${i}" -- "${LXC_REPO_ROOT}/utils/lxc.sh" __show suite \
  387. | prefix_stdout "[${_BBlue}${i}${_creset}] "
  388. echo
  389. fi
  390. done
  391. }
  392. lxc_cmd() {
  393. for i in "${CONTAINERS[@]}"; do
  394. if ! lxc_exists "$i"; then
  395. warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
  396. else
  397. info_msg "lxc $* $i"
  398. lxc "$@" "$i" | prefix_stdout "[${_BBlue}${i}${_creset}] "
  399. fi
  400. done
  401. }
  402. lxc_exec_cmd() {
  403. local name="$1"
  404. shift
  405. exit_val=
  406. info_msg "[${_BBlue}${name}${_creset}] ${_BGreen}${*}${_creset}"
  407. lxc exec -t --cwd "${LXC_REPO_ROOT}" "${name}" -- bash -c "$*"
  408. exit_val=$?
  409. if [[ $exit_val -ne 0 ]]; then
  410. warn_msg "[${_BBlue}${name}${_creset}] exit code (${_BRed}${exit_val}${_creset}) from ${_BGreen}${*}${_creset}"
  411. else
  412. info_msg "[${_BBlue}${name}${_creset}] exit code (${exit_val}) from ${_BGreen}${*}${_creset}"
  413. fi
  414. }
  415. lxc_exec() {
  416. for i in "${CONTAINERS[@]}"; do
  417. if ! lxc_exists "$i"; then
  418. warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
  419. else
  420. lxc_exec_cmd "${i}" "$@" | prefix_stdout "[${_BBlue}${i}${_creset}] "
  421. fi
  422. done
  423. }
  424. lxc_init_all_containers() {
  425. rst_title "init all containers" section
  426. local image_name
  427. local container_name
  428. for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
  429. lxc_init_container "${LXC_SUITE[i+1]}" "${LXC_HOST_PREFIX}-${LXC_SUITE[i+1]}"
  430. done
  431. }
  432. lxc_config_all_containers() {
  433. rst_title "configure all containers" section
  434. for i in "${CONTAINERS[@]}"; do
  435. lxc_config_container "${i}"
  436. done
  437. }
  438. lxc_config_container() {
  439. info_msg "[${_BBlue}$1${_creset}] configure container ..."
  440. info_msg "[${_BBlue}$1${_creset}] map uid/gid from host to container"
  441. # https://lxd.readthedocs.io/en/latest/userns-idmap/#custom-idmaps
  442. echo -e -n "uid $HOST_USER_ID 0\\ngid $HOST_GROUP_ID 0"\
  443. | lxc config set "$1" raw.idmap -
  444. info_msg "[${_BBlue}$1${_creset}] share ${REPO_ROOT} (repo_share) from HOST into container"
  445. # https://lxd.readthedocs.io/en/latest/instances/#type-disk
  446. lxc config device add "$1" repo_share disk \
  447. source="${REPO_ROOT}" \
  448. path="${LXC_REPO_ROOT}" &>/dev/null
  449. # lxc config show "$1" && wait_key
  450. }
  451. lxc_boilerplate_all_containers() {
  452. rst_title "run LXC boilerplate scripts" section
  453. local boilerplate_script
  454. local image_name
  455. for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
  456. image_name="${LXC_SUITE[i+1]}"
  457. boilerplate_script="${image_name}_boilerplate"
  458. boilerplate_script="${!boilerplate_script}"
  459. lxc_install_boilerplate "${LXC_HOST_PREFIX}-${image_name}" "$boilerplate_script"
  460. if [[ -z "${boilerplate_script}" ]]; then
  461. err_msg "[${_BBlue}${container_name}${_creset}] no boilerplate for image '${image_name}'"
  462. fi
  463. done
  464. }
  465. lxc_install_boilerplate() {
  466. # usage: lxc_install_boilerplate <container-name> <string: shell commands ..>
  467. #
  468. # usage: lxc_install_boilerplate searx-archlinux "${archlinux_boilerplate}"
  469. local container_name="$1"
  470. local boilerplate_script="$2"
  471. info_msg "[${_BBlue}${container_name}${_creset}] init .."
  472. if lxc start -q "${container_name}" &>/dev/null; then
  473. sleep 5 # guest needs some time to come up and get an IP
  474. fi
  475. if ! check_connectivity "${container_name}"; then
  476. die 42 "Container ${container_name} has no internet connectivity!"
  477. fi
  478. lxc_init_container_env "${container_name}"
  479. info_msg "[${_BBlue}${container_name}${_creset}] install /.lxcenv.mk .."
  480. cat <<EOF | lxc exec "${container_name}" -- bash | prefix_stdout "[${_BBlue}${container_name}${_creset}] "
  481. rm -f "/.lxcenv.mk"
  482. ln -s "${LXC_REPO_ROOT}/utils/makefile.lxc" "/.lxcenv.mk"
  483. ls -l "/.lxcenv.mk"
  484. EOF
  485. info_msg "[${_BBlue}${container_name}${_creset}] run LXC boilerplate scripts .."
  486. if lxc start -q "${container_name}" &>/dev/null; then
  487. sleep 5 # guest needs some time to come up and get an IP
  488. fi
  489. if [[ -n "${boilerplate_script}" ]]; then
  490. echo "${boilerplate_script}" \
  491. | lxc exec "${container_name}" -- bash \
  492. | prefix_stdout "[${_BBlue}${container_name}${_creset}] "
  493. fi
  494. }
  495. check_connectivity() {
  496. local ret_val=0
  497. info_msg "check internet connectivity ..."
  498. if ! lxc exec "${1}" -- ping -c 1 8.8.8.8 &>/dev/null; then
  499. ret_val=1
  500. err_msg "no internet connectivity!"
  501. info_msg "Most often the connectivity is blocked by a docker installation:"
  502. info_msg "Whenever docker is started (reboot) it sets the iptables policy "
  503. info_msg "for the FORWARD chain to DROP, see:"
  504. info_msg " https://docs.searxng.org/utils/lxc.sh.html#internet-connectivity-docker"
  505. iptables-save | grep ":FORWARD"
  506. fi
  507. return $ret_val
  508. }
  509. # ----------------------------------------------------------------------------
  510. main "$@"
  511. # ----------------------------------------------------------------------------