1 |
#!/bin/sh |
2 |
|
3 |
test -z $vmname && exit 1 |
4 |
|
5 |
vmdir="$HOME/VMs" |
6 |
piddir="$HOME/PIDs" |
7 |
logdir="$HOME/log" |
8 |
vmfile="$vmdir/$vmname" |
9 |
pidfile="$piddir/$vmname" |
10 |
logfile="$logdir/$vmname" |
11 |
qemucmd="qemu-kvm -nographic -pidfile $pidfile -hda $vmfile -net nic,vlan=0 -net user,vlan=0,hostfwd=tcp::$sshport-:22 $QEMUOPT" |
12 |
|
13 |
function running() |
14 |
{ |
15 |
test -f $pidfile || return 1 |
16 |
pid=`cat $pidfile` |
17 |
test -d "/proc/$pid" |
18 |
} |
19 |
|
20 |
function stop() |
21 |
{ |
22 |
test -f $pidfile || return 1 |
23 |
pid=`cat $pidfile` |
24 |
kill "$pid" && rm -f "$pidfile" |
25 |
} |
26 |
|
27 |
function start() |
28 |
{ |
29 |
running && echo "VM is already running" && return 1 |
30 |
nohup $qemucmd > $logfile 2>&1 & |
31 |
} |
32 |
|
33 |
case "$1" in |
34 |
start) |
35 |
start |
36 |
;; |
37 |
stop) |
38 |
stop |
39 |
;; |
40 |
restart) |
41 |
start |
42 |
stop |
43 |
;; |
44 |
status) |
45 |
running |
46 |
vmrunning=$? |
47 |
test $vmrunning -eq 0 && echo "VM $vmname is running" |
48 |
test $vmrunning -eq 0 || echo "VM $vmname is stopped" |
49 |
;; |
50 |
ssh) |
51 |
running && ssh -p $sshport localhost |
52 |
;; |
53 |
esac |