Skip to content

Instantly share code, notes, and snippets.

@niwinz
Forked from timmc/ramdisk-target.sh
Last active May 13, 2016 00:47
Show Gist options
  • Save niwinz/0aa61579e6c50d176b92 to your computer and use it in GitHub Desktop.
Save niwinz/0aa61579e6c50d176b92 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Overwrite ./target with a tmpfs ramdisk. Prompts for sudo.
function usage() {
echo 'Usage: `ramdisk-target.sh recreate|restore`'
}
if [ ! -f "project.clj" ]; then
echo "Not in Clojure project."
exit 2
fi
function remove-old() {
if mountpoint -q ./target; then
if fuser --mount ./target; then
echo "Processes are currently using the ramdisk!"
exit 2
fi
echo "Unmounting ./target"
sudo umount ./target || exit 1
fi
if [ "`stat --format="%F" ./target`" = 'directory' ]; then
echo "Deleting ./target directory"
rm -rf ./target || exit 1
fi
}
## Primitives
function restore() {
remove-old
mkdir ./target
}
function mount-on-point() {
group=`stat --format='%G' .`
user=`stat --format='%U' .`
echo "Creating mount point with ownership $user:$group"
sudo mount -t tmpfs -o size=400M tmpfs ./target
sudo chown $group:$user ./target
}
## Actions
function become-mounted() {
# Remove any existing target so that we don't end up with old stuff
# exposed when we umount.
restore
mount-on-point
}
## Dispatch
case "$1" in
recreate)
become-mounted
;;
restore)
restore
;;
*)
usage
exit 0
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment