lxc -> incus

This commit is contained in:
2025-03-23 11:26:28 +01:00
parent b0067645ba
commit 1b0b27d94a
12 changed files with 155 additions and 113 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
logs/*
kvrt.run

3
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,3 @@
{
"CodeGPT.apiKey": "Ollama"
}

19
backup-incus.sh Executable file
View File

@@ -0,0 +1,19 @@
#!/bin/bash
# Basic shell script to backup required LXD parts ##
## Backup and restore LXD config ##
## Today's date ##
if [[ $1 != 'virtual-machine' ]] && [[ $1 != 'container' ]]; then
echo "use $0 virtual-machine|container"
exit
fi
NOW=$(date +'%Y-%m-%d')
echo $(date) "Starting incus backup ..."
## Backup all container or vm Instances
for i in $(incus list type=$1 -c n --format csv); do
echo $(date) "Making backup of ${i} ..."
incus export "${i}" "/nfs/nas/LXD-Backup/${i}-backup-${NOW}.tar.xz" --optimized-storage --instance-only
done
echo $(date) "incus backup ended."

View File

@@ -8,7 +8,7 @@ NOW=$(date +'%Y-%m-%d')
echo $(date) "Starting backup of LXD ..."
## Dump LXD server config ##
echo $(date) "Making backup of lxd.config ..."
lxd init --dump >"/nfs/nas/LXD-Backup/lxd.config.${NOW}"
lxc init --dump >"/nfs/nas/LXD-Backup/lxd.config.${NOW}"
## Dump all instances list ##
echo $(date) "Making backup of lxd.instances.list ..."
@@ -16,6 +16,6 @@ lxc list >"/nfs/nas/LXD-Backup/lxd.instances.list.${NOW}"
## Make sure we know LXD version too ##
echo $(date) "Making backup of lxd.version ..."
snap list lxd >"/nfs/nas/LXD-Backup/lxd-version.${NOW}"
snap list lxc >"/nfs/nas/LXD-Backup/lxd-version.${NOW}"
echo $(date) "Backup of LXD ended."

4
backup_installed_pkg.sh Executable file
View File

@@ -0,0 +1,4 @@
dpkg --get-selections >~/pkg_backup/Package.list
sudo cp -R /etc/apt/sources.list* ~/pkg_backup
sudo apt-key exportall >~/pkg_backup/Repo.keys
sudo chown -R alex:alex ~/pkg_backup/

View File

@@ -1,13 +0,0 @@
#!/bin/bash
snapshotnum=3
updatescript=$(dirname "$0")/update-lxc-container.sh
echo $(date) "- Check if there're at least ${snapshotnum} lxc containers snapshot..."
if [ $(lxc list type=container -c S --format csv | head -1) -lt ${snapshotnum} ]; then
echo $(date) "- Create VMs/containers snapshot and update containers..."
for vm in $(lxc list type=virtual-machine -c n --format csv); do
lxc snapshot ${vm}
done
${updatescript}
else
echo $(date) "- Do nothing."
fi

7
restore_installed_pkg.sh Executable file
View File

@@ -0,0 +1,7 @@
sudo apt-key add ~/Repo.keys
sudo cp -R ~/sources.list* /etc/apt/
sudo apt-get update
sudo apt-get install dselect
sudo dselect update
sudo dpkg --set-selections <~/Package.list
sudo apt-get dselect-upgrade -y

48
update-incus-container.sh Executable file
View File

@@ -0,0 +1,48 @@
#!/bin/bash
declare -A pids
BRed=$'\033[1;31m' # Red
BGreen=$'\033[1;32m' # Green
Color_Off=$'\033[0m' # Text Reset
TMSTMP=$(date +'%Y%m%d%H%M%S')
function stop_container() {
echo $(date) "- ${BGreen}Stop container ${1}${Color_Off}"
incus stop ${1} | sed "s/^/$(date) - ${BGreen}${1}${Color_Off} /"
wait
echo -e $(date) "- ${BGreen}Container ${1} stopped${Color_Off}"
}
function update_container() {
incus exec ${1} -- sh -c "apt update 2>&1" | sed "s/^/$(date) - ${BGreen}${1}${Color_Off} /"
sleep 2
incus exec ${1} -- sh -c "apt upgrade -y 2>&1" | sed "s/^/$(date) - ${BGreen}${1}${Color_Off} /"
sleep 2
incus exec ${1} -- sh -c "apt autoremove -y 2>&1" | sed "s/^/$(date) - ${BGreen}${1}${Color_Off} /"
sleep 2
echo -e $(date) "- ${BGreen}Refresh snap packages${Color_Off}"
incus exec ${1} -- bash -c "if [[ \$(which snap > /dev/null; echo \$?) == 0 ]]; then snap refresh ; fi" | sed "s/^/$(date) - ${BGreen}${1}${Color_Off} /"
if [ "${2}" == "STOPPED" ]; then
stop_container ${1}
fi
}
echo $(date) "- ${BGreen}Start updating incus container...${Color_Off}"
for cont_name in $(incus list type=container -c n --format csv); do
echo $(date) "- ${BGreen}Make ${cont_name} container snapshot...${Color_Off}"
incus snapshot create ${cont_name} | sed "s/^/$(date) - ${BGreen}${cont_name}${Color_Off} /"
echo $(date) "- ${BGreen}Updating ${cont_name} container...${Color_Off}"
Status=$(incus list type=container -c ns --format csv | grep ${cont_name} | cut -d',' -f 2)
if [ "${Status}" == "STOPPED" ]; then
echo -e $(date) "- ${BGreen}Start container ${cont_name}.${Color_Off}"
incus start ${cont_name} | sed 's/^/'${date}' - '${cont_name}' /'
sleep 5
else
echo -e $(date) "- ${BGreen}Container ${cont_name} is already started.${Color_Off}"
fi
update_container ${cont_name} ${Status} &
done
wait
echo $(date) "- ${BGreen}Updating incus container ended.${Color_Off}"

50
update-incus-virtualMachine.sh Executable file
View File

@@ -0,0 +1,50 @@
#!/bin/bash
declare -A pids
BRed='\033[1;31m' # Red
BGreen='\033[1;32m' # Green
Color_Off='\033[0m' # Text Reset
TMSTMP=$(date +'%Y%m%d%H%M%S')
function stop_virtualMachine() {
sleep 120
incus stop ${1}
echo -e $(date) "- ${BGreen}Virtual Machine ${1} stopped${Color_Off}"
}
echo $(date) "- Start updating incus Virtual Machine..."
for vm_name in $(incus list type=virtual-machine -c n --format csv); do
echo $(date) "- Make ${vm_name} Virtual Machine snapshot..."
incus snapshot create ${vm_name}
echo $(date) "- Updating ${vm_name} Virtual Machine..."
Status=$(incus list type=virtual-machine -c ns --format csv | grep ${vm_name} | cut -d',' -f 2)
if [ "${Status}" == "STOPPED" ]; then
echo -e $(date) "- ${BGreen}Start Virtual Machine ${vm_name}${Color_Off}"
incus start ${vm_name}
sleep 5
else
echo -e $(date) "- ${BGreen}Virtual Machine ${vm_name} is already started${Color_Off}"
fi
incus exec ${vm_name} -- sh -c "apt update"
sleep 2
incus exec ${vm_name} -- sh -c "apt upgrade -y"
sleep 2
incus exec ${vm_name} -- sh -c "apt autoremove"
sleep 2
echo -e $(date) "- Refresh snap packages"
incus exec ${vm_name} -- bash -c "if [[ \$(which snap > /dev/null; echo \$?) == 0 ]]; then snap refresh ; fi"
if [ "${Status}" = "STOPPED" ]; then
echo $(date) "- Stop Virtual Machine ${vm_name}"
stop_virtualMachine ${vm_name} &
pids[${vm_name}]=$!
fi
sleep 2
done
for pid in ${pids[@]}; do
wait $pid
done
echo $(date) "- Updating incus Virtual Machine ended"

View File

@@ -1,50 +0,0 @@
#!/bin/bash
declare -A pids
BRed='\033[1;31m' # Red
BGreen='\033[1;32m' # Green
Color_Off='\033[0m' # Text Reset
TMSTMP=$(date +'%Y%m%d%H%M%S')
function stop_container() {
sleep 120
lxc stop ${1}
echo -e $(date) "- ${BGreen}Container ${1} stopped${Color_Off}"
}
echo $(date) "- Start updating LXC container..."
for cont_name in $(lxc list type=container -c n --format csv); do
echo $(date) "- Make ${cont_name} container snapshot..."
lxc snapshot ${cont_name}
echo $(date) "- Updating ${cont_name} container..."
Status=$(lxc list type=container -c ns --format csv | grep ${cont_name} | cut -d',' -f 2)
if [ "${Status}" == "STOPPED" ]; then
echo -e $(date) "- ${BGreen}Start container ${cont_name}${Color_Off}"
lxc start ${cont_name}
sleep 5
else
echo -e $(date) "- ${BGreen}Container ${cont_name} is already started${Color_Off}"
fi
lxc exec ${cont_name} -- sh -c "apt update"
sleep 2
lxc exec ${cont_name} -- sh -c "apt upgrade -y"
sleep 2
lxc exec ${cont_name} -- sh -c "apt autoremove"
sleep 2
echo -e $(date) "- Refresh snap packages"
lxc exec ${cont_name} -- bash -c "if [[ \$(which snap > /dev/null; echo \$?) == 0 ]]; then snap refresh ; fi"
if [ "${Status}" == "STOPPED" ]; then
echo $(date) "- Stop container ${cont_name}"
stop_container ${cont_name} &
pids[${cont_name}]=$!
fi
sleep 2
done
for pid in ${pids[@]}; do
wait $pid
done
echo $(date) "- Updating LXC container ended"

View File

@@ -1,48 +0,0 @@
#!/bin/bash
BRed='\033[1;31m' # Red
BGreen='\033[1;32m' # Green
Color_Off='\033[0m' # Text Reset
function stop_container() {
sleep 300
lxc stop ${1}
echo -e $(date) "- ${BGreen}Container ${1} stopped${Color_Off}"
}
if [[ $1 == '' ]]; then
echo "use $0 container-name"
exit
fi
echo $(date) "- Start updating $1 LXC container..."
status=$(lxc info $1 | grep Status | cut -d' ' -f 2)
echo $(date) "- Updating $1 container..."
if [ $status == "STOPPED" ]; then
echo -e $(date) "- ${BGreen}Start container $1${Color_Off}"
lxc start $1
sleep 5
else
echo -e $(date) "- ${BGreen}Container $1 is already started${Color_Off}"
fi
lxc exec $1 -- sh -c "apt update"
sleep 2
lxc exec $1 -- sh -c "apt upgrade -y"
sleep 2
lxc exec $1 -- sh -c "apt autoremove"
sleep 2
echo -e $(date) "- Refresh snap packages"
lxc exec $1 -- bash -c "if [[ \$(which snap > /dev/null; echo \$?) == 0 ]]; then snap refresh ; fi"
if [ $status == "STOPPED" ]; then
echo $(date) "- Stop container $1"
stop_container $1 &
pids[$1]=$!
fi
sleep 2
for pid in ${pids[@]}; do
wait $pid
done
echo $(date) "- Updating LXC container ended"

21
update-remote-server.sh Executable file
View File

@@ -0,0 +1,21 @@
#!/bin/bash
BRed=$'\033[1;31m' # Red
BGreen=$'\033[1;32m' # Green
Color_Off=$'\033[0m' # Text Reset
ports=("22" "6229")
remotes=($(grep -v -E "^#|^$|^127|::|hap-gm|dm520" /etc/hosts | cut -d" " -f 2))
echo $(date) "- ${BGreen}Updating remote servers started.${Color_Off}"
for remote in ${remotes[@]}; do
ping ${remote} -4 -w 2 > /dev/null 2>&1
if [ $? == 0 ]; then
echo $(date) "- ${BGreen}Update remote server ${remote}${Color_Off}"
for port in ${ports[@]}; do
ssh -p ${port} -o ConnectTimeout=2 root@${remote} "apt update; apt upgrade -y; apt autoremove -y" 2>&1 | sed "s/^/$(date) - ${BGreen}${remote}${Color_Off} /"
done
else
echo $(date) "- ${BRed}Remote server ${remote} unreachable ${Color_Off}"
fi
done
echo $(date) "- ${BGreen}Updating remote servers ended.${Color_Off}"
exit 0