-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperry-nas-setup.sh
More file actions
232 lines (198 loc) · 7.13 KB
/
perry-nas-setup.sh
File metadata and controls
232 lines (198 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/bin/bash
# Perry-NAS Setup Script – Raspberry Pi OS Trixie Version (v2.0)
# Fokus: PHP 8.4, Stabilität des Webinterfaces und UUID-Nutzung
set -e
# Farbdefinitionen
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m'
print_perry() { echo -e "${PURPLE}[PERRY-NAS]${NC} $1"; }
print_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1"; }
print_error() { echo -e "${RED}[ERROR]${NC} $1"; }
# Banner
echo ""
echo -e "${PURPLE}#############################################${NC}"
echo -e "${PURPLE}# PERRY-NAS (V2.0) #${NC}"
echo -e "${PURPLE}# Raspberry Pi 5 NAS Setup (Trixie) #${NC}"
echo -e "${PURPLE}# mit PCIe SATA & Webinterface #${NC}"
echo -e "${PURPLE}#############################################${NC}"
echo ""
# Root Check
if [ "$EUID" -ne 0 ]; then
print_error "Bitte als root ausführen: sudo $0"
exit 1
fi
# --- 1. System und Benutzer-Konfiguration ---
print_perry "1. Benutzer-Konfiguration und Hostname"
read -p "Benutzername für NAS und Samba (z.B. nasuser): " PERRY_USER
PERRY_HOSTNAME="perry-nas"
echo "$PERRY_HOSTNAME" > /etc/hostname
sed -i "s/127.0.1.1.*/127.0.1.1\t$PERRY_HOSTNAME/g" /etc/hosts
print_success "Hostname gesetzt: $PERRY_HOSTNAME"
# System Update
print_perry "2. System-Aktualisierung und Pakete installieren"
apt update
apt full-upgrade -y
apt autoremove -y
# zusätzliche Treiber für Windows-Dateisysteme
apt install ntfs-3g
apt install exfat-fuse
# PHP 8.4 ist hier als Beispiel gesetzt; falls nicht verfügbar, bitte auf 8.3 ändern!
PHP_VERSION="8.4"
apt install -y parted nginx php${PHP_VERSION}-fpm php${PHP_VERSION}-cli samba ufw curl bc hdparm
print_success "Alle notwendigen Pakete (inkl. PHP ${PHP_VERSION}) installiert."
# --- 2. Festplatte erkennen und mounten ---
print_perry "3. Hardware-Setup und Dateisystem-Check"
lsblk -f
read -p "Bitte Device Name der SATA-Platte (z.B. sda): " DISK
if [ ! -e "/dev/$DISK" ]; then
print_error "Gerät /dev/$DISK existiert nicht."
exit 1
fi
# Partition ermitteln (wir nehmen die erste)
PART="/dev/${DISK}1"
if [ ! -e "$PART" ]; then
print_warning "Keine Partition auf $PART gefunden. Erstelle neue Partition..."
parted /dev/$DISK --script mklabel gpt
parted /dev/$DISK --script mkpart primary ext4 0% 100%
mkfs.ext4 -F $PART
FORMAT_CONFIRM="ja"
else
# Vorhandenes Dateisystem auslesen
FSTYPE=$(lsblk -no FSTYPE $PART)
print_info "Gefundenes Dateisystem auf $PART: ${FSTYPE:-UNBEKANNT}"
read -p "Soll $PART neu als ext4 FORMATIERT werden? (Daten gehen verloren!) (ja/NEIN): " FORMAT_CONFIRM
if [ "$FORMAT_CONFIRM" == "ja" ]; then
umount "$PART"* 2>/dev/null || true
mkfs.ext4 -F $PART
FSTYPE="ext4"
fi
fi
# UUID und Mount-Optionen festlegen
DISK_UUID=$(blkid -s UUID -o value $PART)
case $FSTYPE in
ntfs|ntfs-3g) MOUNT_OPTS="defaults,permissions,noatime,nofail" ;;
vfat|exfat) MOUNT_OPTS="defaults,uid=$(id -u $PERRY_USER),gid=$(id -g $PERRY_USER),nofail" ;;
*) MOUNT_OPTS="defaults,noatime,nofail" ;;
esac
# In fstab eintragen (Duplikate verhindern)
mkdir -p /mnt/perry-nas
if ! grep -q "$DISK_UUID" /etc/fstab; then
echo "UUID=$DISK_UUID /mnt/perry-nas $FSTYPE $MOUNT_OPTS 0 2" >> /etc/fstab
print_success "Eintrag in fstab erstellt ($FSTYPE)."
else
print_info "Eintrag bereits vorhanden. Aktualisiere nur Mount-Optionen..."
sed -i "s|.*$DISK_UUID.*|UUID=$DISK_UUID /mnt/perry-nas $FSTYPE $MOUNT_OPTS 0 2|" /etc/fstab
fi
mount -a
print_success "Festplatte ist unter /mnt/perry-nas einsatzbereit!"
# --- 3. Benutzer und Berechtigungen ---
print_perry "4. Benutzer erstellen und Berechtigungen setzen"
if ! id "$PERRY_USER" &>/dev/null; then
useradd -m -s /bin/bash "$PERRY_USER"
echo "Passwort für $PERRY_USER:";
passwd "$PERRY_USER"
fi
# Setze Berechtigungen für das NAS-Mount (Eigentümer ist NAS-User)
chown -R $PERRY_USER:$PERRY_USER /mnt/perry-nas
chmod -R 775 /mnt/perry-nas # Gut für Samba-Freigaben
# --- 4. Samba Konfiguration ---
print_perry "5. Samba wird konfiguriert..."
cat > /etc/samba/smb.conf << EOF
[global]
workgroup = WORKGROUP
server string = Perry-NAS
security = user
map to guest = bad user
server min protocol = SMB2
server max protocol = SMB3
[Perry-NAS]
path = /mnt/perry-nas
browseable = yes
writable = yes
valid users = $PERRY_USER
force user = $PERRY_USER
create mask = 0775
directory mask = 0775
EOF
print_info "Samba Passwort für $PERRY_USER setzen..."
smbpasswd -a "$PERRY_USER"
systemctl enable smbd
systemctl restart smbd
print_success "Samba läuft."
# --- 5. Webinterface (Nginx & PHP) ---
print_perry "6. Webinterface-Konfiguration (Nginx und PHP-FPM)"
rm -f /var/www/html/index.nginx-debian.html
# WICHTIG: Korrekte Berechtigungen für Nginx/PHP-FPM, um Dateien zu lesen
chown -R www-data:www-data /var/www/html
chmod -R 755 /var/www/html
# Nginx Konfiguration (mit optimierter PHP-Socket-Pfad-Handhabung)
cat > /etc/nginx/sites-available/default << EOF
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm; # Index.htm hinzugefügt
location / {
try_files \$uri \$uri/ =404;
}
# WICHTIG: Sicherstellen, dass der fastcgi_pass-Pfad korrekt ist.
location ~ \.php\$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php${PHP_VERSION}-fpm.sock;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
include fastcgi_params;
}
}
EOF
# Webinterface Status-Datei (index.php)
cat > /var/www/html/index.php << 'EOF'
<?php
// PHP-Sicherheit: Nur die Ausgabe von Statusinformationen, keine Nutzereingaben verarbeitet
$disk_status = shell_exec('df -h /mnt/perry-nas');
$ram_status = shell_exec('free -h');
$load_avg = print_r(sys_getloadavg(), true);
?>
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title>Perry-NAS Status</title></head>
<body style="font-family:Arial;background:#eee;padding:20px;">
<h1>🍐 Perry-NAS Status</h1>
<pre>
Hostname: <?php echo shell_exec('hostname'); ?>
Uptime: <?php echo shell_exec('uptime -p'); ?>
--- Speicherstatus ---
Festplatte (/mnt/perry-nas):
<?php echo $disk_status; ?>
RAM:
<?php echo $ram_status; ?>
Last (Load Average):
<?php echo $load_avg; ?>
</pre>
</body></html>
EOF
systemctl restart nginx
systemctl restart php${PHP_VERSION}-fpm
print_success "Webinterface (Nginx und PHP-FPM) neu gestartet."
# --- 6. Firewall Konfiguration ---
print_perry "7. Firewall (UFW) wird konfiguriert"
ufw --force enable
ufw allow ssh
ufw allow http
ufw allow samba
ufw status verbose
# --- 7. Finaler Status ---
print_perry "8. Perry-NAS Abschlussbericht"
nginx -t && print_success "Nginx Konfiguration OK"
print_info "Festplatten-Performance (Kurztest):"
hdparm -Tt /dev/${DISK}1 | head -5 || true
PERRY_IP=$(hostname -I | awk '{print $1}')
print_success "PERRY-NAS Setup abgeschlossen!"
print_info "Webinterface ist verfügbar unter: http://$PERRY_IP/"
print_info "NAS-Freigabe (Samba) ist erreichbar unter: //$PERRY_HOSTNAME/Perry-NAS"