Update doc/getting-started/gsg_quick_setup.sh

Co-Authored-By: lirui34 <48583653+lirui34@users.noreply.github.com>
This commit is contained in:
David Kinder 2019-03-21 19:08:26 +08:00
parent 64f74b76d6
commit 1312fc6f33

View File

@ -3,37 +3,46 @@
# SPDX-License-Identifier: BSD-3-Clause # SPDX-License-Identifier: BSD-3-Clause
# #
# This script provides a quick and automatic setup of the SOS or UOS. # This script provides a quick and automatic setup of the SOS or UOS.
# You can also upgrade the SOS or UOS with a specified Clear Linux version. # You should run this script with root privilege since it will modify various system parameters.
# Also you should know that system may have boot problems once the script is running failed #
# since it will modify various system parameters with full root access. # Note:
# This script is using /dev/sda1 as default EFI System Partition (ESP).
# The ESP may be different based on your hardware.
# It will typically be something like /dev/mmcblk0p1 on platforms that have an on-board eMMC
# Or /dev/nvme0n1p1 if your system has a non-volatile storage media attached via a PCI Express (PCIe) bus (NVMe).
# You should edit it to the right ESP before running this script.
# For example, you should change 'efi_partition' as following if you are using NVMe:
# efi_partition=/dev/nvme0n1p1
# #
# Usages: # Usages:
# Upgrade sos to 28100 # Upgrade SOS to 28100 without reboot, it's highly recommended so that you can check configurations after upgrade SOS.
# sudo <script> -t 28100 # sudo <script> -s 28100 -d
# Upgrade sos to 28100 with specified proxy server # Upgrade SOS to 28100 with specified proxy server
# sudo <script> -t 28100 -p <your proxy server>:<port> # sudo <script> -s 28100 -p <your proxy server>:<port>
# Upgrade uos to 28100 with specified proxy server # Upgrade UOS to 28100 with specified proxy server
# sudo <script> -t 28100 -p <your proxy server>:<port> -u # sudo <script> -u 28100 -p <your proxy server>:<port>
# Upgrade uos to 28100 without downloading uos image, you should put uos image in /root directory previously. # Upgrade UOS to 28100 without downloading UOS image, you should put UOS image in /root directory previously.
# sudo <script> -t 28100 -u -s # sudo <script> -u 28100 -k
function print_help() function print_help()
{ {
echo "Usage:" echo "Usage:"
echo "Launch this script as: sudo $0 -t 28100" echo "Launch this script as: sudo $0 -s 28100"
echo -e "\t-t to specify clear linux version for upgrading" echo -e "\t-s to upgrade SOS"
echo -e "\t-u to upgrade UOS"
echo -e "\t-p to specify a proxy server" echo -e "\t-p to specify a proxy server"
echo -e "\t-m to use swupd mirror url" echo -e "\t-m to use swupd mirror url"
echo -e "\t-u to upgrade UOS." echo -e "\t-k to skip downloading UOS; if enabled, you have to download UOS img firstly before upgrading, default is off"
echo -e "\t-s, skip downloading uos; if enabled, you have to download uos img firstly before upgrading, default is off" echo -e "\t-d to disable reboot device so that you can check the configurations after upgrading SOS"
exit 1 exit 1
} }
# switch for download uos function # get clear linux version previously
source /etc/os-release
# switcher for download UOS function
skip_download_uos=0 skip_download_uos=0
# turn on if you need to setup uos # switcher for disabling reboot device
upgrade_uos=0 disable_reboot=0
# acrn.conf path # acrn.conf path
acrn_conf_path=/usr/share/acrn/samples/nuc/acrn.conf acrn_conf_path=/usr/share/acrn/samples/nuc/acrn.conf
@ -42,65 +51,39 @@ acrn_efi_path=/usr/lib/acrn/acrn.efi
function upgrade_sos() function upgrade_sos()
{ {
# Check SOS version
[[ `echo $sos_ver | awk '{print length($0)}'` -ne 5 ]] && echo "Please input right SOS version to upgrade" && exit 1
[[ $VERSION_ID -gt $sos_ver ]] && echo "You're trying to install an older version of Clear Linux." && exit 1
echo "Upgrading SOS..."
# setup mirror and proxy url while specified with m and p options # setup mirror and proxy url while specified with m and p options
if [[ -n $mirror ]]; then [[ -n $mirror ]] && echo "Setting swupd mirror to: $mirror" && swupd mirror -s $mirror
echo "Setting swupd mirror to: $mirror" [[ -n $proxy ]] && echo "Setting proxy to: $proxy" && export https_proxy=$proxy
swupd mirror -s $mirror
fi
if [[ -n $proxy ]]; then
echo "Setting proxy to: $proxy"
export https_proxy=$proxy
fi
# get partitions # You should set EFI path before upgrading SOS.
type_efi=EFI\ System efi_partition=/dev/sda1
partition_number=`fdisk -l | grep "$type_efi" | awk -F' ' '{print $1}' | wc -l` # Check EFI path exists.
if [ $partition_number != "1" ]; then [[ ! -b $efi_partition ]] && echo "Please choose the right EFI path firstly." && exit 1
echo "Multiple partitions detected."
c=0
for i in `fdisk -l | grep "$type_efi" | awk -F' ' '{print $1}'`; do
if [[ ! -z $efi_partition ]]; then break; fi
c=$((c+1))
echo "$c. $i"
while true; do
read -p "Do you wish to setup on this partition ? (Y/N)" yn
case $yn in
[Yy]* ) efi_partition=$i; break;;
[Nn]* ) break;;
* ) echo "Please input yes or no.";;
esac
done
done
else
efi_partition=`fdisk -l | grep "$type_efi" | awk -F' ' '{print $1}'`
fi
if [[ -z $efi_partition ]]; then echo "You didn't choose a partition, please try it again."; exit 1; fi
efi_mount_point=`findmnt $efi_partition -n | cut -d' ' -f1` efi_mount_point=`findmnt $efi_partition -n | cut -d' ' -f1`
root_partition=`echo $efi_partition | sed 's/[0-9]$/3/g'` root_partition=`echo $efi_partition | sed 's/1$/3/g'`
partition=`echo $efi_partition | sed 's/[0-9]$//g'` partition=`echo $efi_partition | sed 's/1$//g;s/p$//g'`
# unmount efi partition if it's already mounted
if [[ -n $efi_mount_point ]]; then
echo "Unmount EFI partition: $efi_mount_point..."
umount $efi_mount_point
fi
echo "Disable auto update..." echo "Disable auto update..."
swupd autoupdate --disable swupd autoupdate --disable 2>/dev/null
# Compare with current clear linux and skip upgrade sos if get the same version. # Compare with current clear linux and skip upgrade SOS if get the same version.
source /etc/os-release if [[ $VERSION_ID -eq $sos_ver ]]; then
if [[ $VERSION_ID -eq $target_version ]]; then echo "Clear Linux version $sos_ver is already installed. Continuing to setup SOS..."
echo "Clear Linux version $target_version is already installed. Continuing to setup SOS..."
else else
echo "Upgrading Clear Linux version from $VERSION_ID to $target_version ..." echo "Upgrading Clear Linux version from $VERSION_ID to $sos_ver ..."
swupd verify --fix --picky -m $target_version swupd verify --fix --picky -m $sos_ver 2>/dev/null
fi fi
# Do the setups if previous process succeed. # Do the setups if previous process succeed.
if [[ $? -eq 0 ]]; then if [[ $? -eq 0 ]]; then
echo "Add service-os kernel-iot-lts2018 bundle" echo "Adding the service-os and kernel-iot-lts2018 bundles..."
swupd bundle-add service-os kernel-iot-lts2018 swupd bundle-add service-os kernel-iot-lts2018 2>/dev/null
mount $efi_partition /mnt mount $efi_partition /mnt
echo "Add /mnt/EFI/acrn folder" echo "Add /mnt/EFI/acrn folder"
@ -112,7 +95,7 @@ if [[ $? -eq 0 ]]; then
exit 1 exit 1
fi fi
cp -r $acrn_conf_path /mnt/loader/entries/ cp -r $acrn_conf_path /mnt/loader/entries/
if [[ $? -ne 0 ]]; then echo "You didn't choose an right EFI partition." && exit 1; fi if [[ $? -ne 0 ]]; then echo "Fail to copy $acrn_conf_path" && exit 1; fi
echo "Copy $acrn_efi_path to /mnt/EFI/acrn" echo "Copy $acrn_efi_path to /mnt/EFI/acrn"
if [[ ! -f $acrn_efi_path ]]; then if [[ ! -f $acrn_efi_path ]]; then
echo "Missing acrn.efi file in folder: $acrn_efi_path" echo "Missing acrn.efi file in folder: $acrn_efi_path"
@ -120,25 +103,26 @@ if [[ $? -eq 0 ]]; then
exit 1 exit 1
fi fi
cp -r $acrn_efi_path /mnt/EFI/acrn/ cp -r $acrn_efi_path /mnt/EFI/acrn/
if [[ $? -ne 0 ]]; then echo "Fail to copy $acrn_efi_path" && exit 1; fi
echo "Check ACRN efi boot event" echo "Check ACRN efi boot event"
check_acrn_bootefi=`efibootmgr | grep ACRN` check_acrn_bootefi=`efibootmgr | grep ACRN`
if [[ "$check_arcn_bootefi" -ge "ACRN" ]]; then if [[ "$check_arcn_bootefi" -ge "ACRN" ]]; then
echo "Clean all ACRN efi boot event" echo "Clean all ACRN efi boot event"
efibootmgr | grep ACRN | cut -d'*' -f1 | cut -d't' -f2 | xargs -i efibootmgr -b {} -B efibootmgr | grep ACRN | cut -d'*' -f1 | cut -d't' -f2 | xargs -i efibootmgr -b {} -B >/dev/null
fi fi
echo "Check linux bootloader event" echo "Check linux bootloader event"
number=$(expr `efibootmgr | grep 'Linux bootloader' | wc -l` - 1) number=$(expr `efibootmgr | grep 'Linux bootloader' | wc -l` - 1)
if [[ $number -ge 1 ]]; then if [[ $number -ge 1 ]]; then
echo "Clean all Linux bootloader event" echo "Clean all Linux bootloader event"
efibootmgr | grep 'Linux bootloader' | cut -d'*' -f1 | cut -d't' -f2 | head -n$number | xargs -i efibootmgr -b {} -B efibootmgr | grep 'Linux bootloader' | cut -d'*' -f1 | cut -d't' -f2 | head -n$number | xargs -i efibootmgr -b {} -B >/dev/null
fi fi
echo "Add new ACRN efi boot event" echo "Add new ACRN efi boot event"
efibootmgr -c -l "\EFI\acrn\acrn.efi" -d $partition -p 1 -L "ACRN" efibootmgr -c -l "\EFI\acrn\acrn.efi" -d $partition -p 1 -L "ACRN" >/dev/null
echo "Create loader.conf" echo "Create loader.conf"
mv /mnt/loader/loader.conf /mnt/loader/loader.conf.bck && touch /mnt/loader/loader.conf mv /mnt/loader/loader.conf /mnt/loader/loader.conf.bck && touch /mnt/loader/loader.conf
echo "Add default boot wait time" echo "Add default (5 seconds) boot wait time"
echo "timeout 5" > /mnt/loader/loader.conf echo "timeout 5" > /mnt/loader/loader.conf
echo "Add default boot to ACRN" echo "Add default boot to ACRN"
echo "default acrn" >> /mnt/loader/loader.conf echo "default acrn" >> /mnt/loader/loader.conf
@ -150,100 +134,105 @@ if [[ $? -eq 0 ]]; then
echo "Replacing root partition uuid in acrn.conf" echo "Replacing root partition uuid in acrn.conf"
sed -i "s/<UUID of rootfs partition>/`blkid -s PARTUUID -o value $root_partition`/g" /mnt/loader/entries/acrn.conf sed -i "s/<UUID of rootfs partition>/`blkid -s PARTUUID -o value $root_partition`/g" /mnt/loader/entries/acrn.conf
# test replacing succeed or not.
if [[ -z `grep $(blkid -s PARTUUID -o value $root_partition) /mnt/loader/entries/acrn.conf` ]]; then
echo "Fail to replace root uuid in this file: /mnt/loader/entries/acrn.conf"
exit 1
fi
if [[ "$cur_kernel" == "$new_kernel" ]]; then if [[ "$cur_kernel" == "$new_kernel" ]]; then
echo "No need to replace kernel conf info" echo "No need to replace kernel conf info"
else else
echo "Replace with new sos kernel in acrn.conf" echo "Replace with new SOS kernel in acrn.conf"
sed -i "s/$cur_kernel/$new_kernel/g" /mnt/loader/entries/acrn.conf sed -i "s/$cur_kernel/$new_kernel/g" /mnt/loader/entries/acrn.conf
fi fi
echo "Service OS setup done!" echo "Service OS setup done!"
echo "Rebooting Service OS to taking effect changes."
else else
echo -e "Fail to upgrade sos $target_clearlinux from mirror url." echo "Fail to upgrade SOS to $sos_ver."
echo "Please try upgrade SOS with this command:"
echo "swupd update -m $sos_ver"
exit 1
fi fi
umount /mnt umount /mnt
sync sync
reboot [[ $disable_reboot == 0 ]] && echo "Rebooting Service OS to take effects." && reboot -f
} }
function upgrade_uos() function upgrade_uos()
{ {
# Check UOS version
[[ `echo $uos_ver | awk '{print length($0)}'` -ne 5 ]] && echo "Please input right UOS version to upgrade" && exit 1
echo "Upgrading UOS..."
# UOS download link # UOS download link
uos_image_link="https://download.clearlinux.org/releases/$target_version/clear/clear-$target_version-kvm.img.xz" uos_image_link="https://download.clearlinux.org/releases/$uos_ver/clear/clear-$uos_ver-kvm.img.xz"
# Set proxy if needed. # Set proxy if needed.
if [[ -n $proxy ]]; then [[ -n $proxy ]] && echo "Setting proxy to: $proxy" && export https_proxy=$proxy
echo "Setting proxy to: $proxy" # Corrupt script if /mnt is already mounted.
export https_proxy=$proxy if [[ ! -z `findmnt /mnt -n` ]]; then
echo "/mnt is already mounted, please unmount it if you want to continue upgrade UOS."
exit 1
fi fi
if [[ ! -z `findmnt /mnt -n` ]]; then umount /mnt; fi # Do upgrade UOS process.
# Do upgrade uos process.
if [[ $skip_download_uos == 1 ]]; then if [[ $skip_download_uos == 1 ]]; then
uos_img_xz=$(find ~/ -name clear-$target_version-kvm.img.xz) uos_img_xz=$(find ~/ -name clear-$uos_ver-kvm.img.xz)
uos_img=$(find ~/ -name clear-$target_version-kvm.img) uos_img=$(find ~/ -name clear-$uos_ver-kvm.img)
if [[ -f $uos_img ]] && [[ -f $uos_img.xz ]]; then echo "Moving $uos_img to $uos_img.old."; mv $uos_img $uos_img.old; fi
if [[ ! -f $uos_img_xz ]] && [[ ! -f $uos_img ]]; then if [[ ! -f $uos_img_xz ]] && [[ ! -f $uos_img ]]; then
echo "You should download uos clear-$target_version-kvm.img.xz file firstly." && exit 1 echo "You should download UOS clear-$uos_ver-kvm.img.xz file firstly." && exit 1
fi fi
if [[ -f $uos_img_xz ]]; then if [[ -f $uos_img_xz ]]; then
echo "Unxz uos file: $uos_img_xz" echo "Unxz UOS file: $uos_img_xz"
unxz $uos_img_xz unxz $uos_img_xz
uos_img=`echo $uos_img_xz | sed 's/.xz$//g'` uos_img=`echo $uos_img_xz | sed 's/.xz$//g'`
fi fi
echo "get uos img file: $uos_img"
if [[ $? -eq 0 ]]; then
uos_partition=`losetup -f -P --show $uos_img`p3
else
echo "fail to losetup uos img, please check uos img status" && exit 1
fi
mount $uos_partition /mnt
cp -r /usr/lib/modules/"`readlink /usr/lib/kernel/default-iot-lts2018 | awk -F '2018.' '{print $2}'`.iot-lts2018" /mnt/lib/modules
else else
cd ~ cd ~
echo "Downloading UOS image: $uos_image_link" echo "Downloading UOS image: $uos_image_link"
curl $uos_image_link -o clear-$target_version-kvm.img.xz curl $uos_image_link -o clear-$uos_ver-kvm.img.xz || echo "Download UOS failed." && rm clear-$uos_ver-kvm.img.xz && exit 1
uos_img=clear-$target_version-kvm.img uos_img=clear-$uos_ver-kvm.img
if [[ -f $uos_img ]] && [[ -f $uos_img.xz ]]; then echo "Moving $uos_img to $uos_img.old."; mv $uos_img $uos_img.old; fi if [[ -f $uos_img ]] && [[ -f $uos_img.xz ]]; then echo "Moving $uos_img to $uos_img.old."; mv $uos_img $uos_img.old; fi
if [[ $? -eq 0 ]]; then echo "Unxz UOS image: clear-$uos_ver-kvm.img.xz"
echo "Unxz UOS image: clear-$target_version-kvm.img.xz" unxz clear-$uos_ver-kvm.img.xz
unxz clear-$target_version-kvm.img.xz
fi fi
if [[ $? -eq 0 ]]; then
echo "Get UOS image: $uos_img"
uos_partition=`losetup -f -P --show $uos_img`p3 uos_partition=`losetup -f -P --show $uos_img`p3
else
echo "Missing UOS file: $uos_img, please check if UOS is exist." && exit 1
fi
mount $uos_partition /mnt mount $uos_partition /mnt
[[ $? -ne 0 ]] && echo "Fail to mount UOS partition" && exit 1
cp -r /usr/lib/modules/"`readlink /usr/lib/kernel/default-iot-lts2018 | awk -F '2018.' '{print $2}'`.iot-lts2018" /mnt/lib/modules cp -r /usr/lib/modules/"`readlink /usr/lib/kernel/default-iot-lts2018 | awk -F '2018.' '{print $2}'`.iot-lts2018" /mnt/lib/modules
fi
umount /mnt umount /mnt
losetup -D
sync sync
cp -r /usr/share/acrn/samples/nuc/launch_uos.sh ~/ cp -r /usr/share/acrn/samples/nuc/launch_uos.sh ~/
sed -i "s/\(virtio-blk.*\)\/home\/clear\/uos\/uos.img/\1$(echo $uos_img | sed "s/\//\\\\\//g")/" ~/launch_uos.sh sed -i "s/\(virtio-blk.*\)\/home\/clear\/uos\/uos.img/\1$(echo $uos_img | sed "s/\//\\\\\//g")/" ~/launch_uos.sh
[[ -z `grep $uos_img ~/launch_uos.sh` ]] && echo "Fail to replace uos image in launch script: ~/launch_uos.sh" && exit 1
echo "Upgrade UOS done..." echo "Upgrade UOS done..."
echo "Now you can run this command to start UOS..." echo "Now you can run this command to start UOS..."
echo "# cd ~/ && ./launch_uos.sh" echo "# cd ~/ && ./launch_uos.sh"
exit
} }
# Set script options. # Set script options.
while getopts "t:p:m:huso" opt while getopts "s:u:p:m:kdh" opt
do do
case "$opt" in case "$opt" in
t) target_version="$OPTARG" s) sos_ver="$OPTARG"
;;
u) uos_ver="$OPTARG"
;; ;;
p) proxy="$OPTARG" p) proxy="$OPTARG"
;; ;;
m) mirror="$OPTARG" m) mirror="$OPTARG"
;; ;;
u) upgrade_uos=1 k) skip_download_uos=1
;; ;;
s) skip_download_uos=1 d) disable_reboot=1
;; ;;
h) print_help h) print_help
;; ;;
@ -252,26 +241,8 @@ do
esac esac
done done
# -t opt is must # Check args
if [[ -z $1 || $EUID -ne 0 ]]; then [[ $EUID -ne 0 ]] && echo "You have to run script as root." && exit 1
print_help [[ -z $1 ]] && print_help
fi [[ -n $sos_ver && -n $uos_ver ]] && echo "You should select upgrading SOS or UOS" && exit 1
[[ -n $uos_ver ]] && upgrade_uos || upgrade_sos
# You need to specify the correct clear linux ver number.
if [[ `echo $target_version | awk '{print length($0)}'` -ne 5 ]]; then
echo 'Please input right clearlinux version to upgrade' && exit 1
fi
if [[ $upgrade_uos == 1 ]]; then
echo "Upgrading UOS to: $target_version ..."
upgrade_uos
exit
fi
# Exit script if you specified an older clear linux version.
if [[ $(echo -e `swupd info` | cut -d' ' -f3) -gt $target_version ]]; then
echo -e 'No need to upgrade Service OS.' && exit
else
upgrade_sos
exit
fi