clone command for ZFS users

makes it easy to duplicate an existing guest
This commit is contained in:
Matt Churchyard
2015-10-15 14:55:48 +01:00
parent 16d3f21f1e
commit 6d6ecb6a34
5 changed files with 51 additions and 2 deletions

View File

@@ -215,7 +215,7 @@ If you are using the default csh/tcsh shell built into FreeBSD, the following co
autocomplete to work for all the currently supported functions:
complete vm \
'p@1@(list create install start stop console configure reset poweroff switch iso)@' \
'p@1@(list create install start stop console configure reset poweroff destroy clone switch iso)@' \
'n@create@n@' \
'n@list@n@' \
'n@iso@n@' \

View File

@@ -82,6 +82,9 @@ __parse_cmd(){
configure)
__vm_configure "$@"
;;
clone)
__zfs_clone "$@"
;;
*)
__usage
;;

View File

@@ -77,12 +77,13 @@ Usage: vm ...
install <name> <iso>
start <name>
stop <name>
console <name>
console <name> [com1|com2]
startall
stopall
reset <name>
poweroff <name>
configure <name>
clone <name> <new-name>
iso [url]
image list
image create [-d description] <name>

View File

@@ -86,6 +86,38 @@ __zfs_make_zvol(){
[ $? -ne 0 ] && __err "failed to create new ZVOL ${VM_ZFS_DATASET}/${_name}"
}
# clone a vm
__zfs_clone(){
local _old="$1"
local _new="$2"
local _uuid _uuidpart _fs _newfs
[ -z "${_old}" -o -z "${_new}" ] && __usage
[ ! "${VM_ZFS}" = "1" ] && __err "cannot clone guests unless ZFS support is enabled"
[ ! -e "${vm_dir}/${_old}/${_old}.conf" ] && __err "${_old} does not appear to be an existing virtual machine"
[ -e "${vm_dir}/${_old}/run.lock" ] && __err "${_old} must be powered off first"
[ -d "${vm_dir}/${_new}" ] && __err "directory ${vm_dir}/${_new} already exists"
_uuid=$(uuidgen)
_uuidpart=$(echo "${_uuid}" |awk -F- '{print $1}')
zfs snapshot -r ${VM_ZFS_DATASET}/${_old}@${_uuidpart}
[ $? -ne 0 ] && __err "failed to create snapshot ${VM_ZFS_DATASET}/${_old}@${_uuidpart}"
zfs list -o name -rHt filesystem,volume ${VM_ZFS_DATASET}/${_old} | \
while read _fs; do
_newfs=$(echo "${_fs}" | sed "s@${VM_ZFS_DATASET}/${_old}@${VM_ZFS_DATASET}/${_new}@")
zfs clone ${_fs}@${_uuidpart} ${_newfs}
[ $? -ne 0 ] && __err "error while cloning datasets"
done
# update new guest
mv "${vm_dir}/${_new}/${_old}.conf" "${vm_dir}/${_new}/${_new}.conf"
sysrc -inqf "${vm_dir}/${_new}/${_new}.conf" "uuid=${_uuid}" >/dev/null 2>&1
rm "${vm_dir}/${_new}/vm-bhyve.log" >/dev/null 2>&1
}
# create an image of a vm
__zfs_image_create(){
local _name

13
vm.8
View File

@@ -65,6 +65,10 @@
.Nm
.Cm configure
.Ar name
.Nm
.Cm clone
.Ar name
.Ar new-name
.Pp
.Nm
.Cm iso
@@ -456,6 +460,15 @@ The
command simply opens the virtual machine configuration file in your default editor,
allowing you to easily make changes. Please note, changes do not take effect until
the virtual machine is fully shutdown and restarted.
.It Cm clone Ar name Ar new-name
Create a clone of the virtual machine
.Pa name ,
as long as it is currently powered off. The new machine will be called
.Pa new-name ,
and will be ready to boot with a newly assigned UUID and empty log file.
.Pp
Please note that this function requires ZFS, and a snapshot will be taken of the original
guest, along with any descendant datasets.
.It Cm iso Op Ar url
List all the ISO files currently stored in the
.Pa $vm_dir/.iso