lxd init noninteractive

This commit is contained in:
pvincent
2024-02-26 00:27:21 +04:00
parent fe0522e56c
commit c874f2c180
+81 -81
View File
@@ -32,7 +32,7 @@ function prepare_lxd {
echo "define lxd and assign to user <$USER>" echo "define lxd and assign to user <$USER>"
sudo groupadd --force "$NEW_GROUP" sudo groupadd --force "$NEW_GROUP"
sudo usermod --append --groups "$NEW_GROUP" "$(whoami)" sudo usermod --append --groups "$NEW_GROUP" "$(whoami)"
exec sg "$NEW_GROUP" "exec '$0' $(printf "'%s' " SESSION_RELOAD_REQUIRED "$@")" exec sg "$NEW_GROUP" exec "$0 $TARGET SESSION_RELOAD_REQUIRED $@"
# no further processing because exec has been called! # no further processing because exec has been called!
else else
echo "user <$USER> already belongs to group <lxd>!" echo "user <$USER> already belongs to group <lxd>!"
@@ -40,75 +40,65 @@ function prepare_lxd {
sudo /opt/miaou-bash/tools/idem_apt_install lxd btrfs-progs sudo /opt/miaou-bash/tools/idem_apt_install lxd btrfs-progs
override_lxd_service
# test lxdbr0 # test lxdbr0
if ! lxc network info lxdbr0 &>/dev/null; then if ! lxc network info lxdbr0 &>/dev/null; then
echo "bridge <lxdbr0> down, so initialization will use default preseed..." echo "bridge <lxdbr0> down, so initialization will use default preseed..."
sudo lxd init
# cat <<EOF | sudo lxd init --preseed
# NEW empty_block_partition=''
# networks: if lsblk -o NAME,FSTYPE,GROUP --noheadings -p | grep -E ^.─ | grep disk; then
# - config: # FIXME: when nesting no DISK type partition found!
# ipv4.address: auto empty_block_partition=$(lsblk -o NAME,FSTYPE,GROUP --noheadings -p | grep -E ^.─ | grep disk | awk '{if($3=="") print $1}' | cut -d'/' -f3)
# ipv6.address: none fi
# description: "" if [[ -n "$empty_block_partition" ]]; then
# name: lxdbr0 echo "use empty block partition /dev/$empty_block_partition"
# type: "" cat <<EOF | sudo lxd init --preseed
# project: default config: {}
# storage_pools: networks:
# - config: - config:
# source: /dev/sda4 ipv4.address: auto
# description: "" ipv6.address: none
# name: default description: ""
# driver: btrfs name: lxdbr0
# profiles: type: ""
# - config: {} project: default
# description: "" storage_pools:
# devices: - config:
# eth0: source: /dev/$empty_block_partition
# name: eth0 description: ""
# network: lxdbr0 name: default
# type: nic driver: btrfs
# root: profiles:
# path: / - config: {}
# pool: default description: ""
# type: disk devices:
# name: default eth0:
# projects: [] name: eth0
# cluster: null network: lxdbr0
type: nic
# OLD root:
# networks: path: /
# - config: pool: default
# ipv4.address: auto type: disk
# ipv6.address: none name: default
# description: "" projects: []
# name: lxdbr0 cluster: null
# type: "" EOF
# project: default echo OK
# storage_pools: else
# - config: echo "use dir partition"
# source: /dev/sda4 cat <<EOF | lxd init --preseed
# description: "" config: {}
# name: default networks:
# driver: btrfs - name: lxdbr0
# profiles: type: bridge
# - config: {} config:
# description: "" ipv4.address: auto
# devices: ipv6.address: none
# eth0: EOF
# name: eth0 fi
# network: lxdbr0
# type: nic
# root:
# path: /
# pool: default
# type: disk
# name: default
# projects: []
# cluster: null
echo OK
else else
echo "bridge <lxdbr0> found implies it has been already initialized!" echo "bridge <lxdbr0> found implies it has been already initialized!"
fi fi
@@ -118,7 +108,8 @@ function prepare_lxd {
set_alias 'll' 'list -c ns4mDN' set_alias 'll' 'list -c ns4mDN'
# test environment container hostname # test environment container hostname
local env_container_hostname=$(lxc profile get default environment.container_hostname) local env_container_hostname
env_container_hostname=$(lxc profile get default environment.container_hostname)
if [[ -z "$env_container_hostname" ]]; then if [[ -z "$env_container_hostname" ]]; then
env_container_hostname=$(hostname -s) env_container_hostname=$(hostname -s)
if env | grep -q container_hostname; then if env | grep -q container_hostname; then
@@ -137,12 +128,6 @@ function prepare_lxd {
echo -n "subuid, subgid allowing <$(whoami)> ..." echo -n "subuid, subgid allowing <$(whoami)> ..."
printf "root:$(id -u):1\n" | sudo tee -a /etc/subuid /etc/subgid printf "root:$(id -u):1\n" | sudo tee -a /etc/subuid /etc/subgid
PREFIX="" echoinfo DONE PREFIX="" echoinfo DONE
# root:1000:1
# root:100000:65536
# _lxd:100000:65536
# <USER>:100000:65536
else else
echo "subuid, subgid allowing <$(whoami)> already done!" echo "subuid, subgid allowing <$(whoami)> already done!"
fi fi
@@ -156,8 +141,8 @@ function prepare_lxd {
fi fi
if [[ ! -d "$HOME/LXD/BACKUP" ]]; then if [[ ! -d "$HOME/LXD/BACKUP" ]]; then
echo -n "$HOME/LXD/SHARED creating ... " echo -n "$HOME/LXD/BACKUP creating ... "
mkdir "$HOME/LXD/SHARED" -p mkdir "$HOME/LXD/BACKUP" -p
PREFIX="" echoinfo DONE PREFIX="" echoinfo DONE
else else
echo "folder <$HOME/LXD/BACKUP> already created!" echo "folder <$HOME/LXD/BACKUP> already created!"
@@ -169,11 +154,11 @@ function set_alias {
local name="$1" local name="$1"
local command="$2" local command="$2"
if ! lxc alias list -f csv | grep -q "^$name,"; then if ! lxc alias list -f csv | grep -q "^$name,"; then
echo -n "define lxc alias $name ..." echo -n "defining new lxc alias <$name> ..."
lxc alias add "$name" "$command" lxc alias add "$name" "$command"
PREFIX="" echoinfo OK PREFIX="" echoinfo OK
else else
echo "lxc alias "$name" already defined!" echo "lxc alias <$name> already defined!"
fi fi
} }
@@ -329,7 +314,7 @@ function extra_dev_desktop {
: :
} }
function override_lxd_service_to_reload_nftables { function override_lxd_service {
local PREFIX="lxd:override" local PREFIX="lxd:override"
if [[ ! -d /etc/systemd/system/lxd.service.d ]]; then if [[ ! -d /etc/systemd/system/lxd.service.d ]]; then
@@ -338,8 +323,10 @@ function override_lxd_service_to_reload_nftables {
cat <<EOF | sudo tee /etc/systemd/system/lxd.service.d/override.conf cat <<EOF | sudo tee /etc/systemd/system/lxd.service.d/override.conf
[Service] [Service]
ExecStartPost=systemctl reload nftables.service ExecStartPost=systemctl reload nftables.service
Environment=LANGUAGE=en:en_US
EOF EOF
sudo systemctl daemon-reload sudo systemctl daemon-reload
sudo systemctl restart lxd.service
PREFIX="" echo "OK" PREFIX="" echo "OK"
else else
echo "lxd service already overridden!" echo "lxd service already overridden!"
@@ -357,9 +344,23 @@ function ask_for_credential {
echo "successfully checked!" echo "successfully checked!"
} }
function preload_bookworm_image {
local PREFIX="preload:bookworm"
if [[ $(lxc image list debian/12/cloud -f csv | wc -l) -lt 1 ]]; then
echo -n "downloading images from publc remote, please hold on..."
lxc image copy images:debian/12/cloud local: --copy-aliases --quiet
PREFIX="" echoinfo OK
else
echo -n "refreshing images from publc remote..."
lxc image refresh debian/12/cloud --quiet
PREFIX="" echoinfo DONE
fi
}
### MAIN ### MAIN
if [[ "${1:-}" == "SESSION_RELOAD_REQUIRED" ]]; then if [[ "${2:-}" == "SESSION_RELOAD_REQUIRED" ]]; then
SESSION_RELOAD_REQUIRED=true SESSION_RELOAD_REQUIRED=true
shift shift
else else
@@ -392,12 +393,11 @@ else
add_toolbox_sudoers add_toolbox_sudoers
prepare_nftables prepare_nftables
prepare_lxd "$@" prepare_lxd "$@"
override_lxd_service_to_reload_nftables preload_bookworm_image
miaou_resolver miaou_resolver
miaou_evalfrombashrc miaou_evalfrombashrc
miaou_configfiles miaou_configfiles
ask_for_credential ask_for_credential
prepare_nftables
opt_link opt_link
extra_dev_desktop extra_dev_desktop