lxc.sh 18 KB

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