#!/bin/bash # This is an example of how nbft can be used in bash to parse the nBFT. # Borrowed from https://github.com/dracutdevs/dracut/blob/master/modules.d/95nvmf/parse-nvmf-boot-connections.sh nbft_bin="/usr/bin/nbft2json" # Set some initial defaults (nvme-cli prefers 'none' for uninitialized parameters) trtype=none traddr=none trsvcid=none hosttraddr=none get_namespace() { echo $(jq -r .namespace[$1].transport_type,.namespace[$1].subsystem_transport_address,.namespace[$1].subsystem_namespace_nqn,.namespace[$1].subsystem_transport_port,.namespace[$1].hfi_association[] $2) } get_discovery() { echo $(jq -r .discovery.records[$1].discovery_ctrl_addr $2) } parse_nbft() { nbft_json=nbft.json # Run nbft and save output to $nbft_json. All future queries will be performed on this file. if $nbft_bin > $nbft_json 2>/dev/null; then # Host ID and Host NQN are per host, so we get them first. nbft_hostnqn=$(jq -r .host.initiator_host_nqn $nbft_json) nbft_hostid=$(jq -r .host.host_identifier $nbft_json) # This is required for nvme-cli to work. echo $nbft_hostnqn > /etc/nvme/hostnqn echo $nbft_hostid > /etc/nvme/hostid # Fetch discovery records from nBFT for n in $(seq 0 $(jq '.discovery.number_of_records - 1' $nbft_json 2>/dev/null)); do read trtype traddr trsvcid < <(get_discovery $n $nbft_json | awk -F"/|:|+" '{print $2, $5, $6}') # discovery/connect to all available nqns printf "nvme connect-all -t %s -a %s -s %s\n" $trtype $traddr $trsvcid done # Fetch trtype, traddr, subnqn, trsvcid and hosttraddr for each namespace #for n in $(jq .namespace[].index $nbft_json); do for n in $(seq 0 $(jq '.discovery.number_of_namespaces - 1' $nbft_json 2>/dev/null)); do read trtype traddr subnqn trsvcid HFI < <(get_namespace $n $nbft_json) # Get the hosttraddr from the HFI association (typically the NIC/port) for this namespace. HOSTIP=$(jq -r .hfi[$HFI].hfi_info.ip_address $nbft_json) # This check is to ensure that the hosts's IP address from nBFT (uefi dhcp) matches that assigned by Linux. if ip -o addr | grep -q $HOSTIP; then hosttraddr="$HOSTIP" fi # connect to each nqn printf "nvme connect -t %s -a %s -s %s -w %s -n %s # nbft_namespace:%s\n" $trtype $traddr $trsvcid $hosttraddr $subnqn $n done else echo "No nBFT detected." return 1 fi } parse_nbft