Setup installation

This commit is contained in:
2025-08-07 19:28:22 +02:00
parent e3f6363844
commit 17d74b261d
19 changed files with 6144 additions and 0 deletions

View File

@@ -0,0 +1,490 @@
#!/bin/bash
# USB-SSD Management System - SMB Setup Script
# Automatische Konfiguration von Samba für USB-C SSD Freigaben
#
# Verwendung:
# sudo ./setup-smb.sh [OPTIONS]
#
# Optionen:
# --install-samba Samba-Pakete installieren
# --configure-users Benutzer und Gruppen konfigurieren
# --setup-shares Freigaben konfigurieren
# --enable-services Services aktivieren und starten
# --all Alle Schritte ausführen
set -euo pipefail
# ============================================================================
# Konfiguration
# ============================================================================
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SMB_CONF="/etc/samba/smb.conf"
SMB_CONF_BACKUP="/etc/samba/smb.conf.backup.$(date +%Y%m%d_%H%M%S)"
SSD_MOUNT_POINT="/mnt/ssd-storage"
LOG_FILE="/var/log/ssd-smb-setup.log"
# Farben für Output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# ============================================================================
# Logging-Funktionen
# ============================================================================
log() {
echo -e "${GREEN}[$(date '+%Y-%m-%d %H:%M:%S')] $1${NC}" | tee -a "$LOG_FILE"
}
warn() {
echo -e "${YELLOW}[$(date '+%Y-%m-%d %H:%M:%S')] WARNING: $1${NC}" | tee -a "$LOG_FILE"
}
error() {
echo -e "${RED}[$(date '+%Y-%m-%d %H:%M:%S')] ERROR: $1${NC}" | tee -a "$LOG_FILE"
exit 1
}
info() {
echo -e "${BLUE}[$(date '+%Y-%m-%d %H:%M:%S')] INFO: $1${NC}" | tee -a "$LOG_FILE"
}
# ============================================================================
# Hilfsfunktionen
# ============================================================================
check_root() {
if [[ $EUID -ne 0 ]]; then
error "Dieses Script muss als root ausgeführt werden (sudo)"
fi
}
check_mount_point() {
if [[ ! -d "$SSD_MOUNT_POINT" ]]; then
warn "Mount-Point $SSD_MOUNT_POINT existiert nicht - wird erstellt"
mkdir -p "$SSD_MOUNT_POINT"
chmod 755 "$SSD_MOUNT_POINT"
fi
}
detect_os() {
if [[ -f /etc/os-release ]]; then
. /etc/os-release
OS=$ID
VERSION=$VERSION_ID
else
error "Betriebssystem konnte nicht erkannt werden"
fi
log "Erkanntes OS: $OS $VERSION"
}
# ============================================================================
# Samba-Installation
# ============================================================================
install_samba() {
log "Installiere Samba-Pakete..."
case "$OS" in
ubuntu|debian)
apt update
apt install -y samba samba-common-bin smbclient cifs-utils
;;
centos|rhel|fedora)
if command -v dnf &> /dev/null; then
dnf install -y samba samba-common samba-client cifs-utils
else
yum install -y samba samba-common samba-client cifs-utils
fi
;;
*)
error "Nicht unterstütztes Betriebssystem: $OS"
;;
esac
log "Samba-Installation abgeschlossen"
}
# ============================================================================
# Benutzer und Gruppen
# ============================================================================
configure_users() {
log "Konfiguriere Benutzer und Gruppen..."
# Gruppen erstellen
local groups=(
"ssd-users:Benutzer mit SSD-Zugriff"
"ssd-admins:SSD-Administratoren"
"ssd-readonly:Nur-Lese-Zugriff auf SSD"
"backup-users:Backup-Benutzer"
"backup-admins:Backup-Administratoren"
"media-users:Media-Benutzer"
"developers:Entwickler"
"dev-leads:Entwicklungsleiter"
"archive-admins:Archiv-Administratoren"
)
for group_info in "${groups[@]}"; do
local group_name="${group_info%%:*}"
local group_desc="${group_info##*:}"
if ! getent group "$group_name" &>/dev/null; then
groupadd "$group_name"
log "Gruppe erstellt: $group_name ($group_desc)"
else
info "Gruppe existiert bereits: $group_name"
fi
done
# Standard-Benutzer zu ssd-users hinzufügen
if [[ -n "${SUDO_USER:-}" ]]; then
usermod -a -G ssd-users "$SUDO_USER"
log "Benutzer $SUDO_USER zu ssd-users hinzugefügt"
fi
# Service-Benutzer erstellen
if ! id "ssd-service" &>/dev/null; then
useradd -r -s /bin/false -d /var/lib/ssd-service -c "SSD Service User" ssd-service
usermod -a -G ssd-users ssd-service
log "Service-Benutzer ssd-service erstellt"
fi
log "Benutzer- und Gruppen-Konfiguration abgeschlossen"
}
# ============================================================================
# Verzeichnis-Struktur
# ============================================================================
setup_directories() {
log "Erstelle Verzeichnis-Struktur..."
local directories=(
"$SSD_MOUNT_POINT"
"$SSD_MOUNT_POINT/backup"
"$SSD_MOUNT_POINT/archive"
"$SSD_MOUNT_POINT/media"
"$SSD_MOUNT_POINT/development"
"$SSD_MOUNT_POINT/temp"
"$SSD_MOUNT_POINT/home"
)
for dir in "${directories[@]}"; do
if [[ ! -d "$dir" ]]; then
mkdir -p "$dir"
log "Verzeichnis erstellt: $dir"
fi
done
# Berechtigungen setzen
chown -R root:ssd-users "$SSD_MOUNT_POINT"
chmod -R 775 "$SSD_MOUNT_POINT"
# Spezielle Berechtigungen
chmod 755 "$SSD_MOUNT_POINT/backup"
chown root:backup-users "$SSD_MOUNT_POINT/backup"
chmod 755 "$SSD_MOUNT_POINT/archive"
chown root:archive-admins "$SSD_MOUNT_POINT/archive"
chmod 777 "$SSD_MOUNT_POINT/temp"
log "Verzeichnis-Struktur konfiguriert"
}
# ============================================================================
# SMB-Konfiguration
# ============================================================================
backup_smb_config() {
if [[ -f "$SMB_CONF" ]]; then
cp "$SMB_CONF" "$SMB_CONF_BACKUP"
log "SMB-Konfiguration gesichert: $SMB_CONF_BACKUP"
fi
}
setup_smb_config() {
log "Konfiguriere Samba..."
backup_smb_config
# Neue Konfiguration kopieren
if [[ -f "$SCRIPT_DIR/smb.conf.example" ]]; then
cp "$SCRIPT_DIR/smb.conf.example" "$SMB_CONF"
log "SMB-Konfiguration installiert"
else
error "SMB-Konfigurationsdatei nicht gefunden: $SCRIPT_DIR/smb.conf.example"
fi
# Mount-Point in Konfiguration anpassen
sed -i "s|/mnt/ssd-storage|$SSD_MOUNT_POINT|g" "$SMB_CONF"
# Konfiguration validieren
if testparm -s "$SMB_CONF" &>/dev/null; then
log "SMB-Konfiguration ist gültig"
else
error "SMB-Konfiguration ist ungültig"
fi
log "SMB-Konfiguration abgeschlossen"
}
# ============================================================================
# Service-Konfiguration
# ============================================================================
enable_services() {
log "Aktiviere und starte Services..."
local services=("smbd" "nmbd")
for service in "${services[@]}"; do
systemctl enable "$service"
systemctl restart "$service"
if systemctl is-active --quiet "$service"; then
log "Service $service ist aktiv"
else
error "Service $service konnte nicht gestartet werden"
fi
done
# Firewall-Konfiguration (falls UFW aktiv)
if command -v ufw &> /dev/null && ufw status | grep -q "Status: active"; then
ufw allow samba
log "Firewall-Regel für Samba hinzugefügt"
fi
log "Service-Konfiguration abgeschlossen"
}
# ============================================================================
# Benutzer-Setup
# ============================================================================
setup_samba_users() {
log "Konfiguriere Samba-Benutzer..."
# Aktueller Benutzer (falls vorhanden)
if [[ -n "${SUDO_USER:-}" ]]; then
echo "Samba-Passwort für Benutzer $SUDO_USER setzen:"
smbpasswd -a "$SUDO_USER"
smbpasswd -e "$SUDO_USER"
log "Samba-Benutzer $SUDO_USER konfiguriert"
fi
# Service-Benutzer (ohne Passwort-Login)
smbpasswd -a ssd-service -n
smbpasswd -d ssd-service
log "Samba-Benutzer-Konfiguration abgeschlossen"
}
# ============================================================================
# Tests und Validierung
# ============================================================================
test_smb_config() {
log "Teste SMB-Konfiguration..."
# Konfiguration testen
if ! testparm -s &>/dev/null; then
error "SMB-Konfiguration ist ungültig"
fi
# Services testen
for service in smbd nmbd; do
if ! systemctl is-active --quiet "$service"; then
error "Service $service ist nicht aktiv"
fi
done
# Freigaben testen
if ! smbclient -L localhost -N &>/dev/null; then
warn "SMB-Freigaben sind nicht erreichbar"
else
log "SMB-Freigaben sind erreichbar"
fi
log "SMB-Tests abgeschlossen"
}
# ============================================================================
# Cleanup und Wartung
# ============================================================================
setup_maintenance() {
log "Konfiguriere Wartungs-Scripts..."
# Temp-Verzeichnis Cleanup
cat > /etc/cron.daily/ssd-temp-cleanup << 'EOF'
#!/bin/bash
# Bereinige temporäre SSD-Dateien (älter als 7 Tage)
find /mnt/ssd-storage/temp -type f -mtime +7 -delete 2>/dev/null || true
find /mnt/ssd-storage/temp -type d -empty -delete 2>/dev/null || true
EOF
chmod +x /etc/cron.daily/ssd-temp-cleanup
# Log-Rotation für SMB-Logs
cat > /etc/logrotate.d/ssd-smb << 'EOF'
/var/log/samba/*.log {
daily
rotate 30
compress
delaycompress
missingok
notifempty
create 644 root root
postrotate
systemctl reload smbd nmbd 2>/dev/null || true
endscript
}
EOF
log "Wartungs-Scripts konfiguriert"
}
# ============================================================================
# Hauptfunktionen
# ============================================================================
show_usage() {
cat << EOF
USB-SSD SMB Setup Script
Verwendung: $0 [OPTIONEN]
Optionen:
--install-samba Samba-Pakete installieren
--configure-users Benutzer und Gruppen konfigurieren
--setup-shares Freigaben konfigurieren
--enable-services Services aktivieren und starten
--all Alle Schritte ausführen
--help Diese Hilfe anzeigen
Beispiele:
$0 --all # Vollständige Installation
$0 --install-samba # Nur Samba installieren
$0 --configure-users # Nur Benutzer konfigurieren
$0 --setup-shares # Nur Freigaben konfigurieren
EOF
}
main() {
local install_samba=false
local configure_users=false
local setup_shares=false
local enable_services=false
local all_steps=false
# Parameter parsen
while [[ $# -gt 0 ]]; do
case $1 in
--install-samba)
install_samba=true
shift
;;
--configure-users)
configure_users=true
shift
;;
--setup-shares)
setup_shares=true
shift
;;
--enable-services)
enable_services=true
shift
;;
--all)
all_steps=true
shift
;;
--help)
show_usage
exit 0
;;
*)
error "Unbekannte Option: $1"
;;
esac
done
# Wenn keine spezifischen Optionen, zeige Hilfe
if [[ "$install_samba" == false && "$configure_users" == false && "$setup_shares" == false && "$enable_services" == false && "$all_steps" == false ]]; then
show_usage
exit 1
fi
# Root-Berechtigung prüfen
check_root
# OS erkennen
detect_os
# Mount-Point prüfen
check_mount_point
# Log-Datei initialisieren
mkdir -p "$(dirname "$LOG_FILE")"
touch "$LOG_FILE"
log "USB-SSD SMB Setup gestartet"
# Schritte ausführen
if [[ "$all_steps" == true || "$install_samba" == true ]]; then
install_samba
fi
if [[ "$all_steps" == true || "$configure_users" == true ]]; then
configure_users
setup_directories
fi
if [[ "$all_steps" == true || "$setup_shares" == true ]]; then
setup_smb_config
setup_samba_users
setup_maintenance
fi
if [[ "$all_steps" == true || "$enable_services" == true ]]; then
enable_services
test_smb_config
fi
log "USB-SSD SMB Setup abgeschlossen"
# Zusammenfassung
cat << EOF
${GREEN}=== Setup-Zusammenfassung ===${NC}
SMB-Freigaben:
- //$(hostname)/ssd-storage (Haupt-Freigabe)
- //$(hostname)/ssd-backup (Backup-Speicher)
- //$(hostname)/ssd-archive (Archiv, Read-Only)
- //$(hostname)/ssd-media (Media-Dateien)
- //$(hostname)/ssd-dev (Entwicklung)
- //$(hostname)/ssd-temp (Temporäre Dateien)
Nächste Schritte:
1. USB-SSD anschließen und mounten: ssd-mount-manager.sh mount
2. SMB-Freigaben testen: smbclient -L localhost -U $USER
3. Von Windows: \\\\$(hostname)\\ssd-storage
Log-Datei: $LOG_FILE
Konfiguration: $SMB_CONF
Backup: $SMB_CONF_BACKUP
EOF
}
# Script ausführen
main "$@"

View File

@@ -0,0 +1,338 @@
# USB-SSD Management System - SMB Configuration Example
# Optimierte Samba-Konfiguration für USB-C SSD Freigaben
#
# Installation:
# sudo cp smb.conf.example /etc/samba/smb.conf
# sudo systemctl restart smbd nmbd
# sudo smbpasswd -a $USER
# ============================================================================
# Global Configuration
# ============================================================================
[global]
# Server-Identifikation
workgroup = WORKGROUP
server string = USB-SSD File Server
netbios name = SSD-SERVER
# Protokoll-Versionen (SMB2/3 für bessere Performance)
server min protocol = SMB2
server max protocol = SMB3
client min protocol = SMB2
client max protocol = SMB3
# Security-Konfiguration
security = user
map to guest = bad user
guest account = nobody
# Logging
log file = /var/log/samba/log.%m
log level = 1
max log size = 1000
# Performance-Optimierungen
socket options = TCP_NODELAY IPTOS_LOWDELAY SO_RCVBUF=131072 SO_SNDBUF=131072
read raw = yes
write raw = yes
max xmit = 65535
dead time = 15
getwd cache = yes
# Unicode und Charset
unix charset = UTF-8
display charset = UTF-8
dos charset = CP850
# Erweiterte Attribute für NTFS-Kompatibilität
ea support = yes
store dos attributes = yes
map archive = no
map hidden = no
map read only = no
map system = no
# Locking-Optimierungen
kernel oplocks = no
level2 oplocks = yes
oplocks = yes
# Name-Resolution
name resolve order = lmhosts wins bcast host
dns proxy = no
# Printing (deaktiviert für SSD-Server)
load printers = no
printing = bsd
printcap name = /dev/null
disable spoolss = yes
# ============================================================================
# USB-SSD Storage Share
# ============================================================================
[ssd-storage]
# Basis-Konfiguration
comment = USB-C SSD Storage
path = /mnt/ssd-storage
browseable = yes
writable = yes
guest ok = no
# Benutzer-Zugriff
valid users = @ssd-users, @disk
admin users = @ssd-admins
write list = @ssd-users
read list = @ssd-users, @ssd-readonly
# Permissions
create mask = 0664
directory mask = 0775
force create mode = 0664
force directory mode = 0775
force user = ssd-user
force group = ssd-users
# Performance-Optimierungen für USB-SSD
strict allocate = yes
allocation roundup size = 1048576
read raw = yes
write raw = yes
# NTFS-Kompatibilität
store dos attributes = yes
map archive = no
map hidden = no
map read only = no
map system = no
# Erweiterte Attribute
ea support = yes
acl allow execute always = yes
# Locking für Multi-User Zugriff
oplocks = yes
level2 oplocks = yes
kernel oplocks = no
locking = yes
strict locking = no
# Veto-Dateien (Systemdateien ausschließen)
veto files = /._*/.DS_Store/Thumbs.db/desktop.ini/
delete veto files = yes
# Recycle-Bin (optional)
# vfs objects = recycle
# recycle:repository = .recycle
# recycle:keeptree = yes
# recycle:versions = yes
# ============================================================================
# Backup-spezifische Freigabe
# ============================================================================
[ssd-backup]
comment = USB-SSD Backup Storage
path = /mnt/ssd-storage/backup
browseable = yes
writable = yes
guest ok = no
# Nur Backup-Benutzer
valid users = @backup-users
admin users = @backup-admins
write list = @backup-users
# Restriktive Permissions
create mask = 0640
directory mask = 0750
force create mode = 0640
force directory mode = 0750
# Backup-optimierte Einstellungen
strict allocate = yes
sync always = yes
strict sync = yes
# Audit-Logging
full_audit:prefix = %u|%I|%S
full_audit:success = open opendir write unlink mkdir rmdir rename
full_audit:failure = all
vfs objects = full_audit
# ============================================================================
# Read-Only Archive Share
# ============================================================================
[ssd-archive]
comment = USB-SSD Archive (Read-Only)
path = /mnt/ssd-storage/archive
browseable = yes
writable = no
guest ok = yes
# Read-Only Zugriff
read only = yes
write list = @archive-admins
# Optimiert für große Dateien
read raw = yes
large readwrite = yes
# Caching für bessere Performance
kernel share modes = yes
posix locking = no
# ============================================================================
# Media-Server Integration
# ============================================================================
[ssd-media]
comment = USB-SSD Media Storage
path = /mnt/ssd-storage/media
browseable = yes
writable = yes
guest ok = yes
# Media-optimierte Einstellungen
valid users = @media-users, guest
write list = @media-admins
# Große Dateien optimiert
read raw = yes
write raw = yes
large readwrite = yes
# Media-spezifische Veto-Files
veto files = /._*/.DS_Store/Thumbs.db/.@__thumb/
delete veto files = yes
# Streaming-Optimierungen
strict allocate = no
allocation roundup size = 4096
# DLNA/UPnP Kompatibilität
store dos attributes = no
map archive = no
map hidden = no
# ============================================================================
# Development Share
# ============================================================================
[ssd-dev]
comment = USB-SSD Development Storage
path = /mnt/ssd-storage/development
browseable = yes
writable = yes
guest ok = no
# Entwickler-Zugriff
valid users = @developers
admin users = @dev-leads
write list = @developers
# Entwickler-freundliche Permissions
create mask = 0664
directory mask = 0775
force create mode = 0664
force directory mode = 0775
# Git-Repository Unterstützung
store dos attributes = no
map archive = no
map hidden = no
map read only = no
map system = no
# Symlink-Unterstützung
follow symlinks = yes
wide links = no
unix extensions = yes
# Case-Sensitivity für Entwicklung
case sensitive = auto
default case = lower
preserve case = yes
short preserve case = yes
# ============================================================================
# Temporäre Freigabe
# ============================================================================
[ssd-temp]
comment = USB-SSD Temporary Storage
path = /mnt/ssd-storage/temp
browseable = yes
writable = yes
guest ok = yes
# Temporärer Zugriff
valid users = @ssd-users, guest
# Temporäre Dateien
create mask = 0666
directory mask = 0777
# Automatische Bereinigung (via Cron)
# 0 2 * * * find /mnt/ssd-storage/temp -type f -mtime +7 -delete
# Keine Attribute speichern
store dos attributes = no
ea support = no
# ============================================================================
# Administrative Freigabe
# ============================================================================
[ssd-admin]
comment = USB-SSD Administrative Access
path = /mnt/ssd-storage
browseable = no
writable = yes
guest ok = no
# Nur Administratoren
valid users = @ssd-admins
admin users = @ssd-admins
# Vollzugriff
create mask = 0664
directory mask = 0775
# Audit-Logging für Admin-Zugriffe
full_audit:prefix = ADMIN|%u|%I|%S
full_audit:success = all
full_audit:failure = all
vfs objects = full_audit
# Erweiterte Funktionen
ea support = yes
acl allow execute always = yes
nt acl support = yes
# ============================================================================
# Homes-Integration (optional)
# ============================================================================
[homes]
comment = Home Directories on USB-SSD
browseable = no
writable = yes
guest ok = no
# Benutzer-spezifische Pfade
path = /mnt/ssd-storage/home/%S
valid users = %S
# Standard-Permissions
create mask = 0600
directory mask = 0700
# Privacy-Einstellungen
hide dot files = yes
hide files = /desktop.ini/thumbs.db/
# Quota-Unterstützung (falls aktiviert)
# preexec = /usr/local/bin/setup-user-quota.sh %S