create_ramdisk.ksh revision 9632cfad671ee03057c5878b18702ea3b53be47d
f808c858fa61e7769218966759510a8b1190dfcfraf#!/bin/ksh -p
f808c858fa61e7769218966759510a8b1190dfcfraf#
f808c858fa61e7769218966759510a8b1190dfcfraf# CDDL HEADER START
f808c858fa61e7769218966759510a8b1190dfcfraf#
f808c858fa61e7769218966759510a8b1190dfcfraf# The contents of this file are subject to the terms of the
f808c858fa61e7769218966759510a8b1190dfcfraf# Common Development and Distribution License (the "License").
f808c858fa61e7769218966759510a8b1190dfcfraf# You may not use this file except in compliance with the License.
f808c858fa61e7769218966759510a8b1190dfcfraf#
f808c858fa61e7769218966759510a8b1190dfcfraf# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
f808c858fa61e7769218966759510a8b1190dfcfraf# or http://www.opensolaris.org/os/licensing.
f808c858fa61e7769218966759510a8b1190dfcfraf# See the License for the specific language governing permissions
f808c858fa61e7769218966759510a8b1190dfcfraf# and limitations under the License.
f808c858fa61e7769218966759510a8b1190dfcfraf#
f808c858fa61e7769218966759510a8b1190dfcfraf# When distributing Covered Code, include this CDDL HEADER in each
f808c858fa61e7769218966759510a8b1190dfcfraf# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
f808c858fa61e7769218966759510a8b1190dfcfraf# If applicable, add the following below this CDDL HEADER, with the
f808c858fa61e7769218966759510a8b1190dfcfraf# fields enclosed by brackets "[]" replaced with your own identifying
f808c858fa61e7769218966759510a8b1190dfcfraf# information: Portions Copyright [yyyy] [name of copyright owner]
f808c858fa61e7769218966759510a8b1190dfcfraf#
f808c858fa61e7769218966759510a8b1190dfcfraf# CDDL HEADER END
f808c858fa61e7769218966759510a8b1190dfcfraf#
7c64d3750da7fda7e450b8f9b0b963905ded6379mp
f808c858fa61e7769218966759510a8b1190dfcfraf# Copyright 2006 Sun Microsystems, Inc. All rights reserved.
f808c858fa61e7769218966759510a8b1190dfcfraf# Use is subject to license terms.
f808c858fa61e7769218966759510a8b1190dfcfraf
f808c858fa61e7769218966759510a8b1190dfcfraf# ident "%Z%%M% %I% %E% SMI"
f808c858fa61e7769218966759510a8b1190dfcfraf
f808c858fa61e7769218966759510a8b1190dfcfrafformat=ufs
56a424cca6b3f91f31bdab72a4626c48c779fe8bmpALT_ROOT=
159d09a20817016f09b3ea28d1bdada4a336bb91Mark PhalanNO_AMD64=
159d09a20817016f09b3ea28d1bdada4a336bb91Mark Phalan
f808c858fa61e7769218966759510a8b1190dfcfrafBOOT_ARCHIVE=platform/i86pc/boot_archive
159d09a20817016f09b3ea28d1bdada4a336bb91Mark Phalan
159d09a20817016f09b3ea28d1bdada4a336bb91Mark Phalanexport PATH=$PATH:/usr/sbin:/usr/bin:/sbin
159d09a20817016f09b3ea28d1bdada4a336bb91Mark Phalan
159d09a20817016f09b3ea28d1bdada4a336bb91Mark Phalan#
159d09a20817016f09b3ea28d1bdada4a336bb91Mark Phalan# Parse options
159d09a20817016f09b3ea28d1bdada4a336bb91Mark Phalan#
159d09a20817016f09b3ea28d1bdada4a336bb91Mark Phalanwhile getopts R: OPT 2> /dev/null
f808c858fa61e7769218966759510a8b1190dfcfrafdo
159d09a20817016f09b3ea28d1bdada4a336bb91Mark Phalan case $OPT in
159d09a20817016f09b3ea28d1bdada4a336bb91Mark Phalan R) ALT_ROOT="$OPTARG"
159d09a20817016f09b3ea28d1bdada4a336bb91Mark Phalan if [ "$ALT_ROOT" != "/" ]; then
159d09a20817016f09b3ea28d1bdada4a336bb91Mark Phalan echo "Creating ram disk for $ALT_ROOT"
159d09a20817016f09b3ea28d1bdada4a336bb91Mark Phalan fi
159d09a20817016f09b3ea28d1bdada4a336bb91Mark Phalan ;;
f808c858fa61e7769218966759510a8b1190dfcfraf ?) echo Usage: ${0##*/}: [-R \<root\>]
f808c858fa61e7769218966759510a8b1190dfcfraf exit ;;
f808c858fa61e7769218966759510a8b1190dfcfraf esac
f808c858fa61e7769218966759510a8b1190dfcfrafdone
f808c858fa61e7769218966759510a8b1190dfcfraf
f808c858fa61e7769218966759510a8b1190dfcfrafif [ -x /usr/bin/mkisofs -o -x /tmp/bfubin/mkisofs ] ; then
f808c858fa61e7769218966759510a8b1190dfcfraf format=isofs
f808c858fa61e7769218966759510a8b1190dfcfraffi
f808c858fa61e7769218966759510a8b1190dfcfraf
f808c858fa61e7769218966759510a8b1190dfcfraf#
f808c858fa61e7769218966759510a8b1190dfcfraf# mkisofs on s8 doesn't support functionality used by GRUB boot.
f808c858fa61e7769218966759510a8b1190dfcfraf# Use ufs format for boot archive instead.
f808c858fa61e7769218966759510a8b1190dfcfraf#
f808c858fa61e7769218966759510a8b1190dfcfrafrelease=`uname -r`
f808c858fa61e7769218966759510a8b1190dfcfrafif [ "$release" = "5.8" ]; then
f808c858fa61e7769218966759510a8b1190dfcfraf format=ufs
f808c858fa61e7769218966759510a8b1190dfcfraffi
56a424cca6b3f91f31bdab72a4626c48c779fe8bmp
f808c858fa61e7769218966759510a8b1190dfcfrafshift `expr $OPTIND - 1`
f808c858fa61e7769218966759510a8b1190dfcfraf
f808c858fa61e7769218966759510a8b1190dfcfrafif [ $# -eq 1 ]; then
7c64d3750da7fda7e450b8f9b0b963905ded6379mp ALT_ROOT="$1"
f808c858fa61e7769218966759510a8b1190dfcfraf echo "Creating ram disk for $ALT_ROOT"
f808c858fa61e7769218966759510a8b1190dfcfraffi
f808c858fa61e7769218966759510a8b1190dfcfraf
f808c858fa61e7769218966759510a8b1190dfcfraffunction cleanup
f808c858fa61e7769218966759510a8b1190dfcfraf{
f808c858fa61e7769218966759510a8b1190dfcfraf umount -f "$rdmnt" 2>/dev/null
f808c858fa61e7769218966759510a8b1190dfcfraf lofiadm -d "$rdfile" 2>/dev/null
54925bf60766fbb4f1f2d7c843721406a7b7a3fbwillf rm -fr "$rddir" 2> /dev/null
f808c858fa61e7769218966759510a8b1190dfcfraf}
f808c858fa61e7769218966759510a8b1190dfcfraf
f808c858fa61e7769218966759510a8b1190dfcfraffunction getsize
7c64d3750da7fda7e450b8f9b0b963905ded6379mp{
56a424cca6b3f91f31bdab72a4626c48c779fe8bmp # Estimate image size, add %10 overhead for ufs stuff
f808c858fa61e7769218966759510a8b1190dfcfraf total_size=0
f808c858fa61e7769218966759510a8b1190dfcfraf for file in $filelist
f808c858fa61e7769218966759510a8b1190dfcfraf do
f808c858fa61e7769218966759510a8b1190dfcfraf if [ -e "$ALT_ROOT/$file" ] ; then
f808c858fa61e7769218966759510a8b1190dfcfraf du -sk "$ALT_ROOT/$file" | read size name
f808c858fa61e7769218966759510a8b1190dfcfraf (( total_size += size ))
56a424cca6b3f91f31bdab72a4626c48c779fe8bmp fi
f808c858fa61e7769218966759510a8b1190dfcfraf done
56a424cca6b3f91f31bdab72a4626c48c779fe8bmp (( total_size += total_size * 10 / 100 ))
f808c858fa61e7769218966759510a8b1190dfcfraf}
56a424cca6b3f91f31bdab72a4626c48c779fe8bmp
f808c858fa61e7769218966759510a8b1190dfcfraffunction create_ufs
f808c858fa61e7769218966759510a8b1190dfcfraf{
f808c858fa61e7769218966759510a8b1190dfcfraf # should we exclude amd64 binaries?
f808c858fa61e7769218966759510a8b1190dfcfraf [ $is_amd64 -eq 0 ] && NO_AMD64="-name amd64 -prune"
f808c858fa61e7769218966759510a8b1190dfcfraf
f808c858fa61e7769218966759510a8b1190dfcfraf
f808c858fa61e7769218966759510a8b1190dfcfraf mkfile ${total_size}k "$rdfile"
f808c858fa61e7769218966759510a8b1190dfcfraf lofidev=`lofiadm -a "$rdfile"`
f808c858fa61e7769218966759510a8b1190dfcfraf newfs $lofidev < /dev/null 2> /dev/null
f808c858fa61e7769218966759510a8b1190dfcfraf mkdir "$rdmnt"
f808c858fa61e7769218966759510a8b1190dfcfraf mount -F mntfs mnttab /etc/mnttab > /dev/null 2>&1
f808c858fa61e7769218966759510a8b1190dfcfraf mount -o nologging $lofidev "$rdmnt"
f808c858fa61e7769218966759510a8b1190dfcfraf
f808c858fa61e7769218966759510a8b1190dfcfraf # do the actual copy
f808c858fa61e7769218966759510a8b1190dfcfraf cd "/$ALT_ROOT"
159d09a20817016f09b3ea28d1bdada4a336bb91Mark Phalan
159d09a20817016f09b3ea28d1bdada4a336bb91Mark Phalan find $filelist -print $NO_AMD64 2> /dev/null | \
f808c858fa61e7769218966759510a8b1190dfcfraf cpio -pdum "$rdmnt" 2> /dev/null
f808c858fa61e7769218966759510a8b1190dfcfraf umount "$rdmnt"
f808c858fa61e7769218966759510a8b1190dfcfraf lofiadm -d "$rdfile"
159d09a20817016f09b3ea28d1bdada4a336bb91Mark Phalan rmdir "$rdmnt"
159d09a20817016f09b3ea28d1bdada4a336bb91Mark Phalan
159d09a20817016f09b3ea28d1bdada4a336bb91Mark Phalan # Check if gzip exists in /usr/bin, so we only try to run gzip
159d09a20817016f09b3ea28d1bdada4a336bb91Mark Phalan # on systems that have gzip. Then run gzip out of the patch to
159d09a20817016f09b3ea28d1bdada4a336bb91Mark Phalan # pick it up from bfubin or something like that if needed.
56a424cca6b3f91f31bdab72a4626c48c779fe8bmp #
f808c858fa61e7769218966759510a8b1190dfcfraf if [ -x /usr/bin/gzip ] ; then
56a424cca6b3f91f31bdab72a4626c48c779fe8bmp gzip -c "$rdfile" > "$ALT_ROOT/$BOOT_ARCHIVE-new"
f808c858fa61e7769218966759510a8b1190dfcfraf else
f808c858fa61e7769218966759510a8b1190dfcfraf cat "$rdfile" > "$ALT_ROOT/$BOOT_ARCHIVE-new"
f808c858fa61e7769218966759510a8b1190dfcfraf fi
56a424cca6b3f91f31bdab72a4626c48c779fe8bmp}
f808c858fa61e7769218966759510a8b1190dfcfraf
f808c858fa61e7769218966759510a8b1190dfcfraffunction create_isofs
f808c858fa61e7769218966759510a8b1190dfcfraf{
f808c858fa61e7769218966759510a8b1190dfcfraf # should we exclude amd64 binaries?
f808c858fa61e7769218966759510a8b1190dfcfraf [ $is_amd64 = 0 ] && NO_AMD64="-m amd64"
f808c858fa61e7769218966759510a8b1190dfcfraf
f808c858fa61e7769218966759510a8b1190dfcfraf # create image directory seed with graft points
f808c858fa61e7769218966759510a8b1190dfcfraf mkdir "$rdmnt"
f808c858fa61e7769218966759510a8b1190dfcfraf files=
f808c858fa61e7769218966759510a8b1190dfcfraf isocmd="mkisofs -quiet -graft-points -dlrDJN -relaxed-filenames $NO_AMD64"
f808c858fa61e7769218966759510a8b1190dfcfraf for path in $filelist
f808c858fa61e7769218966759510a8b1190dfcfraf do
f808c858fa61e7769218966759510a8b1190dfcfraf if [ -d "$ALT_ROOT/$path" ]; then
f808c858fa61e7769218966759510a8b1190dfcfraf isocmd="$isocmd $path/=\"$ALT_ROOT/$path\""
f808c858fa61e7769218966759510a8b1190dfcfraf mkdir -p "$rdmnt/$path"
f808c858fa61e7769218966759510a8b1190dfcfraf elif [ -f "$ALT_ROOT/$path" ]; then
f808c858fa61e7769218966759510a8b1190dfcfraf files="$files $path"
f808c858fa61e7769218966759510a8b1190dfcfraf fi
f808c858fa61e7769218966759510a8b1190dfcfraf done
f808c858fa61e7769218966759510a8b1190dfcfraf cd "/$ALT_ROOT"
f808c858fa61e7769218966759510a8b1190dfcfraf find $files 2> /dev/null | cpio -pdum "$rdmnt" 2> /dev/null
f808c858fa61e7769218966759510a8b1190dfcfraf isocmd="$isocmd \"$rdmnt\""
56a424cca6b3f91f31bdab72a4626c48c779fe8bmp rm -f "$errlog"
56a424cca6b3f91f31bdab72a4626c48c779fe8bmp
f808c858fa61e7769218966759510a8b1190dfcfraf # Check if gzip exists in /usr/bin, so we only try to run gzip
f808c858fa61e7769218966759510a8b1190dfcfraf # on systems that have gzip. Then run gzip out of the patch to
f808c858fa61e7769218966759510a8b1190dfcfraf # pick it up from bfubin or something like that if needed.
56a424cca6b3f91f31bdab72a4626c48c779fe8bmp #
f808c858fa61e7769218966759510a8b1190dfcfraf if [ -x /usr/bin/gzip ] ; then
f808c858fa61e7769218966759510a8b1190dfcfraf ksh -c "$isocmd" 2> "$errlog" | \
56a424cca6b3f91f31bdab72a4626c48c779fe8bmp gzip > "$ALT_ROOT/$BOOT_ARCHIVE-new"
f808c858fa61e7769218966759510a8b1190dfcfraf else
f808c858fa61e7769218966759510a8b1190dfcfraf ksh -c "$isocmd" 2> "$errlog" > "$ALT_ROOT/$BOOT_ARCHIVE-new"
f808c858fa61e7769218966759510a8b1190dfcfraf fi
f808c858fa61e7769218966759510a8b1190dfcfraf
f808c858fa61e7769218966759510a8b1190dfcfraf if [ -s "$errlog" ]; then
f808c858fa61e7769218966759510a8b1190dfcfraf grep Error: "$errlog" >/dev/null 2>&1
56a424cca6b3f91f31bdab72a4626c48c779fe8bmp if [ $? -eq 0 ]; then
56a424cca6b3f91f31bdab72a4626c48c779fe8bmp grep Error: "$errlog"
f808c858fa61e7769218966759510a8b1190dfcfraf rm -f "$ALT_ROOT/$BOOT_ARCHIVE-new"
f808c858fa61e7769218966759510a8b1190dfcfraf fi
f808c858fa61e7769218966759510a8b1190dfcfraf fi
f808c858fa61e7769218966759510a8b1190dfcfraf rm -f "$errlog"
}
#
# get filelist
#
filelist=`cat "$ALT_ROOT/boot/solaris/filelist.ramdisk"`
if [ -f "$ALT_ROOT/etc/boot/solaris/filelist.ramdisk" ]; then
filelistI=`cat "$ALT_ROOT/etc/boot/solaris/filelist.ramdisk"`
fi
filelist=`echo $filelist $filelistI | sort -u`
#
# decide if cpu is amd64 capable
#
prtconf -v /devices | grep CPU_not_amd64 > /dev/null 2>&1
is_amd64=$?
scratch=tmp
if [ $format = ufs ] ; then
# calculate image size
getsize
# check to see if there is sufficient space in tmpfs
#
tmp_free=`df -b /tmp | tail -1 | awk '{ printf ($2) }'`
(( tmp_free = tmp_free / 2 ))
if [ $total_size -gt $tmp_free ] ; then
# assumes we have enough scratch space on $ALT_ROOT
scratch="$ALT_ROOT"
fi
fi
rddir="/$scratch/create_ramdisk.$$.tmp"
rdfile="$rddir/rd.file"
rdmnt="$rddir/rd.mount"
errlog="$rddir/rd.errlog"
# make directory for temp files safely
rm -rf "$rddir"
mkdir "$rddir"
# Clean up upon exit.
trap 'cleanup' EXIT
echo "updating $ALT_ROOT/$BOOT_ARCHIVE...this may take a minute"
if [ $format = "ufs" ]; then
create_ufs
else
create_isofs
fi
# sanity check the archive before moving it into place
# the file type check also establishes that the file exists at all
#
ARCHIVE_SIZE=`du -k "$ALT_ROOT/$BOOT_ARCHIVE-new" | cut -f 1`
file "$ALT_ROOT/$BOOT_ARCHIVE-new" | grep gzip > /dev/null
if [ $? = 1 ] && [ -x /usr/bin/gzip ] || [ $ARCHIVE_SIZE -lt 5000 ]; then
echo "update of $ALT_ROOT/$BOOT_ARCHIVE failed"
rm -rf "$rddir"
exit 1
fi
#
# For the diskless case, hardlink archive to /boot to make it
# visible via tftp. /boot is lofs mounted under /tftpboot/<hostname>.
# NOTE: this script must work on both client and server
#
grep "[ ]/[ ]*nfs[ ]" "$ALT_ROOT/etc/vfstab" > /dev/null
if [ $? = 0 ]; then
mv "$ALT_ROOT/$BOOT_ARCHIVE-new" "$ALT_ROOT/$BOOT_ARCHIVE"
rm -f "$ALT_ROOT/boot/boot_archive"
ln "$ALT_ROOT/$BOOT_ARCHIVE" "$ALT_ROOT/boot/boot_archive"
rm -rf "$rddir"
exit
fi
lockfs -f "/$ALT_ROOT"
mv "$ALT_ROOT/$BOOT_ARCHIVE-new" "$ALT_ROOT/$BOOT_ARCHIVE"
lockfs -f "/$ALT_ROOT"
rm -rf "$rddir"