frnmst-archives
/
qvm
Archived
1
0
Fork 0
Browse Source

Added decent functions and options. Cleanup. Updated readme.

dev
frnmst/Franco Masotti 6 years ago
parent
commit
64803e9978
  1. 3
      .gitignore
  2. 41
      README.md
  3. 4
      configvmrc
  4. 119
      qvm

3
.gitignore vendored

@ -0,0 +1,3 @@
*.iso
*.qcow2
*.qcow2.mod

41
README.md

@ -1,8 +1,11 @@
# qvm
Trivial management of 64 bit virtual machines with qemu.
## What this script will do
It can handle:
- Virtual hard disk creation, backup and deletion.
- Basic network management: two ports are exposed to the host
machine (but you can add as many as you want). One of these
@ -12,43 +15,33 @@ It can handle:
these options.
## Setup information and usage
- You need a 64 bit machine.
- Modify `configvmrc` based on your needs.
Variables are self-explanatory and I have kept mine
as an example.
```
# pacman -S qemu
$ . ./configvmrc
$ mkdir -p "$shared_data_path"
$ ./qvm -i
$ ./qvm -b
```
# pacman -S qemu
$ . ./configvmrc
$ mkdir -p "$shared_data_path"
$ ./qvm -i
$ ./qvm -b
- Now you can run the virtual machine:
```
$ ./qvm -r
```
$ ./qvm -r
- On your guest machine add the following in `/etc/fstab`:
```
host_share /home/vm/shared 9p trans=virtio,version=9p2000.L 0 0
```
host_share /home/vm/shared 9p trans=virtio,version=9p2000.L 0 0
This will enable the shared directory automatically (no mount commands of any
sort).
- You can connect to your virtual machine via SSH, using the default values:
```
ssh -p 2222 <guest_user_name>@127.0.0.1
```
### Optional step
- If you are not interested in using the GUI, but you only need textual interface
(i.e: SSH), you can simply add the following, along with the other options present
under `-r`:
```
-display none \
```
ssh -p 2222 <guest_user_name>@127.0.0.1
## License
CC0.

4
configvmrc

@ -10,7 +10,7 @@
# with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.
img_name="antergos.iso"
img_name="parabola-mate-2016.07.27-dual.iso"
vhd_size="16G"
vhd_type="qcow2"
@ -26,5 +26,5 @@ ssh_host_port="2222"
# vm port.
ssh_guest_port="22"
shared_data_path="/home/parabola/dev/unife/intern/qemu/shared_host_guest"
shared_data_path="/home/parabola/dev/unife/qvm/shared_host_guest"
mount_tag="host_share"

119
qvm

@ -12,21 +12,22 @@
# with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.
. ./configvmrc
input="$1"
if [ -z "$input" ] || [ "$input" = "-h" ]; then
help()
{
cat <<-EOF
Usage: qvm [OPTION]
Trivial management of 64 bit virtual machines with qemu.
Options:
-b backup vhd
-c create new vhd
-d delete vhd backup
-h print this help
-i install img on vhd
-r run vm
-b, --backup backup vhd
-c, --create create new vhd
-d, --delete delete vhd backup
--delete-original delete original vhd
-h, --help print this help
-i, --install install img on vhd
-r, --run run vm
--run-nox run vm without opening a graphical window
(useful fir background jobs like SSH)
Only a single option is accepted.
Before running any virtual machine you must create a backup first.
@ -34,30 +35,52 @@ Before running any virtual machine you must create a backup first.
CC0
Written in 2016 by Franco Masotti/frnmst <franco.masotti@student.unife.it>
EOF
exit 1
fi
}
unrecognized_option()
{
printf "%s\n" "Try 'qvm --help' for more information"
} 1>&2-
[ "$input" = "-b" ] \
&& qemu-img create -f "$vhd_type" \
backup()
{
qemu-img create -f "$vhd_type" \
-b "$vhd_name" \
"$vhd_name".mod
}
create()
{
qemu-img create -f "$vhd_type" \
"$vhd_name" \
"$vhd_size"
}
[ "$input" = "-d" ] \
&& rm "$vhd_name".mod
delete()
{
rm "$vhd_name".mod
}
[ "$input" = "-c" ] \
&& qemu-img create -f "$vhd_type" "$vhd_name" "$vhd_size"
delete_original()
{
rm "$vhd_name"
}
[ "$input" = "-i" ] \
&& qemu-system-x86_64 -m "$vm_memory" \
-enable-kvm -cdrom "$img_name" \
-boot order=d "$vhd_name"
installs()
{
qemu-system-x86_64 -m "$vm_memory" \
-enable-kvm \
-cdrom "$img_name" \
-boot order=d \
"$vhd_name" &
}
[ "$input" = "-r" ] \
&& [ -f "$vhd_name".mod ] \
&& qemu-system-x86_64 \
run()
{
argc="$1"
if [ -f "$vhd_name".mod ]; then
qemu-system-x86_64 \
-m "$vm_memory" \
-enable-kvm \
-device e1000,netdev=user.0 \
@ -66,7 +89,47 @@ id=user.0,hostfwd=tcp::"$host_port"-:"$guest_port",\
hostfwd=tcp::"$ssh_host_port"-:"$ssh_guest_port" \
-virtfs local,path="$shared_data_path",\
security_model=none,mount_tag="$mount_tag" \
"$vhd_name".mod \
|| { printf "Make a backup first.\n"; exit 1; }
-display "$argc" \
"$vhd_name".mod &
else
printf "Make a backup and an installation first.\n"
exit 1
fi
}
main()
{
local argc="$1"
local options="bcdhir"
local long_options="backup,create,delete,delete-original\
,help,install,run,run-nox"
local opts
local opt
[ -z "$argc" ] && argc="-r"
opts="$(getopt --options $options --longoptions $long_options -- $argc)"
[ $? -ne 0 ] && unrecognized_option && return 1
eval set -- "$opts"
# Source variables globally.
. ./configvmrc
for opt in $opts; do
case "$opt" in
-- ) ;;
-b | --backup ) backup ;;
-c | --create ) create ;;
-d | --delete ) delete ;;
--delete-original ) delete_original ;;
-h | --help ) help ;;
-i | --install ) installs ;;
-r | --run ) run gtk ;;
--run-nox ) run none ;;
esac
done
}
} &
main "$*"