summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaulo Alcantara <palcantara@suse.de>2018-05-02 18:03:43 -0300
committerPaulo Alcantara <palcantara@suse.de>2018-05-02 18:03:43 -0300
commit789d16bdb8ad7fba57a22497e6455252dd553258 (patch)
tree4c8fe8d999fcaffdee5d203c184ce11a341bd60b
parent6dec74731d9d22cd6d221dc97fdcee08ca22ecfe (diff)
downloadvmhtool-789d16bdb8ad7fba57a22497e6455252dd553258.tar.gz
vmhtool-789d16bdb8ad7fba57a22497e6455252dd553258.tar.xz
vmhtool-789d16bdb8ad7fba57a22497e6455252dd553258.zip
Do some code cleanup.
Signed-off-by: Paulo Alcantara <palcantara@suse.de>
-rwxr-xr-xvmhelper189
1 files changed, 94 insertions, 95 deletions
diff --git a/vmhelper b/vmhelper
index 40d8666..d177b1b 100755
--- a/vmhelper
+++ b/vmhelper
@@ -12,73 +12,62 @@ use File::Copy;
use Cwd 'abs_path';
use File::Basename;
-my $install_iso_path = '';
-my $virtio_drv_path = '';
-my $kernel_path = '';
-my $build_kernel;
-my $config_path = '';
-my $install_modules;
-my $list_vms;
-my $delete_vm;
-my $windows_guest;
-my $print_help;
-my $initrd_img = '';
-my $shared_dirs = '';
-my $gen_new_conf;
-my $nographic;
-my $vm_hdds = '';
+my %cmd_opts = ();
sub usage {
print STDERR "Usage: " . basename($0). " [name] [options]
- -h, --help print this help
- -i, --install image create a new vm and install it from an ISO image
- -v, --virtio image attach virtio drivers ISO image into guest
- -I, --initrd pass initramfs image to kernel
- -k, --kernel path run the guest with a custom kernel image
- -b, --build build custom kernel before running guest
- -c, --config file custom kernel config file
- -m, --modules install kernel modules in rootfs image
- -s, --share=foo,bar,... share directories between host and guest
- -l, --list list all vms
- -d, --delete delete vm
- -w, --win MS windows specifics
- -n, --new generate new config file
- -N, --nographic disable graphics when running vm
- -D, --disk=img0,img1,... boot up a vm with additional hdd images
+ --help print this help
+ --install image create a new vm and install it from an ISO image
+ --virtio image attach virtio drivers ISO image into guest
+ --initrd pass initramfs image to kernel
+ --kernel path run the guest with a custom kernel image
+ --rootfs specify the rootfs that will get passed thourgh the
+ kernel cmdline
+ --build build custom kernel before running guest
+ --config file custom kernel config file
+ --modules install kernel modules in rootfs image
+ --share=foo,bar,... share directories between host and guest
+ --list list all vms
+ --delete delete vm
+ --win MS windows specifics
+ --new generate new config file
+ --nographic disable graphics when running vm
+ --hdd=img0,img1,... boot up a vm with additional hdd images
";
exit 1;
}
-GetOptions("install=s" => \$install_iso_path,
- "virtio=s" => \$virtio_drv_path,
- "kernel=s" => \$kernel_path,
- "build" => \$build_kernel,
- "initrd|I=s" => \$initrd_img,
- "config=s" => \$config_path,
- "modules" => \$install_modules,
- "list" => \$list_vms,
- "delete" => \$delete_vm,
- "help" => \$print_help,
- "win" => \$windows_guest,
- "share=s" => \$shared_dirs,
- "new" => \$gen_new_conf,
- "disk|D=s" => \$vm_hdds,
- "nographic|N" => \$nographic) or usage();
-
-usage() if $print_help;
+GetOptions("install=s" => \$cmd_opts{install_iso_path},
+ "virtio=s" => \$cmd_opts{virtio_drv_path},
+ "kernel=s" => \$cmd_opts{kernel_path},
+ "build" => \$cmd_opts{build_kernel},
+ "initrd" => \$cmd_opts{initrd_img},
+ "config=s" => \$cmd_opts{config_path},
+ "modules" => \$cmd_opts{install_modules},
+ "list" => \$cmd_opts{list_vms},
+ "delete" => \$cmd_opts{delete_vm},
+ "help" => \$cmd_opts{print_help},
+ "win" => \$cmd_opts{windows_guest},
+ "share=s" => \$cmd_opts{shared_dirs},
+ "new" => \$cmd_opts{gen_new_conf},
+ "hdd=s" => \$cmd_opts{vm_hdds},
+ "rootfs=s" => \$cmd_opts{rootfs_path},
+ "nographic" => \$cmd_opts{nographic}) or usage();
+
+usage() if $cmd_opts{print_help};
my $vm_path = $ENV{'VMHELPER_VM_PATH'};
die "VMHELPER_VM_PATH env var is not set" unless $vm_path;
-die "$vm_path does not exist" unless -e $vm_path and -d $vm_path;
+die "$vm_path does not exist" unless -e $vm_path or -d $vm_path;
my $vm_info = $vm_path . '/.vminfo';
my $vm_conf = $vm_path . '/.vmconf';
sub parse_user_hdds {
- return '' unless $vm_hdds;
+ return '' unless $cmd_opts{vm_hdds};
my $args = '';
my $num = 1;
- my @hdds = split(/,/, $vm_hdds);
+ my @hdds = split(/,/, $cmd_opts{vm_hdds});
foreach (@hdds) {
my $id = sprintf "hd%02x", rand(256);
$args .= " -device scsi-hd,drive=$id -drive if=none,file=$_,cache=writeback,id=$id,format=raw,unit=$num ";
@@ -141,7 +130,7 @@ sub parse_conf {
close($f) or die "failed to close config file: $!";
}
-if ($gen_new_conf || ! -e $vm_conf || ! -f $vm_conf) {
+if ($cmd_opts{gen_new_conf} || ! -e $vm_conf || ! -f $vm_conf) {
gen_conf();
exit 0;
}
@@ -166,7 +155,7 @@ sub resolve_name {
return "<unknown>";
}
-if ($list_vms) {
+if ($cmd_opts{list_vms}) {
open(my $conf, "<", $vm_info) or exit 0;
printf STDERR "%-25s%-20s%-15s\n", "name", "macaddr", "ipaddr";
while (<$conf>) {
@@ -242,13 +231,13 @@ my %qemu_linux_opts = (
mach => "-machine q35",
cpu => "-enable-kvm -cpu host -smp sockets=1,cores=1,threads=1",
mem => "-m 1024",
- bus => "-device ich9-ahci,id=ahci -device qemu-xhci,id=xhci -device virtio-scsi-pci,id=scsi",
- hdd => " -device ide-hd,drive=hd,bus=ahci.0 -drive if=none,id=hd,cache=writeback,format=raw,file=%s",
+ bus => "-device qemu-xhci,id=xhci -device virtio-scsi-pci,id=scsi",
+ hdd => " -device scsi-hd,drive=hd -drive if=none,file=%s,cache=writeback,id=hd,format=raw,unit=0",
net => "-device e1000,netdev=net0,mac=%s -netdev type=tap,id=net0,ifname=%s,script=no,downscript=no",
usb => "-device usb-tablet",
cdrom => "-cdrom %s",
kernel => "-kernel %s",
- cmdline => "-append \"root=/dev/sda2 rw console=ttyS0\"",
+ cmdline => "-append \"root=%s rw console=ttyS0\"",
initrd => "-initrd %s",
share => ''
);
@@ -259,7 +248,7 @@ my %qemu_win_opts = (
mem => "-m 1024",
bus => "-device ich9-ahci,id=ahci -device virtio-scsi-pci,id=scsi -device qemu-xhci,id=xhci",
hdd => "-device scsi-hd,drive=hd,bootindex=0 -drive if=none,file=%s,cache=writeback,id=hd,format=raw,unit=0",
- cdrom => "-device scsi-cd,drive=cd,bootindex=1 -drive if=none,file=%s,format=raw,id=cd",
+ cdrom => "-device scsi-cd,drive=cd,bootindex=0 -drive if=none,file=%s,format=raw,id=cd",
virtio => "-device ide-cd,drive=drv,bus=ahci.0 -drive if=none,file=%s,format=raw,id=drv",
usb => "-device usb-tablet",
time => "-localtime",
@@ -331,7 +320,7 @@ sub get_vm_macaddr {
return '';
}
-if ($install_iso_path) {
+if ($cmd_opts{install_iso_path}) {
create_guest_img($vm_img_path);
create_tap_dev();
@@ -342,7 +331,7 @@ if ($install_iso_path) {
}
my $qemu_cmd = "/usr/bin/qemu-system-x86_64";
- if ($windows_guest) {
+ if ($cmd_opts{windows_guest}) {
$qemu_cmd .= " $qemu_win_opts{'mach'} $qemu_win_opts{'cpu'}";
$qemu_cmd .= " $qemu_win_opts{'mem'} $qemu_win_opts{'bus'}";
$qemu_cmd .= " " . sprintf $qemu_win_opts{'hdd'}, $vm_img_path;
@@ -350,9 +339,9 @@ if ($install_iso_path) {
my $user_hdds = parse_user_hdds();
$qemu_cmd .= " $user_hdds" if $user_hdds;
- $qemu_cmd .= " " . sprintf $qemu_win_opts{'cdrom'}, $install_iso_path;
- if ($virtio_drv_path) {
- $qemu_cmd .= " " . sprintf $qemu_win_opts{'virtio'}, $virtio_drv_path;
+ $qemu_cmd .= " " . sprintf $qemu_win_opts{'cdrom'}, $cmd_opts{install_iso_path};
+ if ($cmd_opts{virtio_drv_path}) {
+ $qemu_cmd .= " " . sprintf $qemu_win_opts{'virtio'}, $cmd_opts{virtio_drv_path};
}
$qemu_cmd .= " " . sprintf $qemu_win_opts{'net'}, $macaddr, $tap_iface;
$qemu_cmd .= " $qemu_win_opts{'time'} $qemu_win_opts{'usb'}" .
@@ -361,7 +350,7 @@ if ($install_iso_path) {
$qemu_cmd .= " $qemu_linux_opts{'mach'} $qemu_linux_opts{'cpu'}";
$qemu_cmd .= " $qemu_linux_opts{'mem'} $qemu_linux_opts{'bus'}";
$qemu_cmd .= " " . sprintf $qemu_linux_opts{'hdd'}, $vm_img_path;
- $qemu_cmd .= " " . sprintf $qemu_linux_opts{'cdrom'}, $install_iso_path;
+ $qemu_cmd .= " " . sprintf $qemu_linux_opts{'cdrom'}, $cmd_opts{install_iso_path};
$qemu_cmd .= " " . sprintf $qemu_linux_opts{'net'}, $macaddr, $tap_iface;
}
@@ -377,7 +366,7 @@ if ($install_iso_path) {
die "$vm_img_path does not exist" unless -f $vm_img_path;
-if ($delete_vm) {
+if ($cmd_opts{delete_vm}) {
unlink($vm_img_path) or die "failed to remove $vm_img_path: $!";
rem_vm_info($vm_name);
print "[*] successfully deleted $vm_name\n";
@@ -387,7 +376,7 @@ if ($delete_vm) {
##
## windows vm
#
-if ($windows_guest) {
+if ($cmd_opts{windows_guest}) {
create_tap_dev();
my $macaddr = get_random_macaddr();
@@ -400,8 +389,8 @@ if ($windows_guest) {
my $user_hdds = parse_user_hdds();
$qemu_cmd .= $user_hdds if $user_hdds;
- if ($virtio_drv_path) {
- $qemu_cmd .= " " . sprintf $qemu_win_opts{'virtio'}, $virtio_drv_path;
+ if ($cmd_opts{virtio_drv_path}) {
+ $qemu_cmd .= " " . sprintf $qemu_win_opts{'virtio'}, $cmd_opts{virtio_drv_path};
}
$qemu_cmd .= " " .
sprintf $qemu_win_opts{'net'}, get_vm_macaddr(), $tap_iface;
@@ -424,7 +413,7 @@ chomp($rootfs_path) if $rootfs_path;
sub mount_rootfs {
die "$rootfs_path does not exist" unless $rootfs_path or
-e $rootfs_path or -d $rootfs_path;
- system("guestmount -a $vm_img_path -m /dev/sda2 $rootfs_path") == 0 or
+ system("guestmount -a $vm_img_path -m $cmd_opts{rootfs_path} $rootfs_path") == 0 or
die "failed to mount rootfs: $?";
}
@@ -433,7 +422,10 @@ sub umount_rootfs {
die "failed to umount rootfs: $?";
}
-sub rem_old_shares() {
+sub rem_shared_dirs() {
+ return unless $cmd_opts{shared_dirs};
+ return unless $cmd_opts{rootfs_path};
+
mount_rootfs();
open(my $rh, "<", "$rootfs_path/etc/fstab") or umount_rootfs() and
@@ -457,9 +449,10 @@ sub rem_old_shares() {
}
sub setup_shared_dirs {
- rem_old_shares() and return unless $shared_dirs;
+ return unless $cmd_opts{shared_dirs};
+ return unless $cmd_opts{rootfs_path};
- my @dirs = split(/,/, $shared_dirs);
+ my @dirs = split(/,/, $cmd_opts{shared_dirs});
my $args = '';
my $count = 0;
my $arg = "-fsdev local,security_model=none,id=%s,path=%s -device virtio-9p-pci,id=%s,fsdev=%s,mount_tag=%s";
@@ -512,43 +505,43 @@ my $qemu_cmd = "/usr/bin/qemu-system-x86_64";
$qemu_cmd .= " $qemu_linux_opts{'mach'} $qemu_linux_opts{'cpu'}";
$qemu_cmd .= " $qemu_linux_opts{'mem'} $qemu_linux_opts{'bus'}";
$qemu_cmd .= " $qemu_linux_opts{'usb'}";
-$qemu_cmd .= " " . sprintf $qemu_linux_opts{'hdd'}, $vm_img_path;
+$qemu_cmd .= sprintf $qemu_linux_opts{'hdd'}, $vm_img_path;
my $user_hdds = parse_user_hdds();
-$qemu_cmd .= " $user_hdds" if $user_hdds;
+$qemu_cmd .= $user_hdds if $user_hdds;
setup_shared_dirs();
-$qemu_cmd .= " $qemu_linux_opts{'share'}";
+$qemu_cmd .= $qemu_linux_opts{'share'};
create_tap_dev();
$qemu_cmd .= " " . sprintf $qemu_linux_opts{'net'}, get_vm_macaddr(), $tap_iface;
my $err;
-if ($kernel_path) {
- unless (-e $kernel_path or -d $kernel_path) {
- $err = "$kernel_path does not exist";
+if ($cmd_opts{kernel_path}) {
+ unless ($cmd_opts{rootfs_path}) {
+ $err = "rootfs has not been specified";
+ goto OUT;
+ }
+ unless (-e $cmd_opts{kernel_path} or -d $cmd_opts{kernel_path}) {
+ $err = "$cmd_opts{kernel_path} does not exist";
goto OUT;
}
- if ($build_kernel) {
- unless (chdir($kernel_path)) {
+ if ($cmd_opts{build_kernel}) {
+ unless (chdir($cmd_opts{kernel_path})) {
$err = "failed to change dir: $!";
goto OUT;
}
- if ($config_path) {
- unless (-e $config_path or -f $config_path) {
- $err = "$config_path does not exist";
+ if ($cmd_opts{config_path}) {
+ unless (-e $cmd_opts{config_path} or -f $cmd_opts{config_path}) {
+ $err = "$cmd_opts{config_path} does not exist";
goto OUT;
}
- copy($config_path, "$kernel_path/.config") or
+ copy($cmd_opts{config_path}, "$cmd_opts{kernel_path}/.config") or
die "failed to copy file: $!";
system("make oldconfig");
- } else {
- system("make mrproper");
- system("make x86_64_defconfig");
- system("make kvmconfig");
}
if (system('make -j$(getconf _NPROCESSORS_ONLN)')) {
@@ -556,7 +549,7 @@ if ($kernel_path) {
goto OUT;
}
- if ($install_modules) {
+ if ($cmd_opts{install_modules}) {
mount_rootfs();
if (system("make modules")) {
$err = "failed to build kernel modules: $?";
@@ -569,32 +562,38 @@ if ($kernel_path) {
chdir();
}
- my $linux_img = "$kernel_path/arch/x86/boot/bzImage";
+ my $linux_img = "$cmd_opts{kernel_path}/arch/x86/boot/bzImage";
unless (-e $linux_img or -f $linux_img) {
$err = "$linux_img does not exist";
goto OUT;
}
$qemu_cmd .= " " . sprintf $qemu_linux_opts{'kernel'}, $linux_img;
- $qemu_cmd .= " $qemu_linux_opts{'cmdline'} -nographic";
- if ($initrd_img) {
- unless (-e $initrd_img or -f $initrd_img) {
- $err = "$initrd_img does not exist";
+ $qemu_cmd .= " " . sprintf $qemu_linux_opts{'cmdline'}, $cmd_opts{rootfs_path};
+ $qemu_cmd .= " -nographic";
+ if ($cmd_opts{initrd_img}) {
+ unless (-e $cmd_opts{initrd_img} or -f $cmd_opts{initrd_img}) {
+ $err = "$cmd_opts{initrd_img} does not exist";
goto OUT;
}
- $qemu_cmd .= " " . sprintf $qemu_linux_opts{'initrd'}, $initrd_img;
+ $qemu_cmd .= " " . sprintf $qemu_linux_opts{'initrd'}, $cmd_opts{initrd_img};
}
} else {
- $qemu_cmd .= " -nographic -device virtio-serial-pci -serial mon:stdio" if $nographic;
+ $qemu_cmd .= " -nographic -device virtio-serial-pci -serial mon:stdio" if $cmd_opts{nographic};
}
print "[*] $vm_name vm is up and running\n";
print "[*] ipaddr: " . resolve_name($vm_name) . "\n";
print "[*] macaddr: " . get_vm_macaddr() . "\n";
-system($qemu_cmd . " 2>/dev/null");
+print "[*] cmd: $qemu_cmd\n";
+if (system($qemu_cmd)) {
+ $err = "failed to exec cmd: $qemu_cmd";
+ goto OUT;
+}
print "[*] terminated\n";
OUT:
+rem_shared_dirs();
destroy_tap_dev();
die "$err" if $err;