src/brand/attach
changeset 2390 f1c659f5c28e
parent 2235 1f446820dcb0
child 2391 90c532b69592
equal deleted inserted replaced
2235:1f446820dcb0 2390:f1c659f5c28e
    53 f_no_pref_publisher=$(gettext "Unable to get preferred publisher information for zone '%s'.")
    53 f_no_pref_publisher=$(gettext "Unable to get preferred publisher information for zone '%s'.")
    54 f_nosuch_key=$(gettext "Failed to find key %s for global zone publisher")
    54 f_nosuch_key=$(gettext "Failed to find key %s for global zone publisher")
    55 f_nosuch_cert=$(gettext "Failed to find cert %s for global zone publisher")
    55 f_nosuch_cert=$(gettext "Failed to find cert %s for global zone publisher")
    56 f_ds_config=$(gettext  "Failed to configure dataset %s: could not set %s.")
    56 f_ds_config=$(gettext  "Failed to configure dataset %s: could not set %s.")
    57 f_no_active_ds_mounted=$(gettext  "Failed to locate any dataset mounted at %s.  Attach requires a mounted dataset.")
    57 f_no_active_ds_mounted=$(gettext  "Failed to locate any dataset mounted at %s.  Attach requires a mounted dataset.")
    58 e_dataset_disappeared=$(gettext "Dataset %s was unexpectedly unmounted")
       
    59 e_dataset_not_mounted=$(gettext "Dataset %s exists in boot environment but not mounted")
       
    60 e_dataset_not_in_be=$(gettext "Dataset %s mountpoint %s is not under zone root %s")
       
    61 
    58 
    62 # Clean up on interrupt
    59 # Clean up on interrupt
    63 trap_cleanup() {
    60 trap_cleanup() {
    64 	typeset msg=$(gettext "Installation cancelled due to interrupt.")
    61 	typeset msg=$(gettext "Installation cancelled due to interrupt.")
    65 
    62 
    83 			/usr/lib/brand/ipkg/uninstall $ZONENAME $ZONEPATH -F
    80 			/usr/lib/brand/ipkg/uninstall $ZONENAME $ZONEPATH -F
    84 		else
    81 		else
    85 			# Restore the zone properties for the pre-existing
    82 			# Restore the zone properties for the pre-existing
    86 			# dataset.
    83 			# dataset.
    87 			if [[ -n "$ACTIVE_DS" ]]; then
    84 			if [[ -n "$ACTIVE_DS" ]]; then
    88 				zfs set canmount=off $ACTIVE_DS
       
    89 				(( $? != 0 )) && error "$f_ds_config" \
       
    90 				    "$ACTIVE_DS" "canmount=on"
       
    91 				zfs set zoned=off $ACTIVE_DS
    85 				zfs set zoned=off $ACTIVE_DS
    92 				(( $? != 0 )) && error "$f_ds_config" \
    86 				(( $? != 0 )) && error "$f_ds_config" \
    93 				    "$ACTIVE_DS" "zoned=off"
    87 				    "$ACTIVE_DS" "zoned=off"
       
    88 				zfs set canmount=on $ACTIVE_DS
       
    89 				(( $? != 0 )) && error "$f_ds_config" \
       
    90 				    "$ACTIVE_DS" "canmount=on"
    94 				zfs set mountpoint=$ZONEROOT $ACTIVE_DS
    91 				zfs set mountpoint=$ZONEROOT $ACTIVE_DS
    95 				(( $? != 0 )) && error "$f_ds_config" \
    92 				(( $? != 0 )) && error "$f_ds_config" \
    96 				    "$ACTIVE_DS" "mountpoint=$ZONEROOT"
    93 				    "$ACTIVE_DS" "mountpoint=$ZONEROOT"
    97 				zfs set canmount=on $ACTIVE_DS
       
    98 				(( $? != 0 )) && error "$f_ds_config" \
       
    99 				    "$ACTIVE_DS" "canmount=on"
       
   100 				zfs mount "$ACTIVE_DS" || \
       
   101 				    error "$e_mount1_failed" "$ACTIVE_DS"
       
   102 			fi
    94 			fi
   103 		fi
    95 		fi
   104 		log "$m_failed"
    96 		log "$m_failed"
   105 	fi
    97 	fi
   106 
    98 
   111 install_media="-"
   103 install_media="-"
   112 
   104 
   113 trap trap_cleanup INT
   105 trap trap_cleanup INT
   114 trap trap_exit EXIT
   106 trap trap_exit EXIT
   115 
   107 
       
   108 #set -o xtrace
       
   109 
   116 PKG="/usr/bin/pkg"
   110 PKG="/usr/bin/pkg"
   117 KEYDIR=/var/pkg/ssl
   111 KEYDIR=/var/pkg/ssl
   118 
   112 
   119 # If we weren't passed at least two arguments, exit now.
   113 # If we weren't passed at least two arguments, exit now.
   120 (( $# < 2 )) && exit $ZONE_SUBPROC_USAGE
   114 (( $# < 2 )) && exit $ZONE_SUBPROC_USAGE
   121 
   115 
   122 zone=
   116 ZONENAME="$1"
   123 init_zone zone "$1" "$2"
   117 ZONEPATH="$2"
   124 # Set ZONEPATH, etc.
   118 # XXX shared/common script currently uses lower case zonename & zonepath
   125 eval $(bind_legacy_zone_globals zone)
   119 zonename="$ZONENAME"
       
   120 zonepath="$ZONEPATH"
   126 
   121 
   127 shift; shift	# remove ZONENAME and ZONEPATH from arguments array
   122 shift; shift	# remove ZONENAME and ZONEPATH from arguments array
   128 
   123 
       
   124 ZONEROOT="$ZONEPATH/root"
   129 logdir="$ZONEROOT/var/log"
   125 logdir="$ZONEROOT/var/log"
   130 
   126 
   131 #
   127 #
   132 # Resetting GZ_IMAGE to something besides slash allows for simplified
   128 # Resetting GZ_IMAGE to something besides slash allows for simplified
   133 # debugging of various global zone image configurations-- simply make
   129 # debugging of various global zone image configurations-- simply make
   192 			typeset -C publisher_$publisher_count
   188 			typeset -C publisher_$publisher_count
   193 			typeset -n publisher=publisher_$publisher_count
   189 			typeset -n publisher=publisher_$publisher_count
   194 			typeset publisher.sticky=""
   190 			typeset publisher.sticky=""
   195 			typeset publisher.preferred=""
   191 			typeset publisher.preferred=""
   196 			typeset publisher.enabled=""
   192 			typeset publisher.enabled=""
   197 			typeset -a publisher.origins
   193 			typeset -a publisher.origins=""
   198 			typeset -a publisher.mirrors
   194 			typeset -a publisher.mirrors=""
   199 			typeset publisher.name=$name
   195 			typeset publisher.name=$name
   200 			typeset publisher.keyfile=""
   196 			typeset publisher.keyfile=""
   201 			typeset publisher.certfile=""
   197 			typeset publisher.certfile=""
   202 
   198 
   203 			get_publisher_attrs ${publisher.name} origin | \
   199 			get_publisher_attrs ${publisher.name} origin | \
   299 
   295 
   300 	if [[ ${publisher.preferred} == "true" ]]; then
   296 	if [[ ${publisher.preferred} == "true" ]]; then
   301 		args="$args -P"
   297 		args="$args -P"
   302 	fi
   298 	fi
   303 
   299 
   304 	for origin in "${publisher.origins[@]}"; do
   300 	for origin in ${publisher.origins[*]}; do
   305 		args="$args -g $origin"
   301 		args="$args -g $origin"
   306 	done
   302 	done
   307 
   303 
   308 	for mirror in "${publisher.mirrors[@]}"; do
   304 	for mirror in ${publisher.mirrors[*]}; do
   309 		args="$args -m $mirror"
   305 		args="$args -m $mirror"
   310 	done
   306 	done
   311 
   307 
   312 	if [[ ${publisher.sticky} == "true" ]]; then
   308 	if [[ ${publisher.sticky} == "true" ]]; then
   313 		args="$args --sticky"
   309 		args="$args --sticky"
   372 	# everything will be ok.
   368 	# everything will be ok.
   373 	EXIT_CODE=$ZONE_SUBPROC_OK
   369 	EXIT_CODE=$ZONE_SUBPROC_OK
   374 	exit $ZONE_SUBPROC_OK
   370 	exit $ZONE_SUBPROC_OK
   375 fi
   371 fi
   376 
   372 
   377 LOGFILE=$(/usr/bin/mktemp -t -p /var/tmp ${zone.name}.attach_log.XXXXXX)
   373 LOGFILE=$(/usr/bin/mktemp -t -p /var/tmp $ZONENAME.attach_log.XXXXXX)
   378 if [[ -z "$LOGFILE" ]]; then
   374 if [[ -z "$LOGFILE" ]]; then
   379 	fatal "$e_tmpfile"
   375 	fatal "$e_tmpfile"
   380 fi
   376 fi
   381 exec 2>>"$LOGFILE"
   377 exec 2>>"$LOGFILE"
       
   378 
   382 log "$m_attach_log" "$LOGFILE"
   379 log "$m_attach_log" "$LOGFILE"
   383 
   380 
   384 # Remember what was mounted on the zone root in case the attach fails.
   381 #
   385 get_ds_from_path "${zone.root}" ACTIVE_DS
   382 # TODO - once sxce is gone, move the following block into
   386 attach_datasets -t "$inst_type" -m "$install_media" zone
   383 # usr/lib/brand/shared/common.ksh code to share with other brands using
   387 migrate_export zone
   384 # the same zfs dataset logic for attach. This currently uses get_current_gzbe
       
   385 # so we can't move it yet since beadm isn't in sxce.
       
   386 #
       
   387 
       
   388 # Validate that the zonepath is not in the root dataset.
       
   389 pdir=`dirname $ZONEPATH`
       
   390 get_zonepath_ds $pdir
       
   391 fail_zonepath_in_rootds $ZONEPATH_DS
       
   392 
       
   393 EXIT_CODE=$ZONE_SUBPROC_NOTCOMPLETE
       
   394 
       
   395 if [[ "$install_media" == "-" ]]; then
       
   396 	#
       
   397 	# Since we're using a pre-existing dataset, the dataset currently
       
   398 	# mounted on the {zonepath}/root becomes the active dataset.  We
       
   399 	# can't depend on the usual dataset attributes to detect this since
       
   400 	# the dataset could be a detached zone or one that the user set up by
       
   401 	# hand and lacking the proper attributes.  However, since the zone is
       
   402 	# not attached yet, the 'install_media == -' means the dataset must be
       
   403 	# mounted at this point.
       
   404 	#
       
   405 	ACTIVE_DS=`mount -p | nawk -v zroot=$ZONEROOT '{
       
   406 	    if ($3 == zroot && $4 == "zfs")
       
   407 		    print $1
       
   408 	}'`
       
   409 
       
   410 	[[ -z "$ACTIVE_DS" ]] && fatal "$f_no_active_ds_mounted" $ZONEROOT
       
   411 
       
   412 	# Set up proper attributes on the ROOT dataset.
       
   413 	get_zonepath_ds $ZONEPATH
       
   414 	zfs list -H -t filesystem -o name $ZONEPATH_DS/ROOT >/dev/null 2>&1
       
   415 	(( $? != 0 )) && fatal "$f_no_active_ds"
       
   416 
       
   417 	# need to ensure zoned is off to set mountpoint=legacy.
       
   418 	zfs set zoned=off $ZONEPATH_DS/ROOT
       
   419 	(( $? != 0 )) && fatal "$f_ds_config" $ZONEPATH_DS/ROOT "zoned=off"
       
   420 
       
   421 	zfs set mountpoint=legacy $ZONEPATH_DS/ROOT
       
   422 	(( $? != 0 )) && fatal "$f_ds_config" $ZONEPATH_DS/ROOT \
       
   423 	    "mountpoint=legacy"
       
   424 	zfs set zoned=on $ZONEPATH_DS/ROOT
       
   425 	(( $? != 0 )) && fatal "$f_ds_config" $ZONEPATH_DS/ROOT "zoned=on"
       
   426 
       
   427 	#
       
   428 	# We're typically using a pre-existing mounted dataset so setting the
       
   429 	# following propery changes will cause the {zonepath}/root dataset to
       
   430 	# be unmounted.  However, a p2v with an update-on-attach will have
       
   431 	# created the dataset with the correct properties, so setting these
       
   432 	# attributes won't unmount the dataset.  Thus, we check the mount
       
   433 	# and attempt the remount if necessary.
       
   434 	#
       
   435 	get_current_gzbe
       
   436 	zfs set $PROP_PARENT=$CURRENT_GZBE $ACTIVE_DS
       
   437 	(( $? != 0 )) && fatal "$f_ds_config" $ACTIVE_DS \
       
   438 	    "$PROP_PARENT=$CURRENT_GZBE"
       
   439 	zfs set $PROP_ACTIVE=on $ACTIVE_DS
       
   440 	(( $? != 0 )) && fatal "$f_ds_config" $ACTIVE_DS "$PROP_ACTIVE=on"
       
   441 	zfs set canmount=noauto $ACTIVE_DS
       
   442 	(( $? != 0 )) && fatal "$f_ds_config" $ACTIVE_DS "canmount=noauto"
       
   443 	zfs set zoned=off $ACTIVE_DS
       
   444 	(( $? != 0 )) && fatal "$f_ds_config" $ACTIVE_DS "zoned=off"
       
   445 	zfs inherit mountpoint $ACTIVE_DS
       
   446 	(( $? != 0 )) && fatal "$f_ds_config" $ACTIVE_DS "'inherit mountpoint'"
       
   447 	zfs inherit zoned $ACTIVE_DS
       
   448 	(( $? != 0 )) && fatal "$f_ds_config" $ACTIVE_DS "'inherit zoned'"
       
   449 
       
   450 	mounted_ds=`mount -p | nawk -v zroot=$ZONEROOT '{
       
   451 	    if ($3 == zroot && $4 == "zfs")
       
   452 		    print $1
       
   453 	}'`
       
   454 
       
   455 	if [[ -z $mounted_ds ]]; then
       
   456 		mount -F zfs $ACTIVE_DS $ZONEROOT || fatal "$f_zfs_mount"
       
   457 	fi
       
   458 else
       
   459 	#
       
   460 	# Since we're not using a pre-existing ZFS dataset layout, create
       
   461 	# the zone datasets and mount them.  Start by creating the zonepath
       
   462 	# dataset, similar to what zoneadm would do for an initial install.
       
   463 	#
       
   464 	zds=$(zfs list -H -t filesystem -o name $pdir 2>/dev/null)
       
   465 	if (( $? == 0 )); then
       
   466 		pnm=$(/usr/bin/basename $ZONEPATH)
       
   467 		# The zonepath dataset might already exist.
       
   468 		zfs list -H -t filesystem -o name $zds/$pnm >/dev/null 2>&1
       
   469 		if (( $? != 0 )); then
       
   470 			zfs create "$zds/$pnm"
       
   471 			(( $? != 0 )) && fatal "$f_zfs_create"
       
   472 			vlog "$m_zfs"
       
   473 		fi
       
   474 	fi
       
   475 
       
   476 	create_active_ds
       
   477 fi
       
   478 
       
   479 #
       
   480 # The zone's datasets are now in place.
       
   481 #
       
   482 
       
   483 log "$m_attach_root" "$ZONEROOT"
       
   484 # note \n to add whitespace
       
   485 log "$m_attach_ds\n" "$ACTIVE_DS"
       
   486 
       
   487 install_image "$inst_type" "$install_media"
       
   488 
       
   489 #
       
   490 # End of TODO block to move to common code.
       
   491 #
   388 
   492 
   389 #
   493 #
   390 # Perform a sanity check to confirm that the image is not a global zone.
   494 # Perform a sanity check to confirm that the image is not a global zone.
   391 #
   495 #
   392 VARIANT=variant.opensolaris.zone
   496 VARIANT=variant.opensolaris.zone