docker.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #!/usr/bin/env bash
  2. YW=`echo "\033[33m"`
  3. BL=`echo "\033[94m"`
  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. if [ $opt = '' ]; then
  42. exit;
  43. else
  44. case $opt in
  45. 1) clear;
  46. header_info;
  47. option_picked "Using Privileged Install";
  48. IM=0
  49. break;
  50. ;;
  51. 2) clear;
  52. header_info;
  53. option_picked "Using Unprivileged Install";
  54. IM=1
  55. break;
  56. ;;
  57. x)exit;
  58. ;;
  59. \n)exit;
  60. ;;
  61. *)clear;
  62. option_picked "Please choose a Install Method from the menu";
  63. show_menu;
  64. ;;
  65. esac
  66. fi
  67. done
  68. show_menu2(){
  69. printf " ${YW} 1)${GN} Use Automatic Login ${CL}\n"
  70. printf " ${YW} 2)${GN} Use Password (changeme) ${CL}\n"
  71. printf "Please choose a Password Type and hit enter or ${RD}x${CL} to exit."
  72. read opt
  73. }
  74. option_picked(){
  75. message2=${@:-"${CL}Error: No message passed"}
  76. printf " ${YW}${message1}${CL}\n"
  77. printf " ${YW}${message2}${CL}\n"
  78. }
  79. show_menu2
  80. while [ $opt != '' ]
  81. do
  82. if [ $opt = '' ]; then
  83. exit;
  84. else
  85. case $opt in
  86. 1) clear;
  87. header_info;
  88. option_picked "Using Automatic Login";
  89. PW=" "
  90. break;
  91. ;;
  92. 2) clear;
  93. header_info;
  94. option_picked "Using Password (changeme)";
  95. PW="-password changeme"
  96. break;
  97. ;;
  98. x)exit;
  99. ;;
  100. \n)exit;
  101. ;;
  102. *)clear;
  103. option_picked "Please choose a Password Type from the menu";
  104. show_menu2;
  105. ;;
  106. esac
  107. fi
  108. done
  109. show_menu3(){
  110. printf " ${YW} 1)${GN} If ZFS, Use fuse-overlayfs Storage Driver${CL}\n"
  111. printf " ${YW} 2)${GN} Use Standard overlay2fs Storage Driver${CL}\n"
  112. printf "Please choose a Password Type and hit enter or ${RD}x${CL} to exit."
  113. read opt
  114. }
  115. option_picked(){
  116. message3=${@:-"${CL}Error: No message passed"}
  117. printf " ${YW}${message1}${CL}\n"
  118. printf " ${YW}${message2}${CL}\n"
  119. printf " ${YW}${message3}${CL}\n"
  120. }
  121. show_menu2
  122. while [ $opt != '' ]
  123. do
  124. if [ $opt = '' ]; then
  125. exit;
  126. else
  127. case $opt in
  128. 1) clear;
  129. header_info;
  130. option_picked "Using fuse-overlayfs Storage Driver";
  131. SD=" "
  132. break;
  133. ;;
  134. 2) clear;
  135. header_info;
  136. option_picked "Using overlay2fs Storage Driver";
  137. SD="#"
  138. break;
  139. ;;
  140. x)exit;
  141. ;;
  142. \n)exit;
  143. ;;
  144. *)clear;
  145. option_picked "Please choose a Storage Driver Type from the menu";
  146. show_menu2;
  147. ;;
  148. esac
  149. fi
  150. done
  151. set -o errexit
  152. set -o errtrace
  153. set -o nounset
  154. set -o pipefail
  155. shopt -s expand_aliases
  156. alias die='EXIT=$? LINE=$LINENO error_exit'
  157. trap die ERR
  158. trap cleanup EXIT
  159. function error_exit() {
  160. trap - ERR
  161. local DEFAULT='Unknown failure occured.'
  162. local REASON="\e[97m${1:-$DEFAULT}\e[39m"
  163. local FLAG="\e[91m[ERROR] \e[93m$EXIT@$LINE"
  164. msg "$FLAG $REASON"
  165. [ ! -z ${CTID-} ] && cleanup_ctid
  166. exit $EXIT
  167. }
  168. function warn() {
  169. local REASON="\e[97m$1\e[39m"
  170. local FLAG="\e[93m[WARNING]\e[39m"
  171. msg "$FLAG $REASON"
  172. }
  173. function info() {
  174. local REASON="$1"
  175. local FLAG="\e[36m[INFO]\e[39m"
  176. msg "$FLAG $REASON"
  177. }
  178. function msg() {
  179. local TEXT="$1"
  180. echo -e "$TEXT"
  181. }
  182. function cleanup_ctid() {
  183. if $(pct status $CTID &>/dev/null); then
  184. if [ "$(pct status $CTID | awk '{print $2}')" == "running" ]; then
  185. pct stop $CTID
  186. fi
  187. pct destroy $CTID
  188. elif [ "$(pvesm list $STORAGE --vmid $CTID)" != "" ]; then
  189. pvesm free $ROOTFS
  190. fi
  191. }
  192. function cleanup() {
  193. popd >/dev/null
  194. rm -rf $TEMP_DIR
  195. }
  196. if [ "$IM" == "1" ]; then
  197. FEATURES="nesting=1,keyctl=1,mknod=1"
  198. else
  199. FEATURES="nesting=1"
  200. fi
  201. TEMP_DIR=$(mktemp -d)
  202. pushd $TEMP_DIR >/dev/null
  203. export CTID=$(pvesh get /cluster/nextid)
  204. export PCT_OSTYPE=debian
  205. export PCT_OSVERSION=11
  206. export PCT_DISK_SIZE=4
  207. export PCT_OPTIONS="
  208. -features $FEATURES
  209. -hostname docker
  210. -net0 name=eth0,bridge=vmbr0,ip=dhcp
  211. -onboot 1
  212. -cores 2
  213. -memory 2048
  214. -unprivileged ${IM}
  215. ${PW}
  216. "
  217. bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/ct/create_lxc.sh)" || exit
  218. STORAGE_TYPE=$(pvesm status -storage $(pct config $CTID | grep rootfs | awk -F ":" '{print $2}') | awk 'NR>1 {print $2}')
  219. if [ "$STORAGE_TYPE" == "zfspool" ]; then
  220. ${SD}wget -qL -O fuse-overlayfs https://github.com/containers/fuse-overlayfs/releases/download/v1.8.2/fuse-overlayfs-x86_64
  221. warn "Some addons may not work due to ZFS not supporting 'fallocate'."
  222. fi
  223. LXC_CONFIG=/etc/pve/lxc/${CTID}.conf
  224. cat <<EOF >> $LXC_CONFIG
  225. lxc.cgroup2.devices.allow: a
  226. lxc.cap.drop:
  227. EOF
  228. echo -en "${GN} Starting LXC Container... "
  229. pct start $CTID
  230. ${SD}if [ "$STORAGE_TYPE" == "zfspool" ]; then
  231. ${SD}pct push $CTID fuse-overlayfs /usr/local/bin/fuse-overlayfs -perms 755
  232. ${SD}info "${BL}Using fuse-overlayfs.${CL}"
  233. ${SD}fi
  234. echo -e "${CM}${CL} \r"
  235. alias lxc-cmd="lxc-attach -n $CTID --"
  236. lxc-cmd bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/setup/docker-install.sh)" || exit
  237. IP=$(pct exec $CTID ip a s dev eth0 | sed -n '/inet / s/\// /p' | awk '{print $2}')
  238. echo -e "${GN}Successfully created Docker LXC to${CL} ${BL}$CTID${CL}. \n"