[elbe-devel] [PATCH v3 3/3] elbe: add logic for nested btrfs subvolumes

Daniel Braunwarth daniel at braunwarth.dev
Fri Nov 25 19:18:01 CET 2022


Signed-off-by: Daniel Braunwarth <daniel at braunwarth.dev>
---
 elbepack/fstab.py | 10 ++++++++++
 elbepack/hdimg.py | 12 ++++++++++--
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/elbepack/fstab.py b/elbepack/fstab.py
index efd983f9f..3fa28403b 100644
--- a/elbepack/fstab.py
+++ b/elbepack/fstab.py
@@ -109,6 +109,11 @@ class hdpart:
         return loopdev.decode().rstrip('\n')
 
 
+class BtrfsSubvolume:
+    def __init__(self, xml):
+        self.name = xml.text("name")
+
+
 class fstabentry(hdpart):
 
     # pylint: disable=too-many-instance-attributes
@@ -132,6 +137,11 @@ class fstabentry(hdpart):
             self.passno = entry.text("fs/passno", default="0")
             self.tune = entry.text("fs/tune2fs", default=None)
 
+            self.subvolumes = []
+            if self.fstype == "btrfs":
+                for subvolume in entry.node("fs/subvolumes") or []:
+                    self.subvolumes.append(BtrfsSubvolume(subvolume))
+
         self.id = str(fsid)
 
     def get_str(self):
diff --git a/elbepack/hdimg.py b/elbepack/hdimg.py
index ab2c9c324..562501b74 100644
--- a/elbepack/hdimg.py
+++ b/elbepack/hdimg.py
@@ -10,6 +10,8 @@
 import logging
 import os
 
+from pathlib import Path
+
 import parted
 import _ped
 
@@ -367,12 +369,18 @@ def create_label(disk, part, ppart, fslabel, target, grub):
             entry.get_label_opt(),
             loopdev))
 
-        do('mount %s %s' % (loopdev, os.path.join(target, "imagemnt")))
+        mount_path = Path(target, "imagemnt")
+        do('mount %s %s' % (loopdev, mount_path))
+
+        for subvolume in entry.subvolumes:
+            subvolume_path = Path(mount_path, subvolume.name)
+            do('mkdir --parents %s' % (subvolume_path.parent))
+            do('btrfs subvolume create %s' % (subvolume_path))
 
         try:
             do('cp -a "%s/." "%s/"' %
                (os.path.join(target, "filesystems", entry.id),
-                os.path.join(target, "imagemnt")),
+                mount_path),
                allow_fail=True)
         finally:
             do('umount %s' % loopdev)
-- 
2.38.1



More information about the elbe-devel mailing list