-
Notifications
You must be signed in to change notification settings - Fork 35
/
automount-usb.sh
135 lines (112 loc) · 3.69 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/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>
USER=`whoami`
# Update packages
sudo apt-get update
# Install NTFS Read/Write Support and udisks2
sudo apt-get install -y ntfs-3g udisks2
# Add user to disk group
sudo usermod -a -G disk ${USER}
# Create polkit rule
sudo mkdir -p /etc/polkit-1/rules.d/
sudo mkdir -p /etc/polkit-1/localauthority/50-local.d/
# For polkit > 105
sudo cat <<'EOF' | sudo tee /etc/polkit-1/rules.d/10-udisks2.rules
// Allow udisks2 to mount devices without authentication
// for users in the "disk" group.
polkit.addRule(function(action, subject) {
if ((action.id == "org.freedesktop.udisks2.filesystem-mount-system" ||
action.id == "org.freedesktop.udisks2.filesystem-mount" ||
action.id == "org.freedesktop.udisks2.filesystem-mount-other-seat") &&
subject.isInGroup("disk")) {
return polkit.Result.YES;
}
});
EOF
# For polkit <= 105
sudo cat <<'EOF' | sudo tee /etc/polkit-1/localauthority/50-local.d/10-udisks2.pkla
[Authorize mounting of devices for group disk]
Identity=unix-group:disk
Action=org.freedesktop.udisks2.filesystem-mount-system;org.freedesktop.udisks2.filesystem-mount;org.freedesktop.udisks2.filesystem-mount-other-seat
ResultAny=yes
ResultInactive=yes
ResultActive=yes
EOF
# 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/udisksctl unmount -b /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
UUID=`blkid /dev/${PART} -o value -s UUID`
FS_LABEL=`lsblk -o name,label | grep ${PART} | awk '{print $2}'`
if [ -z ${PART} ]
then
exit
fi
runuser userplaceholder -s /bin/bash -c "udisksctl mount -b /dev/${PART} --no-user-interaction"
if [ -f /usr/local/bin/ps3netsrv++ ]; then
pkill ps3netsrv++
/usr/local/bin/ps3netsrv++ -d /media/userplaceholder/$UUID
fi
#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 = default shared folder
Path = /media/userplaceholder/$UUID
Browseable = yes
Writeable = Yes
only guest = no
create mask = 0777
directory mask = 0777
Public = yes
Guest ok = yes
force user = userplaceholder
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 userplaceholder
sudo /etc/init.d/smbd restart
EOF
sudo sed -i "s/userplaceholder/${USER}/g" /usr/local/bin/automount.sh
# Make script executable
sudo chmod +x /usr/local/bin/automount.sh
# Reload udev rules and triggers
sudo udevadm control --reload-rules && sudo udevadm trigger