From c13aa84f023271f7f2ef8b938dffa08ead7d056e Mon Sep 17 00:00:00 2001 From: David Shea Date: Thu, 4 Oct 2018 13:01:59 -0400 Subject: [PATCH] Make no-virt generated images sparser At the end of disk image installs, use fstrim on the generated filesystem to discard any blocks that were allocated during the install and are now unused. This will allow tools such as qemu-img to create images that do not include deleted data. For raw disk images that do not go through qemu-img, use fallocate --dig-holes to create sparse holes in place of the unused blocks. (cherry picked from commit 9717b3fd98fd41a96a217ab3a031a8105e2b6257) Related: rhbz#1628645 Related: rhbz#1628646 Related: rhbz#1628647 Related: rhbz#1628648 --- src/pylorax/installer.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/pylorax/installer.py b/src/pylorax/installer.py index b61040b8..f7f5c32c 100644 --- a/src/pylorax/installer.py +++ b/src/pylorax/installer.py @@ -379,16 +379,23 @@ def novirt_install(opts, disk_img, disk_size): setfiles_args = ["-e", "/proc", "-e", "/sys", "-e", "/dev", "/etc/selinux/targeted/contexts/files/file_contexts", "/"] - # setfiles may not be available, warn instead of fail - try: - if "--dirinstall" in args: + if "--dirinstall" in args: + # setfiles may not be available, warn instead of fail + try: execWithRedirect("setfiles", setfiles_args, root=dirinstall_path) - else: - with PartitionMount(disk_img) as img_mount: - if img_mount and img_mount.mount_dir: + except (subprocess.CalledProcessError, OSError) as e: + log.warning("Running setfiles on install tree failed: %s", str(e)) + else: + with PartitionMount(disk_img) as img_mount: + if img_mount and img_mount.mount_dir: + try: execWithRedirect("setfiles", setfiles_args, root=img_mount.mount_dir) - except (subprocess.CalledProcessError, OSError) as e: - log.warning("Running setfiles on install tree failed: %s", str(e)) + except (subprocess.CalledProcessError, OSError) as e: + log.warning("Running setfiles on install tree failed: %s", str(e)) + + # For image installs, run fstrim to discard unused blocks. This way + # unused blocks do not need to be allocated for sparse image types + execWithRedirect("fstrim", [img_mount.mount_dir]) except (subprocess.CalledProcessError, OSError) as e: log.error("Running anaconda failed: %s", e) @@ -475,6 +482,9 @@ def novirt_install(opts, disk_img, disk_size): if rc: raise InstallError("novirt_install mktar failed: rc=%s" % rc) + else: + # For raw disk images, use fallocate to deallocate unused space + execWithRedirect("fallocate", ["--dig-holes", disk_img], raise_err=True) def virt_install(opts, install_log, disk_img, disk_size):