Skip to content

Instantly share code, notes, and snippets.

@someara
Created July 20, 2012 04:42
Show Gist options
  • Save someara/3148738 to your computer and use it in GitHub Desktop.
Save someara/3148738 to your computer and use it in GitHub Desktop.
cbash
# boolean checks
function if_mounted() {
local mount_point="$1"
local device="$2"
mount | grep "^`printf '%q' "${mount_point}"`" | awk '{ print $3 }' | grep "`printf '%q' "${device}"`" 2>&1>/dev/null
}
function if_lofi_associated(){
local block_image="$1"
lofiadm | grep "`printf '%q' ${block_image}`" | awk '{ print $2 }' | grep "^${block_image}$" 2>&1>/dev/null
}
# helpers - definitions? return a string.
function get_lofi_association(){
local block_image="$1"
lofiadm | grep "`printf '%q' ${block_image}`" | awk '{ print $1 }'
}
function make_lofi_association(){
local block_image="$1"
if ! if_lofi_associated "${block_image}"; then
lofiadm -a "${block_image}";
fi
}
# attempters / "promises"
function append_if_no_such_line() {
local line="$1";
local file="$2";
if ! grep "^`printf '%q' "${line}"`$" ${file} 2>&1>/dev/null ; then echo "${line}" >> ${file} ; fi
}
function mounted_block_image() {
local filesytem_type="$1";
local block_image="$2";
local block_image_mount="$3";
if if_lofi_associated "${block_image}"; then
local lofidev=`get_lofi_association "${block_image}"`
else
local lofidev=`make_lofi_association "${block_image}"`
fi
if ! if_mounted "${block_image_mount}" "${lofidev}"; then
/usr/bin/mkdir -p "${block_image_mount}"
mount -F ${filesytem_type} ${lofidev}:c ${block_image_mount}
fi
}
function directory() {
local path="$1";
local owner="$2";
local group="$3";
local mode="$4";
if [ ! -d ${path} ]; then mkdir -p ${path}; fi
if [ `stat -c '%U:%G' ${path}` != "${owner}:${group}" ]; then chown "${owner}:${group}" ${path} ; fi
if [ `stat -c '%a' ${path}` != ${mode} ]; then chmod ${mode} ${path}; fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment