diff --git a/xCAT-server/lib/xcat/plugins/debian.pm b/xCAT-server/lib/xcat/plugins/debian.pm index 0f85804422..375b0a8af0 100644 --- a/xCAT-server/lib/xcat/plugins/debian.pm +++ b/xCAT-server/lib/xcat/plugins/debian.pm @@ -750,6 +750,40 @@ sub copycd #------------------------------------------------------------------------------- +=head3 static_ip6_boot_arg + + The dracut ip= argument that gives an IPv6-only node a static address. + + networks.gateway holds what the administrator wrote, and '' is the common + value. Unresolved it reaches the boot argument as "[]", and the node has no + route to a server off its own link. A gateway that does not resolve is dropped: an + address with no route still reaches an image server on the same link. + + Arguments: + $node the node being installed + $nicname the name of the provisioning NIC + $cfg6 optional coderef, for tests; defaults to getNodeNetworkCfg6 + $resolve optional coderef, for tests; defaults to ipv6_server_for_node + Returns: + the argument, or undef when the node has no usable address + +=cut + +#------------------------------------------------------------------------------- +sub static_ip6_boot_arg { + my ($node, $nicname, $cfg6, $resolve) = @_; + + $cfg6 ||= sub { return xCAT::NetworkUtils->getNodeNetworkCfg6($_[0]); }; + $resolve ||= sub { return xCAT::NetworkUtils->ipv6_server_for_node($_[0], $_[1]); }; + + my ($address, undef, $gateway, $prefix) = $cfg6->($node); + $gateway = $resolve->($node, $gateway) + if defined($gateway) && length($gateway); + return xCAT::NetworkUtils::dracut_static_ip6($address, $gateway, $prefix, $nicname); +} + +#------------------------------------------------------------------------------- + =head3 install_iso_name The name the install ISO takes inside the install tree. @@ -2118,10 +2152,7 @@ sub mknetboot # advertisement, so a node that takes the DHCPv6 answer alone cannot reach its # server. if (xCAT::NetworkUtils->node_is_ipv6_only($node)) { - my ($v6addr, undef, $v6gateway, $v6prefix) = - xCAT::NetworkUtils->getNodeNetworkCfg6($node); - my $ip6 = xCAT::NetworkUtils::dracut_static_ip6( - $v6addr, $v6gateway, $v6prefix, $net_params->{nicname}) + my $ip6 = static_ip6_boot_arg($node, $net_params->{nicname}) || $net_params->{ip6}; $kcmdline .= "$ip6 " if $ip6; } diff --git a/xCAT-test/unit/debian_static_ip6_gateway.t b/xCAT-test/unit/debian_static_ip6_gateway.t new file mode 100644 index 0000000000..cc667b705b --- /dev/null +++ b/xCAT-test/unit/debian_static_ip6_gateway.t @@ -0,0 +1,74 @@ +#!/usr/bin/env perl +# The static IPv6 boot argument debian.pm gives an IPv6-only node. +# +# networks.gateway holds what the administrator wrote, and '' is the common value. +# It was passed to dracut_static_ip6 unresolved, so the command line carried +# "[]" in the gateway field. That is not an address, and a node whose image +# server sits off its own link had no route to it. +# +# static_ip6_boot_arg takes its two lookups as coderefs, so the composition is driven here +# while the real dracut_static_ip6 formats the result. +use strict; +use warnings; + +use File::Spec; +use FindBin; +use lib "$FindBin::Bin/../../perl-xCAT"; +use Test::More; + +use xCAT::NetworkUtils; + +my $plugin = File::Spec->catfile( + $FindBin::Bin, '..', '..', 'xCAT-server', 'lib', 'xcat', 'plugins', 'debian.pm' +); +plan skip_all => "debian.pm not found" unless -f $plugin; + +my $src = do { local $/; open my $fh, '<', $plugin or die $!; <$fh> }; +my ($sub) = $src =~ /\n(sub static_ip6_boot_arg \{.*?\n\})\n/s; +die "could not extract static_ip6_boot_arg from debian.pm" unless defined $sub; + +{ + package T; + eval "$sub; 1" or main::BAIL_OUT("could not eval static_ip6_boot_arg: $@"); +} + +# The placeholder is resolved before it reaches the boot argument. +{ + my @asked; + my $cfg6 = sub { return ( '2001:db8::10', $_[0], '', 64 ) }; + my $resolve = sub { push @asked, [@_]; return '2001:db8::1' }; + is( T::static_ip6_boot_arg( 'cn1', 'ens3', $cfg6, $resolve ), + 'ip=[2001:db8::10]::[2001:db8::1]:64::ens3:none', + 'the gateway is resolved before it reaches the boot argument' ); + is_deeply( \@asked, [ [ 'cn1', '' ] ], + '... through the helper that knows the placeholder' ); +} + +# A gateway that does not resolve is dropped, not written as a bad address. +{ + my $cfg6 = sub { return ( '2001:db8::10', $_[0], '', 64 ) }; + my $resolve = sub { return }; + my $arg = T::static_ip6_boot_arg( 'cn1', 'ens3', $cfg6, $resolve ); + is( $arg, 'ip=[2001:db8::10]:::64::ens3:none', + 'an unresolvable gateway leaves the field empty' ); + unlike( $arg, qr/xcatmaster/, '... and the placeholder does not reach the command line' ); +} + +# A literal address is passed through and bracketed. +{ + my $cfg6 = sub { return ( '2001:db8::10', $_[0], '2001:db8::fe', 64 ) }; + my $resolve = sub { return $_[1] }; + like( T::static_ip6_boot_arg( 'cn1', 'ens3', $cfg6, $resolve ), + qr/\Q::[2001:db8::fe]:\E/, + 'a literal gateway reaches the boot argument bracketed' ); +} + +# A node with no IPv6 address gets no argument, and the caller falls back. +{ + my $cfg6 = sub { return ( undef, $_[0], undef, undef ) }; + my $resolve = sub { return $_[1] }; + is( T::static_ip6_boot_arg( 'cn1', 'ens3', $cfg6, $resolve ), undef, + 'a node with no address gets no argument' ); +} + +done_testing();