common.ksh revision edfa49ff6d1bd39465e21e3b28aee863e91c5e3f
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
# Copyright 2009 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
#
# Send the error message to the screen and to the logfile.
#
error()
{
typeset fmt="$1"
shift
printf "${MSG_PREFIX}ERROR: ${fmt}\n" "$@"
}
fatal()
{
typeset fmt="$1"
shift
exit $EXIT_CODE
}
#
# Send the provided printf()-style arguments to the screen and to the logfile.
#
log()
{
typeset fmt="$1"
shift
printf "${MSG_PREFIX}${fmt}\n" "$@"
}
#
# Print provided text to the screen if the shell variable "OPT_V" is set.
# The text is always sent to the logfile.
#
vlog()
{
typeset fmt="$1"
shift
}
# Validate that the directory is safe.
safe_dir()
{
typeset dir="$1"
fi
}
# Only make a copy if we haven't already done so.
{
typeset src="$1"
typeset dst="$2"
fi
}
# Make a copy even if the destination already exists.
{
typeset src="$1"
typeset dst="$2"
fi
}
# Move a file
{
typeset src="$1"
typeset dst="$2"
fi
}
#
# Read zonecfg ipd and fs entries and save the relevant data, one entry per
# line.
# This assumes the properties from the zonecfg output, e.g.:
# inherit-pkg-dir:
# dir: /usr
# fs:
# dir: /opt
# special: /opt
# raw not specified
# type: lofs
# options: [noexec,ro,noatime]
#
# and it assumes the order of the fs properties as above. This also saves the
# inherit-pkg-dir patterns into the ipd.{cpio|pax} temporary files for
# filtering while extracting the image into the zonepath. We have to save the
# IPD patterns in the appropriate format for filtering with the different
# archivers and we don't know what format we'll get until after the flash
# archive is unpacked.
#
{
nawk -v ipdcpiof=$ipdcpiofile -v ipdpaxf=$ipdpaxfile '{
if ($1 == "dir:") {
dir=$2;
printf("%s lofs %s ro\n", dir, dir);
if (substr(dir, 1, 1) == "/") {
printf("%s\n", substr(dir, 2)) >> ipdcpiof
printf("%s/*\n", substr(dir, 2)) >> ipdcpiof
} else {
printf("%s\n", dir) >> ipdcpiof
printf("%s/*\n", dir) >> ipdcpiof
}
if (substr(dir, 1, 1) == "/") {
printf("%s ", substr(dir, 2)) >> ipdpaxf
} else {
printf("%s ", dir) >> ipdpaxf
}
}
}' >> $fstmpfile
if ($1 == "options:") {
# Remove brackets.
options=substr($2, 2, length($2) - 2);
printf("%s %s %s %s\n", dir, type, special, options);
} else if ($1 == "dir:") {
dir=$2;
} else if ($1 == "special:") {
special=$2;
} else if ($1 == "type:") {
type=$2
}
}' >> $fstmpfile
}
#
# Mount zonecfg fs entries into the zonepath.
#
mnt_fs()
{
if [ ! -s $fstmpfile ]; then
return;
fi
# Sort the fs entries so we can handle nested mounts.
sort $fstmpfile | nawk -v zonepath=$zonepath '{
if (NF == 4)
options="-o " $4;
else
options=""
# Create the mount point. Ignore errors since we might have
# a nested mount with a pre-existing mount point.
system(cmd);
zonepath "/root" $1;
if (system(cmd) != 0) {
printf("command failed: %s\n", cmd);
exit 1;
}
}' >>$LOGFILE
}
#
# Unmount zonecfg fs entries from the zonepath.
#
umnt_fs()
{
if [ ! -s $fstmpfile ]; then
return;
fi
# Reverse sort the fs entries so we can handle nested unmounts.
sort -r $fstmpfile | nawk -v zonepath=$zonepath '{
if (system(cmd) != 0) {
printf("command failed: %s\n", cmd);
}
}' >>$LOGFILE
}
#
# Determine flar compression style from identification file.
#
{
typeset ident=$1
print ${line##*=}
}
#
# Determine flar archive style from identification file.
#
{
typeset ident=$1
print ${line##*=}
}
#
# Unpack flar into current directory (which should be zoneroot). The flash
# archive is standard input. See flash_archive(4) man page.
#
# We can't use "flar split" since it will only unpack into a directory called
# "archive". We need to unpack in place in order to properly handle nested
# fs mounts within the zone root. This function does the unpacking into the
# current directory.
#
# we keep the same style as the original.
#
{
typeset result
typeset archiver_command
typeset archiver_arguments
vlog "cd $ZONEROOT && do_flar < \"$install_archive\""
# Read cookie
read -r input_line
if (( $? != 0 )); then
return 1
fi
# The cookie has format FlAsH-aRcHiVe-m.n where m and n are integers.
return 1
fi
while [ true ]
do
# We should always be at the start of a section here
read -r input_line
return 1
fi
section_name=${input_line##*=}
# If we're at the archive, we're done skipping sections.
break
fi
#
# Save identification section to a file so we can determine
# how to unpack the archive.
#
/usr/bin/rm -f identification
while read -r input_line
do
if [[ ${input_line%%=*} == \
"section_begin" ]]; then
/usr/bin/rm -f identification
return 1
fi
if [[ $input_line == \
break;
fi
echo $input_line >> identification
done
continue
fi
#
# Otherwise skip past this section; read lines until detecting
# section_end. According to flash_archive(4) we can have
# an arbitrary number of sections but the archive section
# must be last.
#
success=0
while read -r input_line
do
then
success=1
break
fi
# Fail if we miss the end of the section
/usr/bin/rm -f identification
return 1
fi
done
#
# If we get here we read to the end of the file before
# seeing the end of the section we were reading.
#
/usr/bin/rm -f identification
return 1
fi
done
# Get the information needed to unpack the archive.
# pax archiver specified
if [[ -s $ipdpaxfile ]]; then
archiver_arguments="-r -p e -c \
else
archiver_arguments="-r -p e"
fi
# cpio archived specified OR no archiver specified - use default
archiver_arguments="-icdumfE $ipdcpiofile"
else
# unknown archiver specified
return 1
fi
if [[ ! -x $archiver_command ]]; then
/usr/bin/rm -f identification
return 1
fi
# We're done with the identification file
/usr/bin/rm -f identification
# Extract archive
else
ppriv -e -s A=all,-sys_devices \
fi
result=$?
return 0
}
#
# Unpack cpio archive into zoneroot.
#
{
stage1=$1
archive=$2
cpioopts="-idmfE $ipdcpiofile"
ppriv -e -s A=all,-sys_devices cpio $cpioopts )
}
#
# Unpack pax archive into zoneroot.
#
{
archive=$1
if [[ -s $ipdpaxfile ]]; then
fi
}
#
# Unpack UFS dump into zoneroot.
#
{
archive=$1
#
# ufsrestore goes interactive if you ^C it. To prevent that,
# we make sure its stdin is not a terminal.
# Note that there is no way to filter inherit-pkg-dirs for a full
# restore so there will be warnings in the log file.
#
}
#
# Copy directory hierarchy into zoneroot.
#
{
source_dir=$1
cpioopts="-pdm"
first=1
do
if [[ $first == 1 ]]; then
first=0
else
fi
done)
do
printf "%s " "$i"
done)
findopts="-xdev ( -type d -o -type f -o -type l ) -print"
vlog "cd \"$source_dir\" && find $flist $findopts | "
}
# Setup i18n output
TEXTDOMAIN="SUNW_OST_OSCMD"
export TEXTDOMAIN
#
#
# ZONE_SUBPROC_OK
# ===============
# Installation was successful
#
# ZONE_SUBPROC_USAGE
# ==================
# Improper arguments were passed, so print a usage message before exiting
#
# ZONE_SUBPROC_NOTCOMPLETE
# ========================
# Installation did not complete, but another installation attempt can be
# made without an uninstall
#
# ZONE_SUBPROC_FATAL
# ==================
# Installation failed and an uninstall will be required before another
# install can be attempted
#