#!/bin/bash # this script installs a new release image into a running system. # the new ISO image must be already mounted by caller. # the script sets up a new union mount for the new release. a reboot # is then required to boot into the newly installed release. # the current install type: "union" or "old" CUR_INSTALL=$1 source /opt/vyatta/sbin/install-functions failure_exit () { echo "$*" exit 1 } if [ `whoami` != 'root' ] ; then failure_exit 'This script must be run with root privileges.' fi # On image-installed systems, the image name can be found as the # directory under "/boot" on the path to the running kernel on the # boot line. On non-image-installed systems, this yelds the # name of the kernel image file. CURVER=`awk '{print $1}' /proc/cmdline` CURVER=${CURVER#BOOT_IMAGE=/boot/} CURVER=${CURVER%/vmlinuz*} if [ -z "$CURVER" ]; then failure_exit 'Cannot find current version.' fi # get new version string. this is from the squashfs image. NEWVER=$(grep '^Version ' ${CD_SQUASH_ROOT}${vyatta_sysconfdir}/version \ | tr -s ' ' | cut -d ' ' -f 3) if [ -z "$NEWVER" ]; then failure_exit 'Cannot find new release version.' fi if [ "$CURVER" == "$NEWVER" ]; then echo "Image version $NEWVER is the same as the running system." echo "Cannot install the same release version as the running system." exit 1 fi # this is the default if current install is union BOOT_DIR=/live/image/boot if [ "$CUR_INSTALL" == 'old' ]; then BOOT_DIR=/boot elif [ "$CUR_INSTALL" != 'union' ]; then echo 'Invalid current install type. Exiting...' exit 1 fi if [ -d $BOOT_DIR/$NEWVER ]; then echo "Version $NEWVER is already installed on this system." echo "Proceeding with this installation will delete this copy of" echo "$NEWVER and replace it with a new copy." echo -n "Do you want to replace it? (Yes/No) [No]: " resp=$(get_response "No" "Yes No Y N") if [ "$resp" != 'yes' ] && [ "$resp" != 'y' ]; then echo "OK. Will not replace $NEWVER" echo "Exiting..." exit 1 fi fi # start the install echo "Installing \"$NEWVER\" release." # create the new release directories REL_ROOT=$BOOT_DIR/$NEWVER RW_DIR="$REL_ROOT/live-rw" if ! mkdir -p "$RW_DIR"; then failure_exit 'Cannot create directory for new release.' fi # copy the squashfs image and boot files echo "Copying new release files..." squash_img=${CD_ROOT}/live/filesystem.squashfs boot_dir=${CD_SQUASH_ROOT}/boot boot_files=$(find $boot_dir -maxdepth 1 -type f -o -type l 2>/dev/null) if [ ! -f "$squash_img" ] || [ -z "$boot_files" ]; then becho 'Cannot find the files. Exiting...' exit 1 fi target_squash=$REL_ROOT/$NEWVER.squashfs cp -p $squash_img $target_squash >&/dev/null cp -dp $boot_files $REL_ROOT/ >&/dev/null # mount copied squashfs if ! try_mount "-o loop,ro $target_squash $READ_ROOT"; then failure_exit 'Failed to mount new squashfs image.' fi # set up root for postinst margs="-t unionfs -o noatime,dirs=$RW_DIR=rw:$READ_ROOT=ro unionfs" margs="$margs $INST_ROOT" if ! try_mount "$margs"; then failure_exit 'Failed to set up root directory for postinst.' fi # set up /var/run fstab entry PI_FSTAB=$INST_ROOT/etc/fstab if ! grep -q 'tmpfs /var/run ' $PI_FSTAB >&/dev/null; then # replace the fstab. the default one has header that will cause # it to be wiped out on live boot. echo 'tmpfs /var/run tmpfs nosuid,nodev 0 0' >$PI_FSTAB fi # save current config dir if needed if [ -f "$VYATTA_CFG_DIR/config.boot" ]; then resp='' while [ -z "$resp" ]; do echo 'Would you like to save the current configuration ' echo 'directory and use the current start-up configuration ' echo -n 'for the new version? (Yes/No) [Yes]: ' resp=$(get_response "Yes" "Yes No Y N") if [ "$resp" == 'yes' ] || [ "$resp" == 'y' ]; then echo 'Copying current configuration...' ndir=${INST_ROOT}${VYATTA_CFG_DIR} mkdir -p $ndir find $VYATTA_CFG_DIR -maxdepth 1 -mindepth 1 \ -exec cp '-a' '{}' "$ndir/" ';' chgrp -R vyattacfg $ndir chmod -R 775 $ndir fi done fi # postinst hook PI_SCRIPT=${INST_ROOT}${vyatta_sysconfdir}/install-image/postinst if [ -e "$PI_SCRIPT" ]; then echo "Running post-install script..." $PI_SCRIPT $INST_ROOT fi # set up grub entry (if provided) DEF_GRUB=${INST_ROOT}${vyatta_sysconfdir}/grub/default-union-grub-entry if [ -e "$DEF_GRUB" ]; then echo "Setting up grub configuration..." old_grub_cfg=$BOOT_DIR/grub/grub.cfg new_grub_cfg=/tmp/grub.cfg.$$ sed -n '/^menuentry/q;p' $old_grub_cfg >$new_grub_cfg cat $DEF_GRUB >>$new_grub_cfg sed -n '/^menuentry/,${p}' $old_grub_cfg >>$new_grub_cfg sed -i 's/^set default=[0-9]\+$/set default=0/' $new_grub_cfg mv $new_grub_cfg $old_grub_cfg fi echo 'Done.' # done exit 0