#!/bin/bash -

# Requires libguestfs >= 1.19.26
guestfish="./run ./fish/guestfish"
#guestfish=guestfish

qemu-img create -f qcow2 -o compat=1.1 test1.qcow2 100M

$guestfish -a test1.qcow2 -x <<EOF
run

# Print guest kernel version.
debug sh "uname -a"

# Is TRIM supported by the block device?
debug sh "head -n99999999 /sys/block/sd*/queue/discard_max_bytes"

# Mount ext4 format partition on / with -o discard.
part-disk /dev/sda mbr
mkfs ext4 /dev/sda1
mount-options discard /dev/sda1 /

# Create a 50MB test file.
fill 0x58 50M /data
sync
!ls -lh test1.qcow2

# Remove test file.
rm /data

# Try trimming the filesystem.
fstrim /
sync
!ls -lh test1.qcow2

EOF
