Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

executable file 343 lines (289 sloc) 9.693 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
#!/bin/bash
#
# 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
# or http://www.opensolaris.org/os/licensing.
# 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.
# Copyright (c) 2012 Joyent, Inc. All rights reserved.
# Use is subject to license terms.

ROOT=`pwd`
CUSTOMIZE_SCRIPT=${ROOT}/tools/customize
SMF_IMPORTER=${ROOT}/tools/smf_import
SMF_SEEDS=${ROOT}/tools/build_seeds
BUILDER=${ROOT}/tools/builder/builder
SCANNER=${ROOT}/tools/scanner
PWGEN=${ROOT}/tools/pwgen
GET_STATUS=${ROOT}/tools/get_status
CHECK_DIR="${ROOT}/tools/checks"

BUILDSTAMP=$(cat ${ROOT}/proto/buildstamp)
if [[ -z ${BUILDSTAMP} ]]; then
    echo "Failed to get buildstamp from proto directory."
    exit 1
fi
LOGFILE="${ROOT}/log/build_live.${BUILDSTAMP}.log"
ERROR=0
PLATFORM=i86pc
ISA=i386
ARCH64=amd64
RSIZE=272000
USIZE=390000
KERNEL_64=platform/$PLATFORM/kernel/$ARCH64/unix
if [[ -f ${ROOT}/configure-buildver ]]; then
    BUILDVER=$(head -n1 ${ROOT}/configure-buildver)
fi
if [[ -n "${BUILDVER}" ]]; then
    PREFIX=platform-${BUILDVER}-${BUILDSTAMP}
else
    PREFIX=platform-${BUILDSTAMP}
fi
BOOT_ARCHIVE_64=${PREFIX}/$PLATFORM/$ARCH64/boot_archive
OUT_KERNEL_64=${PREFIX}/$PLATFORM/kernel/$ARCH64/unix

if [ "$(id -u)" != "0" ]; then
  echo "You must have euid 0 to run this tool."
  exit 1
fi

# Send a copy of stdout and stderr to LOGFILE
exec > >(tee ${LOGFILE}) 2>&1

export PATH=/usr/sbin:/usr/bin:/sbin:/opt/local/bin

usage() {
    echo "This tool is for building a live boot_archive amd64."
    echo ""
    echo "Usage: ${0##*/}: <manifest> <outdir> <dir> [<dir> <dir> ...]"
    exit 1
}

MANIFEST=$1
shift
OUT_DIR=$1
shift
IN_DIRS=$*

# We need the manifest file
if [ -z "${MANIFEST}" ] || [ ! -f "${MANIFEST}" ]; then
    echo "FATAL: <manifest> file required"
    usage
else
    # translate to abs path
    NEW_MANIFEST=$(cd $(dirname ${MANIFEST}) && pwd)/$(basename ${MANIFEST})
    MANIFEST=${NEW_MANIFEST}
fi

if [ -z "${OUT_DIR}" ]; then
    echo "FATAL: <outdir> required."
    usage
fi

if [ -z "${IN_DIRS}" ]; then
    echo "FATAL: at least one <dir> required."
    usage
fi

if [ ! -d "${OUT_DIR}" ]; then
    mkdir -p ${OUT_DIR}
    if [ ! -d "${OUT_DIR}" ]; then
        echo "FATAL: Tried to create OUT_DIR: ${OUT_DIR} but failed."
        exit 1
    fi
fi

num=0
echo "=== Options/ ==="
echo "BUILDSTAMP. ${BUILDSTAMP}"
echo "MANIFEST .. ${MANIFEST}"
echo "OUTPUT .... ${OUT_DIR}"
for dir in ${IN_DIRS}; do
  num=$((${num} + 1))
  echo "DIR[$(printf '%02d' ${num})] ... ${dir}"
done
echo "=== /Options ==="


