[PATCH 2/5][blivet] Add support for detecting btrfs default subvolume.

David Lehman dlehman at redhat.com
Fri Nov 22 16:00:08 UTC 2013


---
 blivet/devicelibs/btrfs.py | 14 ++++++++++++++
 blivet/devices.py          | 27 +++++++++++++++++++++++++++
 blivet/devicetree.py       |  3 +++
 3 files changed, 44 insertions(+)

diff --git a/blivet/devicelibs/btrfs.py b/blivet/devicelibs/btrfs.py
index bde165d..531c294 100644
--- a/blivet/devicelibs/btrfs.py
+++ b/blivet/devicelibs/btrfs.py
@@ -116,3 +116,17 @@ def list_subvolumes(mountpoint):
                      "path": group[2]})
 
     return vols
+
+def get_default_subvolume(mountpoint):
+    if not os.path.ismount(mountpoint):
+        raise ValueError("volume not mounted")
+
+    args = ["subvol", "get-default", mountpoint]
+    buf = btrfs(args, capture=True)
+    m = re.match(r'ID (\d+) .*', buf)
+    try:
+        default = int(m.groups()[0])
+    except IndexError:
+        raise BTRFSError("failed to get default subvolume from %s" % mountpoint)
+
+    return default
diff --git a/blivet/devices.py b/blivet/devices.py
index 82f1872..4b71b4c 100644
--- a/blivet/devices.py
+++ b/blivet/devices.py
@@ -4482,6 +4482,8 @@ class BTRFSVolumeDevice(BTRFSDevice):
             subvols = btrfs.list_subvolumes(self.originalFormat._mountpoint)
         except BTRFSError as e:
             log.debug("failed to list subvolumes: %s" % e)
+        else:
+            self._getDefaultSubVolumeID()
         finally:
             self._undo_temp_mount()
 
@@ -4498,6 +4500,31 @@ class BTRFSVolumeDevice(BTRFSDevice):
     def removeSubVolume(self, name):
         raise NotImplementedError()
 
+    def _getDefaultSubVolumeID(self):
+        subvolid = None
+        try:
+            subvolid = btrfs.get_default_subvolume(self.originalFormat._mountpoint)
+        except BTRFSError as e:
+            log.debug("failed to get default subvolume id: %s" % e)
+
+        self.defaultSubVolumeID = subvolid
+
+    @property
+    def defaultSubVolume(self):
+        default = None
+        if self.defaultSubVolumeID is None:
+            return None
+
+        if self.defaultSubVolumeID == self.vol_id:
+            return self
+
+        for sv in self.subvolumes:
+            if sv.vol_id == self.defaultSubVolumeID:
+                default = sv
+                break
+
+        return default
+
     def _create(self):
         log_method_call(self, self.name, status=self.status)
         btrfs.create_volume(devices=[d.path for d in self.parents],
diff --git a/blivet/devicetree.py b/blivet/devicetree.py
index 8ab1e5f..df0bd0c 100644
--- a/blivet/devicetree.py
+++ b/blivet/devicetree.py
@@ -2361,6 +2361,9 @@ class DeviceTree(object):
             elif "subvolid=" in options:
                 attr = "vol_id"
                 val = util.get_option_value("subvolid", options)
+            elif device.defaultSubVolume:
+                # default subvolume
+                device = device.defaultSubVolume
 
             if attr and val:
                 for subvol in device.subvolumes:
-- 
1.8.1.4



More information about the anaconda-patches mailing list