116 lines
2.9 KiB
Bash
Executable File
116 lines
2.9 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
#set -x
|
|
|
|
THIS=$(basename $0)
|
|
|
|
case "${1}" in
|
|
start)
|
|
|
|
if [ ! -f /mass_storage ]; then
|
|
echo "$THIS: no shared storage file, create it" | tee /dev/kmsg
|
|
dd if=/dev/zero of=/mass_storage bs=1M seek=1024 count=0 &&
|
|
cat <<EOF | sfdisk /mass_storage
|
|
,,C
|
|
EOF
|
|
if [ $? -ne 0 ]; then
|
|
echo "$THIS: cannot create shared storage file" | tee /dev/kmsg
|
|
exit 1
|
|
fi
|
|
UNIT_LINE=$(fdisk -l /mass_storage | egrep Units)
|
|
set $UNIT_LINE
|
|
UNIT=${8}
|
|
PART_LINE=$(fdisk -l /mass_storage | egrep /mass_storage1)
|
|
set $PART_LINE
|
|
let START="${2} * $UNIT"
|
|
if [ x"$START" = x -o $START -eq 0 ]; then
|
|
echo "$THIS: cannot find shared storage file start" | tee /dev/kmsg
|
|
exit 2
|
|
fi
|
|
losetup -o$START /dev/loop0 /mass_storage &&
|
|
mkdosfs /dev/loop0 &&
|
|
losetup -d /dev/loop0
|
|
if [ $? -ne 0 ]; then
|
|
echo "$THIS: cannot format shared storage file" | tee /dev/kmsg
|
|
exit 3
|
|
fi
|
|
fi
|
|
lsmod | egrep -q ^g_mass_storage
|
|
if [ $? -eq 0 ]; then
|
|
echo "$THIS; shared storage already started" | tee /dev/kmsg
|
|
exit 4
|
|
fi
|
|
echo "$THIS: start shared storage" | tee /dev/kmsg
|
|
modprobe g_mass_storage file=/mass_storage
|
|
if [ $? -ne 0 ]; then
|
|
echo "$THIS: cannot start shared storage" | tee /dev/kmsg
|
|
exit 5
|
|
fi
|
|
echo "$THIS: shared storage started" | tee /dev/kmsg
|
|
;;
|
|
|
|
stop)
|
|
echo "$THIS: stop shared storage" | tee /dev/kmsg
|
|
lsmod | egrep -q ^g_mass_storage
|
|
if [ $? -eq 0 ]; then
|
|
modprobe -r g_mass_storage
|
|
if [ $? -ne 0 ]; then
|
|
echo "$THIS: cannot stop shared storage" | tee /dev/kmsg
|
|
exit 6
|
|
fi
|
|
else
|
|
echo "$THIS: shared storage not started" | tee /dev/kmsg
|
|
exit 7
|
|
fi
|
|
echo "$THIS: shared storage stopped" | tee /dev/kmsg
|
|
;;
|
|
|
|
mount)
|
|
if [ ! -f /mass_storage ]; then
|
|
echo "$THIS: no shared storage file" | tee /dev/kmsg
|
|
exit 8
|
|
fi
|
|
echo "$THIS: mounting shared storage" | tee /dev/kmsg
|
|
UNIT_LINE=$(fdisk -l /mass_storage | egrep Units)
|
|
set $UNIT_LINE
|
|
UNIT=${8}
|
|
PART_LINE=$(fdisk -l /mass_storage | egrep /mass_storage1)
|
|
set $PART_LINE
|
|
let START="${2} * $UNIT"
|
|
if [ x"$START" = x -o $START -eq 0 ]; then
|
|
echo "$THIS: cannot find shared storage file start" | tee /dev/kmsg
|
|
exit 9
|
|
fi
|
|
losetup -o$START /dev/loop0 /mass_storage &&
|
|
mount -t vfat /dev/loop0 /mnt/
|
|
if [ $? -ne 0 ]; then
|
|
echo "$THIS: cannot mount shared storage file" | tee /dev/kmsg
|
|
exit 10
|
|
fi
|
|
echo "$THIS: shared storage mounted" | tee /dev/kmsg
|
|
;;
|
|
|
|
umount)
|
|
losetup -a | egrep -q /mass_storage
|
|
if [ $? -ne 0 ]; then
|
|
echo "$THIS: no mounted shared storage file" | tee /dev/kmsg
|
|
exit 11
|
|
fi
|
|
echo "unmounting shared storage"
|
|
umount /mnt &
|
|
losetup -d /dev/loop0
|
|
if [ $? -ne 0 ]; then
|
|
echo "$THIS: cannot unmount shared storage" | tee /dev/kmsg
|
|
exit 12
|
|
fi
|
|
echo "$THIS: shared storage unmounted" | tee //dev/kmsg
|
|
;;
|
|
|
|
*)
|
|
echo "Usage ${0} {start|stop|mount|umount}"
|
|
exit 13
|
|
;;
|
|
esac
|
|
|
|
|