1 #!/bin/ksh -p
2 #
3 # This file and its contents are supplied under the terms of the
4 # Common Development and Distribution License ("CDDL"), version 1.0.
5 # You may only use this file in accordance with the terms of version
6 # 1.0 of the CDDL.
7 #
8 # A full copy of the text of the CDDL should have accompanied this
9 # source. A copy of the CDDL is also available via the Internet at
10 # http://www.illumos.org/license/CDDL.
11 #
12 # Copyright 2016 Joyent, Inc. All rights reserved.
13 # Copyright 2016 OmniTI Computer Consulting, Inc. All rights reserved.
14 #
15
16 PATH=/bin:/usr/bin:/usr/sbin
17 export PATH
18
19 . /usr/lib/brand/shared/common.ksh
20
21 ZFS_SEED=""
22 TAR_SEED=""
23
24 bad_usage() {
25 echo "LX zone install bad option"
26 echo "Available options are:"
27 echo " -s <absolute-pathname> Path to ZFS send stream or gzip thereof"
28 echo " -t <absolute-pathname> Path to tar archive or gzip thereof"
29 exit $ZONE_SUBPROC_USAGE
30 }
31
32 while getopts "R:s:t:z:" opt
33 do
34 case "$opt" in
35 R) ZONEPATH="$OPTARG";;
36 z) ZONENAME="$OPTARG";;
37 s) ZFS_SEED="$OPTARG";;
38 t) TAR_SEED="$OPTARG";;
39 *) bad_usage ;;
40 esac
41 done
42 shift OPTIND-1
43
44 if [[ $ZFS_SEED == "" && $TAR_SEED == "" ]]; then
45 echo "The -s <absolute-pathname> argument or the -t <absolute-pathname>"
46 echo "argument is required for LX installation."
47 bad_usage
48 fi
49
50 if [[ $ZFS_SEED != "" && $TAR_SEED != "" ]]; then
51 echo "You must only specify one of -s or -t for LX installation."
52 bad_usage
53 fi
54
55 # Set the ZONEPATH_DS variable so we know the zone's dataset.
56 get_zonepath_ds $ZONEPATH
57
58 # Do something based on whatever ZFS_SEED is.
59
60 if [[ -f $TAR_SEED ]]; then
61 type=`file -b $TAR_SEED | awk '{print $1}'`
62 if [[ $type == "gzip" ]]; then
63 args="-xzf"
64 else
65 args="-xf"
66 fi
67 cd $ZONEPATH
68 # Be very precise about permissions and ownership.
69 mkdir -m 0755 dev
70 chgrp sys dev
71 mkdir -m 0755 root
72 chgrp root sys
73 cd root
74 gtar $args $TAR_SEED
75 exit 0
76 elif [[ ! -f $ZFS_SEED ]]; then
77 # Try and eat a snapshot or a filesystem.
78 outstr=`zfs list -Ht filesystem $ZFS_SEED 2>/dev/null | awk '{print $1}'`
79 if [[ $outstr == $ZFS_SEED ]]; then
80 # We have a zfs filesystem name.
81 # Snapshot it using today's date/time
82 snapname=`date -u "+%Y-%m-%d:%H:%M:%S"`
83 ZFS_SEED=$ZFS_SEED@$snapname
84 zfs snapshot $ZFS_SEED
85 if [[ $? != 0 ]]; then
86 echo "ZFS snapshot ($ZFS_SEED) command failed ($?)."
87 exit $ZONE_SUBPROC_FATAL
88 fi
89 # else continue on with the new snapshot...
90 fi
91
92 outstr=`zfs list -Ht snapshot $ZFS_SEED 2>/dev/null | awk '{print $1}'`
93 if [[ $outstr == $ZFS_SEED ]]; then
94 # Hmmm, we found a snapshot name!
95 echo "Cloning from snapshot $ZFS_SEED"
96 # zoneadm already created $ZONEPATH_DS, destroy it before we clone.
97 zfs destroy $ZONEPATH_DS
98 zfs clone $ZFS_SEED $ZONEPATH_DS
99 if [[ $? != 0 ]]; then
100 echo "ZFS clone ($ZFS_SEED to $ZONEPATH_DS) failed ($?)."
101 exit $ZONE_SUBPROC_FAIL
102 fi
103 # zfs promote $ZONEPATH_DS
104 # if [[ $? != 0 ]]; then
105 # echo "ZFS promote ($ZONEPATH_DS) failed ($?)."
106 # exit $ZONE_SUBPROC_FAIL
107 # fi
108 else
109 echo "Seed file $ZFS_SEED $TAR_SEED not found."
110 bad_usage
111 fi
112 else
113 type=`file -b $ZFS_SEED | awk '{print $1}'`
114
115 # For now, we are dependent on the output of file(1).
116 # I'm being cheesy in checking the first word of file(1)'s output.
117 if [[ $type == "ZFS" ]]; then
118 zfs recv -F $ZONEPATH_DS < $ZFS_SEED
119 elif [[ $type == "gzip" ]]; then
120 gunzip -c $ZFS_SEED | zfs recv -F $ZONEPATH_DS
121 else
122 echo "Seed file $ZFS_SEED not a ZFS receive (or compressed) one."
123 bad_usage
124 fi
125
126 if [[ $? != 0 ]]; then
127 echo "ZFS receive command failed ($?)."
128 exit $ZONE_SUBPROC_FATAL
129 fi
130 fi
131
132 # One Joyent-ism we need to clean up.
133 rmdir $ZONEPATH/cores
134 # And one we should probably adopt.
135 zfs set devices=off $ZONEPATH_DS
136
137 exit 0