Skip to content

Instantly share code, notes, and snippets.

@divx118
Created August 6, 2014 17:25
Show Gist options
  • Save divx118/36357580e8ddfae8bec5 to your computer and use it in GitHub Desktop.
Save divx118/36357580e8ddfae8bec5 to your computer and use it in GitHub Desktop.
mount ext2/3/4 in chromeos scripts should be in /usr/local/bin First run "/usr/local/bin/mountextfs setup" to setup the udev rule.
#!/bin/sh
while [ "x`cat /proc/mounts|grep /dev/loop )`" != "x" -a "x`cat /proc/mounts|grep /media/removable )`" != "x" ];do
cat /proc/mounts|grep /dev/loop|while read line;do
mountpoint=`echo "$line"|cut -d " " -f 2`
loopdevice=`echo "$line"|cut -d " " -f 1`
# check if our ext2/3/4 device is still mounted
device=`cat /proc/mounts|grep $mountpoint|grep -v $loopdevice`
if [ "x$device" = "x" ];then
umount -d $loopdevice
# Be sure the directory is empty before deleting.
if [ "x`ls -A $mountpoint`" = "x" ];then
rm -r $mountpoint
fi
fi
done
sleep 1
done
exit 0
#!/bin/sh
# check if script is run as root
uid=`id -u $USERNAME`
if [ $uid -ne 0 ]; then
echo "$0 must be run as root"
exit 1
fi
# Use this to create a unique name for the loop file.
kernel_devname=$2
# Magic seems to be happening here.
mount_extfs() {
loopfile_path="/usr/local/"
if [ "${ID_FS_TYPE##ext*}" != "$ID_FS_TYPE" ];then
# Creating loop file
dd if=/dev/zero of=$loopfile_path$kernel_devname bs=1KB count=40
mkfs.vfat $loopfile_path$kernel_devname -n $ID_FS_LABEL
# Find unused loop device
loop=`losetup -f`
losetup $loop $loopfile_path$kernel_devname
# Send mount command to crosdisk
dbus-send --print-reply --system --dest=org.chromium.CrosDisks /org/chromium/CrosDisks org.chromium.CrosDisks.Mount string:$loop string:"" array:string:"ro"
found=0
first=0
dbus-monitor --system --monitor 'interface=org.chromium.CrosDisks, member=MountCompleted'|while read line;do
loop_mount=`echo $line|grep "string \"/"`
if [ $first -eq 0 ];then
dbus-send --print-reply --system --dest=org.chromium.CrosDisks /org/chromium/CrosDisks org.chromium.CrosDisks.Mount string:$loop string:"" array:string:"ro"
first=1
fi
if [ "x$loop_mount" != "x" ];then
loop_mount=`echo $loop_mount|cut -d " " -f 2|sed -e s/\"//g`
if [ $found -eq 1 ];then
# TODO: add mount options for permissions and error check
mount $DEVNAME "$loop_mount"
# I guess we should start monitoring /proc/mounts which seems to be the only option.
if [ "x`ps -aux|grep cleanuploop|grep -v grep`" = "x" ];then
/usr/local/bin/cleanuploop
fi
exit 0
fi
if [ "x$loop_mount" = "x$loop" ];then
# we found our loop device next string will be the mountpoint.
found=1
fi
else
# We should add a time restriction so it can't run forever.
continue
fi
done
fi
}
setup_udevrule() {
mkdir -p /run/udev/rules.d
rules_file="/run/udev/rules.d/10-mountextfs.rules"
if [ ! -f "$rules_file" ];then
echo "# udev rule for mounting ext2/3/4 formatted usb devices and sd-cards" > "$rules_file"
echo 'ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd??", RUN+="/usr/local/bin/mountextfs start %k"' >> "$rules_file"
# Update the udev rules
udevadm control --reload-rules
else
echo "$rules_file already exists nothing todo."
exit 0
fi
}
# Just for now everything in one script file.
case "$1" in
start)
mount_extfs
;;
setup)
setup_udevrule
echo "done"
exit 0
;;
*)
echo "Usage: $0 {setup}"
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment