Skip to content

Commit 2e31f72

Browse files
authored
Updated automount-usb.sh to look for a particular parition label "NAS"
This aims to fix the issue where the udev rule spits out a random partition in sda that might not contribute too the partition that has the games. For example lets say we have a USB drive that has sda sda1 - 30m sda2 - 200gb since the udev rule will detect sda being added it will always forward the first partition and update the samba share config with sda1 instead of sda2 at first, the idea was to sort the partitions by size but it was finicky. In the end I settled on looking for a label as it was more straightforward
1 parent 20fec3c commit 2e31f72

File tree

1 file changed

+33
-49
lines changed

1 file changed

+33
-49
lines changed

automount-usb.sh

Lines changed: 33 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,25 @@
88
# This allows for use of USB & HDD in addition to Micro-SD
99
# It also creates a new Samba configuration which exposes the last attached USB drive @ //SMBSHARE/<PARTITION>
1010

11-
USER=`whoami`
12-
1311
# Update packages
1412
sudo apt-get update
1513

16-
# Install NTFS Read/Write Support and udisks2
17-
sudo apt-get install -y ntfs-3g udisks2
18-
19-
# Add user to disk group
20-
sudo usermod -a -G disk ${USER}
21-
22-
# Create polkit rule
23-
sudo mkdir -p /etc/polkit-1/rules.d/
24-
sudo mkdir -p /etc/polkit-1/localauthority/50-local.d/
25-
26-
# For polkit > 105
27-
sudo cat <<'EOF' | sudo tee /etc/polkit-1/rules.d/10-udisks2.rules
28-
// Allow udisks2 to mount devices without authentication
29-
// for users in the "disk" group.
30-
polkit.addRule(function(action, subject) {
31-
if ((action.id == "org.freedesktop.udisks2.filesystem-mount-system" ||
32-
action.id == "org.freedesktop.udisks2.filesystem-mount" ||
33-
action.id == "org.freedesktop.udisks2.filesystem-mount-other-seat") &&
34-
subject.isInGroup("disk")) {
35-
return polkit.Result.YES;
36-
}
37-
});
38-
EOF
14+
# Install NTFS Read/Write Support
15+
sudo apt-get install -y ntfs-3g
3916

40-
# For polkit <= 105
41-
sudo cat <<'EOF' | sudo tee /etc/polkit-1/localauthority/50-local.d/10-udisks2.pkla
42-
[Authorize mounting of devices for group disk]
43-
Identity=unix-group:disk
44-
Action=org.freedesktop.udisks2.filesystem-mount-system;org.freedesktop.udisks2.filesystem-mount;org.freedesktop.udisks2.filesystem-mount-other-seat
45-
ResultAny=yes
46-
ResultInactive=yes
47-
ResultActive=yes
48-
EOF
17+
# Install pmount with ExFAT support
18+
sudo apt-get install -y exfat-fuse exfat-utils autoconf intltool libtool libtool-bin libglib2.0-dev libblkid-dev
19+
cd ~
20+
git clone https://github.com/stigi/pmount-exfat.git
21+
cd pmount-exfat
22+
./autogen.sh
23+
make
24+
sudo make install prefix=usr
25+
sudo sed -i 's/not_physically_logged_allow = no/not_physically_logged_allow = yes/' /etc/pmount.conf
4926

5027
# Create udev rule
5128
sudo cat <<'EOF' | sudo tee /etc/udev/rules.d/usbstick.rules
52-
ACTION=="add", KERNEL=="sd[a-z][0-9]", TAG+="systemd", ENV{SYSTEMD_WANTS}="usbstick-handler@%k"
29+
ACTION=="add", KERNEL=="sd[a-z]*", TAG+="systemd", ENV{SYSTEMD_WANTS}="usbstick-handler@%k"
5330
ENV{DEVTYPE}=="usb_device", ACTION=="remove", SUBSYSTEM=="usb", RUN+="/bin/systemctl --no-block restart usbstick-cleanup@%k.service"
5431
EOF
5532

@@ -64,7 +41,7 @@ After=dev-%i.device
6441
Type=oneshot
6542
RemainAfterExit=yes
6643
ExecStart=/usr/local/bin/automount.sh %I
67-
ExecStop=/usr/bin/udisksctl unmount -b /dev/%I
44+
ExecStop=/usr/bin/pumount /dev/%I
6845
EOF
6946

7047
sudo cat <<'EOF' | sudo tee /lib/systemd/system/[email protected]
@@ -83,53 +60,60 @@ sudo cat <<'EOF' | sudo tee /usr/local/bin/automount.sh
8360
#!/bin/bash
8461
8562
PART=$1
86-
UUID=`blkid /dev/${PART} -o value -s UUID`
63+
echo "The value of PART is: $PART"
8764
FS_LABEL=`lsblk -o name,label | grep ${PART} | awk '{print $2}'`
8865
8966
if [ -z ${PART} ]
9067
then
9168
exit
9269
fi
9370
94-
runuser userplaceholder -s /bin/bash -c "udisksctl mount -b /dev/${PART} --no-user-interaction"
71+
label="NAS"
72+
73+
L_PART=$(blkid -L "$label" | grep -o 'sd[a-z][0-9]*')
9574
96-
if [ -f /usr/local/bin/ps3netsrv++ ]; then
97-
pkill ps3netsrv++
98-
/usr/local/bin/ps3netsrv++ -d /media/userplaceholder/$UUID
75+
if [ -n "$L_PART" ]; then
76+
echo "The device path for label '$label' is: /dev/$L_PART"
77+
# Additional actions based on L_PART...
78+
else
79+
echo "Label '$label' not found or no device path associated."
80+
exit
9981
fi
10082
83+
runuser pi -s /bin/bash -c "/usr/bin/pmount --umask 000 --noatime -w --sync /dev/${L_PART} /media/${L_PART}"
84+
85+
pkill ps3netsrv++
86+
/usr/local/bin/ps3netsrv++ -d /media/$L_PART
87+
10188
#create a new smb share for the mounted drive
10289
cat <<EOS | sudo tee /etc/samba/smb.conf
10390
[global]
104-
server min protocol = NT1
10591
workgroup = WORKGROUP
10692
usershare allow guests = yes
10793
map to guest = bad user
10894
allow insecure wide links = yes
10995
[share]
110-
Comment = default shared folder
111-
Path = /media/userplaceholder/$UUID
96+
Comment = Pi default shared folder
97+
Path = /media/$L_PART
11298
Browseable = yes
11399
Writeable = Yes
114100
only guest = no
115101
create mask = 0777
116102
directory mask = 0777
117103
Public = yes
118104
Guest ok = yes
119-
force user = userplaceholder
105+
force user = pi
120106
follow symlinks = yes
121107
wide links = yes
122108
EOS
123109
124110
#if you wish to create a samba user with password you can use the following:
125-
#sudo smbpasswd -a userplaceholder
111+
#sudo smbpasswd -a pi
126112
sudo /etc/init.d/smbd restart
127113
EOF
128114

129-
sudo sed -i "s/userplaceholder/${USER}/g" /usr/local/bin/automount.sh
130-
131115
# Make script executable
132116
sudo chmod +x /usr/local/bin/automount.sh
133117

134118
# Reload udev rules and triggers
135-
sudo udevadm control --reload-rules && sudo udevadm trigger
119+
sudo udevadm control --reload-rules && udevadm trigger

0 commit comments

Comments
 (0)