aboutsummaryrefslogtreecommitdiff
path: root/tools/XBMCLive/diskmounter
blob: a44e7b7275d431cc204faa3329d76a5d23308d56 (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/bin/bash
####################
# This utility searches for available HFS+, NTFS and FAT32 partitions, creates
# mount points for them and mounts the partitions
#
# (c)2008 Luigi Capriotti <l.capriotti@xbmc.org> for use in XBMCLive
# Base on "diskmount" by Dennis Kaarsemaker <dennis@ubuntu-nl.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
###################

VERBOSE='n'

# Root check
if [[ $UID != 0 ]]; then
    echo 'You should run this program as root or using sudo'
    exit 1
fi

OPTIONS='user,fmask=0133,dmask=0022,uid=1000,gid=1000'
MACOPTIONS='user,file_umask=0133,dir_umask=0022,uid=1000,gid=1000'
EXTOPTIONS=''

# Now for the real work
drivesntfs=`fdisk -l | grep -i 'ntfs' | awk -F '/| ' '{print $3}'`
drivesfat=`fdisk -l | grep -i 'fat32' | awk -F '/| ' '{print $3}'`
driveshfs=`fdisk -l | grep -i 'Apple_HFS' | awk -F '/| ' '{print $3}'`
drivesext=`fdisk -l | grep -i 'Linux' | grep -v 'swap' | awk -F '/| ' '{print $3}'`

if [ ! -d /media ]; then
    mkdir /media
fi

donesomething='n'
for drive in $drivesntfs; do
    if [[ ! `grep $drive /etc/fstab` && ! `pmount | grep $drive` && ! `mount | grep $drive` ]]; then
        if [ -e /sys/block/${drive%?}/removable ]; then
            if [ "$(cat /sys/block/${drive%?}/removable)" = "0" ]; then
		uuid=`blkid -o udev /dev/$drive | grep UUID= | cut -d"=" -f2`
		if [ ! -d /media/$uuid ] ; then
	                mkdir /media/$uuid
		fi
                mount -t ntfs -o rw,$OPTIONS /dev/$drive /media/$uuid

                donesomething='y'
                if [[ $VERBOSE == 'y' ]]; then
                    echo "Mounted /dev/$drive as '/media/$uuid' (read-only)"
                fi
            else
                if [[ $VERBOSE == 'y' ]]; then
                    echo "Ignoring /dev/$drive - removable drive"
                fi
            fi
        fi
    else
        if [[ $VERBOSE == 'y' ]]; then
            echo "Ignoring /dev/$drive - already in /etc/fstab or mounted"
        fi
    fi
done

for drive in $drivesfat; do
    if [[ ! `grep $drive /etc/fstab` && ! `pmount | grep $drive` && ! `mount | grep $drive` ]]; then
	uuid=`blkid /dev/$drive | awk 'BEGIN{RS="UUID="}/-/{gsub(/ .*/,"");print}' | sed s/'"'/''/g`
        if [ -e /sys/block/${drive%?}/removable ]; then
            if [ "$(cat /sys/block/${drive%?}/removable)" = "0" ]; then
		uuid=`blkid -o udev /dev/$drive | grep UUID= | cut -d"=" -f2`
		if [ ! -d /media/$uuid ] ; then
	                mkdir /media/$uuid
		fi
                mount -t vfat -o rw,$OPTIONS /dev/$drive /media/$uuid

                donesomething='y'
                if [[ $VERBOSE == 'y' ]]; then
                    echo "Mounted /dev/$drive as '/media/$uuid' (read-only)"
                fi
            else
                if [[ $VERBOSE == 'y' ]]; then
                    echo "Ignoring /dev/$drive - removable drive"
                fi
            fi
        fi
    else
        if [[ $VERBOSE == 'y' ]]; then
            echo "Ignoring /dev/$drive - already in /etc/fstab or mounted"
        fi
    fi
done


for drive in $driveshfs; do
    if [[ ! `grep $drive /etc/fstab` && ! `pmount | grep $drive` && ! `mount | grep $drive` ]]; then
        if [ -e /sys/block/${drive%?}/removable ]; then
            if [ "$(cat /sys/block/${drive%?}/removable)" = "0" ]; then
		uuid=`blkid -o udev /dev/$drive | grep UUID= | cut -d"=" -f2`
		if [ ! -d /media/$uuid ] ; then
	                mkdir /media/$uuid
		fi
                mount -t hfsplus -o rw,$MACOPTIONS /dev/$drive /media/$uuid

                donesomething='y'
                if [[ $VERBOSE == 'y' ]]; then
                    echo "Mounted /dev/$drive as '/media/$uuid' (read-only)"
                fi
            else
                if [[ $VERBOSE == 'y' ]]; then
                    echo "Ignoring /dev/$drive - removable drive"
                fi
            fi
        fi
    else
        if [[ $VERBOSE == 'y' ]]; then
            echo "Ignoring /dev/$drive - already in /etc/fstab or mounted"
        fi
    fi
done

for drive in $drivesext; do
    if [[ ! `grep $drive /etc/fstab` && ! `pmount | grep $drive` && ! `mount | grep $drive` ]]; then
        if [ -e /sys/block/${drive%?}/removable ]; then
            if [ "$(cat /sys/block/${drive%?}/removable)" = "0" ]; then
		uuid=`blkid -o udev /dev/$drive | grep UUID= | cut -d"=" -f2`
		if [ ! -d /media/$uuid ] ; then
	                mkdir /media/$uuid
		fi
                mount -o rw,$EXTOPTIONS /dev/$drive /media/$uuid

                donesomething='y'
                if [[ $VERBOSE == 'y' ]]; then
                    echo "Mounted /dev/$drive as '/media/$uuid' (read-only)"
                fi
            else
                if [[ $VERBOSE == 'y' ]]; then
                    echo "Ignoring /dev/$drive - removable drive"
                fi
            fi
        fi
    else
        if [[ $VERBOSE == 'y' ]]; then
            echo "Ignoring /dev/$drive - already in /etc/fstab or mounted"
        fi
    fi
done

if [[ $VERBOSE == 'y' ]]; then
    if [[ $donesomething == 'y' ]]; then
       echo "All Linux, Windows and Mac partitions on non-removable disks have been mounted"
    else
       echo "No usable Linux, Windows or Mac partitions found on non-removable disks"
    fi
fi