sonic-buildimage/files/Aboot/boot0.j2

189 lines
5.8 KiB
Plaintext
Raw Normal View History

#!/bin/sh
2016-03-16 01:38:26 -05:00
# Copyright (C) 2016 Arista Networks, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Aboot stage 0 boot
set -x
kernel=boot/vmlinuz-3.16.0-5-amd64
initrd=boot/initrd.img-3.16.0-5-amd64
kernel_params=kernel-params
aboot_machine="arista_unknown"
[ -z "$target_path" ] && target_path=/mnt/flash
2017-04-21 19:23:36 -05:00
image_path="$target_path/image-%%IMAGE_VERSION%%"
# expect the swi to be a non empty file
[ -s "$swipath" ] || exit 1
2016-03-16 01:38:26 -05:00
bootconfigvars="SWI SWI_COPY POST_LEVEL CONSOLESPEED PASSWORD NETDEV NETAUTO NETIP NETMASK NETGW NETDOMAIN NETDNS NETHW memtest"
parse_environment_config() {
2016-03-16 01:38:26 -05:00
for n in ${bootconfigvars}; do
eval v="\$$n"
if [ "$v" ]; then
echo "$n=$v"
fi
done
}
clean_flash() {
## Remove all the other unnecssary files except swi file, boot-config
for f in $(ls -A $target_path); do
if [ $f != "${swipath##*/}" ] &&
[ $f != "boot-config" ] &&
[ $f != "minigraph.xml" ]
then
rm -rf "$target_path/$f"
fi
done
}
extract_image() {
mkdir -p "$image_path"
2017-04-21 19:23:36 -05:00
## Unzip the image except boot0 and dockerfs archive
unzip -oq "$swipath" -x boot0 {{ FILESYSTEM_DOCKERFS }} -d "$image_path"
## detect rootfs type
rootfs_type=`grep " $target_path " /proc/mounts | cut -d' ' -f3`
## vfat does not support symbol link
if [ -n "$sonic_upgrade" ] || [ "$rootfs_type" != "vfat" ]; then
2017-04-21 19:23:36 -05:00
mkdir -p "$image_path/{{ DOCKERFS_DIR }}"
if [ -n "$sonic_upgrade" ]; then
TAR_EXTRA_OPTION="--numeric-owner"
fi
## extract docker archive
unzip -oqp "$swipath" {{ FILESYSTEM_DOCKERFS }} | tar xzf - -C "$image_path/{{ DOCKERFS_DIR }}" $TAR_EXTRA_OPTION
else
## save dockerfs archive in the image directory
unzip -oq "$swipath" {{ FILESYSTEM_DOCKERFS }} -d "$image_path"
echo "$target_path is $rootfs_type, extract {{ FILESYSTEM_DOCKERFS }} in later stage"
fi
2017-04-21 19:23:36 -05:00
## use new reduced-size boot swi
echo "SWI=flash:image-%%IMAGE_VERSION%%/{{ ABOOT_BOOT_IMAGE }}" > "$target_path/boot-config"
## Remove installer swi as it has lots of redundunt contents
rm -f "$swipath"
## sync disk operations
sync
}
write_machine_config() {
## Detect SKU and create a hardware description file
aboot_version=$(grep ^Aboot /etc/cmdline | sed 's/^.*norcal.-//')
aboot_build_date=$(stat -c %y /bin/sysinit | sed 's/ /T/')
cat <<EOF > ${target_path}/machine.conf
aboot_version=$aboot_version
aboot_vendor=arista
aboot_platform=x86_64-$aboot_machine
aboot_machine=$aboot_machine
aboot_arch=x86_64
aboot_build_date=$aboot_build_date
EOF
}
platform_specific() {
local platform="$(grep -Eo 'platform=[^ ]+' /etc/cmdline | cut -f2 -d=)"
local sid="$(grep -Eo 'sid=[^ ]+' /etc/cmdline | cut -f2 -d=)"
# set varlog size to 100MB
local varlog_size=100
local flash_size=$(($(df | grep flash | tr -s ' ' | cut -f2 -d' ') / 1000))
if [ "$platform" = "raven" ]; then
aboot_machine=arista_7050_qx32
flash_size=2000
echo "modprobe.blacklist=radeon" >>/tmp/append
fi
if [ "$platform" = "crow" ]; then
aboot_machine=arista_7050_qx32s
flash_size=3700
echo "modprobe.blacklist=radeon" >>/tmp/append
fi
if [ "$sid" = "Upperlake" ] || [ "$sid" = "UpperlakeES" ]; then
aboot_machine=arista_7060_cx32s
flash_size=3700
echo "amd_iommu=off" >> /tmp/append
fi
if [ "$sid" = "Gardena" ] || [ "$sid" = "GardenaSsd" ]; then
aboot_machine=arista_7260cx3_64
flash_size=28000
fi
if [ "$platform" = "rook" ]; then
readprefdl -f /tmp/.system-prefdl -d > /mnt/flash/.system-prefdl
fi
if [ $flash_size -ge 28000 ]; then
varlog_size=4096
elif [ $flash_size -ge 3700 ]; then
varlog_size=400
fi
echo "varlog_size=$varlog_size" >>/tmp/append
}
2016-03-16 01:38:26 -05:00
# check the hash file in the image, and determine to install or just skip
GIT_REVISION=$(unzip -p "$swipath" .imagehash)
LOCAL_IMAGEHASH=$(cat $image_path/.imagehash 2>/dev/null || true)
if [ "$GIT_REVISION" != "$LOCAL_IMAGEHASH" ]; then
[ -z "$sonic_upgrade" ] && clean_flash
extract_image
fi
[ -z "$sonic_upgrade" ] || exit 0
# build the new cmdline
echo "$append" >/tmp/append
parse_environment_config >>/tmp/append
2016-03-16 01:38:26 -05:00
cat /etc/cmdline | sed "/^\(${bootconfigvars// /\|}\|crashkernel\|loglevel\|ignore_loglevel\)\(\$\|=\)/d;/^\$/d" >>/tmp/append
2017-04-21 19:23:36 -05:00
echo "rw loop=image-%%IMAGE_VERSION%%/fs.squashfs loopfstype=squashfs apparmor=1 security=apparmor quiet" >>/tmp/append
# process platform specific operations
platform_specific
[ -e ${taget_path}/machine.conf ] || write_machine_config
# use extra parameters from kernel-params hook if the file exists
2017-04-21 19:23:36 -05:00
if [ -f "$image_path/$kernel_params" ]; then
cat "$image_path/$kernel_params" >>/tmp/append
fi
# setting root partition if not overridden by kernel-params
if ! grep -q "root=" /tmp/append; then
rootdev=$(mount | grep '/mnt/flash' | cut -f1 -d' ')
rootfstype=$(mount | grep '/mnt/flash' | cut -f5 -d' ')
echo "root=$rootdev" >>/tmp/append
fi
# chainloading using kexec
2017-04-21 19:23:36 -05:00
initrd_path="$image_path/$initrd"
kernel_path="$image_path/$kernel"
cmdline="$(tr '\n' ' ' </tmp/append)"
2016-03-16 01:38:26 -05:00
kexec --load --initrd="$initrd_path" --append="$cmdline" "$kernel_path"
[ -z "$testonly" ] || exit 0
2016-03-16 01:38:26 -05:00
kexec --exec