lxc.sh 12 KB

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