Guest User

Untitled

a guest
Jun 22nd, 2025
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #Note this assumes that the pacoloco arch repo mirror is already up
  4.  
  5. PASSWORD=mypass
  6. TEMPLATE_STORAGE="local"
  7. LXC_STORAGE="lxc"
  8. CTID=9000
  9. DEFAULT_CORES=8
  10. DEFAULT_MEMORY=4096
  11.  
  12. #Try to delete old template
  13. set +e # Disable exit on error
  14. pct stop 9000
  15. sleep 3
  16. pct destroy 9000
  17. set -e # Re-enable exit on error
  18.  
  19. pveam update
  20. ARCHFILE=$(pveam available | grep -i 'archlinux' | awk '{print $2}')
  21. CACHE_PATH="/var/lib/vz/template/cache"
  22.  
  23. # Delete all archlinux-base_*.tar.zst files except the exact $ARCHFILE match
  24. find "$CACHE_PATH" -name "archlinux-base_*.tar.zst" ! -name "$ARCHFILE" -type f -delete
  25.  
  26. if [ ! -f "$CACHE_PATH/$ARCHFILE" ]; then
  27. pveam download $TEMPLATE_STORAGE $ARCHFILE
  28. else
  29. echo "File $ARCHFILE already exists in cache, skipping download."
  30. fi
  31. 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
  32. pct start $CTID
  33. sleep 3
  34.  
  35. echo "Injecting pacoloco mirror"
  36. pct exec $CTID -- sed -i '1iServer = http://10.32.0.3:9129/repo/archlinux/$repo/os/$arch' /etc/pacman.d/mirrorlist
  37. pct exec $CTID -- sed -i '1i#Local pacoloco server' /etc/pacman.d/mirrorlist
  38.  
  39. echo "Populating keys and upgrading"
  40. pct exec $CTID -- pacman-key --init
  41. pct exec $CTID -- pacman-key --populate archlinux
  42. pct exec $CTID -- pacman -Sy gnupg archlinux-keyring --noconfirm
  43. pct exec $CTID -- pacman -Syyu --noconfirm
  44. echo "Installing additional software"
  45. pct exec $CTID -- pacman -S ncdu lsof podman podman-compose git curl tmux htop unzip --noconfirm
  46. pct exec $CTID -- cp /etc/ssh/sshd_config /etc/ssh/bck_sshd_config
  47. pct exec $CTID -- sed -i 's/^#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
  48. pct exec $CTID -- sed -i 's/^#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config
  49. pct exec $CTID -- systemctl enable sshd
  50.  
  51. echo "Removing logs etc"
  52. pct exec $CTID -- rm -f /etc/machine-id || true
  53. pct exec $CTID -- rm -f /etc/ssh/ssh_host_*
  54. pct exec $CTID -- rm -rf /tmp/*
  55. pct exec $CTID -- rm -rf /var/log/*/*.log
  56.  
  57. echo "Stopping container"
  58. pct stop $CTID
  59. sleep 3
  60. echo "Creating template"
  61. pct template $CTID
  62. echo "Done!"
Advertisement
Add Comment
Please, Sign In to add comment