comparison bin/fjail @ 77:5aab3a28895f

Implemented a "copy" command to employ zfs send/receive for recursively copying jail datasets
author Franz Glasner <hg@dom66.de>
date Thu, 15 Aug 2019 17:52:06 +0200
parents fea2ef3ff89a
children f5cf08e8d246
comparison
equal deleted inserted replaced
76:fea2ef3ff89a 77:5aab3a28895f
1 #!/bin/sh 1 #!/bin/sh
2 # -*- indent-tabs-mode: nil; -*- 2 # -*- indent-tabs-mode: nil; -*-
3 # @(#)$HGid$ 3 # @(#)$HGid$
4 4
5 set -e 5 set -eu
6 6
7 VERSION="@@VERSION@@" 7 VERSION="@@VERSION@@"
8 8
9 USAGE=" 9 USAGE="
10 USAGE: fjail [ OPTIONS ] COMMAND [ COMMAND OPTIONS ] [ ARG ... ] 10 USAGE: fjail [ OPTIONS ] COMMAND [ COMMAND OPTIONS ] [ ARG ... ]
28 Adjust some Unix privileges to mounted jail datasets 28 Adjust some Unix privileges to mounted jail datasets
29 29
30 populate MOUNTPOINT BASETXZ 30 populate MOUNTPOINT BASETXZ
31 31
32 Populate the jail directory in MOUNTPOINT with the base system in BASETXZ 32 Populate the jail directory in MOUNTPOINT with the base system in BASETXZ
33
34 copy SOURCE-DATASET DEST-DATASET
35
36 Copy a tree of ZFS datasets with \"zfs send -R\" and \"zfs receive\".
37 Note that the destination dataset must not exist already.
38
39 -u Do not automatically mount received datasets
33 " 40 "
34 41
35 42
36 # Reset to standard umask 43 # Reset to standard umask
37 umask 0022 44 umask 0022
152 tar -C "${_mp}" --exclude=./var/empty -xJp -f "${_basetxz}" || { echo "ERROR: tar encountered errors" >&2; return 1; } 159 tar -C "${_mp}" --exclude=./var/empty -xJp -f "${_basetxz}" || { echo "ERROR: tar encountered errors" >&2; return 1; }
153 } 160 }
154 161
155 162
156 # 163 #
164 # "copy" -- ZFS copy of datasets
165 #
166 # command_copy source-dataset destination-dataset
167 #
168 command_copy() {
169 # source dataset -- destination dataset
170 local _source _dest
171 # dynamic ZFS options
172 local _zfsopts
173
174 _zfsopts=""
175 while getopts "u" _opt ; do
176 case ${_opt} in
177 u)
178 # do not mount newly created datasets
179 _zfsopts="${_zfsopts} -u"
180 ;;
181 \?|:)
182 return 2;
183 ;;
184 esac
185 done
186 shift $((OPTIND-1))
187 OPTIND=1
188
189 _source="$1"
190 if [ -z "${_source}" ]; then
191 echo "ERROR: no source dataset given" >&2
192 return 2
193 fi
194 _dest="$2"
195 if [ -z "${_dest}" ]; then
196 echo "ERROR: no source dataset given" >&2
197 return 2
198 fi
199 zfs send -R -n -v ${_source} || { echo "ERROR: ZFS operation failed in no-op mode" >&2; return 1; }
200 zfs send -R "${_source}" | zfs receive ${_zfsopts} "${_dest}" || { echo "ERROR: ZFS operation failed" >&2; return 1; }
201 }
202
203
204 #
157 # "privs" -- adjust privileges 205 # "privs" -- adjust privileges
158 # 206 #
159 # To be used when all ZFS datasets are mounted. 207 # To be used when all ZFS datasets are mounted.
160 # 208 #
161 command_privs() { 209 command_privs() {
212 command_privs "$@" 260 command_privs "$@"
213 ;; 261 ;;
214 populate) 262 populate)
215 command_populate "$@" 263 command_populate "$@"
216 ;; 264 ;;
265 copy)
266 command_copy "$@"
267 ;;
217 *) 268 *)
218 echo "ERROR: unknown command \`${command}'" >&2 269 echo "ERROR: unknown command \`${command}'" >&2
219 exit 2 270 exit 2
220 ;; 271 ;;
221 esac 272 esac