3 # sdcontrol 1.0 2001/8/8 21:33:19 (Hideki Hayami)
5 # Initialize or shutdown a SD card device
7 # The first argument should be either 'insert' of 'eject'.
12 MOUNT_POINT=/media/card
13 SMB_MOUNT=/home/samba/SD_Card
14 INSTALL_DIR=Documents/Install_Files
17 LOGFILE="/tmp/sdcontrol.log"
22 stamp="`date +"%d-%m-%y %H:%M:%S"`"
24 test "$ENABLE_LOG" = "yes" -a -n "$LOGFILE" && echo -e "$1" | sed "s/\(.*\)/[$stamp]\ \1/">> "$LOGFILE"
28 # import FS mount options from fstab or use defaults
30 SD_OPTS="`grep "$MOUNT_POINT" /etc/fstab | head -1 | awk '{print $4}'`"
33 SD_OPTS="noatime,sync"
34 vecho "\nWARNING: Couldn't determine SD mount options from /etc/fstab, using defaults\n"
37 # The "quiet" option fails on non-VFAT cards. Nice trick ;)
38 SD_OPTS_VFAT="$SD_OPTS,quiet,umask=000,iocharset=utf8"
48 test "$DEBUG" = 1 && echo "<DEBUG> $*"
58 #echo umount >> /tmp/sd
62 if ! (mount | grep -q "$MOUNT_POINT")
67 echo count=$count >> /tmp/sd
68 if [ `expr $count \>= 500` = 1 ]; then
69 #echo time out >> /tmp/sd
72 count=`expr $count + 1`
73 usleep 200000 || sleep 2
80 if ! test -z "`which fuser`"
82 echo "Using 'fuser' to kill \"busy\" tasks"
83 ps_line=`ps ax | grep 'qpe$' | grep -v grep` # no -w on busybox
85 decho "ps_line [$ps_line]"
86 qpe_pid=`get_pid $ps_line`
88 decho "qpe_pid [$qpe_pid]"
91 target_pids=`fuser -m $1 | cut -d : -f2 | sed "s/[a-z]//g"` >/dev/null 2>&1
94 #echo $target_pids >> /tmp/sd
95 if ! (echo "$target_pids" | grep -q "[0-9]"); then
99 decho "Killing PIDs: [$target_pids]"
101 ! test -z "$qpe_pid" && is_exist_qpe=`echo $target_pids | grep "$qpe_pid"` # no -w on busybox
103 decho "is_exist_qpe [$is_exist_qpe]"
104 if [ "$is_exist_qpe" = "" ]; then
107 #echo kill -9 $target_pids >> /tmp/sd
110 #echo "found qpe!!!" >> /tmp/sd
111 target_pids=`echo $target_pids | sed -e "s/$qpe_pid//"`
112 if (echo "$target_pids" | grep -q "[0-9]"); then
116 #echo kill -9 $target_pids >> /tmp/sd
122 echo "No 'fuser' found. Running tasks may keep partitions busy."
125 ###### for QPE ######
131 vecho "Beginning SD auto-mount..:"
133 test "$ENABLE_LOG" = yes && ps ax > "$LOGFILE-ps"
135 # Read available partitions from /proc/partitions.
136 OK_PARTS=` head -20 /proc/partitions |grep mmcda | sort| uniq | awk '{print $4}'`
138 decho "OK_PARTS [$OK_PARTS]"
140 test "$ENABLE_LOG" = yes && echo $OK_PARTS > "$LOGFILE-part"
142 if test "`echo "$OK_PARTS" | wc -l | awk '{print $1}`" -gt 1
144 OK_PARTS="`echo "$OK_PARTS" | grep -v "^mmcda$"`"
147 if test -z "$OK_PARTS"
149 vecho "\n\nWARNING: Trying failsafe partition mode\n\n"
150 OK_PARTS="mmcda1 mmcda2 mmcda3 mmcda4"
154 vecho "-> Valid SD partitions are: [$OK_PARTS]"
156 # Allow for "#" in fstab.
157 fstab_txt="`cat /etc/fstab | grep -v ^#`"
160 for part in $OK_PARTS
162 vecho "\n* * * * * Working on [/dev/$part] * * * * *\n"
163 # Read the mount-point for this partition from fstab
164 FS_MOUNT_POINT="`echo "$fstab_txt" | grep "/dev/$part" | awk '{print $2}`"
167 # Mount the first valid partition as /mnt/card if there was no entry in fstab
168 if test -z "$FS_MOUNT_POINT"
170 # See if /mnt/card is already mounted
171 if ! mount|awk '{print $3}'|grep "^$MOUNT_POINT$" 2>&1 >/dev/null
173 # Check if another *existing* partition is configured as /mnt/card
174 if ! echo "$fstab_txt" | grep "$MOUNT_POINT" | awk '{print $2}'| grep "^$MOUNT_POINT$" 2>&1 >/dev/null
176 # As /mnt/card wasn't configured in fstab, we simply mount the
177 # first unconfigured partition we find
178 FS_MOUNT_POINT="$MOUNT_POINT"
180 # Go through all partitions, and check if one of them is configured
182 for xpart in $OK_PARTS
184 if echo "$fstab_txt" | grep "$xpart" | awk '{print $2}'| grep "^$MOUNT_POINT$" 2>&1 >/dev/null
186 vecho "-> $MOUNT_POINT is reserved by fstab"
193 # Another existing partition is configured for /mnt/card, leave it alone.
194 FS_MOUNT_POINT="$MOUNT_POINT$cnt"
197 FS_MOUNT_POINT="$MOUNT_POINT"
202 FS_MOUNT_POINT="$MOUNT_POINT$cnt"
205 vecho "-> Using mountpoint [$FS_MOUNT_POINT]"
207 vecho "-> Using mountpoint [$FS_MOUNT_POINT] from fstab"
210 ! test -d $FS_MOUNT_POINT && mkdir -p $FS_MOUNT_POINT
213 if ! test -e "$DEVICE"
215 DEV_NUM="`echo "$DEVICE" | sed -n "s/.*\([0-9]\)/\1/p"`"
217 mknod mmcda$DEV_NUM b 60 $DEV_NUM
220 vecho "-> Trying VFAT mount [$SD_OPTS_VFAT]..."
221 mount $FSTYPE -o $SD_OPTS_VFAT $DEVICE $FS_MOUNT_POINT >/dev/null 2>&1
222 MOUNT_RES=`mount | grep $DEVICE`
225 if [ "$MOUNT_RES" = "" ]; then
226 vecho "-> Trying ext2 mount [$SD_OPTS]..."
227 mount $FSTYPE -o $SD_OPTS $DEVICE $FS_MOUNT_POINT
229 vecho "-> VFAT mount was successfull"
232 MOUNT_RES=`mount | grep $DEVICE`
233 if [ "$MOUNT_RES" = "" ]; then
234 vecho "-> Trying failsafe mount..."
235 mount $FSTYPE $DEVICE $FS_MOUNT_POINT
237 vecho "-> EXT2 mount was successfull"
240 # Um what was the function of that?
241 # chkmntsh ${MOUNT_POINT}
245 # I have no idea what this is good for....
246 if [ -d $SMB_MOUNT ] ; then
248 ln -s /mnt/card $SMB_MOUNT
249 mkdir -p $FS_MOUNT_POINT/$INSTALL_DIR
255 # Doesn't work as "fuser" isn't in a base OZ 3.5.[1|2] ROM
256 for part in `mount | grep mmcda|awk '{print $1}'`
269 for part in `mount | grep mmcda|awk '{print $3}'`
272 kill_task "$part" # for QPE
274 umount $part >/dev/null 2>&1 && echo "Umounted [$part]" || echo -e "\n* * * WARNING: umount $part failed! * * *"
276 test -e "$SMB_MOUNT" && rm $SMB_MOUNT
278 test -z "$found_something" && echo "Nothing to do."