diff --git a/perl-xCAT/xCAT/NetworkUtils.pm b/perl-xCAT/xCAT/NetworkUtils.pm index a743fb6feb..78b89b2d0a 100644 --- a/perl-xCAT/xCAT/NetworkUtils.pm +++ b/perl-xCAT/xCAT/NetworkUtils.pm @@ -4402,6 +4402,36 @@ sub neighbour_table_text =cut #------------------------------------------------------------------------------- +#-------------------------------------------------------------------------------- + +=head3 ipxe_script_http_base + + The http base a per-network iPXE boot script uses to reach the management node. + + Arguments: + the address of the management node facing the network, its URI host form, the port suffix + Returns: + the base of every URL in the script, with no trailing slash + +=cut + +#-------------------------------------------------------------------------------- +sub ipxe_script_http_base +{ + my ( $nicip, $uri_host, $portsuffix ) = @_; + if ( defined($nicip) && $nicip eq __PACKAGE__ ) + { + ( $nicip, $uri_host, $portsuffix ) = ( $uri_host, $portsuffix, shift ); + } + $portsuffix = '' unless defined($portsuffix); + + # DHCPv6 has no next-server option. iPXE leaves ${next-server} empty on an IPv6 network, so + # every URL in the script loses its host and fetches nothing. + return 'http://' . '${next-server}' . $portsuffix + unless defined($nicip) && xCAT::NetworkUtils->isIpv6addr($nicip); + return 'http://' . ( defined($uri_host) ? $uri_host : '' ) . $portsuffix; +} + sub mac_of_neighbour { my $address = shift; diff --git a/xCAT-server/lib/xcat/plugins/mknb.pm b/xCAT-server/lib/xcat/plugins/mknb.pm index 1884303737..bb60f693f8 100644 --- a/xCAT-server/lib/xcat/plugins/mknb.pm +++ b/xCAT-server/lib/xcat/plugins/mknb.pm @@ -843,6 +843,8 @@ sub process_request { # node reads the wrong address and reports nothing. Bracket it. my $xcatd_endpoint = xCAT::NetworkUtils::xcatd_cmdline_address($xcatd_address, $xcatdport); my $xcatd_uri_host = xCAT::NetworkUtils->format_uri_host($xcatd_address); + my $http_base = + xCAT::NetworkUtils::ipxe_script_http_base( $nicip, $xcatd_uri_host, $portsuffix ); $net =~ s/\//_/; if (defined($nobootnicips{$nicip}) || ($arch eq 's390x' && defined($nobootnicips{$xcatd_address}))) { @@ -885,11 +887,11 @@ sub process_request { open($cfg, ">", "$nets/$net"); print $cfg "#!gpxe\n"; if ($invisibletouch) { - print $cfg 'imgfetch -n kernel http://${next-server}'.$portsuffix.'/tftpboot/xcat/genesis.kernel.' . "$arch xcatd=$xcatd_endpoint $consolecmdline BOOTIF=01-" . '${netX/mac:hexhyp}' . "\n"; - print $cfg 'imgfetch -n nbfs http://${next-server}'.$portsuffix . "$initrd_file\n"; + print $cfg 'imgfetch -n kernel ' . $http_base . '/tftpboot/xcat/genesis.kernel.' . "$arch xcatd=$xcatd_endpoint $consolecmdline BOOTIF=01-" . '${netX/mac:hexhyp}' . "\n"; + print $cfg 'imgfetch -n nbfs ' . $http_base . "$initrd_file\n"; } else { - print $cfg 'imgfetch -n kernel http://${next-server}'.$portsuffix.'/tftpboot/xcat/nbk.' . "$arch xcatd=$xcatd_endpoint $consolecmdline\n"; - print $cfg 'imgfetch -n nbfs http://${next-server}'.$portsuffix . "$initrd_file\n"; + print $cfg 'imgfetch -n kernel ' . $http_base . '/tftpboot/xcat/nbk.' . "$arch xcatd=$xcatd_endpoint $consolecmdline\n"; + print $cfg 'imgfetch -n nbfs ' . $http_base . "$initrd_file\n"; } print $cfg "imgload kernel\n"; print $cfg "imgexec kernel\n"; @@ -905,15 +907,15 @@ sub process_request { close($cfg); open($cfg, ">", "$nets/$net.uefi"); print $cfg "#!gpxe\n"; - print $cfg 'imgfetch -n kernel http://${next-server}'.$portsuffix.'/tftpboot/xcat/genesis.kernel.' . "$arch\nimgload kernel\n"; + print $cfg 'imgfetch -n kernel ' . $http_base . '/tftpboot/xcat/genesis.kernel.' . "$arch\nimgload kernel\n"; # The shim command reads the selected image, so it comes after imgload. # The loader of xnba does not know the shim command, and no key signs xnba.efi. my $shim = ($method eq 'ipxe') ? xCAT::SecureBoot->shim_url_path(arch => $arch, tftpdir => $tftpdir) : undef; - print $cfg 'shim http://${next-server}' . $portsuffix . $shim . "\n" if ($shim); + print $cfg 'shim ' . $http_base . $shim . "\n" if ($shim); print $cfg "imgargs kernel xcatd=$xcatd_endpoint $consolecmdline BOOTIF=01-" . '${netX/mac:hexhyp}' . " destiny=discover initrd=initrd\n"; - print $cfg 'imgfetch -n initrd http://${next-server}'.$portsuffix . "$initrd_file\nimgexec kernel\n"; + print $cfg 'imgfetch -n initrd ' . $http_base . "$initrd_file\nimgexec kernel\n"; close($cfg); } } diff --git a/xCAT-test/unit/ipxe_script_http_base.t b/xCAT-test/unit/ipxe_script_http_base.t new file mode 100644 index 0000000000..33e316a170 --- /dev/null +++ b/xCAT-test/unit/ipxe_script_http_base.t @@ -0,0 +1,36 @@ +#!/usr/bin/perl +# Every URL in a per-network iPXE boot script needs a host. +# +# mknb writes one script per network under xcat/ipxe/nets, and that script is what an unknown +# client boots: it carries destiny=discover and no node name, so the Genesis image asks to be +# discovered. The script used ${next-server} for the server in every URL. DHCPv4 fills that from +# the siaddr of the offer; DHCPv6 has no next-server option at all, so iPXE leaves it empty and +# the fetch has no host. Measured on xcat88-mn: the per-NODE script, which boots, carries +# http://[2001:db8:188::1]/... while the per-NETWORK script carried http://${next-server}/... +use strict; +use warnings; +use Test::More tests => 7; +use FindBin; +use lib "$FindBin::Bin/../../perl-xCAT"; + +require xCAT::NetworkUtils; + +can_ok( 'xCAT::NetworkUtils', 'ipxe_script_http_base' ); + +my $v6 = xCAT::NetworkUtils::ipxe_script_http_base( '2001:db8:188::1', '[2001:db8:188::1]', '' ); +is( $v6, 'http://[2001:db8:188::1]', + 'an IPv6 network carries the address of the management node, because DHCPv6 has no next-server' ); +unlike( $v6, qr/next-server/, 'and nothing is left for iPXE to fill in' ); + +is( xCAT::NetworkUtils::ipxe_script_http_base( '192.168.88.1', '192.168.88.1', '' ), + 'http://${next-server}', + 'an IPv4 network keeps ${next-server}, which its offer fills in' ); + +is( xCAT::NetworkUtils::ipxe_script_http_base( '2001:db8:188::1', '[2001:db8:188::1]', ':8080' ), + 'http://[2001:db8:188::1]:8080', 'the port suffix is kept on an IPv6 network' ); +is( xCAT::NetworkUtils::ipxe_script_http_base( '192.168.88.1', '192.168.88.1', ':8080' ), + 'http://${next-server}:8080', 'the port suffix is kept on an IPv4 network' ); + +# A network with no address of its own must not produce a URL with a bracketed empty host. +is( xCAT::NetworkUtils::ipxe_script_http_base( undef, undef, '' ), + 'http://${next-server}', 'an unknown family falls back to the DHCPv4 form' );