Skip to content

Commit

Permalink
nisystemreplication: Fix unexpected differences
Browse files Browse the repository at this point in the history
Existing "Set Image" workflow installed grub and safemode afresh from
provisioning media while setting some variables to ensure settings from
system image get carried over but this left undesired changes in some
files.

This commit fixes this by copying nigrub and nibootfs from system image
and later fixing the few files in them that are required to be
different.

Signed-off-by: Chaitanya Vadrevu <[email protected]>
  • Loading branch information
chaitu236 committed Aug 29, 2024
1 parent 805579d commit 1d4eb32
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 45 deletions.
16 changes: 16 additions & 0 deletions recipes-core/initrdscripts/files/ni_provisioning.safemode
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ set -e

source /ni_provisioning.safemode.common

check_all_used_binaries

echo "Installing safemode to: $TARGET_DISK."
echo 6 > /proc/sys/kernel/printk

print_info "Disabling automount..."
disable_automount
print_done

partitioning_disk
wait_for_partitions $TARGET_DISK
create_filesystems

prune_efi_crash_vars
install_grub

install_safemode
install_bootmode_file
install_grubenv
Expand Down
94 changes: 60 additions & 34 deletions recipes-core/initrdscripts/files/ni_provisioning.safemode.common
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,15 @@ check_all_used_binaries()

override_primaryport_grubenv()
{
local grubenv_file=$1
# Add target-specific override of primary ethernet port if the lowest ifIndex is not to be used
local device_code=$(get_target_id)
# CVS-1458RT
if [[ x"$device_code" == x"77AA" ]]; then
# Set eth0 (note: renamed to eth0 via udev) as the primary port. Do not let the primary
# port selection logic decide since it selects the 'eth' port with the lowest ifIndex
# value. (Renaming the port via udev does not alter ifIndex, so eth0 is not the lowest)
grub-editenv $BOOTFS_MOUNTPOINT/grub/grubenv set "ethaddr=`cat /sys/class/net/eth0/address`"
grub-editenv $grubenv_file set "ethaddr=`cat /sys/class/net/eth0/address`"
fi
}

Expand Down Expand Up @@ -199,25 +200,36 @@ create_filesystems()
print_done
}

install_grub()
set_efiboot_entry()
{
# Delete existing NILRT entries with "-B" option
for ENTRY in $(efibootmgr | egrep -i '(LabVIEW RT)|(niboota)|(nibootb)' | egrep -o '[0-9A-Fa-f]{4}' || true);
do
print_info " Drop entry $ENTRY."
EFIMGR=$(efibootmgr -b "$ENTRY" -B 2>&1) || print_warning "efibootmgr -b $ENTRY -B failed with: $EFIMGR"
done
efibootmgr $VERBOSE_ARGS -c -d ${TARGET_DISK} -p 1 -L 'LabVIEW RT' -l '\efi\boot\bootx64.efi'
print_done
}

mount_grub_partition()
{
GRUB_MOUNTPOINT=/var/volatile/grub
mkdir $GRUB_MOUNTPOINT -p
MOUNT_ERROR=`mount -L $PART1_LABEL $GRUB_MOUNTPOINT 2>&1` || die "$MOUNT_ERROR"
}

install_grub()
{
mount_grub_partition

print_info "Configuring EFI grub2..."
GRUB_TARGET_DIR=$GRUB_MOUNTPOINT/efi/boot
mkdir -p $GRUB_TARGET_DIR
GRUB_TARGET=$(uname -m)
cp /boot/EFI/BOOT/bootx64.efi $GRUB_TARGET_DIR
# Delete existing NILRT entries with "-B" option
for ENTRY in $(efibootmgr | egrep -i '(LabVIEW RT)|(niboota)|(nibootb)' | egrep -o '[0-9A-Fa-f]{4}' || true);
do
print_info " Drop entry $ENTRY."
EFIMGR=$(efibootmgr -b "$ENTRY" -B 2>&1) || print_warning "efibootmgr -b $ENTRY -B failed with: $EFIMGR"
done
efibootmgr $VERBOSE_ARGS -c -d ${TARGET_DISK} -p 1 -L 'LabVIEW RT' -l '\efi\boot\bootx64.efi'
print_done

set_efiboot_entry

print_info "Installing grub.cfg..."

Expand All @@ -227,16 +239,21 @@ install_grub()
print_done
}

install_safemode()
mount_bootfs_partition()
{
print_info "Installing safemode kernel and ramdisk..."

BOOTFS_MOUNTPOINT=/var/volatile/bootfs
mkdir $BOOTFS_MOUNTPOINT -p
BOOTFS_ERROR=`mount -L $PART2_LABEL $BOOTFS_MOUNTPOINT 2>&1` || die "$BOOTFS_ERROR"
}

