mirror of
https://github.com/MacRimi/ProxMenux.git
synced 2026-05-31 12:34:48 +00:00
Update scripts share
This commit is contained in:
+220
-18
@@ -9,9 +9,17 @@
|
||||
# Version : 1.0
|
||||
# ==========================================================
|
||||
# Description:
|
||||
# Registers external NFS exports as Proxmox storage via
|
||||
# pvesm add nfs. Proxmox manages the mount natively — no
|
||||
# fstab entries needed on the host.
|
||||
# Mounts an external NFS export on the Proxmox host.
|
||||
# User picks one or both methods:
|
||||
# 1. As Proxmox storage (pvesm add nfs) → /mnt/pve/<id>
|
||||
# 2. As host fstab mount → user-chosen path
|
||||
#
|
||||
# Method 2 is for users who want the host to mount the share
|
||||
# for LXC bind-mounts WITHOUT exposing it as a Proxmox storage
|
||||
# in the Datacenter UI. The mount path is opened up so
|
||||
# unprivileged LXCs can read/write through a bind-mount —
|
||||
# all permission tweaks happen on the host side, NEVER inside
|
||||
# the container.
|
||||
#
|
||||
# Features:
|
||||
# - Auto-discover NFS servers on the local subnet (nmap).
|
||||
@@ -298,11 +306,180 @@ add_proxmox_nfs_storage() {
|
||||
fi
|
||||
}
|
||||
|
||||
# ==========================================================
|
||||
# FSTAB MOUNT (host-only, NOT as Proxmox storage)
|
||||
# ==========================================================
|
||||
|
||||
# Pick a mount path on the host. Default is /mnt/<export-basename>.
|
||||
# Validates the path is absolute and not already in use.
|
||||
select_host_mount_path() {
|
||||
local default_name
|
||||
default_name=$(basename "$NFS_EXPORT")
|
||||
[[ -z "$default_name" || "$default_name" == "/" ]] && default_name="nfs_share"
|
||||
|
||||
while true; do
|
||||
HOST_MOUNT_PATH=$(whiptail --inputbox \
|
||||
"$(translate "Enter the host mount path:")\n\n$(translate "Default location is /mnt/<name>. The share will be mounted here on the host. Use this path in /etc/fstab. For LXC access, bind-mount this path with the LXC Mount Manager.")" \
|
||||
14 70 "/mnt/$default_name" \
|
||||
--title "$(translate "Host Mount Path")" 3>&1 1>&2 2>&3)
|
||||
[[ $? -ne 0 ]] && return 1
|
||||
[[ -z "$HOST_MOUNT_PATH" ]] && HOST_MOUNT_PATH="/mnt/$default_name"
|
||||
|
||||
if [[ ! "$HOST_MOUNT_PATH" =~ ^/.+ ]]; then
|
||||
whiptail --msgbox "$(translate "Mount path must be an absolute path starting with /")" 8 60
|
||||
continue
|
||||
fi
|
||||
|
||||
if mount | grep -q " on $HOST_MOUNT_PATH "; then
|
||||
whiptail --msgbox "$(translate "Something is already mounted at:") $HOST_MOUNT_PATH\n\n$(translate "Choose a different path or unmount it first.")" 10 70
|
||||
continue
|
||||
fi
|
||||
|
||||
if grep -qE "[[:space:]]${HOST_MOUNT_PATH}[[:space:]]" /etc/fstab 2>/dev/null; then
|
||||
if ! whiptail --yesno "$(translate "An fstab entry already exists for:") $HOST_MOUNT_PATH\n\n$(translate "Replace it?")" 10 70 \
|
||||
--title "$(translate "fstab entry exists")"; then
|
||||
continue
|
||||
fi
|
||||
FSTAB_REPLACE=1
|
||||
else
|
||||
FSTAB_REPLACE=0
|
||||
fi
|
||||
break
|
||||
done
|
||||
return 0
|
||||
}
|
||||
|
||||
# Pick NFS mount options for the fstab entry.
|
||||
select_nfs_mount_options() {
|
||||
local choice
|
||||
choice=$(dialog --backtitle "ProxMenux" \
|
||||
--title "$(translate "Mount Options")" \
|
||||
--menu "$(translate "Select mount options:")" 15 70 4 \
|
||||
"1" "$(translate "Read/Write (default)")" \
|
||||
"2" "$(translate "Read-only")" \
|
||||
"3" "$(translate "Custom")" \
|
||||
3>&1 1>&2 2>&3)
|
||||
[[ $? -ne 0 ]] && return 1
|
||||
|
||||
case "$choice" in
|
||||
1) NFS_MOUNT_OPTS="rw,hard,nofail,_netdev,rsize=131072,wsize=131072,timeo=600,retrans=2" ;;
|
||||
2) NFS_MOUNT_OPTS="ro,hard,nofail,_netdev,rsize=131072,wsize=131072,timeo=600,retrans=2" ;;
|
||||
3)
|
||||
NFS_MOUNT_OPTS=$(whiptail --inputbox \
|
||||
"$(translate "Enter custom NFS mount options:")" \
|
||||
10 70 "rw,hard,nofail,_netdev,rsize=131072,wsize=131072,timeo=600,retrans=2" \
|
||||
--title "$(translate "Custom Options")" 3>&1 1>&2 2>&3)
|
||||
[[ $? -ne 0 ]] && return 1
|
||||
[[ -z "$NFS_MOUNT_OPTS" ]] && NFS_MOUNT_OPTS="rw,hard,nofail,_netdev"
|
||||
;;
|
||||
esac
|
||||
return 0
|
||||
}
|
||||
|
||||
# Mount the NFS export on the host and persist to /etc/fstab.
|
||||
# Permission tweaks happen on the host side ONLY — never inside any LXC.
|
||||
mount_nfs_via_fstab() {
|
||||
local server="$1"
|
||||
local export_path="$2"
|
||||
local mount_path="$3"
|
||||
local mount_opts="$4"
|
||||
local replace="$5"
|
||||
|
||||
msg_info "$(translate "Preparing host mount...")"
|
||||
|
||||
if [[ ! -d "$mount_path" ]]; then
|
||||
if ! mkdir -p "$mount_path" 2>/dev/null; then
|
||||
msg_error "$(translate "Failed to create mount point:") $mount_path"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
msg_ok "$(translate "Mount point ready:") $mount_path"
|
||||
|
||||
msg_info "$(translate "Mounting NFS share...")"
|
||||
if ! mount -t nfs -o "$mount_opts" "${server}:${export_path}" "$mount_path" >/dev/null 2>&1; then
|
||||
msg_error "$(translate "Failed to mount NFS share on host.")"
|
||||
return 1
|
||||
fi
|
||||
msg_ok "$(translate "NFS share mounted at:") $mount_path"
|
||||
|
||||
# Verify host can write (informational — NFS server controls write access).
|
||||
if touch "$mount_path/.proxmenux_write_test" 2>/dev/null; then
|
||||
rm -f "$mount_path/.proxmenux_write_test" 2>/dev/null
|
||||
msg_ok "$(translate "Host write access confirmed.")"
|
||||
else
|
||||
msg_warn "$(translate "No host write access — server-side ACL or root_squash. Continuing anyway.")"
|
||||
fi
|
||||
|
||||
# Best-effort: open perms so an unprivileged LXC bind-mounting this path
|
||||
# can read/write. NFS controls perms server-side; these calls succeed only
|
||||
# if the server export allows them. Failures are silent.
|
||||
chmod 1777 "$mount_path" 2>/dev/null || true
|
||||
setfacl -m o::rwx "$mount_path" 2>/dev/null || true
|
||||
|
||||
# Persist in /etc/fstab.
|
||||
if [[ "$replace" == "1" ]]; then
|
||||
sed -i "\|[[:space:]]${mount_path}[[:space:]]|d" /etc/fstab
|
||||
fi
|
||||
echo "${server}:${export_path} $mount_path nfs $mount_opts 0 0" >> /etc/fstab
|
||||
msg_ok "$(translate "Added to /etc/fstab.")"
|
||||
|
||||
systemctl daemon-reload 2>/dev/null || true
|
||||
|
||||
echo -e ""
|
||||
echo -e "${TAB}${BOLD}$(translate "Host fstab Mount:")${CL}"
|
||||
echo -e "${TAB}${BGN}$(translate "Server:")${CL} ${BL}$server${CL}"
|
||||
echo -e "${TAB}${BGN}$(translate "Export:")${CL} ${BL}$export_path${CL}"
|
||||
echo -e "${TAB}${BGN}$(translate "Mount path:")${CL} ${BL}$mount_path${CL}"
|
||||
echo -e "${TAB}${BGN}$(translate "Options:")${CL} ${BL}$mount_opts${CL}"
|
||||
echo -e "${TAB}${BGN}$(translate "Persistent:")${CL} ${BL}$(translate "yes (survives reboot)")${CL}"
|
||||
echo -e ""
|
||||
msg_info2 "$(translate "To use this share from an LXC, bind-mount it via:")"
|
||||
echo -e "${TAB} pct set <ctid> -mpN $mount_path,mp=<container-path>,shared=1,backup=0"
|
||||
echo -e "${TAB} $(translate "or use the ProxMenux LXC Mount Manager.")"
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# ==========================================================
|
||||
# MOUNT METHOD SELECTION
|
||||
# ==========================================================
|
||||
|
||||
# Show a checklist with the two mount methods (pvesm / fstab).
|
||||
# User must mark at least one and press OK; if none is marked,
|
||||
# loop and show the dialog again. Cancel exits the flow.
|
||||
# Sets MODE_PVESM and MODE_FSTAB to 0 or 1 on success.
|
||||
select_mount_methods() {
|
||||
MODE_PVESM=0
|
||||
MODE_FSTAB=0
|
||||
|
||||
while true; do
|
||||
local result
|
||||
result=$(dialog --backtitle "ProxMenux" \
|
||||
--title "$(translate "Mount Method")" \
|
||||
--checklist "\n$(translate "Choose how to mount the NFS share on this host. Mark one or both options:")\n\n$(translate "• Proxmox storage (pvesm): visible in Datacenter > Storage, mount at /mnt/pve/<id>")\n$(translate "• Host fstab: mounted at a path you choose, NOT visible as Proxmox storage")\n$(translate "• Both: mounts twice (pvesm + an independent fstab entry)")" 20 78 2 \
|
||||
"pvesm" "$(translate "As Proxmox storage")" off \
|
||||
"fstab" "$(translate "As host fstab mount only")" off \
|
||||
3>&1 1>&2 2>&3)
|
||||
local rc=$?
|
||||
[[ $rc -ne 0 ]] && return 1 # Cancel → abort the whole flow
|
||||
|
||||
# Parse selection
|
||||
if echo "$result" | grep -qw "pvesm"; then MODE_PVESM=1; fi
|
||||
if echo "$result" | grep -qw "fstab"; then MODE_FSTAB=1; fi
|
||||
|
||||
if [[ "$MODE_PVESM" == "1" || "$MODE_FSTAB" == "1" ]]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
whiptail --msgbox "$(translate "Please mark at least one option, or press Cancel to exit.")" 8 70
|
||||
done
|
||||
}
|
||||
|
||||
# ==========================================================
|
||||
# MAIN OPERATIONS
|
||||
# ==========================================================
|
||||
|
||||
add_nfs_to_proxmox() {
|
||||
mount_nfs_share() {
|
||||
if ! which showmount >/dev/null 2>&1; then
|
||||
msg_info "$(translate "Installing NFS client tools...")"
|
||||
apt-get update &>/dev/null
|
||||
@@ -325,27 +502,52 @@ add_nfs_to_proxmox() {
|
||||
return
|
||||
fi
|
||||
|
||||
show_proxmenux_logo
|
||||
msg_title "$(translate "Add NFS Share as Proxmox Storage")"
|
||||
msg_ok "$(translate "NFS server:")" "$NFS_SERVER"
|
||||
msg_ok "$(translate "NFS export:")" "$NFS_EXPORT"
|
||||
# Step 4: Pick mount method(s) — pvesm, fstab, or both
|
||||
select_mount_methods || return
|
||||
|
||||
# Step 4: Configure storage
|
||||
configure_nfs_storage || return
|
||||
# Step 5a: If pvesm selected, gather storage params
|
||||
if [[ "$MODE_PVESM" == "1" ]]; then
|
||||
configure_nfs_storage || return
|
||||
fi
|
||||
|
||||
# Step 5: Add to Proxmox
|
||||
# Step 5b: If fstab selected, gather host mount params
|
||||
if [[ "$MODE_FSTAB" == "1" ]]; then
|
||||
select_host_mount_path || return
|
||||
select_nfs_mount_options || return
|
||||
fi
|
||||
|
||||
# Step 6: Apply
|
||||
show_proxmenux_logo
|
||||
msg_title "$(translate "Add NFS Share as Proxmox Storage")"
|
||||
msg_title "$(translate "Mount NFS Share on Host")"
|
||||
msg_ok "$(translate "NFS server:") $NFS_SERVER"
|
||||
msg_ok "$(translate "NFS export:") $NFS_EXPORT"
|
||||
msg_ok "$(translate "Storage ID:") $STORAGE_ID"
|
||||
msg_ok "$(translate "Content:") $MOUNT_CONTENT"
|
||||
if [[ "$MODE_PVESM" == "1" ]]; then
|
||||
msg_ok "$(translate "Method:") pvesm ($(translate "Storage ID:") $STORAGE_ID, $(translate "content:") $MOUNT_CONTENT)"
|
||||
fi
|
||||
if [[ "$MODE_FSTAB" == "1" ]]; then
|
||||
msg_ok "$(translate "Method:") fstab ($(translate "Mount path:") $HOST_MOUNT_PATH)"
|
||||
fi
|
||||
echo -e ""
|
||||
|
||||
add_proxmox_nfs_storage "$STORAGE_ID" "$NFS_SERVER" "$NFS_EXPORT" "$MOUNT_CONTENT"
|
||||
local overall_rc=0
|
||||
if [[ "$MODE_PVESM" == "1" ]]; then
|
||||
if ! add_proxmox_nfs_storage "$STORAGE_ID" "$NFS_SERVER" "$NFS_EXPORT" "$MOUNT_CONTENT"; then
|
||||
overall_rc=1
|
||||
fi
|
||||
echo -e ""
|
||||
fi
|
||||
if [[ "$MODE_FSTAB" == "1" ]]; then
|
||||
if ! mount_nfs_via_fstab "$NFS_SERVER" "$NFS_EXPORT" "$HOST_MOUNT_PATH" "$NFS_MOUNT_OPTS" "$FSTAB_REPLACE"; then
|
||||
overall_rc=1
|
||||
fi
|
||||
fi
|
||||
|
||||
echo -e ""
|
||||
msg_success "$(translate "Press Enter to continue...")"
|
||||
if [[ "$overall_rc" == "0" ]]; then
|
||||
msg_success "$(translate "Press Enter to continue...")"
|
||||
else
|
||||
msg_warn "$(translate "Some operations failed — review messages above. Press Enter to continue...")"
|
||||
fi
|
||||
read -r
|
||||
}
|
||||
|
||||
@@ -532,7 +734,7 @@ while true; do
|
||||
CHOICE=$(dialog --backtitle "ProxMenux" \
|
||||
--title "$(translate "NFS Host Manager - Proxmox Host")" \
|
||||
--menu "$(translate "Choose an option:")" 18 70 6 \
|
||||
"1" "$(translate "Add NFS Share as Proxmox Storage")" \
|
||||
"1" "$(translate "Mount NFS Share on Host")" \
|
||||
"2" "$(translate "View NFS Storages")" \
|
||||
"3" "$(translate "Remove NFS Storage")" \
|
||||
"4" "$(translate "Test NFS Connectivity")" \
|
||||
@@ -545,7 +747,7 @@ while true; do
|
||||
fi
|
||||
|
||||
case $CHOICE in
|
||||
1) add_nfs_to_proxmox ;;
|
||||
1) mount_nfs_share ;;
|
||||
2) view_nfs_storages ;;
|
||||
3) remove_nfs_storage ;;
|
||||
4) test_nfs_connectivity ;;
|
||||
|
||||
Reference in New Issue
Block a user