# Script to mount disks to specified directories
# Created: 2025-01-18
# For: yz-nas-local
# Check if running as root
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
# Function to log messages
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
logger -t "mount_helper" "$1" # Also log to system log
}
# Function to show system status
show_mount_status() {
log "=== Mount Status ==="
log "All current mounts:"
mount | grep -E "/dev/(sd|mapper)"
log "Disk usage:"
df -h | grep -E "/dev/(sd|mapper)"
log "Disk information:"
lsblk -o NAME,SIZE,TYPE,MOUNTPOINT | grep -v "part"
log "LVM Status:"
pvs
vgs
lvs
log "Mount point status:"
ls -l /vol1/1000/
log "Processes using mount points:"
lsof | grep "/vol1/1000/mnt" || true
log "==================="
}
# Function to create mount point if it doesn't exist
create_mount_point() {
if [ ! -d "$1" ]; then
log "Creating mount point: $1"
mkdir -p "$1"
if [ $? -ne 0 ]; then
log "Error: Failed to create mount point $1"
exit 1
fi
fi
}
# Function to check if mount point is already mounted
is_mounted() {
mount | grep -q " on $1 "
return $?
}
# Function to get current mount device for a mount point
get_mount_device() {
mount | grep " on $1 " | cut -d' ' -f1
}
# Function to unmount if necessary
safe_unmount() {
local mount_point="$1"
if mount | grep -q " on $mount_point "; then
log "Attempting to unmount $mount_point"
umount "$mount_point"
if [ $? -eq 0 ]; then
log "Successfully unmounted $mount_point"
return 0
else
log "Failed to unmount $mount_point"
return 1
fi
fi
}
# Show initial system status
log "Initial system status:"
show_mount_status
# Create mount points
create_mount_point "/vol1/1000/mnt2t"
create_mount_point "/vol1/1000/mnt4t"
create_mount_point "/vol1/1000/mnt8t"
# Function to mount LVM volume
mount_lvm() {
local vg_name="$1"
local lv_name="$2"
local mount_point="$3"
# Check if device exists
if [ ! -e "/dev/mapper/${vg_name}-${lv_name}" ]; then
log "Error: LVM device /dev/mapper/${vg_name}-${lv_name} does not exist"
return 1
fi
# Check current mount status
if is_mounted "$mount_point"; then
local current_device=$(get_mount_device "$mount_point")
if [ "$current_device" = "/dev/mapper/${vg_name}-${lv_name}" ]; then
log "Device already mounted correctly at $mount_point"
return 0
else
log "Mount point $mount_point is mounted with different device: $current_device"
if ! safe_unmount "$mount_point"; then
log "Error: Could not unmount existing device"
return 1
fi
fi
fi
# Mount the device
log "Mounting /dev/mapper/${vg_name}-${lv_name} to $mount_point"
if [[ "$vg_name" == "vg1000" || "$vg_name" == "vg1002" ]]; then
mount -t btrfs -o subvol=/@syno "/dev/mapper/${vg_name}-${lv_name}" "$mount_point"
else
mount -t btrfs "/dev/mapper/${vg_name}-${lv_name}" "$mount_point"
fi
if [ $? -eq 0 ]; then
log "Successfully mounted /dev/mapper/${vg_name}-${lv_name} to $mount_point"
chmod 755 "$mount_point"
return 0
else
log "Error: Failed to mount /dev/mapper/${vg_name}-${lv_name} to $mount_point"
return 1
fi
}
# Mount the LVM volumes
log "Starting mount process on yz-nas-local"
mount_lvm "trim_b0bbe614_0b01_4cee_a304_cc6b0fe6101c" "0" "/vol1/1000/mnt2t"
mount_lvm "vg1000" "lv" "/vol1/1000/mnt4t"
mount_lvm "vg1002" "lv" "/vol1/1000/mnt8t"
# Show final system status
log "Final system status:"
show_mount_status