Use fstrim to prep the block device

This cuts the image size down alot, esspecially if there were lots of
small file deletes.

The fstrim utility is in the util-linux package and should be on
most all systems. fstrim also works with XFS, ext4, btrfs, etc
prodiving the kernel is new enough.

A reduction of 25% or more in size is common.

Change-Id: I269b4416be450369616f9b8e030f84c30e329804
This commit is contained in:
SamYaple 2016-03-09 22:43:27 +00:00 committed by Sam Yaple
parent 2a10525efe
commit 5b6716cee8
3 changed files with 29 additions and 0 deletions

View File

@ -264,6 +264,14 @@ for X in ${!IMAGE_TYPES[@]}; do
esac esac
done done
# NOTE: fstrim is on most all recent systems. It is provided by the util-linux
# package.
if [[ -z "$(which fstrim)" ]]; then
echo "fstrim utility is not found. This is provided by util-linux package"
echo "Please check your PATH variable is set correctly"
exit 1
fi
# NOTE: Tuning the rootfs uuid works only for ext filesystems. # NOTE: Tuning the rootfs uuid works only for ext filesystems.
# Rely on the below environment variable only for ext filesystems. # Rely on the below environment variable only for ext filesystems.
export DIB_IMAGE_ROOT_FS_UUID=$(uuidgen -r) export DIB_IMAGE_ROOT_FS_UUID=$(uuidgen -r)
@ -387,6 +395,9 @@ for X in ${!IMAGE_TYPES[@]} ; do
fi fi
done done
# Prep filesystem by discarding all unused space
fstrim_image
# Unmount and cleanup the /mnt and /build subdirectories, to save # Unmount and cleanup the /mnt and /build subdirectories, to save
# space before converting the image to some other format. # space before converting the image to some other format.
unmount_image unmount_image

View File

@ -12,6 +12,13 @@ and file system) is created and the tree copied into it. The file system
created is an ext4 filesystem just large enough to hold the file system tree created is an ext4 filesystem just large enough to hold the file system tree
and can be resized up to 1PB in size. and can be resized up to 1PB in size.
To produce the smallest image the utility fstrim is used. When deleting a file
the space is simply marked as free on the disk, the file is still there until
it is overwritten. fstrim informs the underlying disk to drop those bytes the
end result of which is like writting zeros over those sectors. The same effect
could be achieved by creating a large file full of zeros and removing that
file, however that method is far more IO intensive.
An element is a particular set of code that alters how the image is built, or An element is a particular set of code that alters how the image is built, or
runs within the chroot to prepare the image. E.g. the local-config element runs within the chroot to prepare the image. E.g. the local-config element
copies in the http proxy and ssh keys of the user running the image build copies in the http proxy and ssh keys of the user running the image build

View File

@ -29,6 +29,17 @@ function unmount_image () {
fi fi
} }
function fstrim_image () {
# A race condition can occur when trying to fstrim immediately after
# deleting a file resulting in that free space not being reclaimed.
# Calling sync before fstrim is a workaround for this behaviour.
# https://lists.gnu.org/archive/html/qemu-devel/2014-03/msg02978.html
sync
# Discard all unused bytes
sudo fstrim "${TMP_BUILD_DIR}/mnt"
}
function trap_cleanup() { function trap_cleanup() {
exitval=$? exitval=$?
cleanup cleanup