forked from toolboc/psx-pi-smbshare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
automount-usb.sh
107 lines (89 loc) · 2.74 KB
/
automount-usb.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash
#
# psx-pi-smbshare automout-usb script
#
# *What it does*
# This script configures raspbian to automount any usb storage to /media/sd<xy>
# This allows for use of USB & HDD in addition to Micro-SD
# It also creates a new Samba configuration which exposes the last attached USB drive @ //SMBSHARE/<PARTITION>
# Update packages
sudo apt-get update
# Install NTFS Read/Write Support
sudo apt-get install -y ntfs-3g
# Install pmount with ExFAT support
sudo apt-get install -y exfat-fuse exfat-utils autoconf intltool libtool libtool-bin libglib2.0-dev libblkid-dev
cd ~
git clone https://github.com/stigi/pmount-exfat.git
cd pmount-exfat
./autogen.sh
make
sudo make install prefix=usr
sudo sed -i 's/not_physically_logged_allow = no/not_physically_logged_allow = yes/' /etc/pmount.conf
# Create udev rule
sudo cat <<'EOF' | sudo tee /etc/udev/rules.d/usbstick.rules
ACTION=="add", KERNEL=="sd[a-z][0-9]", TAG+="systemd", ENV{SYSTEMD_WANTS}="usbstick-handler@%k"
ENV{DEVTYPE}=="usb_device", ACTION=="remove", SUBSYSTEM=="usb", RUN+="/bin/systemctl --no-block restart usbstick-cleanup@%k.service"
EOF
# Configure systemd
sudo cat <<'EOF' | sudo tee /lib/systemd/system/[email protected]
[Unit]
Description=Mount USB sticks
BindsTo=dev-%i.device
After=dev-%i.device
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/automount.sh %I
ExecStop=/usr/bin/pumount /dev/%I
EOF
sudo cat <<'EOF' | sudo tee /lib/systemd/system/[email protected]
[Unit]
Description=Cleanup USB sticks
BindsTo=dev-%i.device
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/samba-init.sh
EOF
# Configure script to run when an automount event is triggered
sudo cat <<'EOF' | sudo tee /usr/local/bin/automount.sh
#!/bin/bash
PART=$1
FS_LABEL=`lsblk -o name,label | grep ${PART} | awk '{print $2}'`
if [ -z ${PART} ]
then
exit
fi
runuser pi -s /bin/bash -c "/usr/bin/pmount --umask 000 --noatime -w --sync /dev/${PART} /media/${PART}"
pkill ps3netsrv++
/usr/local/bin/ps3netsrv++ -d /media/$PART
#create a new smb share for the mounted drive
cat <<EOS | sudo tee /etc/samba/smb.conf
[global]
server min protocol = NT1
workgroup = WORKGROUP
usershare allow guests = yes
map to guest = bad user
allow insecure wide links = yes
[share]
Comment = Pi default shared folder
Path = /media/$PART
Browseable = yes
Writeable = Yes
only guest = no
create mask = 0777
directory mask = 0777
Public = yes
Guest ok = yes
force user = pi
follow symlinks = yes
wide links = yes
EOS
#if you wish to create a samba user with password you can use the following:
#sudo smbpasswd -a pi
sudo /etc/init.d/smbd restart
EOF
# Make script executable
sudo chmod +x /usr/local/bin/automount.sh
# Reload udev rules and triggers
sudo udevadm control --reload-rules && udevadm trigger