summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaulo Alcantara <palcantara@suse.de>2018-04-06 10:22:24 -0300
committerPaulo Alcantara <palcantara@suse.de>2018-04-06 10:22:24 -0300
commit74d4e33c34f1dbf01b3088e36e471a4f59ccd2de (patch)
tree033d3fd103561ed7a1c1f4fc5f0e5f5d8c61349f
parent8bc207aab4b124b5a78546e78cde7e9c412d11b2 (diff)
downloadvmhtool-74d4e33c34f1dbf01b3088e36e471a4f59ccd2de.tar.gz
vmhtool-74d4e33c34f1dbf01b3088e36e471a4f59ccd2de.tar.xz
vmhtool-74d4e33c34f1dbf01b3088e36e471a4f59ccd2de.zip
Dynamically generate network configuration.
Also introduces a '--new' option to overwrite existing config files. Signed-off-by: Paulo Alcantara <palcantara@suse.de>
-rwxr-xr-xvmhelper130
1 files changed, 102 insertions, 28 deletions
diff --git a/vmhelper b/vmhelper
index db312a6..4d70af8 100755
--- a/vmhelper
+++ b/vmhelper
@@ -24,19 +24,21 @@ my $windows_guest;
my $print_help;
my $initrd_img = '';
my $shared_dirs = '';
-
-GetOptions ("install=s" => \$install_iso_path,
- "virtio=s" => \$virtio_drv_path,
- "kernel=s" => \$kernel_path,
- "build" => \$build_kernel,
- "Initrd=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);
+my $gen_new_conf;
+
+GetOptions("install=s" => \$install_iso_path,
+ "virtio=s" => \$virtio_drv_path,
+ "kernel=s" => \$kernel_path,
+ "build" => \$build_kernel,
+ "Initrd=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);
usage() if $print_help;
@@ -45,6 +47,67 @@ 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;
my $vm_info = $vm_path . '/.vminfo';
+my $vm_conf = $vm_path . '/.vmconf';
+
+my %vm_conf_opts = (
+ ETH_IFACE => 'eth0',
+ BRIDGE_IFACE => 'br0',
+ BRIDGE_CFG_METHOD => 'auto',
+ BRIDGE_IP_ADDR => '',
+ BRIDGE_GW_ADDR => ''
+ );
+
+sub in2opts {
+ my $v = <STDIN>;
+ chomp($v);
+ if ($v) {
+ $vm_conf_opts{$_[0]} = lc($v);
+ }
+}
+
+sub gen_conf {
+ open(my $f, ">", $vm_conf) or die "failed to create config file: $!";
+
+ print "[*] generate initial config file\n";
+ print " eth iface (default $vm_conf_opts{'ETH_IFACE'}): ";
+ in2opts('ETH_IFACE');
+ print " bridge iface (default $vm_conf_opts{'BRIDGE_IFACE'}): ";
+ in2opts('BRIDGE_IFACE');
+ print " bridge method: (default auto) [auto|static]: ";
+ in2opts('BRIDGE_CFG_METHOD');
+ if ($vm_conf_opts{'BRIDGE_CFG_METHOD'} eq 'static') {
+ print " bridge ip: ";
+ in2opts('BRIDGE_IP_ADDR');
+ }
+
+ print " bridge gateway (default auto): ";
+ in2opts('BRIDGE_GW_ADDR');
+
+ print $f "$_ = $vm_conf_opts{$_}\n" for (keys %vm_conf_opts);
+ close($f) or die "failed to close config file: $!";
+
+ print "[*] done\n";
+};
+
+sub parse_conf {
+ open(my $f, "<", $vm_conf) or die "failed to open config file: $!";
+ while (<$f>) {
+ chomp;
+ next if /^#.*$/ or /^\s+$/;
+ my ($k, $v) = split /=/;
+ $k =~ s/^\s+|\s+$//g;
+ $v =~ s/^\s+|\s+$//g;
+ next unless exists $vm_conf_opts{$k} or defined $vm_conf_opts{$k};
+ $vm_conf_opts{$k} = $v;
+ }
+ close($f) or die "failed to close config file: $!";
+}
+
+if ($gen_new_conf || ! -e $vm_conf || ! -f $vm_conf) {
+ gen_conf();
+ exit 0;
+}
+parse_conf();
sub resolve_name {
my $name = $_[0];
@@ -73,12 +136,13 @@ sub usage {
-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
+ -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
";
exit 1;
}
@@ -95,11 +159,9 @@ if ($list_vms) {
exit 0;
}
-my $eth_iface = 'eth0';
-my $bridge_iface = 'br0';
-my $bridge_ipaddr = resolve_name('bridge');
+my $eth_iface = $vm_conf_opts{'ETH_IFACE'};
+my $bridge_iface = $vm_conf_opts{'BRIDGE_IFACE'};
my $tap_iface = '';
-my $router_ipaddr = resolve_name('rpi');
sub is_net_iface_created {
my $iface = $_[0];
@@ -121,10 +183,22 @@ sub create_bridge_dev {
system("sudo /sbin/brctl addbr $bridge_iface");
system("sudo /bin/ip addr flush dev $eth_iface");
system("sudo /sbin/brctl addif $bridge_iface $eth_iface");
- system("sudo /bin/ip addr add dev $bridge_iface $bridge_ipaddr/24");
- system("sudo /bin/ip route delete default via $router_ipaddr > /dev/null 2>&1");
+
+ if ($vm_conf_opts{'BRIDGE_CFG_METHOD'} eq 'static') {
+ my $ip = $vm_conf_opts{'BRIDGE_IP_ADDR'};
+
+ system("sudo /bin/ip addr add dev $bridge_iface $ip/24");
+ } else {
+ system("sudo dhclient --no-pid $bridge_iface");
+ }
+
+ my $gw = $vm_conf_opts{'BRIDGE_GW_ADDR'};
+ if ($gw) {
+ system("sudo /bin/ip route delete default dev $bridge_iface >/dev/null 2>&1");
+ system("sudo /bin/ip route add default via $gw dev $bridge_iface");
+ }
+
system("sudo /bin/ip link set dev $bridge_iface up");
- system("sudo /bin/ip route add default via $router_ipaddr");
}
sub create_tap_dev {
@@ -295,8 +369,8 @@ if ($windows_guest) {
}
$qemu_cmd .= " " .
sprintf $qemu_win_opts{'net'}, get_vm_macaddr(), $tap_iface;
- $qemu_cmd .= " $qemu_win_opts{'time'} $qemu_win_opts{'usb'}" .
- " $qemu_win_opts{'gfx'}";
+ $qemu_cmd .= " $qemu_win_opts{'time'} $qemu_win_opts{'usb'}" .
+ " $qemu_win_opts{'gfx'}";
system($qemu_cmd);
destroy_tap_dev();