284 lines
7.8 KiB
Plaintext
284 lines
7.8 KiB
Plaintext
|
####### Pensando ########
|
||
|
|
||
|
#!/bin/sh
|
||
|
|
||
|
R=""
|
||
|
export LD_LIBRARY_PATH=/platform/lib:/nic/lib:$LD_LIBRARY_PATH
|
||
|
export PATH=/platform/bin:$PATH
|
||
|
|
||
|
root_mnt=$R/mnt
|
||
|
bl_conf_path=$root_mnt
|
||
|
HOST=/host
|
||
|
|
||
|
image_dir=image-$image_version
|
||
|
|
||
|
INSTALLER_PAYLOAD=fs.zip
|
||
|
DOCKERFS_DIR=docker
|
||
|
FILESYSTEM_DOCKERFS=dockerfs.tar.gz
|
||
|
BL_CONF=boot-$image_dir.conf
|
||
|
|
||
|
DATA_PARTUUID=6ED62003-DD8D-44B8-9538-0A2B7C7E628F
|
||
|
ROOT_PARTUUID=C7F48DD2-C265-404B-959D-C64D21D49168
|
||
|
|
||
|
ROOT_PARTSIZE=24G
|
||
|
|
||
|
exec 0< /dev/tty 1> /dev/tty 2> /dev/tty
|
||
|
|
||
|
PKG=""
|
||
|
ACTION=""
|
||
|
|
||
|
root_pn=0
|
||
|
data_pn=0
|
||
|
|
||
|
REPART_NEEDED=0
|
||
|
|
||
|
set -e
|
||
|
|
||
|
fatal()
|
||
|
{
|
||
|
echo "FATAL: $1" >&2
|
||
|
exit 1
|
||
|
}
|
||
|
|
||
|
check_existing_parts()
|
||
|
{
|
||
|
local nparts i partuuid boot_partsize boot_lastsec data_firstsec
|
||
|
|
||
|
nparts=$(sgdisk -p /dev/mmcblk0 | grep '^[ ]*[1-9]' | wc -l)
|
||
|
for i in $(seq $nparts); do
|
||
|
partuuid=$(sgdisk -i $i /dev/mmcblk0 | awk '/Partition unique GUID/ { print $NF }')
|
||
|
case "$partuuid" in
|
||
|
$DATA_PARTUUID) data_pn=$i; ;;
|
||
|
$ROOT_PARTUUID) root_pn=$i; ;;
|
||
|
esac
|
||
|
done
|
||
|
|
||
|
if [ $root_pn -ne 0 ]; then
|
||
|
boot_partsize=$(sgdisk -i $root_pn /dev/mmcblk0 | awk -F '[( ]' '/Partition size/ {print int($6)}')
|
||
|
boot_lastsec=$(sgdisk -i $root_pn /dev/mmcblk0 | awk '/Last sector/ {print $3}')
|
||
|
if [ ${boot_partsize}G = $ROOT_PARTSIZE ]; then
|
||
|
echo "SONiC root partitions already present with requested size. No repartition, only formatting"
|
||
|
else
|
||
|
echo "SONiC root partitions already present with mismatch size ${partsize}G. Repartition needed"
|
||
|
REPART_NEEDED=1
|
||
|
fi
|
||
|
fi
|
||
|
|
||
|
if [ $data_pn -eq 0 ]; then
|
||
|
echo "Data partition not found; Repartition needed"
|
||
|
REPART_NEEDED=1
|
||
|
elif [ $data_pn -ne $nparts ]; then
|
||
|
fatal "Data partition is not the last partition; exiting." >&2
|
||
|
else
|
||
|
data_firstsec=$(sgdisk -i $data_pn /dev/mmcblk0 | awk '/First sector/ {print $3}')
|
||
|
if [ $data_firstsec -ne $((boot_lastsec+1)) ]; then
|
||
|
echo "Data partition not contigent with boot partition. Repartition needed"
|
||
|
REPART_NEEDED=1
|
||
|
fi
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
setup_partitions_multi()
|
||
|
{
|
||
|
echo "==> Setting up partitions..."
|
||
|
|
||
|
set +e
|
||
|
if [ $REPART_NEEDED -eq 0 ]; then
|
||
|
mkfs.ext4 -F -q /dev/mmcblk0p$root_pn >/dev/null
|
||
|
else
|
||
|
|
||
|
if [ $root_pn -ne 0 ]; then
|
||
|
sgdisk -d $root_pn /dev/mmcblk0 >/dev/null
|
||
|
fi
|
||
|
[ $data_pn -ne 0 ] && sgdisk -d $data_pn /dev/mmcblk0 >/dev/null
|
||
|
|
||
|
if [ $root_pn -eq 0 ]; then
|
||
|
root_pn=10
|
||
|
data_pn=$(($root_pn + 1))
|
||
|
fi
|
||
|
|
||
|
if [ $data_pn -eq 0 ]; then
|
||
|
data_pn=$(($root_pn + 1))
|
||
|
fi
|
||
|
|
||
|
sgdisk \
|
||
|
-n $root_pn:+0:+$ROOT_PARTSIZE -t $root_pn:8300 \
|
||
|
-u $root_pn:$ROOT_PARTUUID -c $root_pn:"SONiC Root Filesystem" \
|
||
|
-n $data_pn:+0:0 -t $data_pn:8300 -u $data_pn:$DATA_PARTUUID \
|
||
|
-c $data_pn:"Data Filesystem" \
|
||
|
/dev/mmcblk0 >/dev/null
|
||
|
sgdisk -U R /dev/mmcblk0 >/dev/null
|
||
|
|
||
|
while true; do
|
||
|
partprobe
|
||
|
if [ -e $R/dev/mmcblk0p$data_pn ]; then
|
||
|
break
|
||
|
fi
|
||
|
sleep 1
|
||
|
done
|
||
|
|
||
|
echo "==> Creating filesystems"
|
||
|
for i in $root_pn $data_pn; do
|
||
|
mkfs.ext4 -F -q /dev/mmcblk0p$i >/dev/null
|
||
|
done
|
||
|
fi
|
||
|
set -e
|
||
|
}
|
||
|
|
||
|
setup_partitions()
|
||
|
{
|
||
|
setup_partitions_multi
|
||
|
}
|
||
|
|
||
|
cleanup()
|
||
|
{
|
||
|
echo "==> Cleaning up residual files"
|
||
|
running_sonic_revision=`cat /etc/sonic/sonic_version.yml | grep build_version | awk -F \' '{print $2}'`
|
||
|
config_files=$(find /host -type f -name "boot*.conf" | grep -iv "$running_sonic_revision\|$image_dir" )
|
||
|
if [ -z $config_files ]; then
|
||
|
echo "No config files to remove"
|
||
|
else
|
||
|
echo "config files to remove are: $config_files"
|
||
|
fi
|
||
|
|
||
|
for file in $config_files; do
|
||
|
if [ -f "$file" ]; then
|
||
|
echo "Removing file: $file"
|
||
|
rm "$file"
|
||
|
fi
|
||
|
done
|
||
|
|
||
|
faulty_image_dir=$(find /host -type d -name "image-*" | grep -iv "$running_sonic_revision\|$image_dir")
|
||
|
if [ -z $faulty_image_dir ]; then
|
||
|
echo "No faulty image directories to remove"
|
||
|
else
|
||
|
echo "Faulty image directories to remove are: $faulty_image_dir"
|
||
|
fi
|
||
|
|
||
|
for d in $faulty_image_dir; do
|
||
|
if [ -d "$d" ]; then
|
||
|
echo "Removing directory: $d"
|
||
|
rm -rfd "$d"
|
||
|
fi
|
||
|
done
|
||
|
|
||
|
}
|
||
|
|
||
|
create_bootloader_conf()
|
||
|
{
|
||
|
echo "==> Create bootloader config"
|
||
|
|
||
|
cat <<EOF >> $bl_conf_path/$BL_CONF
|
||
|
default main
|
||
|
|
||
|
label main
|
||
|
kernel /$image_dir/boot/vmlinuz-6.1.0-11-2-arm64
|
||
|
initrd /$image_dir/boot/initrd.img-6.1.0-11-2-arm64
|
||
|
devicetree /$image_dir/boot/elba-asic-psci.dtb
|
||
|
append softdog.soft_panic=1 FW_NAME=mainfwa root=/dev/mmcblk0p10 rw rootwait rootfstype=ext4 loopfstype=squashfs loop=/$image_dir/fs.squashfs
|
||
|
}
|
||
|
EOF
|
||
|
}
|
||
|
|
||
|
set_boot_command()
|
||
|
{
|
||
|
local pn
|
||
|
|
||
|
echo "==> Setting u-boot environment for Debian Boot"
|
||
|
pn=$(printf "%x" $root_pn)
|
||
|
|
||
|
fw_setenv -f baudrate 115200
|
||
|
fw_setenv -f bootcmd 'test -n "$boot_once" && setenv do_boot_once "$boot_once" && setenv boot_once && saveenv && run do_boot_once; run boot_next'
|
||
|
fw_setenv -f bootdelay 0
|
||
|
fw_setenv -f fdt_addr_r bb100000
|
||
|
fw_setenv -f kernel_addr_r a0000000
|
||
|
fw_setenv -f kernel_comp_addr_r 88000000
|
||
|
fw_setenv -f kernel_comp_size 8000000
|
||
|
fw_setenv -f ramdisk_addr_r a4000000
|
||
|
}
|
||
|
|
||
|
set_sonic_env() {
|
||
|
echo "current env is $env"
|
||
|
BL_CONF_OLD=NONE
|
||
|
pn=$(printf "%x" $root_pn)
|
||
|
if [ "$install_env" = "onie" ]; then
|
||
|
echo "set sonic env onie"
|
||
|
fw_setenv -f linuxargs "${extra_cmdline_linux}"
|
||
|
fw_setenv -f sonic_image_1 "sysboot mmc 0:$pn any bf000000 /$BL_CONF"
|
||
|
fw_setenv -f sonic_image_2 "NONE"
|
||
|
fw_setenv -f sonic_dir_1 $image_dir
|
||
|
fw_setenv -f sonic_dir_2 "NONE"
|
||
|
fw_setenv -f sonic_version_1 `echo $image_dir | sed "s/^image-/SONiC-OS-/g"`
|
||
|
fw_setenv -f sonic_version_2 "NONE"
|
||
|
|
||
|
fw_setenv -f boot_next "run sonic_image_1"
|
||
|
else
|
||
|
check_existing_parts
|
||
|
pn=$(printf "%x" $root_pn)
|
||
|
running_sonic_revision=`cat /etc/sonic/sonic_version.yml | grep build_version | awk -F \' '{print $2}'`
|
||
|
SONIC_IMAGE_MAX=2
|
||
|
idx=0
|
||
|
for i in $(seq 1 $SONIC_IMAGE_MAX); do
|
||
|
if [ "`fw_printenv sonic_version_$i 2>/dev/null | awk -F = '{print $2}'`" != "SONiC-OS-$running_sonic_revision" ]; then
|
||
|
idx=$i
|
||
|
break
|
||
|
fi
|
||
|
done
|
||
|
|
||
|
fw_setenv linuxargs "${extra_cmdline_linux}"
|
||
|
fw_setenv sonic_image_$idx "sysboot mmc 0:$pn any bf000000 /$BL_CONF"
|
||
|
fw_setenv sonic_dir_$idx $image_dir
|
||
|
fw_setenv sonic_version_$idx `echo $image_dir | sed "s/^image-/SONiC-OS-/g"`
|
||
|
|
||
|
fw_setenv boot_next "run sonic_image_$idx"
|
||
|
|
||
|
cleanup
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
########################################################################################################################
|
||
|
|
||
|
prepare_boot_menu() {
|
||
|
echo "Sync up cache ..."
|
||
|
sync
|
||
|
echo "Setting up U-Boot environment..."
|
||
|
if [ "$install_env" = "onie" ]; then
|
||
|
bl_conf_path=$root_mnt
|
||
|
else
|
||
|
bl_conf_path=$HOST
|
||
|
fi
|
||
|
file=$bl_conf_path/$BL_CONF
|
||
|
if [ -f "$file" ]; then
|
||
|
rm "$file"
|
||
|
fi
|
||
|
create_bootloader_conf
|
||
|
MTD_UBOOTENV=$(cat /proc/mtd | grep -e 'ubootenv' | awk '{print $1}' | tr -dc '0-9')
|
||
|
FW_ENV_DEFAULT="/dev/mtd$MTD_UBOOTENV 0x0 0x1000 0x10000"
|
||
|
echo $FW_ENV_DEFAULT > /etc/fw_env.config
|
||
|
set_boot_command
|
||
|
set_sonic_env
|
||
|
}
|
||
|
|
||
|
create_partition() {
|
||
|
check_existing_parts
|
||
|
setup_partitions
|
||
|
}
|
||
|
|
||
|
mount_partition() {
|
||
|
demo_mnt=$root_mnt
|
||
|
mounted=$(mount | grep "/dev/mmcblk0p$root_pn on $demo_mnt type ext4" | wc -l)
|
||
|
if [ $mounted -eq 0 ]; then
|
||
|
mount /dev/mmcblk0p$root_pn $demo_mnt
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
bootloader_menu_config() {
|
||
|
# Update uboot Environment
|
||
|
prepare_boot_menu
|
||
|
if [ "$install_env" = "onie" ]; then
|
||
|
chmod -x /bin/onie-nos-mode
|
||
|
fi
|
||
|
}
|