diff --git a/scripts/storage/format-disk.sh b/scripts/storage/format-disk.sh new file mode 100644 index 0000000..2c7f7f3 --- /dev/null +++ b/scripts/storage/format-disk.sh @@ -0,0 +1,286 @@ +#!/bin/bash + +# ========================================================== +# ProxMenu - A menu-driven script for Proxmox VE management +# ========================================================== +# Author : MacRimi +# Copyright : (c) 2024 MacRimi +# License : MIT (https://raw.githubusercontent.com/MacRimi/ProxMenux/main/LICENSE) +# Version : 1.0 +# Last Updated: 28/01/2025 +# Description : Select and format unused physical disks +# ========================================================== + +REPO_URL="https://raw.githubusercontent.com/MacRimi/ProxMenux/main" +BASE_DIR="/usr/local/share/proxmenux" +UTILS_FILE="$BASE_DIR/utils.sh" + +if [[ -f "$UTILS_FILE" ]]; then + source "$UTILS_FILE" +fi +load_language +initialize_cache + +get_disk_info() { + local disk=$1 + MODEL=$(lsblk -dn -o MODEL "$disk" | xargs) + SIZE=$(lsblk -dn -o SIZE "$disk" | xargs) + echo "$MODEL" "$SIZE" +} + +msg_info "$(translate "Detecting available disks...")" + +USED_DISKS=$(lsblk -n -o PKNAME,TYPE | grep 'lvm' | awk '{print "/dev/" $1}') +MOUNTED_DISKS=$(lsblk -ln -o NAME,MOUNTPOINT | awk '$2!="" {print "/dev/" $1}') + +ZFS_DISKS="" +ZFS_RAW=$(zpool list -v -H 2>/dev/null | awk '{print $1}' | grep -v '^NAME$' | grep -v '^-' | grep -v '^mirror') + +for entry in $ZFS_RAW; do + path="" + if [[ "$entry" == wwn-* || "$entry" == ata-* ]]; then + if [ -e "/dev/disk/by-id/$entry" ]; then + path=$(readlink -f "/dev/disk/by-id/$entry") + fi + elif [[ "$entry" == /dev/* ]]; then + path="$entry" + fi + + if [ -n "$path" ]; then + base_disk=$(lsblk -no PKNAME "$path" 2>/dev/null) + if [ -n "$base_disk" ]; then + ZFS_DISKS+="/dev/$base_disk"$'\n' + fi + fi + +done + +ZFS_DISKS=$(echo "$ZFS_DISKS" | sort -u) +LVM_DEVICES=$(pvs --noheadings -o pv_name 2> >(grep -v 'File descriptor .* leaked') | xargs -n1 readlink -f | sort -u) +CONFIG_DATA=$(grep -vE '^\s*#' /etc/pve/qemu-server/*.conf /etc/pve/lxc/*.conf 2>/dev/null) + +FREE_DISKS=() + +while read -r DISK; do + [[ "$DISK" =~ /dev/zd ]] && continue + + INFO=($(get_disk_info "$DISK")) + MODEL="${INFO[@]::${#INFO[@]}-1}" + SIZE="${INFO[-1]}" + LABEL="" + SHOW_DISK=true + + REAL_PATH=$(readlink -f "$DISK") + USED_BY="" + IS_MOUNTED=false + IS_RAID=false + IS_ZFS=false + IS_LVM=false + + while read -r part fstype; do + [[ "$fstype" == "zfs_member" ]] && IS_ZFS=true + [[ "$fstype" == "linux_raid_member" ]] && IS_RAID=true + [[ "$fstype" == "LVM2_member" ]] && IS_LVM=true + if grep -q "/dev/$part" <<< "$MOUNTED_DISKS"; then + IS_MOUNTED=true + fi + done < <(lsblk -ln -o NAME,FSTYPE "$DISK" | tail -n +2) + + if echo "$LVM_DEVICES" | grep -qFx "$REAL_PATH"; then + IS_LVM=true + fi + + if grep -Fq "$REAL_PATH" <<< "$CONFIG_DATA"; then + USED_BY="⚠ $(translate "In use")" + else + for SYMLINK in /dev/disk/by-id/*; do + if [[ "$(readlink -f "$SYMLINK")" == "$REAL_PATH" ]]; then + if grep -Fq "$SYMLINK" <<< "$CONFIG_DATA"; then + USED_BY="⚠ $(translate "In use")" + break + fi + fi + done + fi + + if $IS_RAID && grep -q "$DISK" <<< "$(cat /proc/mdstat)"; then + if grep -q "active raid" /proc/mdstat; then + SHOW_DISK=false + fi + fi + + if $IS_ZFS || $IS_MOUNTED; then + SHOW_DISK=false + fi + + if $SHOW_DISK; then + [[ "$IS_RAID" == true ]] && LABEL+=" ⚠ RAID" + [[ -n "$USED_BY" ]] && LABEL+=" [$USED_BY]" + [[ "$IS_LVM" == true ]] && LABEL+=" ⚠ LVM" + [[ "$IS_ZFS" == true ]] && LABEL+=" ⚠ ZFS" + DESCRIPTION=$(printf "%-30s %10s%s" "$MODEL" "$SIZE" "$LABEL") + FREE_DISKS+=("$DISK" "$DESCRIPTION" "OFF") + fi + +done < <(lsblk -dn -e 7,11 -o PATH) + +if [ ${#FREE_DISKS[@]} -eq 0 ]; then + whiptail --title "$(translate "Error")" --msgbox "$(translate "No available disks found on the host.")" 8 50 + clear + exit 1 +fi + +msg_ok "$(translate "Available disks detected.")" + +MAX_WIDTH=$(printf "%s\n" "${FREE_DISKS[@]}" | awk '{print length}' | sort -nr | head -n1) +TOTAL_WIDTH=$((MAX_WIDTH + 20)) +TOTAL_WIDTH=$((TOTAL_WIDTH < 50 ? 50 : TOTAL_WIDTH)) + +SELECTED=$(whiptail --title "$(translate "Select Disk")" --radiolist \ + "$(translate "Select the disk you want to format:")" 20 $TOTAL_WIDTH 10 "${FREE_DISKS[@]}" 3>&1 1>&2 2>&3) + +if [ -z "$SELECTED" ]; then + whiptail --title "$(translate "Error")" --msgbox "$(translate "No disks were selected.")" 10 64 + clear + exit 1 +fi + +SELECTED=$(echo "$SELECTED" | tr -d '"') +SELECTED_DISK="$SELECTED" + + +REAL_PATH=$(readlink -f "$SELECTED") +CT_MATCH="" +VM_MATCH="" + +while read -r CT_ID CT_NAME; do + if pct config "$CT_ID" | grep -q "$REAL_PATH"; then + STATUS=$(pct status "$CT_ID" | awk '{print $2}') + if [[ "$STATUS" == "running" ]]; then + CT_MATCH="CT $CT_ID ($CT_NAME)" + break + fi + fi +done < <(pct list | awk 'NR>1 {print $1, $3}') + +while read -r VM_ID VM_NAME; do + if qm config "$VM_ID" | grep -q "$REAL_PATH"; then + STATUS=$(qm status "$VM_ID" | awk '{print $2}') + if [[ "$STATUS" == "running" ]]; then + VM_MATCH="VM $VM_ID ($VM_NAME)" + break + fi + fi +done < <(qm list | awk 'NR>1 {print $1, $2}') + +if [[ -n "$CT_MATCH" || -n "$VM_MATCH" ]]; then + whiptail --title "$(translate "Disk In Use")" --msgbox "$(translate "The selected disk is currently assigned to:")\n\n$CT_MATCH $VM_MATCH\n\n$(translate "You must power off the VM or CT before formatting.")" 12 70 + exit 1 +fi + + + + + + + + +######################################################### + + + + +SELECTED_DISK=$(echo "$SELECTED_DISK" | tr -d '"') + +WARNING_FLAGS="" +if lsblk -no FSTYPE "$SELECTED_DISK" | grep -q "linux_raid_member"; then + WARNING_FLAGS+=" RAID" +fi +if lsblk -no FSTYPE "$SELECTED_DISK" | grep -q "LVM2_member"; then + WARNING_FLAGS+=" LVM" +fi +if lsblk -no FSTYPE "$SELECTED_DISK" | grep -q "zfs_member"; then + WARNING_FLAGS+=" ZFS" +fi + +if [ -n "$WARNING_FLAGS" ]; then + whiptail --title "$(translate "Warning")" --msgbox "$(translate "This disk appears to have the following metadata:")$WARNING_FLAGS\\n\\n$(translate "They will be erased during formatting.")" 10 60 +fi + +whiptail --title "$(translate "Confirm Format")" --yesno "$(translate "WARNING: You are about to erase all data on")\\n$SELECTED_DISK\\n\\n$(translate "Are you sure you want to continue?")" 10 70 || exit 0 + +whiptail --title "$(translate "Final Confirmation")" --yesno "$(translate "FINAL WARNING: This operation will completely format the disk")\\n$SELECTED_DISK\\n\\n$(translate "ALL DATA WILL BE LOST. Proceed?")" 10 70 || exit 0 + + + + +######################################## + + + +echo -e "$(translate "Stopping residual RAID or device mappings...")" + +mdadm --misc --stop /dev/md* >/dev/null 2>&1 +dmsetup remove_all >/dev/null 2>&1 + +echo -e "$(translate "Wiping disk metadata and old RAID signatures...")" + +sgdisk --zap-all "$SELECTED_DISK" >/dev/null 2>&1 +wipefs -a "$SELECTED_DISK" >/dev/null 2>&1 + +udevadm settle +partprobe "$SELECTED_DISK" +sleep 2 + +echo -e "$(translate "Creating partition table and partition...")" + +parted -s "$SELECTED_DISK" mklabel gpt +parted -s "$SELECTED_DISK" mkpart primary 0% 100% + +udevadm settle +partprobe "$SELECTED_DISK" +sleep 2 + + + + + + +########################################### + + + +udevadm settle +partprobe "$SELECTED_DISK" +sleep 2 + + +PARTITION=$(lsblk -rno NAME "$SELECTED_DISK" | awk -v disk="$(basename "$SELECTED_DISK")" '$1 != disk {print $1; exit}') +if [ -z "$PARTITION" ]; then + whiptail --title "$(translate "Partition Error")" --msgbox "$(translate "Failed to create partition on disk.")" 8 60 + exit 1 +fi +PARTITION="/dev/$PARTITION" + +FORMAT_TYPE=$(whiptail --title "$(translate "Select Filesystem")" --menu "$(translate "Choose the filesystem for the disk:")" 15 60 5 \ + "ext4" "$(translate "Extended Filesystem 4 (recommended)")" \ + "xfs" "XFS" \ + "btrfs" "Btrfs" 3>&1 1>&2 2>&3) + +[[ -z "$FORMAT_TYPE" ]] && exit 0 + +echo -e "$(translate "Formatting partition") $PARTITION $(translate "as") $FORMAT_TYPE..." + +case "$FORMAT_TYPE" in + ext4) mkfs.ext4 -F "$PARTITION" ;; + xfs) mkfs.xfs -f "$PARTITION" ;; + btrfs) mkfs.btrfs -f "$PARTITION" ;; +esac + +if [ $? -eq 0 ]; then + msg_ok "$(translate "Disk formatted successfully:") $PARTITION" + whiptail --title "$(translate "Success")" --msgbox "$(translate "Disk has been formatted successfully.")" 8 50 +else + whiptail --title "$(translate "Error")" --msgbox "$(translate "Failed to format the disk.")" 8 60 + exit 1 +fi diff --git a/scripts/storage/mount-disk-on-host.sh b/scripts/storage/mount-disk-on-host.sh new file mode 100644 index 0000000..e6e897a --- /dev/null +++ b/scripts/storage/mount-disk-on-host.sh @@ -0,0 +1,341 @@ +#!/bin/bash + +# ========================================================== +# ProxMenu - Mount independent disk on Proxmox host +# ========================================================== +# Author : MacRimi +# Copyright : (c) 2024 MacRimi +# License : MIT (https://raw.githubusercontent.com/MacRimi/ProxMenux/main/LICENSE) +# Version : 1.0 +# Last Updated: 08/04/2025 +# ========================================================== +# Description: +# This script detects unassigned physical disks and allows +# the user to mount one of them on the host Proxmox system. +# - Detects unmounted and unassigned disks. +# - Filters out ZFS, LVM, RAID and system disks. +# - Allows selecting a disk. +# - Prepares partition and filesystem if needed. +# - Mounts the disk in the host at a defined mount point. +# ========================================================== + +# Configuration ============================================ +REPO_URL="https://raw.githubusercontent.com/MacRimi/ProxMenux/main" +BASE_DIR="/usr/local/share/proxmenux" +UTILS_FILE="$BASE_DIR/utils.sh" +VENV_PATH="/opt/googletrans-env" + +if [[ -f "$UTILS_FILE" ]]; then + source "$UTILS_FILE" +fi +load_language +initialize_cache +# ========================================================== + +get_disk_info() { + local disk=$1 + MODEL=$(lsblk -dn -o MODEL "$disk" | xargs) + SIZE=$(lsblk -dn -o SIZE "$disk" | xargs) + echo "$MODEL" "$SIZE" +} + +msg_info "$(translate "Detecting available disks...")" + +USED_DISKS=$(lsblk -n -o PKNAME,TYPE | grep 'lvm' | awk '{print "/dev/" $1}') +MOUNTED_DISKS=$(lsblk -ln -o NAME,MOUNTPOINT | awk '$2!="" {print "/dev/" $1}') + +ZFS_DISKS="" +ZFS_RAW=$(zpool list -v -H 2>/dev/null | awk '{print $1}' | grep -v '^NAME$' | grep -v '^-' | grep -v '^mirror') + +for entry in $ZFS_RAW; do + path="" + if [[ "$entry" == wwn-* || "$entry" == ata-* ]]; then + if [ -e "/dev/disk/by-id/$entry" ]; then + path=$(readlink -f "/dev/disk/by-id/$entry") + fi + elif [[ "$entry" == /dev/* ]]; then + path="$entry" + fi + + if [ -n "$path" ]; then + base_disk=$(lsblk -no PKNAME "$path" 2>/dev/null) + if [ -n "$base_disk" ]; then + ZFS_DISKS+="/dev/$base_disk"$'\n' + fi + fi +done + +ZFS_DISKS=$(echo "$ZFS_DISKS" | sort -u) + +is_disk_in_use() { + local disk="$1" + + while read -r part fstype; do + case "$fstype" in + zfs_member|linux_raid_member) + return 0 ;; + esac + + if echo "$MOUNTED_DISKS" | grep -q "/dev/$part"; then + return 0 + fi + done < <(lsblk -ln -o NAME,FSTYPE "$disk" | tail -n +2) + + if echo "$USED_DISKS" | grep -q "$disk" || echo "$ZFS_DISKS" | grep -q "$disk"; then + return 0 + fi + + return 1 +} + + +FREE_DISKS=() + +LVM_DEVICES=$(pvs --noheadings -o pv_name 2> >(grep -v 'File descriptor .* leaked') | xargs -n1 readlink -f | sort -u) +RAID_ACTIVE=$(grep -Po 'md\d+\s*:\s*active\s+raid[0-9]+' /proc/mdstat | awk '{print $1}' | sort -u) + +while read -r DISK; do + [[ "$DISK" =~ /dev/zd ]] && continue + + INFO=($(get_disk_info "$DISK")) + MODEL="${INFO[@]::${#INFO[@]}-1}" + SIZE="${INFO[-1]}" + LABEL="" + SHOW_DISK=true + + IS_MOUNTED=false + IS_RAID=false + IS_ZFS=false + IS_LVM=false + + while read -r part fstype; do + [[ "$fstype" == "zfs_member" ]] && IS_ZFS=true + [[ "$fstype" == "linux_raid_member" ]] && IS_RAID=true + [[ "$fstype" == "LVM2_member" ]] && IS_LVM=true + if grep -q "/dev/$part" <<< "$MOUNTED_DISKS"; then + IS_MOUNTED=true + fi + done < <(lsblk -ln -o NAME,FSTYPE "$DISK" | tail -n +2) + + REAL_PATH=$(readlink -f "$DISK") + if echo "$LVM_DEVICES" | grep -qFx "$REAL_PATH"; then + IS_MOUNTED=true + fi + + + USED_BY="" + REAL_PATH=$(readlink -f "$DISK") + CONFIG_DATA=$(grep -vE '^\s*#' /etc/pve/qemu-server/*.conf /etc/pve/lxc/*.conf 2>/dev/null) + + if grep -Fq "$REAL_PATH" <<< "$CONFIG_DATA"; then + USED_BY="⚠ $(translate "In use")" + else + for SYMLINK in /dev/disk/by-id/*; do + if [[ "$(readlink -f "$SYMLINK")" == "$REAL_PATH" ]]; then + if grep -Fq "$SYMLINK" <<< "$CONFIG_DATA"; then + USED_BY="⚠ $(translate "In use")" + break + fi + fi + done + fi + + + + if $IS_RAID && grep -q "$DISK" <<< "$(cat /proc/mdstat)"; then + if grep -q "active raid" /proc/mdstat; then + SHOW_DISK=false + fi + fi + + if $IS_ZFS; then + SHOW_DISK=false + fi + + if $IS_MOUNTED; then + SHOW_DISK=false + fi + + if $SHOW_DISK; then + [[ -n "$USED_BY" ]] && LABEL+=" [$USED_BY]" + [[ "$IS_RAID" == true ]] && LABEL+=" ⚠ RAID" + [[ "$IS_LVM" == true ]] && LABEL+=" ⚠ LVM" + [[ "$IS_ZFS" == true ]] && LABEL+=" ⚠ ZFS" + + DESCRIPTION=$(printf "%-30s %10s%s" "$MODEL" "$SIZE" "$LABEL") + FREE_DISKS+=("$DISK" "$DESCRIPTION" "OFF") + fi + +done < <(lsblk -dn -e 7,11 -o PATH) + +if [ "${#FREE_DISKS[@]}" -eq 0 ]; then + cleanup + whiptail --title "$(translate "Error")" --msgbox "$(translate "No available disks found on the host.")" 8 50 + clear + exit 1 +fi + +msg_ok "$(translate "Available disks detected.")" + +MAX_WIDTH=$(printf "%s\n" "${FREE_DISKS[@]}" | awk '{print length}' | sort -nr | head -n1) +TOTAL_WIDTH=$((MAX_WIDTH + 20)) +TOTAL_WIDTH=$((TOTAL_WIDTH < 50 ? 50 : TOTAL_WIDTH)) + +SELECTED=$(whiptail --title "$(translate "Select Disk")" --radiolist \ + "$(translate "Select the disk you want to mount on the host:")" 20 $TOTAL_WIDTH 10 "${FREE_DISKS[@]}" 3>&1 1>&2 2>&3) + +if [ -z "$SELECTED" ]; then + whiptail --title "$(translate "Error")" --msgbox "$(translate "No disk was selected.")" 10 50 + clear + exit 1 +fi + + + +msg_ok "$(translate "Disk selected successfully:") $SELECTED" + + + + + +################################################################ + + + + +PARTITION=$(lsblk -rno NAME "$SELECTED" | awk -v disk="$(basename "$SELECTED")" '$1 != disk {print $1; exit}') + +SKIP_FORMAT=false +DEFAULT_MOUNT="/mnt/data_shared" + +if [ -n "$PARTITION" ]; then + PARTITION="/dev/$PARTITION" + CURRENT_FS=$(lsblk -no FSTYPE "$PARTITION" | xargs) + + if [[ "$CURRENT_FS" == "ext4" || "$CURRENT_FS" == "xfs" || "$CURRENT_FS" == "btrfs" ]]; then + SKIP_FORMAT=true + msg_ok "$(translate "Detected existing filesystem") $CURRENT_FS $(translate "on") $PARTITION." + else + whiptail --title "$(translate "Unsupported Filesystem")" --yesno "$(translate "The partition") $PARTITION $(translate "has an unsupported filesystem ($CURRENT_FS).\\nDo you want to format it?")" 10 70 + if [ $? -ne 0 ]; then + exit 0 + fi + fi +else + CURRENT_FS=$(lsblk -no FSTYPE "$SELECTED" | xargs) + + if [[ "$CURRENT_FS" == "ext4" || "$CURRENT_FS" == "xfs" || "$CURRENT_FS" == "btrfs" ]]; then + SKIP_FORMAT=true + PARTITION="$SELECTED" + msg_ok "$(translate "Detected filesystem") $CURRENT_FS $(translate "directly on disk") $SELECTED." + else + whiptail --title "$(translate "No Valid Partitions")" --yesno "$(translate "The disk has no partitions and no valid filesystem. Do you want to create a new partition and format it?")" 10 70 + if [ $? -ne 0 ]; then + exit 0 + fi + + echo -e "$(translate "Creating partition table and partition...")" + parted -s "$SELECTED" mklabel gpt + parted -s "$SELECTED" mkpart primary 0% 100% + sleep 2 + partprobe "$SELECTED" + sleep 2 + + PARTITION=$(lsblk -rno NAME "$SELECTED" | awk -v disk="$(basename "$SELECTED")" '$1 != disk {print $1; exit}') + if [ -n "$PARTITION" ]; then + PARTITION="/dev/$PARTITION" + else + whiptail --title "$(translate "Partition Error")" --msgbox "$(translate "Failed to create partition on disk") $SELECTED." 8 70 + exit 1 + fi + fi +fi + +if [ "$SKIP_FORMAT" != true ]; then + FORMAT_TYPE=$(whiptail --title "$(translate "Select Format Type")" --menu "$(translate "Select the filesystem type for") $PARTITION:" 15 60 5 \ + "ext4" "$(translate "Extended Filesystem 4 (recommended)")" \ + "xfs" "XFS" \ + "btrfs" "Btrfs" 3>&1 1>&2 2>&3) + + if [ -z "$FORMAT_TYPE" ]; then + whiptail --title "$(translate "Format Cancelled")" --msgbox "$(translate "Format operation cancelled. The disk will not be added.")" 8 60 + exit 0 + fi + + whiptail --title "$(translate "WARNING")" --yesno "$(translate "WARNING: This operation will FORMAT the disk") $PARTITION $(translate "with") $FORMAT_TYPE.\\n\\n$(translate "ALL DATA ON THIS DISK WILL BE PERMANENTLY LOST!")\\n\\n$(translate "Are you sure you want to continue")" 15 70 + if [ $? -ne 0 ]; then + exit 0 + fi + + echo -e "$(translate "Formatting partition") $PARTITION $(translate "with") $FORMAT_TYPE..." + case "$FORMAT_TYPE" in + "ext4") mkfs.ext4 -F "$PARTITION" ;; + "xfs") mkfs.xfs -f "$PARTITION" ;; + "btrfs") mkfs.btrfs -f "$PARTITION" ;; + esac + + if [ $? -ne 0 ]; then + cleanup + whiptail --title "$(translate "Format Failed")" --msgbox "$(translate "Failed to format partition") $PARTITION $(translate "with") $FORMAT_TYPE." 12 70 + exit 1 + else + msg_ok "$(translate "Partition") $PARTITION $(translate "successfully formatted with") $FORMAT_TYPE." + partprobe "$SELECTED" + sleep 2 + fi +fi + + + + +################################################################ + + + + + +MOUNT_POINT=$(whiptail --title "$(translate "Mount Point")" \ + --inputbox "$(translate "Enter the mount point for the disk (e.g., /mnt/data_shared):")" \ + 10 60 "$DEFAULT_MOUNT" 3>&1 1>&2 2>&3) + +if [ -z "$MOUNT_POINT" ]; then + whiptail --title "$(translate "Error")" --msgbox "$(translate "No mount point was specified.")" 8 40 + exit 1 +fi + +msg_ok "$(translate "Mount point specified:") $MOUNT_POINT" + +mkdir -p "$MOUNT_POINT" + +UUID=$(blkid -s UUID -o value "$PARTITION") + +# Obtener sistema de archivos real +FS_TYPE=$(lsblk -no FSTYPE "$PARTITION" | xargs) + +FSTAB_ENTRY="UUID=$UUID $MOUNT_POINT $FS_TYPE defaults 0 0" + +if grep -q "UUID=$UUID" /etc/fstab; then + sed -i "s|^.*UUID=$UUID.*|$FSTAB_ENTRY|" /etc/fstab + msg_ok "$(translate "fstab entry updated for") $UUID" +else + echo "$FSTAB_ENTRY" >> /etc/fstab + msg_ok "$(translate "fstab entry added for") $UUID" +fi + + +################################################################## + +mount "$MOUNT_POINT" 2> >(grep -v "systemd still uses") + +################################################################## + + +if [ $? -eq 0 ]; then + chown root:root "$MOUNT_POINT" + chmod 775 "$MOUNT_POINT" + whiptail --title "$(translate "Success")" --msgbox "$(translate "The disk has been successfully mounted at") $MOUNT_POINT" 8 60 + msg_ok "$(translate "Disk mounted at") $MOUNT_POINT" +else + whiptail --title "$(translate "Mount Error")" --msgbox "$(translate "Failed to mount the disk at") $MOUNT_POINT" 8 60 + exit 1 +fi diff --git a/scripts/storage/mount-point-to-ct.sh b/scripts/storage/mount-point-to-ct.sh new file mode 100644 index 0000000..6dc1048 --- /dev/null +++ b/scripts/storage/mount-point-to-ct.sh @@ -0,0 +1,145 @@ +#!/bin/bash + +# ========================================================== +# ProxMenu - Mount point from host into LXC container (CT) +# ========================================================== +# Author : MacRimi +# License : MIT +# Description : Mount a folder from /mnt on the host to a mount point in a CT +# ========================================================== + +REPO_URL="https://raw.githubusercontent.com/MacRimi/ProxMenux/main" +BASE_DIR="/usr/local/share/proxmenux" +UTILS_FILE="$BASE_DIR/utils.sh" + +if [[ -f "$UTILS_FILE" ]]; then + source "$UTILS_FILE" +fi +load_language +initialize_cache + +####################################################### + +CT_LIST=($(pct list | awk 'NR>1 {print $1":"$3}')) + +if [[ ${#CT_LIST[@]} -eq 0 ]]; then + whiptail --title "$(translate "No CTs")" --msgbox "$(translate "No containers found.")" 8 40 + exit 0 +fi + + + + + +CT_OPTIONS=() +for entry in "${CT_LIST[@]}"; do + ID="${entry%%:*}" + NAME="${entry##*:}" + CT_OPTIONS+=("$ID" "$NAME") +done + +CTID=$(whiptail --title "$(translate "Select CT")" --menu "$(translate "Select the container:")" 20 60 10 "${CT_OPTIONS[@]}" 3>&1 1>&2 2>&3) +[[ -z "$CTID" ]] && exit 0 + + + + +CT_STATUS=$(pct status "$CTID" | awk '{print $2}') +if [ "$CT_STATUS" != "running" ]; then + msg_info "$(translate "Starting CT") $CTID..." + pct start "$CTID" + sleep 2 + if [ "$(pct status "$CTID" | awk '{print $2}')" != "running" ]; then + msg_error "$(translate "Failed to start the CT.")" + exit 1 + fi + msg_ok "$(translate "CT started successfully.")" +fi + + +####################################################### + + +select_origin_path() { + METHOD=$(whiptail --title "$(translate "Select Host Folder")" --menu "$(translate "How do you want to select the host folder to mount?")" 15 60 5 \ + "auto" "$(translate "Select from /mnt")" \ + "manual" "$(translate "Enter path manually")" 3>&1 1>&2 2>&3) + + case "$METHOD" in + auto) + HOST_DIRS=(/mnt/*) + OPTIONS=() + for dir in "${HOST_DIRS[@]}"; do + [[ -d "$dir" ]] && OPTIONS+=("$dir" "") + done + + ORIGIN=$(whiptail --title "$(translate "Select Folder")" --menu "$(translate "Select the folder to mount:")" 20 60 10 "${OPTIONS[@]}" 3>&1 1>&2 2>&3) + [[ -z "$ORIGIN" ]] && return 1 + ;; + + manual) + ORIGIN=$(whiptail --title "$(translate "Enter Path")" --inputbox "$(translate "Enter the full path to the host folder:")" 10 60 "/mnt/" 3>&1 1>&2 2>&3) + [[ -z "$ORIGIN" ]] && return 1 + ;; + esac + + if [[ ! -d "$ORIGIN" ]]; then + whiptail --title "$(translate "Error")" --msgbox "$(translate "The selected path is not a valid directory:")\n$ORIGIN" 8 60 + return 1 + fi + + return 0 +} + +select_origin_path || exit 0 + + + +####################################################### + + + +CT_NAME=$(pct config "$CTID" | awk -F: '/hostname/ {print $2}' | xargs) +DEFAULT_MOUNT_POINT="/mnt/host_share" + +MOUNT_POINT=$(whiptail --title "$(translate "Mount Point")" \ +--inputbox "$(translate "Enter the mount point inside the CT (e.g., /mnt/host_share):")" \ +10 70 "$DEFAULT_MOUNT_POINT" 3>&1 1>&2 2>&3) + +if [[ -z "$MOUNT_POINT" ]]; then + whiptail --title "$(translate "Error")" --msgbox "$(translate "No mount point specified.")" 8 60 + exit 1 +fi + + +if ! pct exec "$CTID" -- test -d "$MOUNT_POINT"; then + if whiptail --yesno "$(translate "Directory does not exist in the CT.")\n\n$MOUNT_POINT\n\n$(translate "Do you want to create it?")" 12 70 --title "$(translate "Create Directory")"; then + pct exec "$CTID" -- mkdir -p "$MOUNT_POINT" + msg_ok "$(translate "Directory created inside CT:") $MOUNT_POINT" + else + msg_error "$(translate "Directory not created. Operation cancelled.")" + exit 1 + fi +fi + + +INDEX=0 +while pct config "$CTID" | grep -q "mp${INDEX}:"; do + ((INDEX++)) + + + [[ $INDEX -ge 100 ]] && msg_error "Too many mount points." && exit 1 +done + + +msg_info "$(translate "Mounting folder from host to CT...")" +RESULT=$(pct set "$CTID" -mp${INDEX} "$ORIGIN,mp=$MOUNT_POINT,backup=0,ro=0,acl=1" 2>&1) + +if [[ $? -eq 0 ]]; then + msg_ok "$(translate "Successfully mounted:")\n$ORIGIN → $CT_NAME:$MOUNT_POINT" +else + msg_error "$(translate "Error mounting folder:")\n$RESULT" + exit 1 +fi + +exit 0 diff --git a/scripts/storage/unmount-disk-from-host.sh b/scripts/storage/unmount-disk-from-host.sh new file mode 100644 index 0000000..8278a84 --- /dev/null +++ b/scripts/storage/unmount-disk-from-host.sh @@ -0,0 +1,73 @@ +#!/bin/bash + +# ========================================================== +# ProxMenu - A menu-driven script for Proxmox VE management +# ========================================================== +# Author : MacRimi +# Copyright : (c) 2024 MacRimi +# License : MIT (https://raw.githubusercontent.com/MacRimi/ProxMenux/main/LICENSE) +# Version : 1.0 +# Last Updated: 28/01/2025 +# Description : Allows unmounting a previously mounted disk +# ========================================================== + +REPO_URL="https://raw.githubusercontent.com/MacRimi/ProxMenux/main" +BASE_DIR="/usr/local/share/proxmenux" +UTILS_FILE="$BASE_DIR/utils.sh" + +if [[ -f "$UTILS_FILE" ]]; then + source "$UTILS_FILE" +fi +load_language +initialize_cache + + +MOUNTED_DISKS=($(mount | grep '^/dev/' | grep 'on /mnt/' | awk '{print $3}')) + +if [[ ${#MOUNTED_DISKS[@]} -eq 0 ]]; then + whiptail --title "$(translate "No Disks")" --msgbox "$(translate "No mounted disks found under /mnt.")" 8 50 + exit 0 +fi + + +MENU_ITEMS=() +for MNT in "${MOUNTED_DISKS[@]}"; do + UUID=$(blkid | grep "$MNT" | awk '{print $2}' | tr -d '"') + DESC="$MNT $UUID" + MENU_ITEMS+=("$MNT" "$DESC") +done + +SELECTED=$(whiptail --title "$(translate "Unmount Disk")" --menu "$(translate "Select the disk you want to unmount:")" 20 70 10 "${MENU_ITEMS[@]}" 3>&1 1>&2 2>&3) + +[[ -z "$SELECTED" ]] && exit 0 + + +whiptail --title "$(translate "Confirm Unmount")" --yesno "$(translate "Are you sure you want to unmount") $SELECTED?" 10 60 || exit 0 + + +umount "$SELECTED" 2>/dev/null +if [ $? -ne 0 ]; then + whiptail --title "$(translate "Error")" --msgbox "$(translate "Failed to unmount disk at") $SELECTED" 8 60 + exit 1 +else + msg_ok "$(translate "Unmounted:") $SELECTED" +fi + + +whiptail --title "$(translate "Delete Mount Folder")" --yesno "$(translate "Do you want to delete the mount point folder") $SELECTED?" 10 60 +if [ $? -eq 0 ]; then + rm -rf "$SELECTED" + msg_ok "$(translate "Deleted folder:") $SELECTED" +fi + + +DEVICE=$(findmnt -no SOURCE "$SELECTED") +UUID=$(blkid -s UUID -o value "$DEVICE") + +if [ -n "$UUID" ]; then + sed -i "/UUID=$UUID/d" /etc/fstab + msg_ok "$(translate "fstab entry removed for") $UUID" +fi + +whiptail --title "$(translate "Done")" --msgbox "$(translate "Disk unmounted and cleaned successfully.")" 8 60 +