lxc.sh 18 KB

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