dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / brand / shared / zone / common.ksh
blob52441d814a6b93e47d12a32b5adbb80ee0281c93
2 # CDDL HEADER START
4 # The contents of this file are subject to the terms of the
5 # Common Development and Distribution License (the "License").
6 # You may not use this file except in compliance with the License.
8 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 # or http://www.opensolaris.org/os/licensing.
10 # See the License for the specific language governing permissions
11 # and limitations under the License.
13 # When distributing Covered Code, include this CDDL HEADER in each
14 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 # If applicable, add the following below this CDDL HEADER, with the
16 # fields enclosed by brackets "[]" replaced with your own identifying
17 # information: Portions Copyright [yyyy] [name of copyright owner]
19 # CDDL HEADER END
21 # Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
25 # Send the error message to the screen and to the logfile.
27 error()
29 typeset fmt="$1"
30 shift
32 printf "${MSG_PREFIX}ERROR: ${fmt}\n" "$@"
33 [[ -n $LOGFILE ]] && printf "[$(date)] ERROR: ${fmt}\n" "$@" >&2
36 fatal()
38 typeset fmt="$1"
39 shift
41 error "$fmt" "$@"
42 exit $EXIT_CODE
45 fail_fatal() {
46 typeset fmt="$1"
47 shift
49 error "$fmt" "$@"
50 exit $ZONE_SUBPROC_FATAL
54 # Send the provided printf()-style arguments to the screen and to the logfile.
56 log()
58 typeset fmt="$1"
59 shift
61 printf "${MSG_PREFIX}${fmt}\n" "$@"
62 [[ -n $LOGFILE ]] && printf "[$(date)] ${MSG_PREFIX}${fmt}\n" "$@" >&2
66 # Print provided text to the screen if the shell variable "OPT_V" is set.
67 # The text is always sent to the logfile.
69 vlog()
71 typeset fmt="$1"
72 shift
74 [[ -n $OPT_V ]] && printf "${MSG_PREFIX}${fmt}\n" "$@"
75 [[ -n $LOGFILE ]] && printf "[$(date)] ${MSG_PREFIX}${fmt}\n" "$@" >&2
79 # Validate that the directory is safe.
81 # It is possible for a malicious zone root user to modify a zone's filesystem
82 # so that modifications made to the zone's filesystem by administrators in the
83 # global zone modify the global zone's filesystem. We can prevent this by
84 # ensuring that all components of paths accessed by scripts are real (i.e.,
85 # non-symlink) directories.
87 # NOTE: The specified path should be an absolute path as would be seen from
88 # within the zone. Also, this function does not check parent directories.
89 # If, for example, you need to ensure that every component of the path
90 # '/foo/bar/baz' is a directory and not a symlink, then do the following:
92 # safe_dir /foo
93 # safe_dir /foo/bar
94 # safe_dir /foo/bar/baz
96 safe_dir()
98 typeset dir="$1"
100 if [[ -h $ZONEROOT/$dir || ! -d $ZONEROOT/$dir ]]; then
101 fatal "$e_baddir" "$dir"
105 # Like safe_dir except the dir doesn't have to exist.
106 safe_opt_dir()
108 typeset dir="$1"
110 [[ ! -e $ZONEROOT/$dir ]] && return
112 if [[ -h $ZONEROOT/$dir || ! -d $ZONEROOT/$dir ]]; then
113 fatal "$e_baddir" "$dir"
117 # Only make a copy if we haven't already done so.
118 safe_backup()
120 typeset src="$1"
121 typeset dst="$2"
123 if [[ ! -h $src && ! -h $dst && ! -d $dst && ! -f $dst ]]; then
124 /usr/bin/cp -p $src $dst || fatal "$e_badfile" "$src"
128 # Make a copy even if the destination already exists.
129 safe_copy()
131 typeset src="$1"
132 typeset dst="$2"
134 if [[ ! -h $src && ! -h $dst && ! -d $dst ]]; then
135 /usr/bin/cp -p $src $dst || fatal "$e_badfile" "$src"
139 # Move a file
140 safe_move()
142 typeset src="$1"
143 typeset dst="$2"
145 if [[ ! -h $src && ! -h $dst && ! -d $dst ]]; then
146 /usr/bin/mv $src $dst || fatal "$e_badfile" "$src"
150 safe_rm()
152 if [[ ! -h $ZONEROOT/$1 && -f $ZONEROOT/$1 ]]; then
153 rm -f "$ZONEROOT/$1"
158 # Replace the file with a wrapper pointing to the native brand code.
159 # However, we only do the replacement if the file hasn't already been
160 # replaced with our wrapper. This function expects the cwd to be the
161 # location of the file we're replacing.
163 # Some of the files we're replacing are hardlinks to isaexec so we need to 'rm'
164 # the file before we setup the wrapper while others are hardlinks to rc scripts
165 # that we need to maintain.
167 safe_replace()
169 typeset filename="$1"
170 typeset runname="$2"
171 typeset mode="$3"
172 typeset own="$4"
173 typeset rem="$5"
175 if [ -h $filename -o ! -f $filename ]; then
176 return
179 egrep -s "Solaris Brand Replacement" $filename
180 if [ $? -eq 0 ]; then
181 return
184 safe_backup $filename $filename.pre_p2v
185 if [ $rem = "remove" ]; then
186 rm -f $filename
189 cat <<-END >$filename || exit 1
190 #!/bin/sh -p
192 # Solaris Brand Replacement
194 # Attention. This file has been replaced with a new version for
195 # use in a virtualized environment. Modification of this script is not
196 # supported and all changes will be lost upon reboot. The
197 # {name}.pre_p2v version of this file is a backup copy of the
198 # original and should not be deleted.
202 echo ". $runname \"\$@\"" >>$filename || exit 1
204 chmod $mode $filename
205 chown $own $filename
208 safe_wrap()
210 typeset filename="$1"
211 typeset runname="$2"
212 typeset mode="$3"
213 typeset own="$4"
215 if [ -f $filename ]; then
216 log "$e_cannot_wrap" "$filename"
217 exit 1
220 cat <<-END >$filename || exit 1
221 #!/bin/sh
223 # Solaris Brand Wrapper
225 # Attention. This file has been created for use in a
226 # virtualized environment. Modification of this script
227 # is not supported and all changes will be lost upon reboot.
231 echo ". $runname \"\$@\"" >>$filename || exit 1
233 chmod $mode $filename
234 chown $own $filename
238 # Read zonecfg fs entries and save the relevant data, one entry per
239 # line.
240 # This assumes the properties from the zonecfg output, e.g.:
241 # fs:
242 # dir: /opt
243 # special: /opt
244 # raw not specified
245 # type: lofs
246 # options: [noexec,ro,noatime]
248 # and it assumes the order of the fs properties as above.
250 get_fs_info()
252 zonecfg -z $zonename info fs | nawk '{
253 if ($1 == "options:") {
254 # Remove brackets.
255 options=substr($2, 2, length($2) - 2);
256 printf("%s %s %s %s\n", dir, type, special, options);
257 } else if ($1 == "dir:") {
258 dir=$2;
259 } else if ($1 == "special:") {
260 special=$2;
261 } else if ($1 == "type:") {
262 type=$2
264 }' >> $fstmpfile
268 # Mount zonecfg fs entries into the zonepath.
270 mnt_fs()
272 if [ ! -s $fstmpfile ]; then
273 return;
276 # Sort the fs entries so we can handle nested mounts.
277 sort $fstmpfile | nawk -v zonepath=$zonepath '{
278 if (NF == 4)
279 options="-o " $4;
280 else
281 options=""
283 # Create the mount point. Ignore errors since we might have
284 # a nested mount with a pre-existing mount point.
285 cmd="/usr/bin/mkdir -p " zonepath "/root" $1 " >/dev/null 2>&1"
286 system(cmd);
288 cmd="/usr/sbin/mount -F " $2 " " options " " $3 " " \
289 zonepath "/root" $1;
290 if (system(cmd) != 0) {
291 printf("command failed: %s\n", cmd);
292 exit 1;
294 }' >>$LOGFILE
298 # Unmount zonecfg fs entries from the zonepath.
300 umnt_fs()
302 if [ ! -s $fstmpfile ]; then
303 return;
306 # Reverse sort the fs entries so we can handle nested unmounts.
307 sort -r $fstmpfile | nawk -v zonepath=$zonepath '{
308 cmd="/usr/sbin/umount " zonepath "/root" $1
309 if (system(cmd) != 0) {
310 printf("command failed: %s\n", cmd);
312 }' >>$LOGFILE
315 # Find the dataset mounted on the zonepath.
316 get_zonepath_ds() {
317 ZONEPATH_DS=`/usr/sbin/zfs list -H -t filesystem -o name,mountpoint | \
318 /usr/bin/nawk -v zonepath=$1 '{
319 if ($2 == zonepath)
320 print $1
323 if [ -z "$ZONEPATH_DS" ]; then
324 fail_fatal "$f_no_ds"
329 # Perform validation and cleanup in the zoneroot after unpacking the archive.
331 post_unpack()
334 # Check if the image was created with a valid libc.so.1.
336 hwcap=`moe -v -32 $ZONEROOT/lib/libc.so.1 2>&1`
337 if (( $? != 0 )); then
338 vlog "$f_hwcap_info" "$hwcap"
339 fail_fatal "$f_sanity_hwcap"
342 ( cd "$ZONEROOT" && \
343 find . \( -type b -o -type c \) -exec rm -f "{}" \; )
347 # Determine flar compression style from identification file.
349 get_compression()
351 typeset ident=$1
352 typeset line=$(grep "^files_compressed_method=" $ident)
354 print ${line##*=}
358 # Determine flar archive style from identification file.
360 get_archiver()
362 typeset ident=$1
363 typeset line=$(grep "^files_archived_method=" $ident)
365 print ${line##*=}
369 # Unpack flar into current directory (which should be zoneroot). The flash
370 # archive is standard input. See flash_archive(4) man page.
372 # We can't use "flar split" since it will only unpack into a directory called
373 # "archive". We need to unpack in place in order to properly handle nested
374 # fs mounts within the zone root. This function does the unpacking into the
375 # current directory.
377 # This code is derived from the gen_split() function in /usr/sbin/flar so
378 # we keep the same style as the original.
380 install_flar()
382 typeset result
383 typeset archiver_command
384 typeset archiver_arguments
386 vlog "cd $ZONEROOT && $stage1 "$insrc" | install_flar"
388 # Read cookie
389 read -r input_line
390 if (( $? != 0 )); then
391 log "$not_readable" "$install_media"
392 return 1
394 # The cookie has format FlAsH-aRcHiVe-m.n where m and n are integers.
395 if [[ ${input_line%%-[0-9]*.[0-9]*} != "FlAsH-aRcHiVe" ]]; then
396 log "$not_flar"
397 return 1
400 while [ true ]
402 # We should always be at the start of a section here
403 read -r input_line
404 if [[ ${input_line%%=*} != "section_begin" ]]; then
405 log "$bad_flar"
406 return 1
408 section_name=${input_line##*=}
410 # If we're at the archive, we're done skipping sections.
411 if [[ "$section_name" == "archive" ]]; then
412 break
416 # Save identification section to a file so we can determine
417 # how to unpack the archive.
419 if [[ "$section_name" == "identification" ]]; then
420 /usr/bin/rm -f identification
421 while read -r input_line
423 if [[ ${input_line%%=*} == \
424 "section_begin" ]]; then
425 /usr/bin/rm -f identification
426 log "$bad_flar"
427 return 1
430 if [[ $input_line == \
431 "section_end=$section_name" ]]; then
432 break;
434 echo $input_line >> identification
435 done
437 continue
441 # Otherwise skip past this section; read lines until detecting
442 # section_end. According to flash_archive(4) we can have
443 # an arbitrary number of sections but the archive section
444 # must be last.
446 success=0
447 while read -r input_line
449 if [[ $input_line == "section_end=$section_name" ]];
450 then
451 success=1
452 break
454 # Fail if we miss the end of the section
455 if [[ ${input_line%%=*} == "section_begin" ]]; then
456 /usr/bin/rm -f identification
457 log "$bad_flar"
458 return 1
460 done
461 if (( $success == 0 )); then
463 # If we get here we read to the end of the file before
464 # seeing the end of the section we were reading.
466 /usr/bin/rm -f identification
467 log "$bad_flar"
468 return 1
470 done
472 # Check for an archive made from a ZFS root pool.
473 egrep -s "^rootpool=" identification
474 if (( $? == 0 )); then
475 /usr/bin/rm -f identification
476 log "$bad_zfs_flar"
477 return 1
480 # Get the information needed to unpack the archive.
481 archiver=$(get_archiver identification)
482 if [[ $archiver == "pax" ]]; then
483 # pax archiver specified
484 archiver_command="/usr/bin/pax"
485 if [[ -s $fspaxfile ]]; then
486 archiver_arguments="-r -p e -c \
487 $(/usr/bin/cat $fspaxfile)"
488 else
489 archiver_arguments="-r -p e"
491 elif [[ $archiver == "cpio" || -z $archiver ]]; then
492 # cpio archived specified OR no archiver specified - use default
493 archiver_command="/usr/bin/cpio"
494 archiver_arguments="-icdumfE $fscpiofile"
495 else
496 # unknown archiver specified
497 log "$unknown_archiver" $archiver
498 return 1
501 if [[ ! -x $archiver_command ]]; then
502 /usr/bin/rm -f identification
503 log "$cmd_not_exec" $archiver_command
504 return 1
507 compression=$(get_compression identification)
509 # We're done with the identification file
510 /usr/bin/rm -f identification
512 # Extract archive
513 if [[ $compression == "compress" ]]; then
514 /usr/bin/zcat | \
515 $archiver_command $archiver_arguments 2>/dev/null
516 else
517 $archiver_command $archiver_arguments 2>/dev/null
519 result=$?
521 post_unpack
523 (( $result != 0 )) && return 1
525 return 0
529 # Get the archive base.
531 # We must unpack the archive in the right place within the zonepath so
532 # that files are installed into the various mounted filesystems that are set
533 # up in the zone's configuration. These are already mounted for us by the
534 # mntfs function.
536 # Archives can be made of either a physical host's root file system or a
537 # zone's zonepath. For a physical system, if the archive is made using an
538 # absolute path (/...) we can't use it. For a zone the admin can make the
539 # archive from a variety of locations;
541 # a) zonepath itself: This will be a single dir, probably named with the
542 # zone name, it will contain a root dir and under the root we'll see all
543 # the top level dirs; etc, var, usr... We must be above the ZONEPATH
544 # when we unpack the archive but this will only work if the the archive's
545 # top-level dir name matches the ZONEPATH base-level dir name. If not,
546 # this is an error.
548 # b) inside the zonepath: We'll see root and it will contain all the top
549 # level dirs; etc, var, usr.... We must be in the ZONEPATH when we unpack
550 # the archive.
552 # c) inside the zonepath root: We'll see all the top level dirs, ./etc,
553 # ./var, ./usr.... This is also the case we see when we get an archive
554 # of a physical sytem. We must be in ZONEROOT when we unpack the archive.
556 # Note that there can be a directory named "root" under the ZONEPATH/root
557 # directory.
559 # This function handles the above possibilities so that we reject absolute
560 # path archives and figure out where in the file system we need to be to
561 # properly unpack the archive into the zone. It sets the ARCHIVE_BASE
562 # variable to the location where the achive should be unpacked.
564 get_archive_base()
566 stage1=$1
567 archive=$2
568 stage2=$3
570 vlog "$m_analyse_archive"
572 base=`$stage1 $archive | $stage2 2>/dev/null | nawk -F/ '{
573 # Check for an absolute path archive
574 if (substr($0, 1, 1) == "/")
575 exit 1
577 if ($1 != ".")
578 dirs[$1] = 1
579 else
580 dirs[$2] = 1
582 END {
583 for (d in dirs) {
584 cnt++
585 if (d == "bin") sawbin = 1
586 if (d == "etc") sawetc = 1
587 if (d == "root") sawroot = 1
588 if (d == "var") sawvar = 1
591 if (cnt == 1) {
592 # If only one top-level dir named root, we are in the
593 # zonepath, otherwise this must be an archive *of*
594 # the zonepath so print the top-level dir name.
595 if (sawroot)
596 print "*zonepath*"
597 else
598 for (d in dirs) print d
599 } else {
600 # We are either in the zonepath or in the zonepath/root
601 # (or at the top level of a full system archive which
602 # looks like the zonepath/root case). Figure out which
603 # one.
604 if (sawroot && !sawbin && !sawetc && !sawvar)
605 print "*zonepath*"
606 else
607 print "*zoneroot*"
611 if (( $? != 0 )); then
612 umnt_fs
613 fatal "$e_absolute_archive"
616 if [[ "$base" == "*zoneroot*" ]]; then
617 ARCHIVE_BASE=$ZONEROOT
618 elif [[ "$base" == "*zonepath*" ]]; then
619 ARCHIVE_BASE=$ZONEPATH
620 else
621 # We need to be in the dir above the ZONEPATH but we need to
622 # validate that $base matches the final component of ZONEPATH.
623 bname=`basename $ZONEPATH`
625 if [[ "$bname" != "$base" ]]; then
626 umnt_fs
627 fatal "$e_mismatch_archive" "$base" "$bname"
629 ARCHIVE_BASE=`dirname $ZONEPATH`
634 # Unpack cpio archive into zoneroot.
636 install_cpio()
638 stage1=$1
639 archive=$2
641 get_archive_base "$stage1" "$archive" "cpio -it"
643 cpioopts="-idmfE $fscpiofile"
645 vlog "cd \"$ARCHIVE_BASE\" && $stage1 \"$archive\" | cpio $cpioopts"
647 # Ignore errors from cpio since we expect some errors depending on
648 # how the archive was made.
649 ( cd "$ARCHIVE_BASE" && $stage1 "$archive" | cpio $cpioopts )
651 post_unpack
653 return 0
657 # Unpack pax archive into zoneroot.
659 install_pax()
661 archive=$1
663 get_archive_base "cat" "$archive" "pax"
665 if [[ -s $fspaxfile ]]; then
666 filtopt="-c $(/usr/bin/cat $fspaxfile)"
669 vlog "cd \"$ARCHIVE_BASE\" && pax -r -f \"$archive\" $filtopt"
671 # Ignore errors from pax since we expect some errors depending on
672 # how the archive was made.
673 ( cd "$ARCHIVE_BASE" && pax -r -f "$archive" $filtopt )
675 post_unpack
677 return 0
681 # Unpack UFS dump into zoneroot.
683 install_ufsdump()
685 archive=$1
687 vlog "cd \"$ZONEROOT\" && ufsrestore rf \"$archive\""
690 # ufsrestore goes interactive if you ^C it. To prevent that,
691 # we make sure its stdin is not a terminal.
693 ( cd "$ZONEROOT" && ufsrestore rf "$archive" < /dev/null )
694 result=$?
696 post_unpack
698 return $result
702 # Copy directory hierarchy into zoneroot.
704 install_dir()
706 source_dir=$1
708 cpioopts="-pdm"
710 first=1
711 filt=$(for i in $(cat $fspaxfile)
713 echo $i | egrep -s "/" && continue
714 if [[ $first == 1 ]]; then
715 printf "^%s" $i
716 first=0
717 else
718 printf "|^%s" $i
720 done)
722 list=$(cd "$source_dir" && ls -d * | egrep -v "$filt")
723 flist=$(for i in $list
725 printf "%s " "$i"
726 done)
727 findopts="-xdev ( -type d -o -type f -o -type l ) -print"
729 vlog "cd \"$source_dir\" && find $flist $findopts | "
730 vlog "cpio $cpioopts \"$ZONEROOT\""
732 # Ignore errors from cpio since we expect some errors depending on
733 # how the archive was made.
734 ( cd "$source_dir" && find $flist $findopts | \
735 cpio $cpioopts "$ZONEROOT" )
737 post_unpack
739 return 0
743 # This is a common function for laying down a zone image from a variety of
744 # different sources. This can be used to either install a fresh zone or as
745 # part of zone migration during attach.
747 # The first argument specifies the type of image: archive, directory or stdin.
748 # The second argument specifies the image itself. In the case of stdin, the
749 # second argument specifies the format of the stream (cpio, flar, etc.).
750 # Any validation or post-processing on the image is done elsewhere.
752 # This function calls a 'sanity_check' function which must be provided by
753 # the script which includes this code.
755 install_image()
757 intype=$1
758 insrc=$2
760 if [[ -z "$intype" || -z "$insrc" ]]; then
761 return 1
764 filetype="unknown"
765 filetypename="unknown"
766 stage1="cat"
768 if [[ "$intype" == "directory" ]]; then
769 if [[ "$insrc" == "-" ]]; then
770 # Indicates that the existing zonepath is prepopulated.
771 filetype="existing"
772 filetypename="existing"
773 else
774 if [[ "$(echo $insrc | cut -c 1)" != "/" ]]; then
775 fatal "$e_path_abs" "$insrc"
778 if [[ ! -e "$insrc" ]]; then
779 log "$e_not_found" "$insrc"
780 fatal "$e_install_abort"
783 if [[ ! -r "$insrc" ]]; then
784 log "$e_not_readable" "$insrc"
785 fatal "$e_install_abort"
788 if [[ ! -d "$insrc" ]]; then
789 log "$e_not_dir"
790 fatal "$e_install_abort"
793 sanity_check $insrc
795 filetype="directory"
796 filetypename="directory"
799 else
800 # Common code for both archive and stdin stream.
802 if [[ "$intype" == "archive" ]]; then
803 if [[ ! -f "$insrc" ]]; then
804 log "$e_unknown_archive"
805 fatal "$e_install_abort"
807 ftype="$(LC_ALL=C file $insrc | cut -d: -f 2)"
808 else
809 # For intype == stdin, the insrc parameter specifies
810 # the stream format coming on stdin.
811 ftype="$insrc"
812 insrc="-"
815 # Setup vars for the archive type we have.
816 case "$ftype" in
817 *cpio*) filetype="cpio"
818 filetypename="cpio archive"
820 *bzip2*) filetype="bzip2"
821 filetypename="bzipped cpio archive"
823 *gzip*) filetype="gzip"
824 filetypename="gzipped cpio archive"
826 *ufsdump*) filetype="ufsdump"
827 filetypename="ufsdump archive"
829 "flar")
830 filetype="flar"
831 filetypename="flash archive"
833 "flash")
834 filetype="flar"
835 filetypename="flash archive"
837 *Flash\ Archive*)
838 filetype="flar"
839 filetypename="flash archive"
841 "tar")
842 filetype="tar"
843 filetypename="tar archive"
845 *USTAR\ tar\ archive)
846 filetype="tar"
847 filetypename="tar archive"
849 "pax")
850 filetype="xustar"
851 filetypename="pax (xustar) archive"
853 *USTAR\ tar\ archive\ extended\ format*)
854 filetype="xustar"
855 filetypename="pax (xustar) archive"
857 "zfs")
858 filetype="zfs"
859 filetypename="ZFS send stream"
861 *ZFS\ snapshot\ stream*)
862 filetype="zfs"
863 filetypename="ZFS send stream"
865 *) log "$e_unknown_archive"
866 fatal "$e_install_abort"
868 esac
871 vlog "$filetypename"
873 # Check for a non-empty root if no '-d -' option.
874 if [[ "$filetype" != "existing" ]]; then
875 cnt=$(ls $ZONEROOT | wc -l)
876 if (( $cnt != 0 )); then
877 fatal "$e_root_full" "$ZONEROOT"
881 fstmpfile=$(/usr/bin/mktemp -t -p /var/tmp)
882 if [[ -z "$fstmpfile" ]]; then
883 fatal "$e_tmpfile"
886 # Make sure we always have the files holding the directories to filter
887 # out when extracting from a CPIO or PAX archive. We'll add the fs
888 # entries to these files in get_fs_info()
889 fscpiofile=$(/usr/bin/mktemp -t -p /var/tmp fs.cpio.XXXXXX)
890 if [[ -z "$fscpiofile" ]]; then
891 rm -f $fstmpfile
892 fatal "$e_tmpfile"
895 # Filter out these directories.
896 echo 'dev/*' >>$fscpiofile
897 echo 'devices/*' >>$fscpiofile
898 echo 'devices' >>$fscpiofile
899 echo 'proc/*' >>$fscpiofile
900 echo 'tmp/*' >>$fscpiofile
901 echo 'var/run/*' >>$fscpiofile
902 echo 'system/contract/*' >>$fscpiofile
903 echo 'system/object/*' >>$fscpiofile
905 fspaxfile=$(/usr/bin/mktemp -t -p /var/tmp fs.pax.XXXXXX)
906 if [[ -z "$fspaxfile" ]]; then
907 rm -f $fstmpfile $fscpiofile
908 fatal "$e_tmpfile"
911 printf "%s " \
912 "dev devices proc tmp var/run system/contract system/object" \
913 >>$fspaxfile
915 # Set up any fs mounts so the archive will install into the correct
916 # locations.
917 get_fs_info
918 mnt_fs
919 if (( $? != 0 )); then
920 umnt_fs >/dev/null 2>&1
921 rm -f $fstmpfile $fscpiofile $fspaxfile
922 fatal "$mount_failed"
925 if [[ "$filetype" == "existing" ]]; then
926 log "$no_installing"
927 else
928 log "$installing"
932 # Install the image into the zonepath.
934 unpack_result=0
935 stage1="cat"
936 if [[ "$filetype" == "gzip" ]]; then
937 stage1="gzcat"
938 filetype="cpio"
939 elif [[ "$filetype" == "bzip2" ]]; then
940 stage1="bzcat"
941 filetype="cpio"
944 if [[ "$filetype" == "cpio" ]]; then
945 install_cpio "$stage1" "$insrc"
946 unpack_result=$?
948 elif [[ "$filetype" == "flar" ]]; then
949 ( cd "$ZONEROOT" && $stage1 $insrc | install_flar )
950 unpack_result=$?
952 elif [[ "$filetype" == "xustar" ]]; then
953 install_pax "$insrc"
954 unpack_result=$?
956 elif [[ "$filetype" = "tar" ]]; then
957 vlog "cd \"$ZONEROOT\" && tar -xf \"$insrc\""
958 # Ignore errors from tar since we expect some errors depending
959 # on how the archive was made.
960 ( cd "$ZONEROOT" && tar -xf "$insrc" )
961 unpack_result=0
962 post_unpack
964 elif [[ "$filetype" == "ufsdump" ]]; then
965 install_ufsdump "$insrc"
966 unpack_result=$?
968 elif [[ "$filetype" == "directory" ]]; then
969 install_dir "$insrc"
970 unpack_result=$?
972 elif [[ "$filetype" == "zfs" ]]; then
974 # Given a 'zfs send' stream file, receive the snapshot into
975 # the zone's dataset. We're getting the original system's
976 # zonepath dataset. Destroy the existing dataset created
977 # above since this recreates it.
979 if [[ -z "$DATASET" ]]; then
980 fatal "$f_nodataset"
982 /usr/sbin/zfs destroy "$DATASET"
983 if (( $? != 0 )); then
984 log "$f_zfsdestroy" "$DATASET"
987 vlog "$stage1 $insrc | zfs receive -F $DATASET"
988 ( $stage1 $insrc | /usr/sbin/zfs receive -F $DATASET )
989 unpack_result=$?
992 # Clean up any fs mounts used during unpacking.
993 umnt_fs
994 rm -f $fstmpfile $fscpiofile $fspaxfile
996 chmod 700 $zonepath
998 (( $unpack_result != 0 )) && fatal "$f_unpack_failed"
1000 # Verify this is a valid image.
1001 sanity_check $ZONEROOT
1003 return 0
1006 # Setup i18n output
1007 TEXTDOMAIN="SUNW_OST_OSCMD"
1008 export TEXTDOMAIN
1010 e_cannot_wrap=$(gettext "%s: error: wrapper file already exists")
1011 e_baddir=$(gettext "Invalid '%s' directory within the zone")
1012 e_badfile=$(gettext "Invalid '%s' file within the zone")
1013 e_path_abs=$(gettext "Pathname specified to -a '%s' must be absolute.")
1014 e_not_found=$(gettext "%s: error: file or directory not found.")
1015 e_install_abort=$(gettext "Installation aborted.")
1016 e_not_readable=$(gettext "Cannot read directory '%s'")
1017 e_not_dir=$(gettext "Error: must be a directory")
1018 e_unknown_archive=$(gettext "Error: Unknown archive format. Must be a flash archive, a cpio archive (can also be gzipped or bzipped), a pax XUSTAR archive, or a level 0 ufsdump archive.")
1019 e_absolute_archive=$(gettext "Error: archive contains absolute paths instead of relative paths.")
1020 e_mismatch_archive=$(gettext "Error: the archive top-level directory (%s) does not match the zonepath (%s).")
1021 e_tmpfile=$(gettext "Unable to create temporary file")
1022 e_root_full=$(gettext "Zonepath root %s exists and contains data; remove or move aside prior to install.")
1023 f_mkdir=$(gettext "Unable to create directory %s.")
1024 f_chmod=$(gettext "Unable to chmod directory %s.")
1025 f_chown=$(gettext "Unable to chown directory %s.")
1026 f_hwcap_info=$(gettext "HWCAP: %s\n")
1027 f_sanity_hwcap=$(gettext \
1028 "The image was created with an incompatible libc.so.1 hwcap lofs mount.\n"\
1029 " The zone will not boot on this platform. See the zone's\n"\
1030 " documentation for the recommended way to create the archive.")
1032 m_analyse_archive=$(gettext "Analysing the archive")
1034 not_readable=$(gettext "Cannot read file '%s'")
1035 not_flar=$(gettext "Input is not a flash archive")
1036 bad_flar=$(gettext "Flash archive is a corrupt")
1037 bad_zfs_flar=$(gettext "Flash archive contains a ZFS send stream.\n\tRecreate the flar using the -L option with cpio or pax.")
1038 f_unpack_failed=$(gettext "Unpacking the archive failed")
1039 unknown_archiver=$(gettext "Archiver %s is not supported")
1040 cmd_not_exec=$(gettext "Required command '%s' not executable!")
1043 # Exit values used by the script, as #defined in <sys/zone.h>
1045 # ZONE_SUBPROC_OK
1046 # ===============
1047 # Installation was successful
1049 # ZONE_SUBPROC_USAGE
1050 # ==================
1051 # Improper arguments were passed, so print a usage message before exiting
1053 # ZONE_SUBPROC_NOTCOMPLETE
1054 # ========================
1055 # Installation did not complete, but another installation attempt can be
1056 # made without an uninstall
1058 # ZONE_SUBPROC_FATAL
1059 # ==================
1060 # Installation failed and an uninstall will be required before another
1061 # install can be attempted
1063 ZONE_SUBPROC_OK=0
1064 ZONE_SUBPROC_USAGE=253
1065 ZONE_SUBPROC_NOTCOMPLETE=254
1066 ZONE_SUBPROC_FATAL=255