-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebian-qemu.sh
executable file
·94 lines (84 loc) · 1.87 KB
/
debian-qemu.sh
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
#!/bin/bash
set -e
#share_folder=""
#image_path=""
vm_username="deb"
vm_ipaddr="localhost"
port_number="2222"
bin_path=$(which qemu-system-x86_64)
img_path="$HOME/.local/virtual-machines/debian/debian.img"
shrd_fold_path="$HOME/Public/share/debian"
vm="debian"
ssh_pid=$(ps a -o pid,cmd \
| grep ssh \
| grep "\-p ${port_number}" \
| wc -l)
qemu_pid=$(ps a -o pid,cmd \
| grep $vm \
| grep ${bin_path} \
| awk '{print $1;}')
## some qemu argumets {{{
# -daemonize
# -nographics
# -serial stdio
# -net nic -net user,smb=/home/hos/Public/share/arch
# -net user,hostfwd=tcp::7777-:8001
# -cdrom /home/prime/dl/iso/manjaro-i3-21.1.2-minimal-210907-linux513.iso
# -redir tcp:2222::22
# -device usb-kbd
# -device usb-mouse
# -clock unix
## }}}
function run () {
if [[ -z ${qemu_pid} ]]; then
nohup \
${bin_path} \
-boot order=d \
-drive file=${img_path},format=qcow2 \
-m 1024 \
-smp 1 \
-nographic \
-enable-kvm \
-net nic,model=virtio \
-net user,hostfwd=tcp::2222-:22,smb=${shrd_fold_path} \
>/dev/null \
&
else
printf '\n%s\n' "The vm is already running"
printf '%s\n' "qemu PID: ${qemu_pid}"
fi
}
function pwroff () {
printf '%s\n' "[sudo shutdown -h now]"
if [[ ! -z ${qemu_pid} ]]; then
ssh ${vm_username}@${vm_ipaddr} -p ${port_number} -t "sudo shutdown -h now"
else
printf '\n%s\n' "The vm is powered off"
fi
}
function connect () {
ssh ${vm_username}@${vm_ipaddr} -p ${port_number}
}
function help () {
cat << _EOF_
on/r: Run the virtual machine
ssh/c: Connect to the machine via ssh
off: Poweroff the machine
_EOF_
if [[ ! -z ${qemu_pid} ]]; then
printf '\n%s\n' "qemu PID: ${qemu_pid}"
if [[ ! -z ${ssh_pid} ]]; then
printf '%s\n' "${ssh_pid} ssh session(s)"
else
printf '%s\n' "No ssh session"
fi
else
printf '\n%s\n' "The vm is powered off"
fi
}
case $1 in
r|on) run ;;
ssh|c) connect ;;
off) pwroff ;;
*) help ;;
esac