function cleanup
{
    umount -f "${rdmnt}/usr" 2>/dev/null
    umount -f "${rdmnt}" 2>/dev/null
    lofiadm -d "${rdfile}" 2>/dev/null
    lofiadm -d "${usrfile}" 2>/dev/null
    [ -n "${rddir}" ] && rm -fr "${rddir}" 2> /dev/null
}
function gen_etcrelease
{
  rdmnt=$1
  cat <<EOF > ${rdmnt}/etc/release
SmartOS ${BUILDSTAMP} x86_64
Copyright 2010 Sun Microsystems, Inc. All Rights Reserved.
Copyright 2010-2012 Joyent, Inc. All Rights Reserved.
Use is subject to license terms.

Built with the following components:

EOF
  (cd ${ROOT} && ${GET_STATUS}) >> ${rdmnt}/etc/release
}
function create_ufs
{
    archive=$1
    lofidev=$2
    lofiusrdev=$3
    usrfile=$4

    newfs -o space -m 0 -i 50000 -b 4096 ${lofidev} < /dev/null 2> /dev/null
    mkdir "${rdmnt}"
    mount -F ufs -o nologging ${lofidev} "${rdmnt}"

    newfs -o space -m 0 -i 14000 -b 4096 ${lofiusrdev} < /dev/null 2> /dev/null
    mkdir "${rdmnt}/usr"
    mount -F ufs -o nologging ${lofiusrdev} "${rdmnt}/usr"

    # do the actual copy
    ${BUILDER} ${MANIFEST} ${rdmnt} ${IN_DIRS}
    if [ $? -ne 0 ]; then
        echo "ERROR: copy onto the image failed"
        echo "(may need to increase the size or manifest file not found)"
        ERROR=1
        echo "Failures:"
            grep "FAILED$" ${LOGFILE}
        return
    fi
    ${CUSTOMIZE_SCRIPT} ${rdmnt} ${BUILDSTAMP}
    ROOT=${ROOT} ${SMF_IMPORTER} ${rdmnt}
    echo ${SMF_SEEDS} ${rdmnt}
    ${SMF_SEEDS} ${rdmnt}
    if [ $? -ne 0 ]; then
        echo "ERROR: setting up live image SMF repoistory failed"
        ERROR=1
        return
    fi

    echo "Setting root password..."
    create_password "${rdmnt}"

    echo "Adding git manifest..."
    (cd ${ROOT} && ${GET_STATUS}) > ${archive}.gitstatus

    echo "Writing md5sum manifest..."
    (cd ${rdmnt} && find . -type f | sort | xargs md5sum) > ${rdmnt}/var/log/manifest
    #INCLUDE_MD5SUM=1 ${SCANNER} ${rdmnt} . | tee ${rdmnt}/var/log/manifest >/dev/null
    cp ${rdmnt}/var/log/manifest ${archive}.manifest

    # make sure the kernel we copy over matches our boot archive by copying from there.
    mkdir -p `dirname ${OUT_DIR}/${OUT_KERNEL_64}`
    cp ${rdmnt}/${KERNEL_64} ${OUT_DIR}/${OUT_KERNEL_64}

    # Set up the /usr files we need before /usr is mounted
    echo "tmpdir $rddir/usr"
    umount -f "$rdmnt/usr"
    mkdir $rddir/usr
    mount -F ufs -o nologging ${lofiusrdev} "${rddir}/usr"
    (cd ${rddir}/usr && tar cbf 512 - \
        bin/[ bin/cat bin/head bin/i86/ksh93 bin/ls kernel/sched/amd64/FSS \
        lib/fm/libfmevent.so lib/fm/libfmevent.so.1 lib/fm/libtopo.so \
        lib/fm/libtopo.so.1 lib/libast.so lib/libast.so.1 lib/libcmd.so \
        lib/libcmd.so.1 lib/libdll.so lib/libdll.so.1 lib/libexacct.so \
        lib/libexacct.so.1 lib/libfstyp.so lib/libfstyp.so.1 lib/libidmap.so \
        lib/libidmap.so.1 lib/libipmi.so lib/libipmi.so.1 lib/libpkcs11.so \
        lib/libpkcs11.so.1 lib/libpool.so lib/libpool.so.1 lib/libproject.so \
        lib/libproject.so.1 lib/libshell.so lib/libshell.so.1 lib/libsmbios.so \
        lib/libsmbios.so.1 lib/libsum.so lib/libsum.so.1 sbin/lofiadm) | \
        (cd ${rdmnt}/usr; tar xbf 512 -)

    umount -f "${rddir}/usr"

    (cd $rdmnt/usr/bin && ln -s "i86/ksh93" $rdmnt/usr/bin/sh)

    # Now lofi compress our /usr filesystem
    echo "lofi compressing usr file system"
    lofiadm -d "$lofiusrdev" 2>/dev/null
    lofiadm -C $usrfile
    mv $usrfile $rdmnt/usr.lgz

    gen_etcrelease $rdmnt

    umount -f "$rdmnt"
    rmdir "$rdmnt"

    mv "${rdfile}" "${archive}-new"
    if [ $? -ne 0 ] ; then
        rm -f "${archive}-new"
        rm -f "${archive}.manifest"
        rm -f "${archive}.gitstatus"
    fi
}

function create_archive
{
    archive=$1
    lofidev=$2
    lofiusrdev=$3
    usrfile=$4

    echo "creating ${archive} (${lofidev}) (${lofiusrdev})"
    mkdir -p `dirname ${archive}`

    create_ufs "$archive" "$lofidev" "$lofiusrdev" "$usrfile"
    [ $ERROR -eq 1 ] && return


    # sanity check the archive before moving it into place
    #
    ARCHIVE_SIZE=`ls -l "${archive}-new" 2> /dev/null | nawk '{ print $5 }'`
    ls "${archive}-new" >/dev/null 2>&1

    if [ $? -eq 1 -o "$ARCHIVE_SIZE" -lt 10000 ]
    then
        #
        # Two of these functions may be run in parallel. We
        # need to allow the other to clean up, so we can't
        # exit immediately. Instead, we set a flag.
        #
        echo "update of $archive failed"
        ERROR=1
    else
        lockfs -f "/$OUT_DIR" 2>/dev/null
        mv "${archive}-new" "$archive"
        lockfs -f "/$OUT_DIR" 2>/dev/null
    fi
}

function create_password
{
    rdmnt=$1

    password="$(${PWGEN} -B -c -n 16 1)"
    if [[ -z ${password} ]]; then
        fatal_error "Unable to generate password."
    fi

    # actually write password into ${rdmnt}/etc/shadow
    enc_password="$(${ROOT}/tools/cryptpass "${password}")"
    if [[ -z ${enc_password} ]]; then
        fatal_error "Unable to generate encrypted password."
    fi
    gsed -i -e "s|^root:[^\:]*:|root:${enc_password}:|" ${rdmnt}/etc/shadow

    # write in plaintext to platform so we can find it later if we need it
    echo "${password}" > ${OUT_DIR}/${PREFIX}/root.password
}

function fatal_error
{
    print -u2 $*
    exit 1
}

function run_checks
{
    ${CHECK_DIR}/mancheck -f ${MANIFEST} -s
    [[ $? -eq 0 ]] || fatal_error "you are not mancheck -s clean"
}

# Create tmp dir to do the work in
rddir="/tmp/create_ramdisk.$$.tmp"
rm -rf "$rddir"
mkdir "$rddir" || fatal_error "Could not create temporary directory $rddir"

# Clean up upon exit.
trap 'cleanup' EXIT

echo "Running pre-flight checks"
run_checks

rdfile="$rddir/rd.file.32"
rdmnt="$rddir/rd.mnt.32"
usrfile="$rddir/usr.file"

echo "Creating ${RSIZE}k ramdisk image: ${rdfile}"
mkfile ${RSIZE}k "${rdfile}"
chown ${USER} ${rdfile}
echo "Creating ${USIZE}k usr image: ${usrfile}"
mkfile ${USIZE}k "${usrfile}"
chown ${USER} ${usrfile}

lofidev32=`lofiadm -a "${rdfile}"`
lofidevusr=`lofiadm -a "${usrfile}"`

create_archive "${OUT_DIR}/${BOOT_ARCHIVE_64}" $lofidev32 $lofidevusr $usrfile

lofiadm -d "$lofidev32" 2>/dev/null

if [ $ERROR -eq 0 ]; then
    chmod 0644 ${OUT_DIR}/${BOOT_ARCHIVE_64}
    echo "${BUILDSTAMP}" > ${OUT_DIR}/buildstamp
    (cd ${OUT_DIR} && gtar -zcf ${PREFIX}.tgz ${PREFIX})
fi

if [ $ERROR -eq 1 ]; then
    cleanup
    exit 1
fi

exit 0
Something went wrong with that request. Please try again.