2021-04-07 22:20:10 +02:00
#!/bin/bash
2022-05-21 23:40:31 +02:00
set -euo pipefail
2021-04-07 22:20:10 +02:00
2021-04-08 00:27:47 +02:00
# This script will create and fire up a standard debian buster lxc container on your Proxmox VE.
# On a Proxmox cluster, the script will create the container on the local node, where it's executed.
# The container ID will be automatically assigned by increasing (+1) the highest number of
# existing LXC containers in your environment. If the assigned ID is already taken by a VM
# or no containers exist yet, the script falls back to the ID 100.
2021-04-07 22:20:10 +02:00
2021-04-08 00:27:47 +02:00
# Authors:
# (C) 2021 Idea an concept by Christian Zengel <christian@sysops.de>
2021-04-16 16:58:46 +02:00
# (C) 2021 Script design and prototype by Markus Helmke <m.helmke@nettwarker.de>
2021-04-09 18:10:07 +02:00
# (C) 2021 Script rework and documentation by Thorsten Spille <thorsten@spille-edv.de>
2021-04-07 22:20:10 +02:00
2021-04-08 18:59:26 +02:00
# IMPORTANT NOTE:
# Please adjust th settings in 'zamba.conf' to your needs before running the script
2021-04-07 22:20:10 +02:00
2021-04-08 18:59:26 +02:00
############### ZAMBA INSTALL SCRIPT ###############
2022-05-21 23:40:31 +02:00
prog = " $( basename $0 ) "
2021-04-25 18:02:35 +02:00
usage( ) {
cat >& 2 <<-EOF
2022-06-09 18:11:24 +02:00
usage: $prog [ -h] [ -d] [ -i CTID] [ -s SERVICE] [ -c CFGFILE]
2021-04-25 18:02:35 +02:00
installs a preconfigured lxc container on your proxmox server
-i CTID provide a container id instead of auto detection
-s SERVICE provide the service name and skip the selection dialog
-c CFGFILE use a different config file than 'zamba.conf'
2026-01-19 09:12:08 +00:00
-r remove zamba.conf inside container
2022-06-09 18:11:24 +02:00
-d Debug mode inside LXC container
2021-04-27 20:30:28 +02:00
-h displays this help text
2021-04-25 18:02:35 +02:00
---------------------------------------------------------------------------
2021-05-03 09:02:16 +02:00
( C) 2021 zamba-lxc-toolbox by bashclub ( https://github.com/bashclub)
2021-04-25 18:02:35 +02:00
---------------------------------------------------------------------------
EOF
exit $1
}
ctid = 0
service = ask
2021-04-27 20:30:28 +02:00
config = $PWD /conf/zamba.conf
2022-06-09 18:11:24 +02:00
debug = 0
2026-01-19 09:12:08 +00:00
preserve_install_scripts = 1
2021-04-25 18:02:35 +02:00
2026-01-19 09:12:08 +00:00
while getopts "hi:s:c:dr" opt; do
2021-04-25 18:02:35 +02:00
case $opt in
h) usage 0 ; ;
i) ctid = $OPTARG ; ;
s) service = $OPTARG ; ;
c) config = $OPTARG ; ;
2026-01-19 09:12:08 +00:00
r) preserve_install_scripts = 0 ; ;
2022-06-09 18:11:24 +02:00
d) debug = 1 ; ;
2021-04-25 18:02:35 +02:00
*) usage 1 ; ;
esac
done
shift $(( OPTIND-1))
2021-04-24 00:00:27 +02:00
2022-05-21 23:40:31 +02:00
OPTS = $( find src/ -maxdepth 1 -mindepth 1 -type d -exec basename -a { } + | sort -n)
2021-04-24 00:00:27 +02:00
2021-04-27 20:30:28 +02:00
valid = 0
2021-04-25 18:02:35 +02:00
if [ [ " $service " = = "ask" ] ] ; then
select svc in $OPTS quit; do
if [ [ " $svc " != "quit" ] ] ; then
2022-05-21 23:40:31 +02:00
for line in $OPTS ; do
2021-04-25 18:02:35 +02:00
if [ [ " $svc " = = " $line " ] ] ; then
service = $svc
echo " Installation of $service selected. "
2021-04-27 20:30:28 +02:00
valid = 1
2021-04-25 18:02:35 +02:00
break
fi
done
2021-04-24 00:00:27 +02:00
else
2021-04-25 18:02:35 +02:00
echo "Selected 'quit' exiting without action..."
exit 0
fi
2021-04-27 20:30:28 +02:00
if [ [ " $valid " = = "1" ] ] ; then
2021-04-25 18:02:35 +02:00
break
2021-04-24 00:00:27 +02:00
fi
done
2021-04-25 18:02:35 +02:00
else
2022-05-21 23:40:31 +02:00
for line in $OPTS ; do
2021-04-25 18:02:35 +02:00
if [ [ " $service " = = " $line " ] ] ; then
echo " Installation of $service selected. "
2021-04-27 20:30:28 +02:00
valid = 1
2021-04-25 18:02:35 +02:00
break
fi
done
fi
2021-04-27 20:30:28 +02:00
if [ [ " $valid " != "1" ] ] ; then
2021-04-25 18:02:35 +02:00
echo "Invalid option, exiting..."
usage 1
2021-04-24 00:00:27 +02:00
fi
2021-04-17 10:13:49 +02:00
2022-01-17 19:46:06 +01:00
# Load configuration file
echo " Loading config file ' $config '... "
2022-05-21 23:40:31 +02:00
if [ ! -e " $config " ] ; then
echo "Configuration files does not exist"
exit 1
fi
source "src/functions.sh"
source " $config "
2022-01-17 19:46:06 +01:00
2022-05-21 23:40:31 +02:00
source " $PWD /src/ $service /constants-service.conf "
2021-04-23 19:39:58 +02:00
2024-11-28 21:27:56 +01:00
if [ [ $service = = "zmb-ad-restore" ] ] ; then
if find ./ | grep samba-backup*.tar.bz2 ; then
sambabackup = $( find $PWD / | grep samba-backup*.tar.bz2 | tail -1)
else
echo " No samba backup found in $PWD . Please place a samba online backup into $PWD . Canceling... "
exit 1
fi
fi
2023-02-10 18:13:45 +01:00
if [ $LXC_MEM -lt $LXC_MEM_MIN ] ; then
LXC_MEM = $LXC_MEM_MIN
fi
2023-01-24 23:04:00 +01:00
if [ $LXC_AUTOTAG -gt 0 ] ; then
TAGS = " --tags ${ LXC_TAGS } , ${ SERVICE_TAGS } "
fi
2023-02-10 18:13:45 +01:00
# Check is the newest template available, else download it.
pveam update
2022-03-29 11:41:01 +02:00
TMPL_NAME = $( pveam available --section system | grep $LXC_TEMPLATE_VERSION | tail -1 | cut -d' ' -f11)
2023-02-10 18:13:45 +01:00
pveam download $LXC_TEMPLATE_STORAGE $TMPL_NAME
2021-04-07 22:20:10 +02:00
2021-04-25 18:02:35 +02:00
if [ $ctid -gt 99 ] ; then
LXC_CHK = $ctid
else
# Get next free LXC-number
2024-07-08 20:15:53 +02:00
LXC_CHK = $(( $( pct list | cut -d' ' -f1 | tail -1) + 1 ))
2021-04-25 18:02:35 +02:00
fi
2021-04-07 22:20:10 +02:00
if [ $LXC_CHK -lt 100 ] || [ -f /etc/pve/qemu-server/$LXC_CHK .conf ] ; then
LXC_NBR = $( pvesh get /cluster/nextid) ;
else
LXC_NBR = $LXC_CHK ;
fi
echo " Will now create LXC Container $LXC_NBR ! " ;
2023-10-07 15:09:15 +02:00
if [ $LXC_THREADS -gt 0 ] ; then
2023-10-29 22:27:34 +01:00
LXC_CORES = --cores\ $LXC_THREADS
2023-10-07 15:09:15 +02:00
fi
2023-10-29 22:35:07 +01:00
if [ [ $LXC_RESSOURCE_POOL != "" ] ] ; then
LXC_POOL = --pool\ $LXC_RESSOURCE_POOL
fi
2021-04-07 22:20:10 +02:00
# Create the container
2023-06-25 08:32:01 +02:00
set +u
2024-07-04 18:20:03 +02:00
pct create $LXC_NBR $TAGS $LXC_CORES $LXC_POOL --password $LXC_PWD -unprivileged $LXC_UNPRIVILEGED $LXC_TEMPLATE_STORAGE :vztmpl/$TMPL_NAME -rootfs $LXC_ROOTFS_STORAGE :$LXC_ROOTFS_SIZE ,acl= 1;
2023-06-25 08:32:01 +02:00
set -u
2021-04-07 22:20:10 +02:00
sleep 2;
2026-01-06 11:47:56 +00:00
if [ [ $SERVICE_TAGS = = *"docker" * ] ] ; then
echo "lxc.apparmor.profile: unconfined" >> /etc/pve/lxc/${ LXC_NBR } .conf
fi
2021-04-08 20:34:40 +02:00
# Check vlan configuration
2022-05-21 23:40:31 +02:00
if [ [ $LXC_VLAN != "NONE" ] ] ; then VLAN = " ,tag= $LXC_VLAN " ; else VLAN = "" ; fi
2021-04-08 20:34:40 +02:00
# Reconfigure conatiner
2024-01-21 13:46:06 +01:00
pct set $LXC_NBR -memory $LXC_MEM -swap $LXC_SWAP -hostname $LXC_HOSTNAME -onboot 1 -timezone $LXC_TIMEZONE -features nesting = $LXC_NESTING ,keyctl= $LXC_KEYCTL ;
2021-04-22 19:37:46 +02:00
if [ $LXC_DHCP = = true ] ; then
2022-05-21 23:40:31 +02:00
pct set $LXC_NBR -net0 " name=eth0,bridge= $LXC_BRIDGE ,ip=dhcp,type=veth $VLAN "
2021-04-22 19:37:46 +02:00
else
2022-05-21 23:40:31 +02:00
pct set $LXC_NBR -net0 " name=eth0,bridge= $LXC_BRIDGE ,firewall=1,gw= $LXC_GW ,ip= $LXC_IP ,type=veth $VLAN " -nameserver $LXC_DNS -searchdomain $LXC_DOMAIN
2021-04-22 19:37:46 +02:00
fi
2024-05-05 11:13:27 +02:00
2021-04-17 10:13:49 +02:00
sleep 2
if [ $LXC_MP -gt 0 ] ; then
2023-06-30 11:45:29 +02:00
pct set $LXC_NBR -mp0 $LXC_SHAREFS_STORAGE :$LXC_SHAREFS_SIZE ,backup= 1,mp= /$LXC_SHAREFS_MOUNTPOINT
2024-07-05 18:33:52 +02:00
if [ [ " $( pvesm status | grep $LXC_SHAREFS_STORAGE | cut -d ' ' -f6) " = = "zfspool" ] ] ; then
pool = $( grep -A 4 $LXC_SHAREFS_STORAGE /etc/pve/storage.cfg | grep -m1 "pool " | cut -d ' ' -f2)
dataset = $( grep mp0 /etc/pve/lxc/$LXC_NBR .conf | cut -d ':' -f3 | cut -d',' -f1)
zfs set recordsize = $LXC_MP_RECORDSIZE $pool /$dataset
fi
2021-04-17 10:13:49 +02:00
fi
2024-05-05 11:13:27 +02:00
2021-04-07 22:20:10 +02:00
sleep 2;
PS3 = "Select the Server-Function: "
2021-04-13 23:40:43 +02:00
pct start $LXC_NBR ;
2021-04-08 20:34:40 +02:00
sleep 5;
2022-05-21 23:40:31 +02:00
# Set the root ssh key
2023-06-24 18:40:58 +02:00
pct exec $LXC_NBR -- mkdir -p /root/.ssh
2021-04-25 18:02:35 +02:00
pct push $LXC_NBR $LXC_AUTHORIZED_KEY /root/.ssh/authorized_keys
2022-05-21 23:40:31 +02:00
pct push $LXC_NBR " $config " /root/zamba.conf
2026-01-19 09:12:08 +00:00
for f in " $PWD /src/functions.sh " " $PWD /src/constants.conf " " $PWD /src/lxc-base.sh " " $PWD /src/ $service /install-service.sh " " $PWD /src/ $service /constants-service.conf " ; do
pct push $LXC_NBR $f /root/$( basename $f )
done
2021-04-23 19:39:58 +02:00
2026-01-19 09:12:08 +00:00
if [ [ $service = = "zmb-ad" ] ] || [ [ $service = = "zmb-ad-join" ] ] ; then
pct push $LXC_NBR scripts/zmb-ad_auto-map-root.sh /root/zmb-ad_auto-map-root.sh
pct push $LXC_NBR scripts/create-service-account /usr/bin/create-service-account
2024-11-28 21:27:56 +01:00
fi
2026-01-19 09:12:08 +00:00
pct exec $LXC_NBR -- sed -i " s,\${service}, ${ service } , " /root/zamba.conf
pct exec $LXC_NBR -- echo " LXC_NBR= $LXC_NBR " /root/zamba.conf
2022-06-09 18:11:24 +02:00
if [ $debug -gt 0 ] ; then dbg = -vx; else dbg = "" ; fi
2021-04-23 19:39:58 +02:00
echo "Installing basic container setup..."
2022-06-09 18:11:24 +02:00
pct exec $LXC_NBR -- su - root -c " bash $dbg /root/lxc-base.sh "
2021-04-25 18:02:35 +02:00
echo " Install ' $service '! "
2022-06-09 18:11:24 +02:00
pct exec $LXC_NBR -- su - root -c " bash $dbg /root/install-service.sh "
2021-04-12 01:49:10 +02:00
2023-02-11 14:45:43 +01:00
pct shutdown $LXC_NBR
2021-04-25 18:02:35 +02:00
if [ [ $service = = "zmb-ad" ] ] ; then
2022-05-21 23:40:31 +02:00
## set nameserver, ${LXC_IP%/*} extracts the ip address from cidr format
pct set $LXC_NBR -nameserver ${ LXC_IP %/* }
2024-11-28 21:27:56 +01:00
elif [ [ $service = = "zmb-ad-restore" ] ] ; then
## set nameserver, ${LXC_IP%/*} extracts the ip address from cidr format
pct set $LXC_NBR -nameserver ${ LXC_IP %/* }
2023-02-12 15:09:17 +01:00
elif [ [ $service = = "zmb-ad-join" ] ] ; then
pct set $LXC_NBR -nameserver " ${ LXC_IP %/* } $LXC_DNS "
2022-01-17 19:46:06 +01:00
fi
2023-06-30 11:45:29 +02:00
pct start $LXC_NBR
2023-10-29 22:58:18 +01:00
if [ [ $service = = "zmb-ad" ] ] || [ [ $service = = "zmb-ad-join" ] ] ; then
sleep 5
pct exec $LXC_NBR /usr/local/bin/smb-backup 7
2024-05-05 11:31:59 +02:00
fi
2026-01-19 09:12:08 +00:00
if [ $preserve_install_scripts -eq 0 ] ; then
for f in constants.conf constants-service.conf functions.sh install-service.sh lxc-base.sh zamba.conf; do
pct exec $LXC_NBR -- if [ -f /root/$f ] ; then rm -f /root/${ f } ; fi
done
fi