Tags: patch

Hi,

I've wrote a patch to implement this feature and am attaching it.
I haven't find any fault through test deployment,
but would you please review this closely?

I wish this helps.
>From decf70c5a4dd9731525db9e668c1ff611d9fe720 Mon Sep 17 00:00:00 2001
From: Katsuhiko Nishimra <ktns...@gmail.com>
Date: Thu, 9 Jul 2015 04:01:00 +0900
Subject: [PATCH] Btrfs chroot model class

---
 debian/control                         |  2 +-
 debian/mini-buildd-sudoers             |  3 +++
 mini_buildd/models/__init__.py         |  1 +
 mini_buildd/models/chroot.py           | 46 +++++++++++++++++++++++++++++++++-
 mini_buildd/templates/admin/index.html |  1 +
 5 files changed, 51 insertions(+), 2 deletions(-)

diff --git a/debian/control b/debian/control
index dd3a997..5c57ca6 100644
--- a/debian/control
+++ b/debian/control
@@ -71,7 +71,7 @@ Depends: ${misc:Depends},
          devscripts (>= 2.14.5~),
          lintian
 Recommends: python-apt
-Suggests: haveged, lvm2, qemu-user-static, binfmt-support
+Suggests: haveged, lvm2, qemu-user-static, binfmt-support, btrfs-tools
 Breaks: mini-buildd-rep (<< 1.0.0~),
         mini-buildd-bld (<< 1.0.0~)
 Replaces: mini-buildd-rep,
diff --git a/debian/mini-buildd-sudoers b/debian/mini-buildd-sudoers
index 89d8ab2..4f337ab 100644
--- a/debian/mini-buildd-sudoers
+++ b/debian/mini-buildd-sudoers
@@ -14,3 +14,6 @@ mini-buildd ALL=NOPASSWD:/sbin/pvcreate,/sbin/pvremove,/sbin/vgcreate,/sbin/vgre
 
 # LOOP: LoopLVMChroot
 mini-buildd ALL=NOPASSWD:/sbin/losetup,/bin/dd
+
+# BTRFS: BtrfsSnapshotChroot
+mini-buildd ALL=NOPASSWD:/sbin/btrfs
diff --git a/mini_buildd/models/__init__.py b/mini_buildd/models/__init__.py
index bf17d26..0a4c7c5 100644
--- a/mini_buildd/models/__init__.py
+++ b/mini_buildd/models/__init__.py
@@ -39,6 +39,7 @@ def import_all():
         chroot.FileChroot,
         chroot.LVMChroot,
         chroot.LoopLVMChroot,
+        chroot.BtrfsSnapshotChroot,
         daemon.Daemon,
         subscription.Subscription]
 
diff --git a/mini_buildd/models/chroot.py b/mini_buildd/models/chroot.py
index 55f3de4..6807a37 100644
--- a/mini_buildd/models/chroot.py
+++ b/mini_buildd/models/chroot.py
@@ -114,7 +114,7 @@ chroots (with <tt>qemu-user-static</tt> installed).
                                             f=self.mbd_get_backend().mbd_backend_flavor())
 
     def mbd_get_backend(self):
-        for cls, sub in {"filechroot": [], "dirchroot": [], "lvmchroot": ["looplvmchroot"]}.items():
+        for cls, sub in {"filechroot": [], "dirchroot": [], "lvmchroot": ["looplvmchroot"], "btrfssnapshotchroot": []}.items():
             if hasattr(self, cls):
                 c = getattr(self, cls)
                 for s in sub:
@@ -494,3 +494,47 @@ class LoopLVMChroot(LVMChroot):
 
             (["/sbin/vgcreate", "--verbose", self.mbd_get_volume_group(), loop_device],
              ["/sbin/vgremove", "--verbose", "--force", self.mbd_get_volume_group()])] + super(LoopLVMChroot, self).mbd_get_pre_sequence()
+
+
+class BtrfsSnapshotChroot(Chroot):
+    """ Btrfs Snapshot chroot backend. """
+    class Meta(Chroot.Meta):
+        pass
+
+    class Admin(Chroot.Admin):
+        fieldsets = Chroot.Admin.fieldsets # TODO: quota settings
+
+        @classmethod
+        def mbd_meta_add_base_sources(cls, msglog):
+            cls._mbd_meta_add_base_sources(BtrfsSnapshotChroot, msglog)
+
+    def mbd_backend_flavor(self):
+        return "btrfs_snapshot"
+
+    def mbd_get_chroot_dir(self):
+        return os.path.join(self.mbd_get_path(), "source")
+
+    def mbd_get_snapshot_dir(self):
+        return os.path.join(self.mbd_get_path(), "snapshot")
+
+    def mbd_get_schroot_conf(self):
+        return """\
+type=btrfs-snapshot
+btrfs-source-subvolume={source}
+btrfs-snapshot-directory={snapshot}
+""".format(source=self.mbd_get_chroot_dir(),
+        snapshot=self.mbd_get_snapshot_dir())
+
+    def mbd_get_pre_sequence(self):
+        return [
+            (["/sbin/btrfs", "subvolume", "create", self.mbd_get_chroot_dir()],
+             ["/sbin/btrfs", "subvolume", "delete", self.mbd_get_chroot_dir()]),
+
+            (["/bin/mount",  "-v", "-o", "bind", self.mbd_get_chroot_dir(), self.mbd_get_tmp_dir()],
+             ["/bin/umount", "-v", "-o", "bind", self.mbd_get_tmp_dir()]),
+
+            (["/bin/mkdir", "--verbose", self.mbd_get_snapshot_dir()],
+             ["/bin/rm", "--recursive", "--one-file-system", "--force", self.mbd_get_snapshot_dir()])]
+
+    def mbd_get_post_sequence(self):
+        return [(["/bin/umount", "-v", self.mbd_get_tmp_dir()], [])]
diff --git a/mini_buildd/templates/admin/index.html b/mini_buildd/templates/admin/index.html
index f04ccae..6b00754 100644
--- a/mini_buildd/templates/admin/index.html
+++ b/mini_buildd/templates/admin/index.html
@@ -44,6 +44,7 @@
 							{% include "admin/snippet_index_table_row.html" with model_name="File chroots" model_path="chroot.FileChroot" wiz0_function="add_base_sources" wiz0_name="Defaults" wiz0_title="Add default chroots for all active base sources and host architectures" %}
 							{% include "admin/snippet_index_table_row.html" with model_name="Lvm chroots" model_path="chroot.LVMChroot" %}
 							{% include "admin/snippet_index_table_row.html" with model_name="Loop lvm chroots" model_path="chroot.LoopLVMChroot" wiz0_function="add_base_sources" wiz0_name="Defaults" wiz0_title="Add default chroots for all active base sources and host architectures" %}
+							{% include "admin/snippet_index_table_row.html" with model_name="Btrfs snapshot chroots" model_path="chroot.BtrfsSnapshotChroot" wiz0_function="add_base_sources" wiz0_name="Defaults" wiz0_title="Add default chroots for all active base sources and host architectures" %}
 
 							<!-- REMOTES -->
 							{% include "admin/snippet_index_table_header.html" with title="Remotes" %}
-- 
2.1.4

Attachment: 0001-Btrfs-chroot-model-class.patch.sig
Description: PGP signature

Reply via email to