Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #Note this assumes that the pacoloco arch repo mirror is already up
- PASSWORD=mypass
- TEMPLATE_STORAGE="local"
- LXC_STORAGE="lxc"
- CTID=9000
- DEFAULT_CORES=8
- DEFAULT_MEMORY=4096
- #Try to delete old template
- set +e # Disable exit on error
- pct stop 9000
- sleep 3
- pct destroy 9000
- set -e # Re-enable exit on error
- pveam update
- ARCHFILE=$(pveam available | grep -i 'archlinux' | awk '{print $2}')
- CACHE_PATH="/var/lib/vz/template/cache"
- # Delete all archlinux-base_*.tar.zst files except the exact $ARCHFILE match
- find "$CACHE_PATH" -name "archlinux-base_*.tar.zst" ! -name "$ARCHFILE" -type f -delete
- if [ ! -f "$CACHE_PATH/$ARCHFILE" ]; then
- pveam download $TEMPLATE_STORAGE $ARCHFILE
- else
- echo "File $ARCHFILE already exists in cache, skipping download."
- fi
- pct create $CTID $TEMPLATE_STORAGE:vztmpl/$ARCHFILE --net0 name=eth0,ip=dhcp,bridge=vmbr0 --storage $LXC_STORAGE --hostname arch --unprivileged 1 --features nesting=1 --arch amd64 --cores $DEFAULT_CORES --memory $DEFAULT_MEMORY --password $PASSWORD --swap 0
- pct start $CTID
- sleep 3
- echo "Injecting pacoloco mirror"
- pct exec $CTID -- sed -i '1iServer = http://10.32.0.3:9129/repo/archlinux/$repo/os/$arch' /etc/pacman.d/mirrorlist
- pct exec $CTID -- sed -i '1i#Local pacoloco server' /etc/pacman.d/mirrorlist
- echo "Populating keys and upgrading"
- pct exec $CTID -- pacman-key --init
- pct exec $CTID -- pacman-key --populate archlinux
- pct exec $CTID -- pacman -Sy gnupg archlinux-keyring --noconfirm
- pct exec $CTID -- pacman -Syyu --noconfirm
- echo "Installing additional software"
- pct exec $CTID -- pacman -S ncdu lsof podman podman-compose git curl tmux htop unzip --noconfirm
- pct exec $CTID -- cp /etc/ssh/sshd_config /etc/ssh/bck_sshd_config
- pct exec $CTID -- sed -i 's/^#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
- pct exec $CTID -- sed -i 's/^#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config
- pct exec $CTID -- systemctl enable sshd
- echo "Removing logs etc"
- pct exec $CTID -- rm -f /etc/machine-id || true
- pct exec $CTID -- rm -f /etc/ssh/ssh_host_*
- pct exec $CTID -- rm -rf /tmp/*
- pct exec $CTID -- rm -rf /var/log/*/*.log
- echo "Stopping container"
- pct stop $CTID
- sleep 3
- echo "Creating template"
- pct template $CTID
- echo "Done!"
Advertisement
Add Comment
Please, Sign In to add comment