lxc.sh 13 KB

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