lxc.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. # load environment of the LXC suite
  8. LXC_ENV="${LXC_ENV:-${REPO_ROOT}/utils/lxc-searx.env}"
  9. source "$LXC_ENV"
  10. lxc_set_suite_env
  11. # ----------------------------------------------------------------------------
  12. # config
  13. # ----------------------------------------------------------------------------
  14. #
  15. # read also:
  16. # - https://lxd.readthedocs.io/en/latest/
  17. LXC_HOST_PREFIX="${LXC_HOST_PREFIX:-test}"
  18. # where all folders from HOST are mounted
  19. LXC_SHARE_FOLDER="/share"
  20. LXC_REPO_ROOT="${LXC_SHARE_FOLDER}/$(basename "${REPO_ROOT}")"
  21. ubu1604_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. "
  27. ubu1804_boilerplate="$ubu1604_boilerplate"
  28. ubu1904_boilerplate="$ubu1804_boilerplate"
  29. ubu1910_boilerplate="$ubu1904_boilerplate"
  30. # shellcheck disable=SC2034
  31. ubu2004_boilerplate="
  32. $ubu1910_boilerplate
  33. echo 'Set disable_coredump false' >> /etc/sudo.conf
  34. "
  35. # shellcheck disable=SC2034
  36. archlinux_boilerplate="
  37. pacman -Syu --noconfirm
  38. pacman -S --noconfirm git curl wget sudo
  39. echo 'Set disable_coredump false' >> /etc/sudo.conf
  40. "
  41. # shellcheck disable=SC2034
  42. fedora31_boilerplate="
  43. dnf update -y
  44. dnf install -y git curl wget hostname
  45. echo 'Set disable_coredump false' >> /etc/sudo.conf
  46. "
  47. REMOTE_IMAGES=()
  48. CONTAINERS=()
  49. LOCAL_IMAGES=()
  50. for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
  51. REMOTE_IMAGES=("${REMOTE_IMAGES[@]}" "${LXC_SUITE[i]}")
  52. CONTAINERS=("${CONTAINERS[@]}" "${LXC_HOST_PREFIX}-${LXC_SUITE[i+1]}")
  53. LOCAL_IMAGES=("${LOCAL_IMAGES[@]}" "${LXC_SUITE[i+1]}")
  54. done
  55. HOST_USER="${SUDO_USER:-$USER}"
  56. HOST_USER_ID=$(id -u "${HOST_USER}")
  57. HOST_GROUP_ID=$(id -g "${HOST_USER}")
  58. # ----------------------------------------------------------------------------
  59. usage() {
  60. # ----------------------------------------------------------------------------
  61. _cmd="$(basename "$0")"
  62. cat <<EOF
  63. usage::
  64. $_cmd build [containers]
  65. $_cmd copy [images]
  66. $_cmd remove [containers|<name>|images|subordinate]
  67. $_cmd add [subordinate]
  68. $_cmd [start|stop] [containers|<name>]
  69. $_cmd show [info|config|suite|images]
  70. $_cmd cmd [--|<name>] ...
  71. $_cmd install [suite]
  72. build
  73. :containers: build & launch all LXC containers of the suite
  74. copy:
  75. :images: copy remote images of the suite into local storage
  76. remove
  77. :containers: delete all 'containers' or only <container-name>
  78. :images: delete local images of the suite
  79. add / remove
  80. :subordinate: LXD permission to map ${HOST_USER}'s user/group id through
  81. start/stop
  82. :containers: start/stop all 'containers' from the suite
  83. :<name>: start/stop conatiner <name> from suite
  84. show
  85. :info: show info of all the containers from LXC suite
  86. :config: show config of all the containers from the LXC suite
  87. :suite: show services of all the containers from the LXC suite
  88. :images: show information of local images
  89. cmd
  90. -- run command ... in all containers of the LXC suite
  91. :<name>: run command ... in container <name>
  92. install
  93. :suite: install LXC suite, includes morty & filtron
  94. EOF
  95. usage_images
  96. echo
  97. usage_containers
  98. echo
  99. [ -n "${1+x}" ] && err_msg "$1"
  100. }
  101. usage_containers() {
  102. cat <<EOF
  103. LXC suite containers:
  104. $(echo " ${CONTAINERS[*]}" | $FMT)
  105. EOF
  106. [ -n "${1+x}" ] && err_msg "$1"
  107. }
  108. usage_images() {
  109. cat <<EOF
  110. LXC suite images:
  111. $(echo " ${LOCAL_IMAGES[*]}" | $FMT)
  112. EOF
  113. }
  114. lxd_info() {
  115. cat <<EOF
  116. LXD is needed, to install run::
  117. snap install lxd
  118. lxd init --auto
  119. EOF
  120. }
  121. main() {
  122. local exit_val
  123. local _usage="unknown or missing $1 command $2"
  124. # don't check prerequisite when in recursion
  125. if [[ ! $1 == __* ]]; then
  126. ! required_commands lxc && lxd_info && exit 42
  127. [[ -z $LXC_SUITE ]] && err_msg "missing LXC_SUITE" && exit 42
  128. fi
  129. case $1 in
  130. --source-only) ;;
  131. -h|--help) usage; exit 0;;
  132. build)
  133. sudo_or_exit
  134. case $2 in
  135. ''|containers) build_instances ;;
  136. *) usage "$_usage"; exit 42;;
  137. esac
  138. ;;
  139. copy)
  140. case $2 in
  141. ''|images) lxc_copy_images_localy;;
  142. *) usage "$_usage"; exit 42;;
  143. esac
  144. ;;
  145. remove)
  146. sudo_or_exit
  147. case $2 in
  148. ''|containers) remove_instances ;;
  149. images) lxc_delete_images_localy ;;
  150. subordinate) echo; del_subordinate_ids ;;
  151. ${LXC_HOST_PREFIX}-*)
  152. ! lxc_exists "$2" && usage_containers "unknown container: $2" && exit 42
  153. if ask_yn "Do you really want to delete conatiner $2"; then
  154. lxc_delete_container "$2"
  155. fi
  156. ;;
  157. *) usage "uknown or missing container <name> $2"; exit 42;;
  158. esac
  159. ;;
  160. add)
  161. sudo_or_exit
  162. case $2 in
  163. subordinate) echo; add_subordinate_ids ;;
  164. *) usage "$_usage"; exit 42;;
  165. esac
  166. ;;
  167. start|stop)
  168. sudo_or_exit
  169. case $2 in
  170. ''|containers) lxc_cmd "$1" ;;
  171. ${LXC_HOST_PREFIX}-*)
  172. ! lxc_exists "$2" && usage_containers "unknown container: $2" && exit 42
  173. info_msg "lxc $1 $2"
  174. lxc "$1" "$2" | prefix_stdout "[${_BBlue}${i}${_creset}] "
  175. ;;
  176. *) usage "uknown or missing container <name> $2"; exit 42;;
  177. esac
  178. ;;
  179. show)
  180. sudo_or_exit
  181. case $2 in
  182. suite) show_suite ;;
  183. images) show_images ;;
  184. config)
  185. rst_title "container configurations"
  186. echo
  187. lxc list "$LXC_HOST_PREFIX-"
  188. echo
  189. lxc_cmd config show
  190. ;;
  191. info)
  192. rst_title "container info"
  193. echo
  194. lxc_cmd info
  195. ;;
  196. *) usage "$_usage"; exit 42;;
  197. esac
  198. ;;
  199. __show)
  200. case $2 in
  201. suite) lxc_suite_info ;;
  202. esac
  203. ;;
  204. cmd)
  205. sudo_or_exit
  206. shift
  207. case $1 in
  208. --)
  209. shift
  210. for name in "${CONTAINERS[@]}"; do
  211. lxc_exec_cmd "${name}" "$@"
  212. done
  213. ;;
  214. ${LXC_HOST_PREFIX}-*)
  215. ! lxc_exists "$1" && usage_containers "unknown container: $1" && exit 42
  216. local name=$1
  217. shift
  218. lxc_exec_cmd "${name}" "$@"
  219. ;;
  220. *) usage "uknown or missing container <name> $2"; exit 42;;
  221. esac
  222. ;;
  223. install)
  224. sudo_or_exit
  225. case $2 in
  226. suite) install_suite ;;
  227. *) usage "$_usage"; exit 42 ;;
  228. esac
  229. ;;
  230. __install)
  231. case $2 in
  232. suite) lxc_suite_install ;;
  233. esac
  234. ;;
  235. doc)
  236. echo
  237. echo ".. generic utils/lxc.sh documentation"
  238. ;;
  239. -*) usage "unknown option $1"; exit 42;;
  240. *) usage "unknown or missing command $1"; exit 42;;
  241. esac
  242. }
  243. build_instances() {
  244. rst_title "Build LXC instances"
  245. echo
  246. add_subordinate_ids
  247. lxc_copy_images_localy
  248. echo
  249. rst_title "build containers" section
  250. echo
  251. lxc_init_containers
  252. lxc_config_containers
  253. lxc_boilerplate_containers
  254. echo
  255. lxc list "$LXC_HOST_PREFIX"
  256. }
  257. remove_instances() {
  258. rst_title "Remove LXC instances"
  259. rst_para "existing containers matching ${_BGreen}$LXC_HOST_PREFIX-*${_creset}"
  260. echo
  261. lxc list "$LXC_HOST_PREFIX-"
  262. echo -en "\\n${_BRed}LXC containers to delete::${_creset}\\n\\n ${CONTAINERS[*]}\\n" | $FMT
  263. if ask_yn "Do you really want to delete these conatiners"; then
  264. for i in "${CONTAINERS[@]}"; do
  265. lxc_delete_container "$i"
  266. done
  267. fi
  268. echo
  269. lxc list "$LXC_HOST_PREFIX-"
  270. }
  271. # images
  272. # ------
  273. lxc_copy_images_localy() {
  274. rst_title "copy images" section
  275. echo
  276. for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
  277. if lxc_image_exists "local:${LXC_SUITE[i+1]}"; then
  278. info_msg "image ${LXC_SUITE[i]} already copied --> ${LXC_SUITE[i+1]}"
  279. else
  280. info_msg "copy image locally ${LXC_SUITE[i]} --> ${LXC_SUITE[i+1]}"
  281. lxc image copy "${LXC_SUITE[i]}" local: \
  282. --alias "${LXC_SUITE[i+1]}" | prefix_stdout
  283. fi
  284. done
  285. # lxc image list local: && wait_key
  286. }
  287. lxc_delete_images_localy() {
  288. rst_title "Delete LXC images"
  289. rst_para "local existing images"
  290. echo
  291. lxc image list local:
  292. echo -en "\\n${_BRed}LXC images to delete::${_creset}\\n\\n ${LOCAL_IMAGES[*]}\\n"
  293. if ask_yn "Do you really want to delete these images"; then
  294. for i in "${LOCAL_IMAGES[@]}"; do
  295. lxc_delete_local_image "$i"
  296. done
  297. fi
  298. echo
  299. lxc image list local:
  300. }
  301. show_images(){
  302. rst_title "local images"
  303. echo
  304. lxc image list local:
  305. echo -en "\\n${_Green}LXC suite images::${_creset}\\n\\n ${LOCAL_IMAGES[*]}\\n"
  306. wait_key
  307. for i in "${LOCAL_IMAGES[@]}"; do
  308. if lxc_image_exists "$i"; then
  309. info_msg "lxc image info ${_BBlue}${i}${_creset}"
  310. lxc image info "$i" | prefix_stdout "[${_BBlue}${i}${_creset}] "
  311. else
  312. warn_msg "image ${_BBlue}$i${_creset} does not yet exists"
  313. fi
  314. done
  315. }
  316. # container
  317. # ---------
  318. show_suite(){
  319. rst_title "LXC suite ($LXC_HOST_PREFIX-*)"
  320. echo
  321. lxc list "$LXC_HOST_PREFIX-"
  322. echo
  323. for i in "${CONTAINERS[@]}"; do
  324. if ! lxc_exists "$i"; then
  325. warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
  326. else
  327. lxc exec -t "${i}" -- "${LXC_REPO_ROOT}/utils/lxc.sh" __show suite \
  328. | prefix_stdout "[${_BBlue}${i}${_creset}] "
  329. echo
  330. fi
  331. done
  332. }
  333. install_suite() {
  334. for i in "${CONTAINERS[@]}"; do
  335. if ! lxc_exists "$i"; then
  336. warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
  337. else
  338. info_msg "[${_BBlue}${i}${_creset}] ${_BGreen}${LXC_REPO_ROOT}/utils/lxc.sh install suite${_creset}"
  339. lxc exec -t "${i}" -- "${LXC_REPO_ROOT}/utils/lxc.sh" __install suite \
  340. | prefix_stdout "[${_BBlue}${i}${_creset}] "
  341. fi
  342. done
  343. }
  344. lxc_cmd() {
  345. for i in "${CONTAINERS[@]}"; do
  346. if ! lxc_exists "$i"; then
  347. warn_msg "container ${_BBlue}$i${_creset} does not yet exists"
  348. else
  349. info_msg "lxc $* $i"
  350. lxc "$@" "$i" | prefix_stdout "[${_BBlue}${i}${_creset}] "
  351. echo
  352. fi
  353. done
  354. }
  355. lxc_exec_cmd() {
  356. local name="$1"
  357. shift
  358. exit_val=
  359. info_msg "[${_BBlue}${name}${_creset}] ${_BGreen}${*}${_creset}"
  360. lxc exec --cwd "${LXC_REPO_ROOT}" "${name}" -- "$@"
  361. exit_val=$?
  362. if [[ $exit_val -ne 0 ]]; then
  363. warn_msg "[${_BBlue}${name}${_creset}] exit code (${_BRed}${exit_val}${_creset}) from ${_BGreen}${*}${_creset}"
  364. else
  365. info_msg "[${_BBlue}${name}${_creset}] exit code (${exit_val}) from ${_BGreen}${*}${_creset}"
  366. fi
  367. echo
  368. }
  369. lxc_init_containers() {
  370. local image_name
  371. local container_name
  372. for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
  373. image_name="${LXC_SUITE[i+1]}"
  374. container_name="${LXC_HOST_PREFIX}-${image_name}"
  375. if lxc info "${container_name}" &>/dev/null; then
  376. info_msg "container '${container_name}' already exists"
  377. else
  378. info_msg "create conatiner instance: ${container_name}"
  379. lxc init "local:${image_name}" "${container_name}"
  380. fi
  381. done
  382. }
  383. lxc_config_containers() {
  384. for i in "${CONTAINERS[@]}"; do
  385. info_msg "[${_BBlue}${i}${_creset}] configure container ..."
  386. info_msg "[${_BBlue}${i}${_creset}] map uid/gid from host to container"
  387. # https://lxd.readthedocs.io/en/latest/userns-idmap/#custom-idmaps
  388. echo -e -n "uid $HOST_USER_ID 1000\\ngid $HOST_GROUP_ID 1000"\
  389. | lxc config set "$i" raw.idmap -
  390. info_msg "[${_BBlue}${i}${_creset}] share ${REPO_ROOT} (repo_share) from HOST into container"
  391. # https://lxd.readthedocs.io/en/latest/instances/#type-disk
  392. lxc config device add "$i" repo_share disk \
  393. source="${REPO_ROOT}" \
  394. path="${LXC_REPO_ROOT}" &>/dev/null
  395. # lxc config show "$i" && wait_key
  396. done
  397. }
  398. lxc_boilerplate_containers() {
  399. local image_name
  400. local container_name
  401. local boilerplate_script
  402. for ((i=0; i<${#LXC_SUITE[@]}; i+=2)); do
  403. image_name="${LXC_SUITE[i+1]}"
  404. container_name="${LXC_HOST_PREFIX}-${image_name}"
  405. boilerplate_script="${image_name}_boilerplate"
  406. boilerplate_script="${!boilerplate_script}"
  407. info_msg "[${_BBlue}${container_name}${_creset}] install boilerplate"
  408. if lxc start -q "${container_name}" &>/dev/null; then
  409. sleep 5 # guest needs some time to come up and get an IP
  410. fi
  411. if [[ -n "${boilerplate_script}" ]]; then
  412. echo "${boilerplate_script}" \
  413. | lxc exec "${container_name}" -- bash \
  414. | prefix_stdout "[${_BBlue}${container_name}${_creset}] "
  415. else
  416. err_msg "[${_BBlue}${container_name}${_creset}] no boilerplate for image '${image_name}'"
  417. fi
  418. done
  419. }
  420. # subordinates
  421. # ------------
  422. #
  423. # see man: subgid(5), subuid(5), https://lxd.readthedocs.io/en/latest/userns-idmap
  424. #
  425. # E.g. in the HOST you have uid=1001(user) and/or gid=1001(user) ::
  426. #
  427. # root:1001:1
  428. #
  429. # in the CONTAINER::
  430. #
  431. # config:
  432. # raw.idmap: |
  433. # uid 1001 1000
  434. # gid 1001 1000
  435. add_subordinate_ids() {
  436. if grep "root:${HOST_USER_ID}:1" /etc/subuid -qs; then
  437. info_msg "lxd already has permission to map ${HOST_USER_ID}'s user/group id through"
  438. else
  439. info_msg "add lxd permission to map ${HOST_USER_ID}'s user/group id through"
  440. usermod --add-subuids "${HOST_USER_ID}-${HOST_USER_ID}" \
  441. --add-subgids "${HOST_GROUP_ID}-${HOST_GROUP_ID}" root
  442. fi
  443. }
  444. del_subordinate_ids() {
  445. local out
  446. local exit_val
  447. if grep "root:${HOST_USER_ID}:1" /etc/subuid -qs; then
  448. # TODO: root user is always in use by process 1, how can we remove subordinates?
  449. info_msg "remove lxd permission to map ${HOST_USER_ID}'s user/group id through"
  450. out=$(usermod --del-subuids "${HOST_USER_ID}-${HOST_USER_ID}" --del-subgids "${HOST_GROUP_ID}-${HOST_GROUP_ID}" root 2>&1)
  451. exit_val=$?
  452. if [ $exit_val -ne 0 ]; then
  453. err_msg "$out"
  454. fi
  455. else
  456. info_msg "lxd does not have permission to map ${HOST_USER_ID}'s user/group id through"
  457. fi
  458. }
  459. # ----------------------------------------------------------------------------
  460. main "$@"
  461. # ----------------------------------------------------------------------------