#!/bin/sh ################################################ # Declarations ################################################ this_script=${0##*/} name_default=".crypt.dsk" path_default="/home" vnode_default="svnd0c" mountpoint_default="/crypt" size="100" # command paths dd="/bin/dd" mkdir="/bin/mkdir" newfs="/sbin/newfs" mount="/sbin/mount" umount="/sbin/umount" vnconfig="/sbin/vnconfig" cmds="$dd $mkdir $newfs $mount $umount $vnconfig" ################################################ # Functions ################################################ f_usage () { echo "Usage: $this_script -(c|m|u)" echo " -c = create" echo " -m = mount" echo " -u = unmount" } f_getvar () { echo -n "$2 [$3]: " read entry entry=${entry:=$3} eval $1=$entry } f_gather () { while [[ -z $vnode ]] || [[ -z $mountpoint ]]; do f_getvar vnode "Vnode" $vnode_default f_getvar mountpoint "Mount point" $mountpoint_default done vdev="/dev/$vnode" if [[ $1 = "-c" ]] || [[ $1 = "-m" ]]; then while [[ -z $name ]] || [[ -z $path ]]; do f_getvar path "Disk image path" $path_default f_getvar name "Disk image name" $name_default done image="$path/$name" fi } f_mount () { ## Check mount point if [ ! -d $mountpoint ]; then echo "Mount point $mountpoint does not exist." f_getvar yesno "Create it?" "y" if [ $yesno = "y" ]; then $mkdir $mountpoint fi fi if [ -d $mountpoint ]; then ## Assign an encrypted virtual inode $vnconfig -ck -v $vdev $image if [ $1 = "-c" ]; then ## Format the disk $newfs $vnode fi ## Mount it $mount $vdev $mountpoint fi } f_umount () { ## Unmount it $umount $mountpoint ## Unassign the virtual inode $vnconfig -u -v $vdev } f_create () { yesno="y" if [ -f $image ]; then yesno="n" echo "Disk image $image already exists." f_getvar yesno "Overwrite it?" $yesno fi if [ $yesno = "y" ]; then # Warning: there is no bounds check on $size f_getvar size "Enter desired size of $image in MB" $size echo "Creating $image..." $dd if=/dev/arandom of=$image bs=1048576 count=$size f_mount $1 fi } ################################################ # Main ################################################ for i in `echo $cmds`; do if [ ! -x $i ]; then missing="$missing $i" fi done if [[ -z $missing ]]; then if [[ $# = '1' ]]; then f_gather $1 case $1 in '-m') if [[ -f $image ]]; then f_mount $1 else echo "The $image does not exist." fi ;; '-u') f_umount $1 ;; '-c') f_create $1 ;; *) f_usage ;; esac else f_usage fi else for i in `echo $missing`; do echo "Missing: $i" done fi