Skip to content

Instantly share code, notes, and snippets.

@bahamas10
Last active October 26, 2018 05:02
Show Gist options
  • Select an option

  • Save bahamas10/24f892b708a47f4ebcb9ca92c5b24698 to your computer and use it in GitHub Desktop.

Select an option

Save bahamas10/24f892b708a47f4ebcb9ca92c5b24698 to your computer and use it in GitHub Desktop.
--- a/overlay/generic/usr/lib/brand/jcommon/cinstall
+++ b/overlay/generic/usr/lib/brand/jcommon/cinstall
@@ -19,7 +19,7 @@
#
# CDDL HEADER END
#
-# Copyright (c) 2016, Joyent, Inc. All rights reserved.
+# Copyright (c) 2018, Joyent, Inc.
# Use is subject to license terms.
#
@@ -52,17 +52,48 @@ function fixup_images
# and when it exists, we use that. However, when it does not exist we
# still use the old method of creating a zones/<uuid>@<uuid> snapshot
# so we can support old datasets.
- exists=$(zfs list -Ho name ${PDS_NAME}/${TMPLZONE}@final 2>&1)
- if [[ $? == 0 && ${exists} == "${PDS_NAME}/${TMPLZONE}@final" ]]; then
- zfs clone -o devices=off -F ${QUOTA_ARG} ${PDS_NAME}/${TMPLZONE}@final \
- ${PDS_NAME}/${bname} || fatal "failed to clone zone dataset"
- elif [[ ${exists} =~ "dataset does not exist" ]]; then
- zfs snapshot ${PDS_NAME}/${TMPLZONE}@${bname}
- zfs clone -o devices=off -F ${QUOTA_ARG} ${PDS_NAME}/${TMPLZONE}@${bname} \
- ${PDS_NAME}/${bname} || fatal "failed to clone zone dataset"
+
+ typeset exists
+ typeset snap="$PDS_NAME/$TMPLZONE@final"
+ typeset dataset="$PDS_NAME/$bname"
+ typeset dataset_tmp="${dataset}_install_tmp"
+ typeset cfgdir f
+
+ exists=$(zfs list -Ho name "$snap" 2>&1)
+
+ if [[ $? == 0 && $exists == "$snap" ]]; then
+ # snap exists, do nothing
+ true
+ elif [[ $exists =~ 'dataset does not exist' ]]; then
+ # create and use <uuid> snapshot
+ snap="$PDS_NAME/$TMPLZONE@$bname"
+ zfs snapshot "$snap"
else
- fatal "Unable to determine snapshot for ${PDS_NAME}/${TMPLZONE}"
+ fatal "Unable to determine snapshot for $PDS_NAME/$TMPLZONE"
fi
+
+ # At this point, $snap should exist and point to the correct
+ # image snapshot that we can use to clone from. We will:
+ # 1. Clone to a temporary dataset location
+ # 2. Perform any image cleanup (such as clear out existing JSON metadata
+ # files)
+ # 3. Rename the temporary dataset to the final location
+
+ # 1. Clone to temp dataset
+ zfs clone -o devices=off -F $QUOTA_ARG "$snap" "$dataset_tmp" \
+ || fatal "failed to clone zone dataset $snap -> $dataset_tmp"
+
+ # 2. Clean up remnants from the image
+ cfgdir="/$dataset_tmp/config"
+ rm -rf "$cfgdir"
+ mkdir -m755 "$cfgdir"
+ for f in tags.json metadata.json routes.json; do
+ echo '{}' > "$cfgdir/$f"
+ done
+
+ # 3. Move temp dataset to final location
+ zfs rename -f "$dataset_tmp" "$dataset" \
+ || fatal "failed to rename zone dataset $dataset_tmp -> $dataset"
}
while getopts "rR:t:U:q:z:" opt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment