/
kvm
executable file
·145 lines (133 loc) · 3.57 KB
/
kvm
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/bash
set -e
BASEDIR=$(dirname $0)
export CONFIG_DIR="$HOME/.kvmrc"
function abort(){
echo "Error: $@"
print_usage
exit -1
}
function print_usage(){
cat << EOF
qemu wrapper
Usage: $(basename $0) [<OPTIONS>] <VM NAME>
Options:
-d, --debug
Enable debug mode.
-h, --help
Prints this help text.
-q, --quiet
Suppresses the use of qmpcat
-s, --socket-file <FILE>
Use specified file as socket file for qmpcat
EOF
}
function forward_port(){
local src_port=$1
local dest_port=$2
[[ "${src_port}" =~ ^[1-9]+[0-9]{0,4}$ ]] || abort "Invalid source port: $src_port"
[[ "${dest_port}" =~ ^[1-9]+[0-9]{0,4}$ ]] || abort "Invalid destination port: $dest_port"
OPTS+=" -redir tcp:${src_port}::${dest_port}"
OPTS+=" -redir udp:${src_port}::${dest_port}"
}
function check_config(){
mkdir -p $CONFIG_DIR
[ -f $CONFIG_DIR/default ] && return
read -p "It seems you don't have a default config. Create one? [Y/n] " create_defaults
[[ "$create_defaults" =~ [Nn] ]] && return
cat >> $CONFIG_DIR/default << EOF
MEMORY=2048
KEYBOARD_LAYOUT="de"
# c for hard drive, d for cdrom
BOOT_DEVICE="c"
# Spawn a graphical window
OPTS="-vga std"
# Comment the above line and uncomment the below line to suppress the graphical window
#OPTS="-display none"
EOF
cat >> $CONFIG_DIR/usbtest << EOF
source $CONFIG_DIR/default
BOOT_DEVICE="d"
# Uncomment the lines below as needed.
# Creation of a hard disk image will be proposed if needed
#DISK_IMAGE=/path/to/usbtest.img
#CDROM=/path/to/cdimage.iso
EOF
}
check_config
VM=""
NO_OPTS="n"
QUIET="n"
while [ $# -gt 0 ];do
if [ "x$NO_OPTS" == "xy" ] && [[ "$1" =~ \-.* ]];then
VM=$1
else
case $1 in
'--')
NO_OPTS="y"
;;
'-h'|'--help')
print_usage
exit 0
;;
'-d'|'--debug')
set -ex
;;
'-s'|'--socket-file')
shift
[ -n "$1" ] || abort "Option -s, --socket-file requires an argument!"
SOCKET_FILE=$1
;;
'-q'|'--quiet')
QUIET="y"
;;
*)
if [ -n "$VM" ];then
abort "Too many VM names specified!"
else
VM=$1
fi
;;
esac
fi
shift
done
if [ -z "$VM" ]; then
echo "Error: Missing argument: VM"
exit -1
elif [ ! -r "$CONFIG_DIR/$VM" ]; then
echo "Error: VM $VM does not exist or is not readable!"
exit -1
fi
source $CONFIG_DIR/$VM
if [ -n "$DISK_IMAGE" ] && [ ! -e "$DISK_IMAGE" ];then
read -p "Hard disk image $DISK_IMAGE does not exit. Create it? [y/N]: " CREATE_DISK
if [[ "$CREATE_DISK" =~ [YyJj] ]];then
read -p "Enter size in GB: " SIZE
qemu-img create "$DISK_IMAGE" -f qcow2 ${SIZE}G || exit -1
fi
fi
OPTS+=" $@"
[ -n "$CDROM" ] && BOOT_DEVICE="d"
echo "Running qemu with options \"$OPTS\""
TEMP_FILE=${SOCKET_FILE:-$(mktemp)}
function run_qemu () {
qemu-system-x86_64 \
-k $KEYBOARD_LAYOUT \
-enable-kvm \
-m $MEMORY \
-localtime \
$( [ -n "$DISK_IMAGE" ] && [ -r "$DISK_IMAGE" ] && echo "-hda $DISK_IMAGE") \
$( [ -n "$CDROM" ] && [ -r "$CDROM" ] && echo "-cdrom $CDROM") \
-boot once=${BOOT_DEVICE},menu=on \
-name "$VM_NAME" \
-qmp unix:$TEMP_FILE,server,nowait \
$OPTS
rm -f $TEMP_FILE
}
run_qemu &
sleep 1
if [ "x$QUIET" != "xy" ]; then
[ -e $TEMP_FILE ] && $BASEDIR/qmpcat $TEMP_FILE
rm -f $TEMP_FILE
fi