install_safemode()
{
print_info "Installing safemode kernel and ramdisk..."

mount_bootfs_partition
mkdir -p $BOOTFS_MOUNTPOINT/.safe

cp "$SOURCE_DIR"/*Image "$BOOTFS_MOUNTPOINT/.safe/"
cp "$SOURCE_DIR"/*Image "$BOOTFS_MOUNTPOINT/.safe/"
cp "$SOURCE_DIR"/ramdisk.* "$BOOTFS_MOUNTPOINT/.safe/"
cp "$SOURCE_DIR"/bootimage.* "$BOOTFS_MOUNTPOINT/.safe/"

Expand All @@ -251,6 +268,13 @@ install_bootmode_file()
print_done
}

add_rootuuid_to_grubenv()
{
# save rootfs UUID for grub to reference
ROOTUUID=`lsblk ${TARGET_DISK}${PART_SEPARATOR}4 -n -o PARTUUID`
echo set rootuuid=$ROOTUUID >> $GRUB_MOUNTPOINT/grubvar_readonly
}

install_grubenv()
{
NI_TARGET="false"
Expand All @@ -266,7 +290,7 @@ install_grubenv()
cp $SOURCE_DIR/SMBIOS_NI_vars $BOOTFS_MOUNTPOINT/.safe
cp $SOURCE_DIR/EFI_NI_vars $BOOTFS_MOUNTPOINT/.safe
add_USB_gadget_args_to_grubenv
override_primaryport_grubenv
override_primaryport_grubenv $BOOTFS_MOUNTPOINT/grub/grubenv
else
cp $SOURCE_DIR/grubenv_non_ni_target $BOOTFS_MOUNTPOINT/grub/grubenv
fi
Expand All @@ -281,9 +305,7 @@ install_grubenv()
echo "$varname" >> "$BOOTFS_MOUNTPOINT/.safe/GRUB_NI_readonly_vars"
done

# save rootfs UUID for grub to reference
ROOTUUID=`lsblk ${TARGET_DISK}${PART_SEPARATOR}4 -n -o PARTUUID`
echo set rootuuid=$ROOTUUID >> $GRUB_MOUNTPOINT/grubvar_readonly
add_rootuuid_to_grubenv

# set proper permissions on and backup firmware variable files
chown 0:500 $BOOTFS_MOUNTPOINT/grub/grubenv
Expand Down Expand Up @@ -324,6 +346,26 @@ install_grubenv()
fi
}

fix_grubenv_after_usbreplication()
{
# Fix files in nigrub filesystem
rm $GRUB_MOUNTPOINT/grubvar_readonly
if is_ni_device ; then
add_USB_gadget_args_to_grubenv
fi
set_serial_port
add_rootuuid_to_grubenv

# Fix files in nibootfs filesystem
if is_ni_device ; then
override_primaryport_grubenv $BOOTFS_MOUNTPOINT/grub/grubenv
override_primaryport_grubenv $BOOTFS_MOUNTPOINT/grub/grubenv.bak
else
grub-editenv $BOOTFS_MOUNTPOINT/grub/grubenv set "serial#=ABCDEFG"
fi
grub-editenv $BOOTFS_MOUNTPOINT/grub/grubenv unset primaryethaddr
}

set_versions()
{
BUILD_IDENTIFIER=$(get_image_info BUILD_IDENTIFIER)
Expand Down Expand Up @@ -403,20 +445,4 @@ sanity_check()
print_done
}

check_all_used_binaries

echo "Installing safemode to: $TARGET_DISK."
echo 6 > /proc/sys/kernel/printk

SOURCE_DIR=/payload

print_info "Disabling automount..."
disable_automount
print_done

partitioning_disk
wait_for_partitions $TARGET_DISK
create_filesystems

prune_efi_crash_vars
install_grub
30 changes: 19 additions & 11 deletions recipes-ni/ni-systemreplication/files/nisystemreplication
Original file line number Diff line number Diff line change
Expand Up @@ -189,22 +189,30 @@ image_set () {
# Start with "none" so that default features are not enabled if not listed in $ext4_features
MKFS_ARGS="${MKFS_ARGS} -O none,$ext4_features"
source /ni_provisioning.safemode.common
install_safemode

check_all_used_binaries

echo "Installing to: $TARGET_DISK."
echo 6 > /proc/sys/kernel/printk

print_info "Disabling automount..."
disable_automount
print_done

partitioning_disk
wait_for_partitions $TARGET_DISK
create_filesystems

prune_efi_crash_vars

echo "Applying system image $image_name. This may take a while" >&2
cat $NIRECOVERY_MOUNTPOINT/Images/$image_name/systemimage.tgz* | nisystemimage setall -d -x tgz -p reset -s reset

# Retain some grubenv settings
if grep --quiet "^consoleoutenable=" $BOOTFS_MOUNTPOINT/grub/grubenv; then
grubenv_consoleoutenable=$(grub-editenv $BOOTFS_MOUNTPOINT/grub/grubenv list |grep "^consoleoutenable=" | cut -f 2 -d '=')
fi
if grep --quiet "^bootdelay=" $BOOTFS_MOUNTPOINT/grub/grubenv; then
grubenv_bootdelay=$(grub-editenv $BOOTFS_MOUNTPOINT/grub/grubenv list |grep "^bootdelay=" | cut -f 2 -d '=')
fi
rm -rf mkdir $BOOTFS_MOUNTPOINT/grub
set_efiboot_entry

install_grubenv
set_versions
mount_grub_partition
mount_bootfs_partition
fix_grubenv_after_usbreplication

echo $LOG_LEVEL > /proc/sys/kernel/printk
sanity_check
Expand Down

0 comments on commit 1d4eb32

Please sign in to comment.