I created standalone version of alternative script to
Here is script code (called
Latest version can be found on my GitHub project: https://github.com/hpaluch-pil/freebsd-scripts/blob/master/vm-bhyve/vm-iml.sh
I'm curious if anybody else may find this script useful.
vm image list
to:- add
SIZE_MB
column to print compressed image size - sort items by
NAME,CREATED
(original command sorts byUUID
only) - make script much faster than
vm image list
thanks to using singlesource
instead of severalsysrc
calls - at the potential expense of security (*.manifest
file must be well formed to avoid shell hijack and/or error)
Code:
$ /opt/sbin/vm-iml.sh
UUID NAME CREATED SIZE_MB DESCRIPTION
7de44d22-524d-11f0-89ca-4cd717a0f39c alpine1 20250626-072202 59 No description provided
6e03ad04-445c-4bf6-8438-70b2173aa88d alpine2-nat 20250627-093946 59 No description provided
4a8db133-5272-11f0-afbf-f8b1569b53fe alpine3-priv 20250626-114527 80 Alpine in private network
1de4459e-613e-11f0-9490-4cd717a0f39c arch-gcc 20250715-073946 2413 ArchLinux with gcc15,cmake4
1bb3a2d3-5e52-11f0-a8b8-4cd717a0f39c deb12-gccbox 20250711-142519 6195 distros with ...
456a0daf-5c98-11f0-83a4-4cd717a0f39c deb12-min 20250709-094231 506 clean install
Here is script code (called
vm-iml.sh
):
Bash:
#!/bin/sh
# list VM images with ZFS archive size and sorted by NAME,CREATED
# It is like "advanced" version of "vm image list" command
# Requirements: installed and configured "vm-bhyve" package
# Copyright: many parts come from /usr/local/lib/vm-bhyve/vm-* scripts
set -euo pipefail
errx() {
echo "ERROR: $@" >&2
exit 1
}
# extract ZFS dataset for vm-bhyve "datastore" configuration
extract_zfs_ds() {
local vm_dir vm_ds
vm_dir="$(sysrc -n vm_dir)"
[ -n "$vm_dir" ] || errx "No vm_dir variable defined in /etc/rc.conf"
[ "${vm_dir%%:*}" = "zfs" ] || errx "vm_dir='$vm_dir' has no 'zfs:' prefix"
vm_ds="${vm_dir#zfs:}"
[ -n "$vm_ds" ] || errx "Unable to extract ZFS dataset name from vm_dir='$vm_dir'"
echo "$vm_ds"
}
vm_ds=$( extract_zfs_ds )
vm_dir=$(mount | grep "^${vm_ds} " |cut -d' ' -f3)
[ -n "$vm_dir" ] || errx "Unable to find mount point for ZFS dataset '$vm_ds'"
[ "${vm_dir#/}" != "$vm_dir" ] || errx "Mount point '$vm_dir' does not start with '/'"
im_dir="$vm_dir/images"
[ -d "$im_dir" ] || errx "Unable to find Image dir '$im_dir' under '$vm_dir'"
_formath='%s^%s^%s^%s^%s\n'
_format='%s^%s^%s^%7d^%s\n'
# top level block to align '^' separated output to columns
{
printf "${_formath}" "UUID" "NAME" "CREATED" "SIZE_MB" "DESCRIPTION"
# nested block to properly sort output data by NAME,CREATED
{
ls -1 ${vm_dir}/images/ | \
while read _file; do
if [ "${_file##*.}" = "manifest" ]; then
_uuid=${_file%.*}
# NOTE: sourcing with '.' is much faster than several calls of sysrc
. "${vm_dir}/images/${_uuid}.manifest"
# convert date to ASCII sortable
sortable_created=$( date -j -f '%+' '+%Y%m%d-%H%M%S' "${created}" )
# get file size of compressed ZFS dataset
zfs_size=$( stat -f "%z" "${vm_dir}/images/$_uuid.zfs.z" )
zfs_size_mb=$(( $zfs_size / 1024 / 1024 ))
printf "${_format}" "${_uuid}" "${name}" "${sortable_created}" "${zfs_size_mb}" "${description}"
fi
done
} | sort -t ^ -k 2,3
} | column -ts^
exit 0
Latest version can be found on my GitHub project: https://github.com/hpaluch-pil/freebsd-scripts/blob/master/vm-bhyve/vm-iml.sh
Disclaimer: script works only with regular ZFS dataset, tested/etc/rc.conf
value:vm_dir="zfs:zbsd/vm-bhyve"
I'm curious if anybody else may find this script useful.