Skip to content

Instantly share code, notes, and snippets.

@NorkzYT
Last active June 30, 2024 15:50
Show Gist options
  • Save NorkzYT/14449b247dae9ac81ba4664564669299 to your computer and use it in GitHub Desktop.
Save NorkzYT/14449b247dae9ac81ba4664564669299 to your computer and use it in GitHub Desktop.
Proxmox CIFS Share Mount Wizard Script
#!/bin/bash
# This script is designed to assist in mounting CIFS/SMB shares to a Proxmox LXC container.
# It automates the process of creating a mount point on the Proxmox VE (PVE) host, adding the
# CIFS share to the /etc/fstab for persistent mounts, and configuring the LXC container to
# recognize the share. This script is intended for use on a Proxmox Virtual Environment and
# requires an LXC container to be specified that will access the mounted share.
#
# Prerequisites:
# - Proxmox Virtual Environment setup.
# - An LXC container already created and running on Proxmox.
# - CIFS/SMB share details (hostname/IP, share name, SMB username, and password).
# - Root privileges on the Proxmox host.
#
# How to Use:
# 1. Ensure the target LXC container is running before executing this script.
# 2. Run this script as root or with sudo privileges.
# 3. Follow the prompts to enter the required information for the CIFS/SMB share
# and the LXC container details.
#
# Note: This script must be run as root to modify system files and perform mount operations.
# Ensure the script is run as root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
# Ask user for necessary inputs
read -p "Enter the folder name (e.g., nas_rwx): " folder_name
read -p "Enter the CIFS hostname or IP (e.g., NAS): " cifs_host
read -p "Enter the share name (e.g., media): " share_name
read -p "Enter SMB username: " smb_username
read -sp "Enter SMB password: " smb_password && echo
read -p "Enter the LXC ID: " lxc_id
read -p "Enter the username within the LXC that needs access to the share (e.g., jellyfin, plex): " lxc_username
# Step 1: Configure LXC
echo "Creating group 'lxc_shares' with GID=10000 in LXC..."
pct exec $lxc_id -- groupadd -g 10000 lxc_shares
echo "Adding user $lxc_username to group 'lxc_shares'..."
pct exec $lxc_id -- usermod -aG lxc_shares $lxc_username
echo "Shutting down the LXC..."
pct stop $lxc_id
# Wait for the LXC to stop
while [ "$(pct status $lxc_id)" != "status: stopped" ]; do
echo "Waiting for LXC $lxc_id to stop..."
sleep 1
done
# Step 2: Configure PVE host
# Create mount point
echo "Creating mount point on PVE host..."
mkdir -p /mnt/lxc_shares/$folder_name
# Check if the fstab entry exists
fstab_entry="//${cifs_host}/${share_name} /mnt/lxc_shares/${folder_name} cifs _netdev,x-systemd.automount,noatime,nobrl,uid=100000,gid=110000,dir_mode=0770,file_mode=0770,username=${smb_username},password=${smb_password} 0 0"
if ! grep -q "//${cifs_host}/${share_name} /mnt/lxc_shares/${folder_name}" /etc/fstab ; then
echo "Adding NAS CIFS share to /etc/fstab with nobrl option..."
echo "$fstab_entry" >> /etc/fstab
else
echo "Entry for ${cifs_host}/${share_name} on /mnt/lxc_shares/${folder_name} already exists in /etc/fstab."
fi
# Reload systemd to recognize changes to fstab
echo "Reloading systemd daemon to apply fstab changes..."
systemctl daemon-reload
# Before mounting, ensure the mount point is not already in use
if mountpoint -q "/mnt/lxc_shares/$folder_name"; then
echo "Unmounting the already mounted share to avoid conflicts..."
umount -l "/mnt/lxc_shares/$folder_name"
fi
# Mount the share
echo "Mounting the share on the PVE host..."
mount "/mnt/lxc_shares/$folder_name"
# Add a bind mount of the share to the LXC config
echo "Determining the next available mount point index..."
config_file="/etc/pve/lxc/${lxc_id}.conf"
if [ -f "$config_file" ]; then
last_mp_index=$(grep -oP 'mp\d+:' "$config_file" | grep -oP '\d+' | sort -nr | head -n1)
next_mp_index=$((last_mp_index + 1))
else
next_mp_index=0
fi
echo "Adding a bind mount of the share to the LXC config..."
lxc_config_entry="mp${next_mp_index}: /mnt/lxc_shares/${folder_name},mp=/mnt/${folder_name}"
echo "$lxc_config_entry" >> "$config_file"
# Step 3: Start the LXC
echo "Starting the LXC..."
pct start $lxc_id
echo "Configuration complete."
@rucknapucknavitz
Copy link

This is great - thank you.
I do think you need to revert to the original instructions in the first part of step 1 from the revision on March 7.

10000 in the LXC will map correctly to 110000 in the PVE
(mapping 110000 in the LXC will incorrectly map on the PVE)

When I made this change it’s working great on my tteck Jellyfin container.

echo "Creating group 'lxc_shares' with GID=10000 in LXC..."
pct exec $lxc_id -- groupadd -g 10000 lxc_shares```

@NorkzYT
Copy link
Author

NorkzYT commented Jun 9, 2024

@rucknapucknavitz

No problem, I have revised the script with the change you requested. Thank you.

I will be changing the following line as well to use 10000 instead of 110000:
https://github.com/NorkzYT/Wolflith/blob/b10bc7b4680bf240a1055ea7d7f36435107e6c15/Ansible/playbooks/provision-proxmox-lxc.yml#L145

@campmeharder
Copy link

campmeharder commented Jun 13, 2024

Hi there for some reason not working with me, new to proxmox that is why Im using this, thanks btw, but even though it says complete it doesnt show. I tried it with the *arr that are in a lxc. Also type on line 34 read -sp "Enter SMB password: " smb_password && echo

the message im getting is mount error(16): Device or resource busy. Im trying to connect it to my synology rackstation so not sure what could be giving that error.

@Puntoboy
Copy link

This works great, but I'm having an issue with sub folder permissions. Any existing folder in the share I cannot write to so I see errors in the web GUIs for the LXCs. If I create a folder in the root from the LXC, then it's writeable, so it's existing folders that are the issue.

@Puntoboy
Copy link

Puntoboy commented Jun 22, 2024

The permissions look the same, test3 is a folder I created within the LXC, Movies is one I created inside the NAS server before connecting the LXC.

I can connect to test3 fine, but I cannot write to Movies or any of the other subfolders (root folder is fine).

image

@rucknapucknavitz
Copy link

This isn’t a script issue so not really a topic for this thread, it’s a permissions issue.
Checkout some chmod tutorials online, like this one: https://www.tomshardware.com/how-to/change-file-directory-permissions-linux

@Puntoboy
Copy link

This isn’t a script issue so not really a topic for this thread, it’s a permissions issue. Checkout some chmod tutorials online, like this one: https://www.tomshardware.com/how-to/change-file-directory-permissions-linux

this script is supposed to fix the permissions issue with the LXCs. Hence why I asked.

@rucknapucknavitz
Copy link

Sounds like it could be a share host permission issue, unless you’re sharing directly from the proxmox host?

@Puntoboy
Copy link

I'm sharing from a Cockpit LXC on the same host.

Problem is now, since rebooting, the host won't even mount that share now.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment