rvm

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

commit 6f60bece27810dd998230911f508fce33591835d
Author: sin <sin@2f30.org>
Date:   Sun, 22 Sep 2013 18:20:35 +0100

Initial commit

Diffstat:
AREADME | 27+++++++++++++++++++++++++++
Arvm | 162+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 189 insertions(+), 0 deletions(-)

diff --git a/README b/README @@ -0,0 +1,27 @@ +What is it? +=========== + +You can create tmpfs based Arch Linux installations that you +can chroot inside of them. + +How to use it? +============== + +First set the `user' variable in the script to your username. + +sudo ./rvm create arch /home/<user>/arch-mnt + +This will create a volume named `arch' in the specified path. +It will also backup the rootfs into a .tgz and place it in +/home/<user>/.rvm. + +sudo ./rvm start arch /home/<user>/arch-mnt + +Starts the volume named `arch', at this point you can use +`arch-chroot /home/<user>/arch-mnt /bin/bash' or similar to +chroot into the volume. + +sudo ./rvm stop arch /home/<user>/arch-mnt + +Stops the volume named `arch'. It will backup the rootfs +into /home/<user>/.rvm/arch.tgz. diff --git a/rvm b/rvm @@ -0,0 +1,162 @@ +#!/bin/bash +# +# Arch Linux ramdisk volume manager by quantumdream v0.1 2012 +# Released under WTFPL + +# Modify this to suit your setup +user=dp +rvm_volumes_path=/home/${user}/.rvm + +usage() +{ +cat << EOF +usage: $(basename $0) [create|remove|start|stop] <rvm-name> <rvm-volume-path> +EOF +} + +out() +{ + printf "$1 $2\n" "${@:3}" +} + +error() +{ + out "==> ERROR:" "$@"; +} >&2 + +die() +{ + error "$@" + exit 1 +} + +cleanup() +{ + mountpoint -q ${rvm_path} + if [[ $? -eq 0 ]]; then + umount ${rvm_path} + fi + exit 0 +} + +rvm_create() +{ + rvm_name=$1 + rvm_path=$2 + + if [[ -z ${rvm_name} ]]; then + usage + exit 1 + fi + + if [[ -z ${rvm_path} ]]; then + usage + exit 1 + fi + + echo "Volume name is ${rvm_name}" + echo "Installing Arch Linux to ${rvm_path}" + echo + + if [[ -f ${rvm_volumes_path}/${rvm_name}.tgz ]]; then + die "A volume named ${rvm_name} already exists" + fi + + trap cleanup INT TERM EXIT + + mkdir -p ${rvm_path} + mount -t tmpfs -o size=2G tmpfs ${rvm_path} || exit 1 + + pacstrap -G ${rvm_path} base base-devel || exit 1 + + mkdir -p ${rvm_volumes_path} + echo "Packing up into ${rvm_volumes_path}/${rvm_name}.tgz" + pushd ${rvm_path} >/dev/null + tar cfz ${rvm_volumes_path}/${rvm_name}.tgz . || exit 1 + popd >/dev/null +} + +rvm_remove() +{ + rvm_name=$1 + rvm_path=$2 + + if [[ -z ${rvm_name} ]]; then + usage + exit 1 + fi + + if [[ -z ${rvm_path} ]]; then + usage + exit 1 + fi + + mountpoint -q ${rvm_path} + if [[ $? -eq 0 ]]; then + die "${rvm_name} has been started, please stop it first" + fi + + rm ${rvm_volumes_path}/${rvm_name}.tgz + echo "Removed volume ${rvm_name}" +} + +rvm_start() +{ + rvm_name=$1 + rvm_path=$2 + + mountpoint -q ${rvm_path} + if [[ $? -eq 0 ]]; then + die "${rvm_name} is already started!" + fi + + mkdir -p ${rvm_path} + mount -t tmpfs -o size=2G tmpfs ${rvm_path} || exit 1 + + echo "Unpacking ${rvm_volumes_path}/${rvm_name}.tgz to ${rvm_path}" + tar xzf ${rvm_volumes_path}/${rvm_name}.tgz -C ${rvm_path} || exit 1 + echo "You can now use 'arch-chroot ${rvm_path} /bin/bash'" +} + +rvm_stop() +{ + rvm_name=$1 + rvm_path=$2 + + mountpoint -q ${rvm_path} + if [[ ! $? -eq 0 ]]; then + die "${rvm_name} is not started!" + fi + + trap cleanup INT TERM EXIT + + echo "Packing up into ${rvm_volumes_path}/${rvm_name}.tgz.tmp" + pushd ${rvm_path} >/dev/null + tar cfz ${rvm_volumes_path}/${rvm_name}.tgz.tmp . || exit 1 + popd >/dev/null + + echo "Overwriting ${rvm_volumes_path}/${rvm_name}.tgz" + mv ${rvm_volumes_path}/${rvm_name}.tgz.tmp \ + ${rvm_volumes_path}/${rvm_name}.tgz +} + +action=$1 + +if [[ -z ${action} ]]; then + usage + exit 1 +fi + +if [[ ! ${EUID} = 0 ]]; then + die "This script must be run with root privileges" +fi + +if [[ ${action} = "create" ]]; then + rvm_create $2 $3 +elif [[ ${action} = "remove" ]]; then + rvm_remove $2 $3 +elif [[ ${action} = "start" ]]; then + rvm_start $2 $3 +elif [[ ${action} = "stop" ]]; then + rvm_stop $2 $3 +fi