[lxc-devel] [PATCH 3/3] lxc-debian: add btrfs support

Laurent Vivier laurent at vivier.eu
Fri Jun 17 01:19:32 UTC 2016


copied from lxc-ubuntu.in

Signed-off-by: Laurent Vivier <laurent at vivier.eu>
---
 templates/lxc-debian.in | 55 ++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 50 insertions(+), 5 deletions(-)

diff --git a/templates/lxc-debian.in b/templates/lxc-debian.in
index 6c4eb81..f620fb8 100644
--- a/templates/lxc-debian.in
+++ b/templates/lxc-debian.in
@@ -257,10 +257,45 @@ configure_debian_systemd()
     return 0
 }
 
+# Check if given path is in a btrfs partition
+is_btrfs()
+{
+    [ -e $1 -a $(stat -f -c '%T' $1) = "btrfs" ]
+}
+
+# Check if given path is the root of a btrfs subvolume
+is_btrfs_subvolume()
+{
+    [ -d $1 -a $(stat -f -c '%T' $1) = "btrfs" -a $(stat -c '%i' $1) -eq 256 ]
+}
+
+try_mksubvolume()
+{
+    path=$1
+    [ -d $path ] && return 0
+    mkdir -p $(dirname $path)
+    if which btrfs >/dev/null 2>&1 && is_btrfs $(dirname $path); then
+        btrfs subvolume create $path
+    else
+        mkdir -p $path
+    fi
+}
+
+try_rmsubvolume()
+{
+    path=$1
+    [ -d $path ] || return 0
+    if which btrfs >/dev/null 2>&1 && is_btrfs_subvolume $path; then
+        btrfs subvolume delete $path
+    else
+        rm -rf $path
+    fi
+}
+
 cleanup()
 {
-    rm -rf $cache/partial-$release-$arch
-    rm -rf $cache/rootfs-$release-$arch
+    try_rmsubvolume $cache/partial-$release-$arch
+    try_rmsubvolume $cache/rootfs-$release-$arch
 }
 
 download_debian()
@@ -306,7 +341,7 @@ openssh-server
             | gpg --import --no-default-keyring --keyring=${releasekeyring}
     fi
     # check the mini debian was not already downloaded
-    mkdir -p "$cache/partial-$release-$arch"
+    try_mksubvolume "$cache/partial-$release-$arch"
     if [ $? -ne 0 ]; then
         echo "Failed to create '$cache/partial-$release-$arch' directory"
         return 1
@@ -362,8 +397,18 @@ copy_debian()
 
     # make a local copy of the minidebian
     echo -n "Copying rootfs to $rootfs..."
-    mkdir -p $rootfs
-    rsync -Ha "$cache/rootfs-$release-$arch"/ $rootfs/ || return 1
+    try_mksubvolume $rootfs
+    if which btrfs >/dev/null 2>&1 && \
+       is_btrfs_subvolume "$cache/rootfs-$release-$arch" && \
+       is_btrfs_subvolume $rootfs; then
+      realrootfs=$(dirname $config)/rootfs
+      [ "$rootfs" = "$realrootfs" ] || umount $rootfs || return 1
+      btrfs subvolume delete $realrootfs || return 1
+      btrfs subvolume snapshot "$cache/rootfs-$release-$arch" $realrootfs || return 1
+      [ "$rootfs" = "$realrootfs" ] || mount --bind $realrootfs $rootfs || return 1
+    else
+        rsync -Ha "$cache/rootfs-$release-$arch"/ $rootfs/ || return 1
+    fi
     return 0
 }
 
-- 
2.5.5



More information about the lxc-devel mailing list