gamuntu.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #!/usr/bin/env bash
  2. YW=`echo "\033[33m"`
  3. BL=`echo "\033[36m"`
  4. RD=`echo "\033[01;31m"`
  5. CM='\xE2\x9C\x94\033'
  6. GN=`echo "\033[1;92m"`
  7. CL=`echo "\033[m"`
  8. APP="GamUntu"
  9. HN=$(echo ${APP,,} | tr -d ' ')
  10. while true; do
  11. read -p "This will create a New ${APP} LXC. Proceed(y/n)?" yn
  12. case $yn in
  13. [Yy]* ) break;;
  14. [Nn]* ) exit;;
  15. * ) echo "Please answer yes or no.";;
  16. esac
  17. done
  18. clear
  19. function header_info {
  20. echo -e "${GN}
  21. _____ _ _ _
  22. / ____| | | | | | |
  23. | | __ __ _ _ __ ___ | | | |_ __ | |_ _ _
  24. | | |_ |/ _ | _ _ \| | | | _ \| __| | | |
  25. | |__| | (_| | | | | | | |__| | | | | |_| |_| |
  26. \_____|\__,_|_| |_| |_|\____/|_| |_|\__|\__,_|
  27. ${CL}"
  28. }
  29. header_info
  30. show_menu(){
  31. printf " ${YW} 1)${GN} Privileged ${CL}\n"
  32. printf " ${YW} 2)${RD} Unprivileged (no device passthrough)${CL}\n"
  33. printf "Please choose a Install Method and hit enter or ${RD}x${CL} to exit."
  34. read opt
  35. }
  36. option_picked(){
  37. message1=${@:-"${CL}Error: No message passed"}
  38. printf " ${YW}${message1}${CL}\n"
  39. }
  40. show_menu
  41. while [ $opt != '' ]
  42. do
  43. if [ $opt = '' ]; then
  44. exit;
  45. else
  46. case $opt in
  47. 1) clear;
  48. header_info;
  49. option_picked "Using Privileged Install";
  50. IM=0
  51. break;
  52. ;;
  53. 2) clear;
  54. header_info;
  55. option_picked "Using Unprivileged Install";
  56. IM=1
  57. break;
  58. ;;
  59. x)exit;
  60. ;;
  61. \n)exit;
  62. ;;
  63. *)clear;
  64. option_picked "Please choose a Install Method from the menu";
  65. show_menu;
  66. ;;
  67. esac
  68. fi
  69. done
  70. show_menu2(){
  71. printf " ${YW} 1)${GN} Use Automatic Login ${CL}\n"
  72. printf " ${YW} 2)${GN} Use Password (changeme) ${CL}\n"
  73. printf "Please choose a Password Type and hit enter or ${RD}x${CL} to exit."
  74. read opt
  75. }
  76. option_picked(){
  77. message=${@:-"${CL}Error: No message passed"}
  78. printf " ${YW}${message1}${CL}\n"
  79. printf " ${YW}${message}${CL}\n"
  80. }
  81. show_menu2
  82. while [ $opt != '' ]
  83. do
  84. if [ $opt = '' ]; then
  85. exit;
  86. else
  87. case $opt in
  88. 1) clear;
  89. header_info;
  90. option_picked "Using Automatic Login";
  91. PW=" "
  92. break;
  93. ;;
  94. 2) clear;
  95. header_info;
  96. option_picked "Using Password (changeme)";
  97. PW="-password changeme"
  98. break;
  99. ;;
  100. x)exit;
  101. ;;
  102. \n)exit;
  103. ;;
  104. *)clear;
  105. option_picked "Please choose a Password Type from the menu";
  106. show_menu2;
  107. ;;
  108. esac
  109. fi
  110. done
  111. set -o errexit
  112. set -o errtrace
  113. set -o nounset
  114. set -o pipefail
  115. shopt -s expand_aliases
  116. alias die='EXIT=$? LINE=$LINENO error_exit'
  117. trap die ERR
  118. trap cleanup EXIT
  119. function error_exit() {
  120. trap - ERR
  121. local DEFAULT='Unknown failure occured.'
  122. local REASON="\e[97m${1:-$DEFAULT}\e[39m"
  123. local FLAG="\e[91m[ERROR] \e[93m$EXIT@$LINE"
  124. msg "$FLAG $REASON"
  125. [ ! -z ${CTID-} ] && cleanup_ctid
  126. exit $EXIT
  127. }
  128. function warn() {
  129. local REASON="\e[97m$1\e[39m"
  130. local FLAG="\e[93m[WARNING]\e[39m"
  131. msg "$FLAG $REASON"
  132. }
  133. function info() {
  134. local REASON="$1"
  135. local FLAG="\e[36m[INFO]\e[39m"
  136. msg "$FLAG $REASON"
  137. }
  138. function msg() {
  139. local TEXT="$1"
  140. echo -e "$TEXT"
  141. }
  142. function cleanup_ctid() {
  143. if $(pct status $CTID &>/dev/null); then
  144. if [ "$(pct status $CTID | awk '{print $2}')" == "running" ]; then
  145. pct stop $CTID
  146. fi
  147. pct destroy $CTID
  148. elif [ "$(pvesm list $STORAGE --vmid $CTID)" != "" ]; then
  149. pvesm free $ROOTFS
  150. fi
  151. }
  152. function cleanup() {
  153. popd >/dev/null
  154. rm -rf $TEMP_DIR
  155. }
  156. if [ "$IM" == "1" ]; then
  157. FEATURES="nesting=1,keyctl=1"
  158. else
  159. FEATURES="nesting=1"
  160. fi
  161. TEMP_DIR=$(mktemp -d)
  162. pushd $TEMP_DIR >/dev/null
  163. export CTID=$(pvesh get /cluster/nextid)
  164. export PCT_OSTYPE=ubuntu
  165. export PCT_OSVERSION=21.10
  166. export PCT_DISK_SIZE=8
  167. export PCT_OPTIONS="
  168. -features $FEATURES
  169. -hostname $HN
  170. -net0 name=eth0,bridge=vmbr0,ip=dhcp
  171. -onboot 1
  172. -cores 2
  173. -memory 2048
  174. -unprivileged ${IM}
  175. ${PW}
  176. "
  177. bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/ct/create_lxc.sh)" || exit
  178. STORAGE_TYPE=$(pvesm status -storage $(pct config $CTID | grep rootfs | awk -F ":" '{print $2}') | awk 'NR>1 {print $2}')
  179. if [ "$STORAGE_TYPE" == "zfspool" ]; then
  180. warn "Some addons may not work due to ZFS not supporting 'fallocate'."
  181. fi
  182. LXC_CONFIG=/etc/pve/lxc/${CTID}.conf
  183. cat <<EOF >> $LXC_CONFIG
  184. lxc.cgroup2.devices.allow: a
  185. lxc.cap.drop:
  186. lxc.cgroup2.devices.allow: c 188:* rwm
  187. lxc.cgroup2.devices.allow: c 189:* rwm
  188. lxc.cgroup2.devices.allow: c 226:0 rwm
  189. lxc.cgroup2.devices.allow: c 226:128 rwm
  190. lxc.cgroup2.devices.allow: c 29:0 rwm
  191. lxc.mount.entry: /dev/uinput dev/uinput none bind,optional,create=dir
  192. lxc.mount.entry: /dev/dri dev/dri none bind,optional,create=dir
  193. lxc.mount.entry: /dev/fb0 dev/fb0 none bind,optional,create=file
  194. lxc.mount.entry: /dev/dri/renderD128 dev/renderD128 none bind,optional,create=file
  195. lxc.mount.entry: /dev/serial/by-id dev/serial/by-id none bind,optional,create=dir
  196. lxc.mount.entry: /dev/ttyUSB0 dev/ttyUSB0 none bind,optional,create=file
  197. lxc.mount.entry: /dev/ttyACM0 dev/ttyACM0 none bind,optional,create=file
  198. EOF
  199. echo -en "${GN} Starting LXC Container... "
  200. pct start $CTID
  201. echo -e "${CM}${CL} \r"
  202. alias lxc-cmd="lxc-attach -n $CTID --"
  203. lxc-cmd bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/setup/$HN-install.sh)" || exit
  204. IP=$(pct exec $CTID ip a s dev eth0 | sed -n '/inet / s/\// /p' | awk '{print $2}')
  205. echo -e "${GN}Successfully created ${APP} LXC to${CL} ${BL}$CTID${CL}.
  206. ${BL}Webmin${CL} should be reachable by going to the following URL.
  207. ${BL}https://${IP}:10000${CL} \n"