samba.sh 4.9 KB

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