aboutsummaryrefslogtreecommitdiff
path: root/kvm-creator
blob: 744cb960fc6e38223c62abf98067b74eeffab3b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/bash
#set -x

# Author: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
# Date: 2009-10-08
# License: GPL v3+

CMD="$1"
shift

NAME="$1"
VG="${2:-vg_$(hostname)0}"
SIZE="${3:-3G}"
RAM="${4:-512}"
DISK="/dev/mapper/${VG}-${NAME}"

[ "$CMD" == "create" ] && mkdir -p /etc/sv/kvm

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:-$(printf "%s:%02x" "$(cat /etc/sv/kvm/*/env/MAC | sort | head -n1 | cut -f 1-5 -d: )" "$(( 0x$( cat /etc/sv/kvm/*/env/MAC | cut -f 6 -d:  | sort -n  | tail -n 1 ) + 1 ))" )}"
else
  TAP=tap0
  # use a locally-administered MAC address:
  MAC=02:00:00:00:00:01
fi

usage() {

	die "USAGE: kvm-creator create|destroy|demo guestname [volumegroup [disksize [ram [tap [mac] ] ] ] ]"

}

die() {

  echo "$1"
  exit_code=1
  [ -n "$2" ] && exit_code="$2"
  exit $exit_code

}

destroy() {

    update-service --remove "/etc/sv/kvm/$NAME"
    rm -rf "/etc/sv/kvm/$NAME"
    deluser "$NAME"
    lvremove "$VG/$NAME"
    rm -rf "/home/$NAME"

}

validate() {

  # Make sure none of the pieces already exist.
  [ -z "$NAME" ] && die "Please pass the name of the virtual server to create" || :
  [ -z "$VG" ] && [ "$CMD" == "create" ] && die "Please pass the name of the volume group to use" || :
  getent passwd "$NAME" > /dev/null && die "The username '$NAME' already exists." || :
  getent group "$NAME" > /dev/null && 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." || :
  [ -e $DISK ] && die "The logical volume $NAME already exists." || :

}

create() {

    set -e
    validate
    adduser "$NAME" --disabled-password --gecos "$NAME KVM user,,,"
    mkdir -p "/home/$NAME/"{.ssh,vms,"vms/$NAME"}
    chown "$NAME:$NAME" "/home/$NAME/"{.ssh,vms,"vms/$NAME"}
    cp /root/.ssh/authorized_keys "/home/$NAME/.ssh/"
    lvcreate --name "$NAME" --size "$SIZE" $VG
    mkdir "/etc/sv/kvm/$NAME"{,/log,/env}
    cat > "/etc/sv/kvm/$NAME/log/run" <<EOF
#!/bin/sh

exec 2>&1
exec chpst -e ../env /usr/local/sbin/kvm-manager log
EOF
    cat > "/etc/sv/kvm/$NAME/run" <<EOF
#!/bin/sh

exec 2>&1
exec chpst -e ./env /usr/local/sbin/kvm-manager up
EOF
    cat > "/etc/sv/kvm/$NAME/finish" <<EOF
#!/bin/sh

exec 2>&1
exec chpst -e ./env /usr/local/sbin/kvm-manager down
EOF
    chmod a+x "/etc/sv/kvm/$NAME/"{run,finish,log/run}
    echo "$NAME" > "/etc/sv/kvm/$NAME/env/OWNER"
    echo "$NAME" > "/etc/sv/kvm/$NAME/env/VMNAME"
    echo "$TAP" > "/etc/sv/kvm/$NAME/env/TAP"
    echo "$RAM" > "/etc/sv/kvm/$NAME/env/RAM"
    echo "$MAC" > "/etc/sv/kvm/$NAME/env/MAC"
		echo "$DISK" > "/etc/sv/kvm/$NAME/env/KVMOPTS"

}

demo() {

    validate
    for foo in NAME VG TAP RAM MAC SIZE DISK ; do
      echo "$foo : ${!foo}"
    done

}

[ "$CMD" != "create" ] && [ "$CMD" != "destroy" ] && [ "$CMD" != "demo" ] && usage

"$CMD"