64b87c
From db1e692c13290fba4d1c9a5e81b7374c677cb421 Mon Sep 17 00:00:00 2001
64b87c
From: Fabian Deutsch <fabiand@fedoraproject.org>
64b87c
Date: Wed, 18 Feb 2015 14:31:40 +0100
64b87c
Subject: [PATCH] dmsquash: Add rd.live.overlay.thin
64b87c
64b87c
This option changes the underlying mechanism for the overlay in the
64b87c
dmsquash module.
64b87c
Instead of a plain dm snapshot a dm thin snapshot is used. The advantage
64b87c
of the thin snapshot is, that the TRIM command is recognized, which
64b87c
means that at runtime, only the occupied blocks will be claimed from
64b87c
memory, and freed blocks will really be freed in ram.
64b87c
64b87c
Signed-off-by: Fabian Deutsch <fabiand@fedoraproject.org>
64b87c
(cherry picked from commit d6e34d362a05cda61baaf8e231ad3f0e8665a9cc)
64b87c
---
49e6a8
 dracut.cmdline.7.asc                          |  7 +++++
49e6a8
 .../90dmsquash-live/dmsquash-live-root.sh     | 27 ++++++++++++++++++-
64b87c
 2 files changed, 33 insertions(+), 1 deletion(-)
64b87c
64b87c
diff --git a/dracut.cmdline.7.asc b/dracut.cmdline.7.asc
1755ca
index 0493dcd8..0c3bc295 100644
64b87c
--- a/dracut.cmdline.7.asc
64b87c
+++ b/dracut.cmdline.7.asc
64b87c
@@ -802,6 +802,13 @@ Enables debug output from the live boot process.
64b87c
 Specifies the directory within the squashfs where the ext3fs.img or rootfs.img
64b87c
 can be found.  By default, this is __LiveOS__.
64b87c
 
64b87c
+**rd.live.overlay.thin=**1::
64b87c
+Enables the usage of thin snapshots instead of classic dm snapshots.
64b87c
+The advantage of thin snapshots is, that they support discards, and will free
64b87c
+blocks which are not claimed by the filesystem. In this use case this means,
64b87c
+that memory is given back to the kernel, when the filesystem does not claim it
64b87c
+anymore.
64b87c
+
64b87c
 **rd.writable.fsimg=**1::
64b87c
 Enables writable filesystem support.  The system will boot with a fully 
64b87c
 writable filesystem without snapshots __(see notes above about available live boot options)__.
64b87c
diff --git a/modules.d/90dmsquash-live/dmsquash-live-root.sh b/modules.d/90dmsquash-live/dmsquash-live-root.sh
1755ca
index c6c02c74..0645a0bd 100755
64b87c
--- a/modules.d/90dmsquash-live/dmsquash-live-root.sh
64b87c
+++ b/modules.d/90dmsquash-live/dmsquash-live-root.sh
64b87c
@@ -30,6 +30,8 @@ getargbool 0 rd.live.overlay.readonly -d -y readonly_overlay && readonly_overlay
64b87c
 overlay=$(getarg rd.live.overlay -d overlay)
64b87c
 getargbool 0 rd.writable.fsimg -d -y writable_fsimg && writable_fsimg="yes"
64b87c
 
64b87c
+getargbool 0 rd.live.overlay.thin && thin_snapshot="yes"
64b87c
+
64b87c
 # CD/DVD media check
64b87c
 [ -b $livedev ] && fs=$(blkid -s TYPE -o value $livedev)
64b87c
 if [ "$fs" = "iso9660" -o "$fs" = "udf" ]; then
64b87c
@@ -146,7 +148,30 @@ do_live_overlay() {
64b87c
         base=$BASE_LOOPDEV
64b87c
         over=$OVERLAY_LOOPDEV
64b87c
     fi
64b87c
-    echo 0 $sz snapshot $base $over p 8 | dmsetup create live-rw
64b87c
+    if [ -n "$thin_snapshot" ]; then
64b87c
+        modprobe dm_thin_pool
64b87c
+        mkdir /run/initramfs/thin-overlay
64b87c
+
64b87c
+        # In block units (512b)
64b87c
+        thin_data_sz=$(( $overlay_size * 1024 * 1024 / 512 ))
64b87c
+        thin_meta_sz=$(( $thin_data_sz / 10 ))
64b87c
+
64b87c
+        # It is important to have the backing file on a tmpfs
64b87c
+        # this is needed to let the loopdevice support TRIM
64b87c
+        dd if=/dev/null of=/run/initramfs/thin-overlay/meta bs=1b count=1 seek=$((thin_meta_sz)) 2> /dev/null
64b87c
+        dd if=/dev/null of=/run/initramfs/thin-overlay/data bs=1b count=1 seek=$((thin_data_sz)) 2> /dev/null
64b87c
+
64b87c
+        THIN_META_LOOPDEV=$( losetup --show -f /run/initramfs/thin-overlay/meta )
64b87c
+        THIN_DATA_LOOPDEV=$( losetup --show -f /run/initramfs/thin-overlay/data )
64b87c
+
64b87c
+        echo 0 $thin_data_sz thin-pool $THIN_META_LOOPDEV $THIN_DATA_LOOPDEV 1024 1024 | dmsetup create live-overlay-pool
64b87c
+        dmsetup message /dev/mapper/live-overlay-pool 0 "create_thin 0"
64b87c
+
64b87c
+        # Create a snapshot of the base image
64b87c
+        echo 0 $sz thin /dev/mapper/live-overlay-pool 0 $base | dmsetup create live-rw
64b87c
+    else
64b87c
+        echo 0 $sz snapshot $base $over p 8 | dmsetup create live-rw
64b87c
+    fi
64b87c
 
64b87c
     # Create a device that always points to a ro base image
64b87c
     echo 0 $sz linear $base 0 | dmsetup create --readonly live-base