Update netdrivelink.sh

This commit is contained in:
edemilov committed 2026-04-18 12:27:30 +00:00
1 parent d46650b30e
commit 8a3dbc0117
1 file changed
+192 -265
+192 -265
View File
@@ -1,285 +1,212 @@
#!/bin/bash
# ═══════════════════════════════════════════════════════════════════
# netdrivelink.sh
# vanga homelab — Samba mounts + 2nd drive symlinks
# Works on: Arch, Debian, Ubuntu, Parrot, Kali, Mint
#
# curl -s https://git.edemilov.ru/edemilov/scripts/raw/branch/main/netdrivelink.sh | bash
# OR for interactive (recommended first time):
# bash <(curl -s https://git.edemilov.ru/edemilov/scripts/raw/branch/main/netdrivelink.sh)
# ═══════════════════════════════════════════════════════════════════
# Universal Linux Setup Script
# Works on: Debian, Ubuntu, Parrot, Arch, Fedora, openSUSE, etc.
# Run after fresh install: bash <(curl -s https://git.edemilov.ru/edemilov/scripts/raw/main/setup-universal.sh)
set -e
# ── Colors ──────────────────────────────────────────────────────────
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'
CYAN='\033[0;36m'; BOLD='\033[1m'; NC='\033[0m'
info() { echo -e "${CYAN}[•]${NC} $1"; }
success() { echo -e "${GREEN}[✓]${NC} $1"; }
warn() { echo -e "${YELLOW}[!]${NC} $1"; }
die() { echo -e "${RED}[✗]${NC} $1"; exit 1; }
header() { echo -e "\n${BOLD}${CYAN}── $1 ──────────────────────────────${NC}"; }
# ── Root check ──────────────────────────────────────────────────────
[[ $EUID -ne 0 ]] && die "Run as root: sudo bash netdrivelink.sh"
echo -e "${BOLD}${CYAN}═══════════════════════════════════════════════════${NC}"
echo -e "${BOLD}${CYAN} Universal Linux Setup - Works on Any Distro${NC}"
echo -e "${BOLD}${CYAN}═══════════════════════════════════════════════════${NC}"
echo -e "${BOLD}"
echo " ╔══════════════════════════════════════════════╗"
echo " ║ vanga homelab — netdrivelink.sh ║"
echo " ║ Samba mounts + 2nd drive setup ║"
echo " ╚══════════════════════════════════════════════╝"
echo -e "${NC}"
# ── Detect distro & package manager ─────────────────────────────────
header "Detecting system"
# Detect distribution
detect_distro() {
if [[ -f /etc/os-release ]]; then
if [ -f /etc/os-release ]; then
. /etc/os-release
DISTRO_ID="${ID,,}"
DISTRO_LIKE="${ID_LIKE,,}"
DISTRO=$ID
DISTRO_FAMILY=""
case $DISTRO in
debian|ubuntu|parrot|kali|linuxmint|pop) DISTRO_FAMILY="debian" ;;
fedora|rhel|centos|rocky|alma) DISTRO_FAMILY="fedora" ;;
arch|manjaro|endeavouros) DISTRO_FAMILY="arch" ;;
opensuse|suse) DISTRO_FAMILY="suse" ;;
*) DISTRO_FAMILY="unknown" ;;
esac
else
die "Cannot detect distro — /etc/os-release missing"
DISTRO="unknown"
DISTRO_FAMILY="unknown"
fi
if command -v pacman &>/dev/null; then
PKG_MGR="pacman"
INSTALL_CMD="pacman -S --noconfirm --needed"
CIFS_PKG="cifs-utils"
elif command -v apt-get &>/dev/null; then
PKG_MGR="apt"
INSTALL_CMD="apt-get install -y -qq"
CIFS_PKG="cifs-utils"
apt-get update -qq
elif command -v dnf &>/dev/null; then
PKG_MGR="dnf"
INSTALL_CMD="dnf install -y -q"
CIFS_PKG="cifs-utils"
else
die "Unsupported package manager"
fi
info "Distro: ${DISTRO_ID} | Package manager: ${PKG_MGR}"
echo -e "${CYAN}[•]${NC} Detected: ${GREEN}$DISTRO${NC} (family: $DISTRO_FAMILY)"
}
detect_distro
# ── Install cifs-utils ───────────────────────────────────────────────
if ! command -v mount.cifs &>/dev/null; then
info "Installing cifs-utils..."
$INSTALL_CMD $CIFS_PKG
success "cifs-utils installed"
else
success "cifs-utils already installed"
fi
# Install packages based on distro
install_packages() {
local packages=("$@")
echo -e "\n${CYAN}[•]${NC} Installing packages: ${packages[*]}"
case $DISTRO_FAMILY in
debian)
sudo apt update
sudo apt install -y "${packages[@]}"
;;
fedora)
sudo dnf install -y "${packages[@]}"
;;
arch)
sudo pacman -S --noconfirm "${packages[@]}"
;;
suse)
sudo zypper install -y "${packages[@]}"
;;
*)
echo -e "${YELLOW}[!]${NC} Unknown distro. Install manually: ${packages[*]}"
;;
esac
}
# ── Get current user (who will own the mounts) ──────────────────────
# Works even when script is run with sudo
REAL_USER="${SUDO_USER:-$(logname 2>/dev/null || echo $USER)}"
REAL_UID=$(id -u "$REAL_USER" 2>/dev/null || echo 1000)
REAL_GID=$(id -g "$REAL_USER" 2>/dev/null || echo 1000)
info "Mount owner: ${REAL_USER} (uid=${REAL_UID} gid=${REAL_GID})"
# Setup Tailscale (universal method)
setup_tailscale() {
echo -e "\n${CYAN}[•]${NC} Setting up Tailscale..."
if ! command -v tailscale &> /dev/null; then
case $DISTRO_FAMILY in
debian)
curl -fsSL https://tailscale.com/install.sh | sh
;;
fedora)
sudo dnf config-manager --add-repo https://pkgs.tailscale.com/stable/fedora/tailscale.repo
sudo dnf install -y tailscale
;;
arch)
sudo pacman -S --noconfirm tailscale
;;
*)
echo -e "${YELLOW}[!]${NC} Install Tailscale manually from https://tailscale.com/download"
;;
esac
fi
sudo tailscale up --accept-routes=true --accept-dns=false
sleep 2
echo -e "${GREEN}[✓]${NC} Tailscale configured"
}
# ── Samba credentials ────────────────────────────────────────────────
header "Samba credentials"
CRED_FILE="/etc/samba/credentials.emilovi"
if [[ -f "$CRED_FILE" ]]; then
warn "Credentials file already exists: $CRED_FILE"
read -rp " Overwrite? [y/N]: " OVERWRITE
[[ "${OVERWRITE,,}" != "y" ]] && info "Keeping existing credentials" || WRITE_CRED=true
else
WRITE_CRED=true
fi
if [[ "$WRITE_CRED" == "true" ]]; then
read -rp " Samba username: " SAMBA_USER
read -rsp " Samba password: " SAMBA_PASS
echo ""
mkdir -p /etc/samba
cat > "$CRED_FILE" << EOF
username=${SAMBA_USER}
password=${SAMBA_PASS}
domain=WORKGROUP
EOF
chmod 600 "$CRED_FILE"
success "Credentials saved to $CRED_FILE (permissions: 600)"
fi
# ── Common mount options (exact copy of desktop pattern) ────────────
SAMBA_SERVER="10.0.99.100"
CIFS_OPTS="credentials=${CRED_FILE},uid=${REAL_UID},gid=${REAL_GID},vers=3.0,iocharset=utf8,cache=none,nofail,noauto,x-systemd.automount,x-systemd.idle-timeout=600"
# ── Define mounts ────────────────────────────────────────────────────
# Format: "share_name|mount_point"
MOUNTS=(
# Media — 12TB HDD
"Movies|/mnt/media/movies"
"TVShows|/mnt/media/tvshows"
"Music|/mnt/media/music"
"Audiobooks|/mnt/media/audiobooks"
"Ebooks|/mnt/media/ebooks"
# Shared — 4TB NVMe
"Family-Documents|/mnt/shared/documents"
"Family-Photos|/mnt/shared/photos"
"Family-Downloads|/mnt/shared/downloads"
# Users — 4TB NVMe
"Edemilov|/mnt/users/edemilov"
"Aemilova|/mnt/users/aemilova"
"Memilova|/mnt/users/memilova"
"Nemilova|/mnt/users/nemilova"
"Ediemilov|/mnt/users/ediemilov"
# Depo — HDD
"Depo|/mnt/depo"
# Isos
"Isos|/mnt/depo/isos"
# PCBackups
"PCBackups|/mnt/pcbackups"
)
# ── Create mount dirs ────────────────────────────────────────────────
header "Creating mount directories"
for MOUNT in "${MOUNTS[@]}"; do
MPOINT="${MOUNT##*|}"
mkdir -p "$MPOINT"
info " Created: $MPOINT"
done
success "All mount directories ready"
# ── Write fstab ──────────────────────────────────────────────────────
header "Configuring /etc/fstab"
FSTAB_MARKER="# vanga homelab Samba — ${SAMBA_SERVER}"
# Remove existing vanga block if present
if grep -q "$FSTAB_MARKER" /etc/fstab; then
warn "Existing vanga fstab entries found — replacing..."
# Use python3 to safely remove the block
python3 -c "
import re
with open('/etc/fstab', 'r') as f:
content = f.read()
# Remove from marker to next blank section
content = re.sub(r'# vanga homelab.*?(?=\n\n|\Z)', '', content, flags=re.DOTALL)
with open('/etc/fstab', 'w') as f:
f.write(content)
"
fi
# Append new entries
{
echo ""
echo "# ═══════════════════════════════════════════════════════"
echo "# vanga homelab Samba — ${SAMBA_SERVER}"
echo "# Generated by netdrivelink.sh on $(date '+%Y-%m-%d %H:%M')"
echo "# ═══════════════════════════════════════════════════════"
echo "# Media — 12TB HDD"
echo "//${SAMBA_SERVER}/Movies /mnt/media/movies cifs ${CIFS_OPTS} 0 0"
echo "//${SAMBA_SERVER}/TVShows /mnt/media/tvshows cifs ${CIFS_OPTS} 0 0"
echo "//${SAMBA_SERVER}/Music /mnt/media/music cifs ${CIFS_OPTS} 0 0"
echo "//${SAMBA_SERVER}/Audiobooks /mnt/media/audiobooks cifs ${CIFS_OPTS} 0 0"
echo "//${SAMBA_SERVER}/Ebooks /mnt/media/ebooks cifs ${CIFS_OPTS} 0 0"
echo "# Shared — 4TB NVMe"
echo "//${SAMBA_SERVER}/Family-Documents /mnt/shared/documents cifs ${CIFS_OPTS} 0 0"
echo "//${SAMBA_SERVER}/Family-Photos /mnt/shared/photos cifs ${CIFS_OPTS} 0 0"
echo "//${SAMBA_SERVER}/Family-Downloads /mnt/shared/downloads cifs ${CIFS_OPTS} 0 0"
echo "# Users — 4TB NVMe"
echo "//${SAMBA_SERVER}/Edemilov /mnt/users/edemilov cifs ${CIFS_OPTS} 0 0"
echo "//${SAMBA_SERVER}/Aemilova /mnt/users/aemilova cifs ${CIFS_OPTS} 0 0"
echo "//${SAMBA_SERVER}/Memilova /mnt/users/memilova cifs ${CIFS_OPTS} 0 0"
echo "//${SAMBA_SERVER}/Nemilova /mnt/users/nemilova cifs ${CIFS_OPTS} 0 0"
echo "//${SAMBA_SERVER}/Ediemilov /mnt/users/ediemilov cifs ${CIFS_OPTS} 0 0"
echo "# Depo — HDD"
echo "//${SAMBA_SERVER}/Depo /mnt/depo cifs ${CIFS_OPTS} 0 0"
echo "//${SAMBA_SERVER}/Isos /mnt/depo/isos cifs ${CIFS_OPTS} 0 0"
echo "# PCBackups"
echo "//${SAMBA_SERVER}/PCBackups /mnt/pcbackups cifs ${CIFS_OPTS} 0 0"
} >> /etc/fstab
success "fstab updated"
# ── Reload systemd to pick up new automount units ────────────────────
systemctl daemon-reload
success "systemd daemon reloaded"
# ── Test mount (Edemilov share only) ─────────────────────────────────
header "Testing connection"
info "Testing mount of /mnt/users/edemilov..."
if timeout 10 mount /mnt/users/edemilov 2>/dev/null; then
success "Samba connection working! ✓"
umount /mnt/users/edemilov
else
warn "Test mount failed — check credentials or server reachability"
warn "Ping test: $(ping -c1 -W2 ${SAMBA_SERVER} &>/dev/null && echo OK || echo FAILED)"
fi
# ── 2nd drive setup ──────────────────────────────────────────────────
header "2nd drive — /nvme-data symlinks"
NVME_DATA="/nvme-data"
HOME_DIR="/home/${REAL_USER}"
# Check if 2nd drive is mounted
if [[ ! -d "$NVME_DATA" ]]; then
warn "/nvme-data not found — skipping 2nd drive setup"
warn "Mount your 2nd drive first, then re-run this script"
else
info "Found $NVME_DATA — setting up folder structure and symlinks..."
# Create standard folders on the nvme drive
FOLDERS=("Documents" "Downloads" "Pictures" "Videos" "Projects" "Music" "Archive" "Scripts")
for FOLDER in "${FOLDERS[@]}"; do
mkdir -p "${NVME_DATA}/${FOLDER}"
chown "${REAL_USER}:${REAL_USER}" "${NVME_DATA}/${FOLDER}" 2>/dev/null || true
done
success "Folder structure created on /nvme-data"
# Symlink ~/XDG dirs → /nvme-data/X
SYMLINKS=(
"Documents"
"Downloads"
"Pictures"
"Videos"
"Projects"
"Music"
)
info "Creating symlinks in ${HOME_DIR}..."
for LINK in "${SYMLINKS[@]}"; do
TARGET="${NVME_DATA}/${LINK}"
LINKPATH="${HOME_DIR}/${LINK}"
if [[ -L "$LINKPATH" ]]; then
warn " Symlink already exists: ${LINK} → $(readlink $LINKPATH)"
elif [[ -d "$LINKPATH" ]]; then
warn " Real directory exists: $LINKPATH"
info " Moving contents to ${TARGET}..."
mv "${LINKPATH}"/* "${TARGET}/" 2>/dev/null || true
rmdir "${LINKPATH}" 2>/dev/null || rm -rf "${LINKPATH}"
ln -s "${TARGET}" "${LINKPATH}"
chown -h "${REAL_USER}:${REAL_USER}" "${LINKPATH}"
success " Migrated & linked: ~/${LINK} → ${TARGET}"
else
ln -s "${TARGET}" "${LINKPATH}"
chown -h "${REAL_USER}:${REAL_USER}" "${LINKPATH}"
success " Linked: ~/${LINK} → ${TARGET}"
# Create fstab entry for NVMe (works on all distros)
setup_nvme_drive() {
echo -e "\n${CYAN}[•]${NC} Setting up NVMe data drive..."
# Detect NVMe drive (priority: nvme0n1p1, nvme1n1p1, etc.)
NVME_PART=""
for part in /dev/nvme0n1p1 /dev/nvme1n1p1 /dev/nvme0n1 /dev/nvme1n1; do
if [ -b "$part" ]; then
# Check if it has a filesystem
if blkid "$part" &>/dev/null || file -s "$part" 2>/dev/null | grep -q "filesystem"; then
NVME_PART="$part"
break
fi
fi
done
# Special: ~/Scripts → /nvme-data/Scripts
SCRIPTS_LINK="${HOME_DIR}/Scripts"
if [[ ! -L "$SCRIPTS_LINK" ]]; then
ln -s "${NVME_DATA}/Scripts" "${SCRIPTS_LINK}"
chown -h "${REAL_USER}:${REAL_USER}" "${SCRIPTS_LINK}"
success " Linked: ~/Scripts → /nvme-data/Scripts"
if [ -z "$NVME_PART" ]; then
echo -e "${YELLOW}[!]${NC} No NVMe drive found. Skipping..."
return
fi
fi
echo -e "${GREEN}[✓]${NC} Found NVMe: $NVME_PART"
# Create mount point
sudo mkdir -p /nvme-data
# Get filesystem type
FS_TYPE=$(blkid "$NVME_PART" -s TYPE -o value 2>/dev/null || echo "ext4")
# Add to fstab if not already there
if ! grep -q "$NVME_PART" /etc/fstab; then
echo "$NVME_PART /nvme-data $FS_TYPE defaults,noatime 0 2" | sudo tee -a /etc/fstab
echo -e "${GREEN}[✓]${NC} Added to fstab"
fi
# Mount it
sudo mount -a 2>/dev/null || sudo mount "$NVME_PART" /nvme-data
}
# ── Done ─────────────────────────────────────────────────────────────
echo ""
echo -e "${BOLD}${GREEN}═══════════════════════════════════════════════════${NC}"
echo -e "${BOLD} ✓ netdrivelink.sh complete!${NC}"
echo -e "${GREEN}═══════════════════════════════════════════════════${NC}"
echo ""
echo -e " ${CYAN}Mounts are automounted on first access${NC}"
echo -e " Try: ${YELLOW}ls /mnt/users/edemilov${NC}"
echo -e " Or: ${YELLOW}ls ~/Documents${NC} (if 2nd drive was found)"
echo ""
echo -e " ${CYAN}To manually trigger all mounts:${NC}"
echo -e " ${YELLOW}sudo mount -a${NC}"
echo ""
# Setup Samba mounts (universal)
setup_samba_mounts() {
echo -e "\n${CYAN}[•]${NC} Setting up Samba mounts..."
# Create all mount points
sudo mkdir -p /mnt/users/edemilov /mnt/depo /mnt/media/{music,movies,tvshows,audiobooks,ebooks}
sudo mkdir -p /mnt/shared/{documents,photos,downloads} /mnt/pcbackups
# Create credentials file
sudo mkdir -p /etc/samba
if [ ! -f /etc/samba/credentials.emilovi ]; then
echo -e "${YELLOW}[!]${NC} Creating credentials file - EDIT YOUR PASSWORD!"
echo "username=edemilov" | sudo tee /etc/samba/credentials.emilovi > /dev/null
echo "password=CHANGE_ME" | sudo tee -a /etc/samba/credentials.emilovi > /dev/null
sudo chmod 600 /etc/samba/credentials.emilovi
echo -e "${RED}[!]${NC} EDIT /etc/samba/credentials.emilovi with your actual password!"
fi
# Add fstab entries (if not already there)
if ! grep -q "//10.0.99.100/Edemilov" /etc/fstab; then
cat << 'EOF' | sudo tee -a /etc/fstab
# Samba mounts from vanga (10.0.99.100)
//10.0.99.100/Edemilov /mnt/users/edemilov cifs credentials=/etc/samba/credentials.emilovi,uid=$(id -u),gid=$(id -g),vers=3.0,iocharset=utf8,nofail,noauto,x-systemd.automount,x-systemd.idle-timeout=600 0 0
//10.0.99.100/Depo /mnt/depo cifs credentials=/etc/samba/credentials.emilovi,uid=$(id -u),gid=$(id -g),vers=3.0,iocharset=utf8,nofail,noauto,x-systemd.automount,x-systemd.idle-timeout=600 0 0
//10.0.99.100/Music /mnt/media/music cifs credentials=/etc/samba/credentials.emilovi,uid=$(id -u),gid=$(id -g),vers=3.0,iocharset=utf8,nofail,noauto,x-systemd.automount,x-systemd.idle-timeout=600 0 0
//10.0.99.100/Movies /mnt/media/movies cifs credentials=/etc/samba/credentials.emilovi,uid=$(id -u),gid=$(id -g),vers=3.0,iocharset=utf8,nofail,noauto,x-systemd.automount,x-systemd.idle-timeout=600 0 0
EOF
fi
}
# Setup home symlinks (works on any distro)
setup_symlinks() {
echo -e "\n${CYAN}[•]${NC} Setting up home symlinks..."
cd ~
# Create folders on NVMe if they don't exist
sudo mkdir -p /nvme-data/{Documents,Downloads,photos,Projects,backups,VMs,Music,Videos,Nextcloud}
sudo chown -R $(id -u):$(id -g) /nvme-data
# Backup and create symlinks
for folder in Documents Downloads Pictures; do
[ -d "$folder" ] && [ ! -L "$folder" ] && mv "$folder" "${folder}.backup"
[ -L "$folder" ] && rm "$folder"
done
ln -sf /nvme-data/Documents Documents
ln -sf /nvme-data/Downloads Downloads
ln -sf /nvme-data/photos Pictures
echo -e "${GREEN}[✓]${NC} Symlinks created"
}
# Main execution
main() {
detect_distro
# Install common packages
install_packages curl wget git cifs-utils
# Setup components
setup_tailscale
setup_nvme_drive
setup_samba_mounts
setup_symlinks
# Final test
echo -e "\n${CYAN}[•]${NC} Testing setup..."
sudo mount -a 2>/dev/null || true
echo -e "\n${BOLD}${GREEN}═══════════════════════════════════════════════════${NC}"
echo -e "${BOLD}${GREEN} Setup Complete!${NC}"
echo -e "${BOLD}${GREEN}═══════════════════════════════════════════════════${NC}"
echo ""
echo -e "${CYAN}📁 NVMe drive:${NC} df -h /nvme-data"
echo -e "${CYAN}🔗 Tailscale:${NC} tailscale status"
echo -e "${CYAN}💾 Samba mounts:${NC} ls /mnt/users/edemilov"
echo ""
echo -e "${YELLOW}⚠️ Don't forget to:${NC}"
echo " 1. Edit /etc/samba/credentials.emilovi with your password"
echo " 2. Run: sudo mount -a"
}
# Run only if executed directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi