rvm

ramdisk volume manager
git clone git://git.2f30.org/rvm
Log | Files | Refs | README

rvm (2797B)


      1 #!/bin/bash
      2 #
      3 # Arch Linux ramdisk volume manager by quantumdream v0.1 2012
      4 # Released under WTFPL
      5 
      6 # Modify this to suit your setup
      7 user=dp
      8 rvm_volumes_path=/home/${user}/.rvm
      9 
     10 usage()
     11 {
     12 cat << EOF
     13 usage: $(basename $0) [create|remove|start|stop] <rvm-name> <rvm-volume-path>
     14 EOF
     15 }
     16 
     17 out()
     18 {
     19 	printf "$1 $2\n" "${@:3}"
     20 }
     21 
     22 error()
     23 {
     24 	out "==> ERROR:" "$@";
     25 } >&2
     26 
     27 die()
     28 {
     29 	error "$@"
     30 	exit 1
     31 }
     32 
     33 cleanup()
     34 {
     35 	mountpoint -q ${rvm_path}
     36 	if [[ $? -eq 0 ]]; then
     37 		umount ${rvm_path}
     38 	fi
     39 	exit 0
     40 }
     41 
     42 rvm_create()
     43 {
     44 	rvm_name=$1
     45 	rvm_path=$2
     46 
     47 	if [[ -z ${rvm_name} ]]; then
     48 		usage
     49 		exit 1
     50 	fi
     51 
     52 	if [[ -z ${rvm_path} ]]; then
     53 		usage
     54 		exit 1
     55 	fi
     56 
     57 	echo "Volume name is ${rvm_name}"
     58 	echo "Installing Arch Linux to ${rvm_path}"
     59 	echo
     60 
     61 	if [[ -f ${rvm_volumes_path}/${rvm_name}.tgz ]]; then
     62 		die "A volume named ${rvm_name} already exists"
     63 	fi
     64 
     65 	trap cleanup INT TERM EXIT
     66 
     67 	mkdir -p ${rvm_path}
     68 	mount -t tmpfs -o size=2G tmpfs ${rvm_path} || exit 1
     69 
     70 	pacstrap -G ${rvm_path} base base-devel || exit 1
     71 
     72 	mkdir -p ${rvm_volumes_path}
     73 	echo "Packing up into ${rvm_volumes_path}/${rvm_name}.tgz"
     74 	pushd ${rvm_path} >/dev/null
     75 	tar cfz ${rvm_volumes_path}/${rvm_name}.tgz . || exit 1
     76 	popd >/dev/null
     77 }
     78 
     79 rvm_remove()
     80 {
     81 	rvm_name=$1
     82 	rvm_path=$2
     83 
     84 	if [[ -z ${rvm_name} ]]; then
     85 		usage
     86 		exit 1
     87 	fi
     88 
     89 	if [[ -z ${rvm_path} ]]; then
     90 		usage
     91 		exit 1
     92 	fi
     93 
     94 	mountpoint -q ${rvm_path}
     95 	if [[ $? -eq 0 ]]; then
     96 		die "${rvm_name} has been started, please stop it first"
     97 	fi
     98 
     99 	rm ${rvm_volumes_path}/${rvm_name}.tgz
    100 	echo "Removed volume ${rvm_name}"
    101 }
    102 
    103 rvm_start()
    104 {
    105 	rvm_name=$1
    106 	rvm_path=$2
    107 
    108 	mountpoint -q ${rvm_path}
    109 	if [[ $? -eq 0 ]]; then
    110 		die "${rvm_name} is already started!"
    111 	fi
    112 
    113 	mkdir -p ${rvm_path}
    114 	mount -t tmpfs -o size=2G tmpfs ${rvm_path} || exit 1
    115 
    116 	echo "Unpacking ${rvm_volumes_path}/${rvm_name}.tgz to ${rvm_path}"
    117 	tar xzf ${rvm_volumes_path}/${rvm_name}.tgz -C ${rvm_path} || exit 1
    118 	echo "You can now use 'arch-chroot ${rvm_path} /bin/bash'"
    119 }
    120 
    121 rvm_stop()
    122 {
    123 	rvm_name=$1
    124 	rvm_path=$2
    125 
    126 	mountpoint -q ${rvm_path}
    127 	if [[ ! $? -eq 0 ]]; then
    128 		die "${rvm_name} is not started!"
    129 	fi
    130 
    131 	trap cleanup INT TERM EXIT
    132 
    133 	echo "Packing up into ${rvm_volumes_path}/${rvm_name}.tgz.tmp"
    134 	pushd ${rvm_path} >/dev/null
    135 	tar cfz ${rvm_volumes_path}/${rvm_name}.tgz.tmp . || exit 1
    136 	popd >/dev/null
    137 
    138 	echo "Overwriting ${rvm_volumes_path}/${rvm_name}.tgz"
    139 	mv ${rvm_volumes_path}/${rvm_name}.tgz.tmp \
    140 		${rvm_volumes_path}/${rvm_name}.tgz
    141 }
    142 
    143 action=$1
    144 
    145 if [[ -z ${action} ]]; then
    146 	usage
    147 	exit 1
    148 fi
    149 
    150 if [[ ! ${EUID} = 0 ]]; then
    151 	die "This script must be run with root privileges"
    152 fi
    153 
    154 if [[ ${action} = "create" ]]; then
    155 	rvm_create $2 $3
    156 elif [[ ${action} = "remove" ]]; then
    157 	rvm_remove $2 $3
    158 elif [[ ${action} = "start" ]]; then
    159 	rvm_start $2 $3
    160 elif [[ ${action} = "stop" ]]; then
    161 	rvm_stop $2 $3
    162 fi