diff options
-rwxr-xr-x | kvm-creator | 57 |
1 files changed, 32 insertions, 25 deletions
diff --git a/kvm-creator b/kvm-creator index eb7fb96..763b6b9 100755 --- a/kvm-creator +++ b/kvm-creator @@ -17,18 +17,20 @@ RAM="${4:-512}" ls /etc/sv/kvm/* &> /dev/null if [ "$?" -eq 0 ]; then - TAP="${5:-tap$(( $( cat /etc/sv/kvm/*/env/TAP | sed 's/^tap//' | sort -n | tail -n 1 ) + 1 ))}" - MAC="${6:-$(cat /etc/sv/kvm/*/env/MAC | head -n1 | cut -f 1-5 -d: ):$(( $( cat /etc/sv/kvm/*/env/MAC | cut -f 6 -d: | sort -n | tail -n 1 ) + 1 ))}" + TAP="${5:-tap$(( $( cat /etc/sv/kvm/*/env/TAP | sed 's/^tap//' | sort -n | tail -n 1 ) + 1 ))}" + MAC="${6:-$(cat /etc/sv/kvm/*/env/MAC | head -n1 | cut -f 1-5 -d: ):$(( $( cat /etc/sv/kvm/*/env/MAC | cut -f 6 -d: | sort -n | tail -n 1 ) + 1 ))}" else - TAP=tap0 - MAC=00:00:00:00:00:00 + TAP=tap0 + MAC=00:00:00:00:00:00 fi die() { - echo "$1" - exit_code=1 - [ -n "$2" ] && exit_code="$2" - exit $exit_code + + echo "$1" + exit_code=1 + [ -n "$2" ] && exit_code="$2" + exit $exit_code + } destroy() { @@ -42,26 +44,29 @@ destroy() { } validate() { - # Make sure none of the pieces already exist. - [ -z "$NAME" ] && die "Please pass the name of the virtual server to create" - [ -z "$VG" ] && die "Please pass the name of the volume group to use" - getent passwd "$NAME" > /dev/null - [ "$?" -eq "0" ] && die "The username '$NAME' already exists." - getent group "$NAME" > /dev/null - [ "$?" -eq 0 ] && die "The group '$NAME' already exists." - [ -d /home/"$NAME" ] && die "The directory '/home/$NAME' already exists." - [ -d /etc/sv/kvm/"$NAME" ] && die "The directory '/etc/sv/kvm/$NAME' already exists." - [ -d /dev/mapper/${VG}-${NAME} ] && die "The logical volume $NAME already exists." + + # Make sure none of the pieces already exist. + [ -z "$NAME" ] && die "Please pass the name of the virtual server to create" + [ -z "$VG" ] && die "Please pass the name of the volume group to use" + getent passwd "$NAME" > /dev/null + [ "$?" -eq "0" ] && die "The username '$NAME' already exists." + getent group "$NAME" > /dev/null + [ "$?" -eq 0 ] && die "The group '$NAME' already exists." + [ -d /home/"$NAME" ] && die "The directory '/home/$NAME' already exists." + [ -d /etc/sv/kvm/"$NAME" ] && die "The directory '/etc/sv/kvm/$NAME' already exists." + [ -d /dev/mapper/${VG}-${NAME} ] && die "The logical volume $NAME already exists." + } create() { + set -e - validate + validate adduser "$NAME" --disabled-password --gecos "$NAME KVM user,,," - addgroup "$NAME" kvm - for dir in .ssh vms "vms/$NAME"; do - [ ! -d "/home/$NAME/$dir" ] && mkdir "/home/$NAME/$dir" - done + addgroup "$NAME" kvm + for dir in .ssh vms "vms/$NAME"; do + [ ! -d "/home/$NAME/$dir" ] && mkdir "/home/$NAME/$dir" + done touch "/home/$NAME/vms/$NAME/netboot" chown "$NAME:$NAME" "/home/$NAME/"{.ssh,vms,"vms/$NAME","vms/$NAME/netboot"} cp /root/.ssh/authorized_keys "/home/$NAME/.ssh/" @@ -95,10 +100,12 @@ EOF } demo() { - validate + + validate for foo in NAME VG TAP RAM MAC SIZE ; do - echo "$foo : ${!foo}" + echo "$foo : ${!foo}" done + } "$CMD" |