1
0

samba.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env bash
  2. set -Eeuo pipefail
  3. : "${SAMBA:="Y"}"
  4. [[ "$SAMBA" == [Nn]* ]] && return 0
  5. [[ "$NETWORK" == [Nn]* ]] && return 0
  6. hostname="host.lan"
  7. interface="dockerbridge"
  8. if [[ "$DHCP" == [Yy1]* ]]; then
  9. hostname="$IP"
  10. interface="$VM_NET_DEV"
  11. fi
  12. if [[ "${NETWORK,,}" == "user"* ]]; then
  13. interface="127.0.0.1"
  14. fi
  15. addShare() {
  16. local dir="$1"
  17. local name="$2"
  18. local comment="$3"
  19. mkdir -p "$dir" || return 1
  20. if [ -z "$(ls -A "$dir")" ]; then
  21. chmod 777 "$dir"
  22. { echo "--------------------------------------------------------"
  23. echo " $APP for Docker v$(</run/version)..."
  24. echo " For support visit $SUPPORT"
  25. echo "--------------------------------------------------------"
  26. echo ""
  27. echo "Using this folder you can share files with the host machine."
  28. echo ""
  29. echo "To change its location, include the following bind mount in your compose file:"
  30. echo ""
  31. echo " volumes:"
  32. echo " - \"/home/example:/${name,,}\""
  33. echo ""
  34. echo "Or in your run command:"
  35. echo ""
  36. echo " -v \"/home/example:/${name,,}\""
  37. echo ""
  38. echo "Replace the example path /home/example with the desired shared folder."
  39. echo ""
  40. } | unix2dos > "$dir/readme.txt"
  41. fi
  42. { echo ""
  43. echo "[$name]"
  44. echo " path = $dir"
  45. echo " comment = $comment"
  46. echo " writable = yes"
  47. echo " guest ok = yes"
  48. echo " guest only = yes"
  49. echo " force user = root"
  50. echo " force group = root"
  51. } >> "/etc/samba/smb.conf"
  52. return 0
  53. }
  54. { echo "[global]"
  55. echo " server string = Dockur"
  56. echo " netbios name = $hostname"
  57. echo " workgroup = WORKGROUP"
  58. echo " interfaces = $interface"
  59. echo " bind interfaces only = yes"
  60. echo " security = user"
  61. echo " guest account = nobody"
  62. echo " map to guest = Bad User"
  63. echo " server min protocol = NT1"
  64. echo " follow symlinks = yes"
  65. echo " wide links = yes"
  66. echo " unix extensions = no"
  67. echo ""
  68. echo " # disable printing services"
  69. echo " load printers = no"
  70. echo " printing = bsd"
  71. echo " printcap name = /dev/null"
  72. echo " disable spoolss = yes"
  73. } > "/etc/samba/smb.conf"
  74. share="/data"
  75. [ ! -d "$share" ] && [ -d "$STORAGE/data" ] && share="$STORAGE/data"
  76. [ ! -d "$share" ] && [ -d "/shared" ] && share="/shared"
  77. [ ! -d "$share" ] && [ -d "$STORAGE/shared" ] && share="$STORAGE/shared"
  78. addShare "$share" "Data" "Shared" || error "Failed to create shared folder!"
  79. [ -d "/data2" ] && addShare "/data2" "Data2" "Shared"
  80. [ -d "/data3" ] && addShare "/data3" "Data3" "Shared"
  81. IFS=',' read -r -a dirs <<< "${SHARES:-}"
  82. for dir in "${dirs[@]}"; do
  83. [ ! -d "$dir" ] && continue
  84. dir_name=$(basename "$dir")
  85. addShare "$dir" "$dir_name" "Shared $dir_name" || error "Failed to create shared folder for $dir!"
  86. done
  87. # Fix Samba permissions
  88. [ -d /run/samba/msg.lock ] && chmod -R 0755 /run/samba/msg.lock
  89. [ -d /var/log/samba/cores ] && chmod -R 0700 /var/log/samba/cores
  90. [ -d /var/cache/samba/msg.lock ] && chmod -R 0755 /var/cache/samba/msg.lock
  91. if ! smbd; then
  92. error "Samba daemon failed to start!"
  93. smbd -i --debug-stdout || true
  94. else
  95. if [[ "${NETWORK,,}" == "user"* ]]; then
  96. NET_OPTS="${NET_OPTS/,hostfwd/,guestfwd=tcp:${VM_NET_IP%.*}.1:445-tcp:127.0.0.1:445,hostfwd}"
  97. fi
  98. fi
  99. [[ "${NETWORK,,}" == "user"* ]] && return 0
  100. if [[ "${BOOT_MODE:-}" == "windows_legacy" ]]; then
  101. # Enable NetBIOS on Windows 7 and lower
  102. if ! nmbd; then
  103. error "NetBIOS daemon failed to start!"
  104. nmbd -i --debug-stdout || true
  105. fi
  106. else
  107. # Enable Web Service Discovery on Vista and up
  108. wsdd -i "$interface" -p -n "$hostname" &
  109. echo "$!" > /var/run/wsdd.pid
  110. fi
  111. return 0