docker-v2.sh 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. while true; do
  9. read -p "This will create a New Docker LXC. Proceed(y/n)?" yn
  10. case $yn in
  11. [Yy]* ) break;;
  12. [Nn]* ) exit;;
  13. * ) echo "Please answer yes or no.";;
  14. esac
  15. done
  16. clear
  17. function header_info {
  18. echo -e "${BL}
  19. _____ _
  20. | __ \ | |
  21. | | | | ___ ___| | _____ _ __
  22. | | | |/ _ \ / __| |/ / _ \ __|
  23. | |__| | (_) | (__| < __/ |
  24. |_____/ \___/ \___|_|\_\___|_|
  25. ${CL}"
  26. }
  27. header_info
  28. show_menu(){
  29. printf " ${YW} 1)${YW} Privileged ${CL}\n"
  30. printf " ${YW} 2)${GN} Unprivileged ${CL}\n"
  31. printf "Please choose a Install Method and hit enter or ${RD}x${CL} to exit."
  32. read opt
  33. }
  34. option_picked(){
  35. message1=${@:-"${CL}Error: No message passed"}
  36. printf " ${YW}${message1}${CL}\n"
  37. }
  38. show_menu
  39. while [ "$opt" != " " ]
  40. do
  41. case $opt in
  42. 1) clear;
  43. header_info;
  44. option_picked "Using Privileged Install";
  45. IM=0
  46. break;
  47. ;;
  48. 2) clear;
  49. header_info;
  50. option_picked "Using Unprivileged Install";
  51. IM=1
  52. break;
  53. ;;
  54. x)exit;
  55. ;;
  56. \n)exit;
  57. ;;
  58. *)clear;
  59. option_picked "Please choose a Install Method from the menu";
  60. show_menu;
  61. ;;
  62. esac
  63. done
  64. show_menu2(){
  65. printf " ${YW} 1)${GN} Use Automatic Login ${CL}\n"
  66. printf " ${YW} 2)${GN} Use Password (changeme) ${CL}\n"
  67. printf "Please choose a Password Type and hit enter or ${RD}x${CL} to exit."
  68. read opt
  69. }
  70. option_picked(){
  71. message2=${@:-"${CL}Error: No message passed"}
  72. printf " ${YW}${message1}${CL}\n"
  73. printf " ${YW}${message2}${CL}\n"
  74. }
  75. show_menu2
  76. while [ "$opt" != " " ]
  77. do
  78. case $opt in
  79. 1) clear;
  80. header_info;
  81. option_picked "Using Automatic Login";
  82. PW=" "
  83. break;
  84. ;;
  85. 2) clear;
  86. header_info;
  87. option_picked "Using Password (changeme)";
  88. PW="-password changeme"
  89. break;
  90. ;;
  91. x)exit;
  92. ;;
  93. \n)exit;
  94. ;;
  95. *)clear;
  96. option_picked "Please choose a Password Type from the menu";
  97. show_menu2;
  98. ;;
  99. esac
  100. done
  101. show_menu3(){
  102. printf " ${RD} If Using ZFS, You Have Storage Driver Options${CL}\n"
  103. printf " ${RD} Non ZFS, Select Standard overlay2 Storage Driver${CL}\n"
  104. printf " ${YW} 1)${GN} Use fuse-overlayfs Storage Driver${CL}\n"
  105. printf " ${YW} 2)${GN} Use Standard overlay2 Storage Driver${CL}\n"
  106. printf "Please choose a Storage Driver and hit enter or ${RD}x${CL} to exit."
  107. read opt
  108. }
  109. option_picked(){
  110. message3=${@:-"${CL}Error: No message passed"}
  111. printf " ${YW}${message1}${CL}\n"
  112. printf " ${YW}${message2}${CL}\n"
  113. printf " ${YW}${message3}${CL}\n"
  114. }
  115. show_menu3
  116. while [ "$opt" != " " ]
  117. do
  118. case $opt in
  119. 1) clear;
  120. header_info;
  121. option_picked "Using fuse-overlayfs Storage Driver";
  122. STORAGE_DRIVER="fuse"
  123. break;
  124. ;;
  125. 2) clear;
  126. header_info;
  127. option_picked "Using overlay2 Storage Driver";
  128. STORAGE_DRIVER=" "
  129. break;
  130. ;;
  131. x)exit;
  132. ;;
  133. \n)exit;
  134. ;;
  135. *)clear;
  136. option_picked "Please choose a Storage Driver from the menu";
  137. show_menu3;
  138. ;;
  139. esac
  140. done
  141. show_menu4(){
  142. printf " ${YW} 1)${GN} Automatic DHCP ${CL}\n"
  143. printf " ${YW} 2)${GN} Manual DHCP ${CL}\n"
  144. printf "Please choose a DHCP Type and hit enter or ${RD}x${CL} to exit."
  145. read opt
  146. }
  147. option_picked(){
  148. message4=${@:-"${CL}Error: No message passed"}
  149. printf " ${YW}${message1}${CL}\n"
  150. printf " ${YW}${message2}${CL}\n"
  151. printf " ${YW}${message3}${CL}\n"
  152. printf " ${YW}${message4}${CL}\n"
  153. }
  154. show_menu4
  155. while [ "$opt" != " " ]
  156. do
  157. case $opt in
  158. 1) clear;
  159. header_info;
  160. option_picked "Using Automatic DHCP";
  161. DHCP=" "
  162. break;
  163. ;;
  164. 2) clear;
  165. header_info;
  166. option_picked "Using Manual DHCP";
  167. DHCP="1"
  168. break;
  169. ;;
  170. x)exit;
  171. ;;
  172. \n)exit;
  173. ;;
  174. *)clear;
  175. option_picked "Please choose a DHCP Type from the menu";
  176. show_menu4;
  177. ;;
  178. esac
  179. done
  180. set -o errexit
  181. set -o errtrace
  182. set -o nounset
  183. set -o pipefail
  184. shopt -s expand_aliases
  185. alias die='EXIT=$? LINE=$LINENO error_exit'
  186. trap die ERR
  187. trap cleanup EXIT
  188. function error_exit() {
  189. trap - ERR
  190. local DEFAULT='Unknown failure occured.'
  191. local REASON="\e[97m${1:-$DEFAULT}\e[39m"
  192. local FLAG="\e[91m[ERROR] \e[93m$EXIT@$LINE"
  193. msg "$FLAG $REASON"
  194. [ ! -z ${CTID-} ] && cleanup_ctid
  195. exit $EXIT
  196. }
  197. function warn() {
  198. local REASON="\e[97m$1\e[39m"
  199. local FLAG="\e[93m[WARNING]\e[39m"
  200. msg "$FLAG $REASON"
  201. }
  202. function info() {
  203. local REASON="$1"
  204. local FLAG="\e[36m[INFO]\e[39m"
  205. msg "$FLAG $REASON"
  206. }
  207. function msg() {
  208. local TEXT="$1"
  209. echo -e "$TEXT"
  210. }
  211. function cleanup_ctid() {
  212. if $(pct status $CTID &>/dev/null); then
  213. if [ "$(pct status $CTID | awk '{print $2}')" == "running" ]; then
  214. pct stop $CTID
  215. fi
  216. pct destroy $CTID
  217. elif [ "$(pvesm list $STORAGE --vmid $CTID)" != "" ]; then
  218. pvesm free $ROOTFS
  219. fi
  220. }
  221. function cleanup() {
  222. popd >/dev/null
  223. rm -rf $TEMP_DIR
  224. }
  225. if [ "$IM" == "1" ] && [ "$STORAGE_DRIVER" == " " ]; then
  226. FEATURES="nesting=1,keyctl=1"
  227. elif
  228. [ "$IM" == "1" ] && [ "$STORAGE_DRIVER" == "fuse" ]; then
  229. FEATURES="nesting=1,keyctl=1,fuse=1"
  230. elif
  231. [ "$IM" == "0" ] && [ "$STORAGE_DRIVER" == "fuse" ]; then
  232. FEATURES="nesting=1,fuse=1"
  233. else
  234. FEATURES="nesting=1"
  235. fi
  236. TEMP_DIR=$(mktemp -d)
  237. pushd $TEMP_DIR >/dev/null
  238. export CTID=$(pvesh get /cluster/nextid)
  239. export PCT_OSTYPE=debian
  240. export PCT_OSVERSION=11
  241. export PCT_DISK_SIZE=4
  242. export PCT_OPTIONS="
  243. -features $FEATURES
  244. -hostname docker
  245. -net0 name=eth0,bridge=vmbr0,ip=dhcp
  246. -onboot 1
  247. -cores 2
  248. -memory 2048
  249. -unprivileged ${IM}
  250. ${PW}
  251. "
  252. bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/ct/create_lxc.sh)" || exit
  253. STORAGE_TYPE=$(pvesm status -storage $(pct config $CTID | grep rootfs | awk -F ":" '{print $2}') | awk 'NR>1 {print $2}')
  254. if [ "$STORAGE_TYPE" == "zfspool" ]; then
  255. wget -qL -O fuse-overlayfs https://github.com/containers/fuse-overlayfs/releases/download/v1.8.2/fuse-overlayfs-x86_64
  256. warn "Some containers may not work properly due to ZFS not supporting 'fallocate'."
  257. fi
  258. LXC_CONFIG=/etc/pve/lxc/${CTID}.conf
  259. cat <<EOF >> $LXC_CONFIG
  260. lxc.cgroup2.devices.allow: a
  261. lxc.cap.drop:
  262. EOF
  263. if [ "$DHCP" == "1" ]; then
  264. MAC=$(pct config $CTID \
  265. | grep -i hwaddr \
  266. | awk '{print substr($2, 31, length($3) 17 ) }') \
  267. echo -e "MAC Address ${BL}$MAC${CL}"
  268. dhcp_reservation(){
  269. printf "Please set DHCP reservation and press Enter."
  270. read
  271. }
  272. dhcp_reservation
  273. fi
  274. echo -en "${GN} Starting LXC Container... "
  275. pct start $CTID
  276. echo -e "${CM}${CL} \r"
  277. if [ "$STORAGE_TYPE" == "zfspool" ] && [ "$STORAGE_DRIVER" == "fuse" ]; then
  278. pct push $CTID fuse-overlayfs /usr/local/bin/fuse-overlayfs -perms 755
  279. info "Using ${BL}fuse-overlayfs${CL} Storage Driver."
  280. else
  281. info "Using ${BL}overlay2${CL} Storage Driver."
  282. fi
  283. alias lxc-cmd="lxc-attach -n $CTID --"
  284. lxc-cmd bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/setup/docker-install.sh)" || exit
  285. IP=$(pct exec $CTID ip a s dev eth0 | sed -n '/inet / s/\// /p' | awk '{print $2}')
  286. echo -e "${GN}Successfully created Docker LXC to${CL} ${BL}$CTID${CL}. \n"