All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kristian Amlie <kristian.amlie@mender.io>
To: openembedded-core@lists.openembedded.org
Subject: [PATCH] do_image: Implement IMAGE_ROOTFS_EXCLUDE_PATH feature.
Date: Wed, 26 Apr 2017 17:03:51 +0200	[thread overview]
Message-ID: <1493219031-12843-2-git-send-email-kristian.amlie@mender.io> (raw)
In-Reply-To: <1493219031-12843-1-git-send-email-kristian.amlie@mender.io>

This is a direct followup from the earlier f6a064d969f414 commit in
wic. It works more or less the same way: The variable specifies a list
of directories relative to the root of the rootfs, and these
directories will be excluded from the resulting rootfs image. If an
entry ends with a slash, only the contents are omitted, not the
directory itself.

Signed-off-by: Kristian Amlie <kristian.amlie@mender.io>
---
 meta/classes/image.bbclass | 73 ++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 71 insertions(+), 2 deletions(-)

diff --git a/meta/classes/image.bbclass b/meta/classes/image.bbclass
index 405fd73..a309435 100644
--- a/meta/classes/image.bbclass
+++ b/meta/classes/image.bbclass
@@ -496,8 +496,8 @@ python () {
         d.setVar('do_image_%s' % t, '\n'.join(cmds))
         d.setVarFlag('do_image_%s' % t, 'func', '1')
         d.setVarFlag('do_image_%s' % t, 'fakeroot', '1')
-        d.setVarFlag('do_image_%s' % t, 'prefuncs', debug + 'set_image_size')
-        d.setVarFlag('do_image_%s' % t, 'postfuncs', 'create_symlinks')
+        d.setVarFlag('do_image_%s' % t, 'prefuncs', debug + 'set_image_size prepare_excluded_directories')
+        d.setVarFlag('do_image_%s' % t, 'postfuncs', 'create_symlinks cleanup_excluded_directories')
         d.setVarFlag('do_image_%s' % t, 'subimages', ' '.join(subimages))
         d.appendVarFlag('do_image_%s' % t, 'vardeps', ' '.join(vardeps))
         d.appendVarFlag('do_image_%s' % t, 'vardepsexclude', 'DATETIME')
@@ -506,6 +506,75 @@ python () {
         bb.build.addtask('do_image_%s' % t, 'do_image_complete', after, d)
 }
 
+python prepare_excluded_directories() {
+    exclude_var = d.getVar('IMAGE_ROOTFS_EXCLUDE_PATH')
+    if not exclude_var:
+        return
+
+    import shutil
+    from oe.path import copyhardlinktree
+
+    exclude_list = exclude_var.split()
+
+    taskname = d.getVar("BB_CURRENTTASK")
+
+    rootfs_orig = d.getVar('IMAGE_ROOTFS')
+    # We need a new rootfs directory we can delete files from. Copy to
+    # workdir.
+    new_rootfs = os.path.realpath(os.path.join(d.getVar("WORKDIR"), "rootfs.%s" % taskname))
+
+    if os.path.lexists(new_rootfs):
+        shutil.rmtree(os.path.join(new_rootfs))
+
+    copyhardlinktree(rootfs_orig, new_rootfs)
+
+    for orig_path in exclude_list:
+        path = orig_path
+        if os.path.isabs(path):
+            msger.error("IMAGE_ROOTFS_EXCLUDE_PATH: Must be relative: %s" % orig_path)
+
+        full_path = os.path.realpath(os.path.join(new_rootfs, path))
+
+        # Disallow climbing outside of parent directory using '..',
+        # because doing so could be quite disastrous (we will delete the
+        # directory).
+        if not full_path.startswith(new_rootfs):
+            msger.error("'%s' points to a path outside the rootfs" % orig_path)
+
+        if path.endswith(os.sep):
+            # Delete content only.
+            for entry in os.listdir(full_path):
+                full_entry = os.path.join(full_path, entry)
+                if os.path.isdir(full_entry) and not os.path.islink(full_entry):
+                    shutil.rmtree(full_entry)
+                else:
+                    os.remove(full_entry)
+        else:
+            # Delete whole directory.
+            shutil.rmtree(full_path)
+
+    # Save old value for cleanup later.
+    d.setVar('IMAGE_ROOTFS_ORIG', rootfs_orig)
+    d.setVar('IMAGE_ROOTFS', new_rootfs)
+}
+
+python cleanup_excluded_directories() {
+    exclude_var = d.getVar('IMAGE_ROOTFS_EXCLUDE_PATH')
+    if not exclude_var:
+        return
+
+    import shutil
+
+    rootfs_dirs_excluded = d.getVar('IMAGE_ROOTFS')
+    rootfs_orig = d.getVar('IMAGE_ROOTFS_ORIG')
+    # This should never happen, since we should have set it to a different
+    # directory in the prepare function.
+    assert rootfs_dirs_excluded != rootfs_orig
+
+    shutil.rmtree(rootfs_dirs_excluded)
+    d.setVar('IMAGE_ROOTFS', rootfs_orig)
+}
+
 #
 # Compute the rootfs size
 #
-- 
2.7.4



  reply	other threads:[~2017-04-26 15:04 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-26 15:03 do_image: Adding support for IMAGE_ROOTFS_EXCLUDE_PATH Kristian Amlie
2017-04-26 15:03 ` Kristian Amlie [this message]
2017-05-22  7:08 ` Kristian Amlie
2017-05-22  8:46   ` Ed Bartosh
2017-05-22  9:38     ` Kristian Amlie
2017-05-30  7:46       ` Kristian Amlie
2017-05-30  7:46         ` [PATCH v2] do_image: Implement IMAGE_ROOTFS_EXCLUDE_PATH feature Kristian Amlie
2017-06-19  9:01           ` Kristian Amlie
2017-06-19 14:01             ` Kristian Amlie
2017-06-19 14:01               ` [PATCH v3] " Kristian Amlie
2017-08-23 11:44                 ` [PATCH v4] " Kristian Amlie
2017-08-23 11:46                 ` Kristian Amlie
2017-08-23 11:46                   ` [PATCH v4] " Kristian Amlie
2017-08-23 12:39                     ` Kristian Amlie
2017-08-23 12:39                       ` [PATCH v5] " Kristian Amlie
2017-08-23 12:47                         ` Richard Purdie
2017-08-23 13:19                           ` Kristian Amlie
2017-08-28 15:47                             ` Kristian Amlie
2017-08-28 15:47                               ` [PATCH v6] " Kristian Amlie
2017-09-18  6:45                                 ` Kristian Amlie
2017-10-13  9:08                                   ` Kristian Amlie
2017-10-13  9:08                                     ` [PATCH v7] " Kristian Amlie
2017-10-13 10:37                                     ` [PATCH v6] " Alexander Kanavin
2017-10-13 11:22                                       ` Kristian Amlie
2017-11-22 13:13                                       ` Kristian Amlie
2017-11-22 13:13                                         ` [PATCH v7] " Kristian Amlie
2017-11-22 13:31                                         ` [PATCH v6] " Alexander Kanavin
2017-11-22 13:35                                           ` Kristian Amlie
2018-01-25 10:33                                           ` Kristian Amlie
2018-01-25 10:33                                             ` [PATCH v7] " Kristian Amlie
2018-01-25 10:58                                               ` Martin Hundebøll
2018-02-13  1:29                                               ` Cal Sullivan
2018-03-15  9:29                                                 ` Kristian Amlie
2017-08-23 12:46                       ` Richard Purdie
2017-08-23 13:19                         ` Kristian Amlie
2017-05-30  8:01       ` ✗ patchtest: failure for do_image: Implement IMAGE_ROOTFS_EXCLUDE_PATH feature. (rev2) Patchwork
2017-06-19 14:31       ` ✗ patchtest: failure for do_image: Implement IMAGE_ROOTFS_EXCLUDE_PATH feature. (rev3) Patchwork
2017-08-23 12:04       ` ✗ patchtest: failure for do_image: Implement IMAGE_ROOTFS_EXCLUDE_PATH feature. (rev4) Patchwork
2017-08-23 12:04       ` ✗ patchtest: failure for do_image: Implement IMAGE_ROOTFS_EXCLUDE_PATH feature. (rev5) Patchwork
2017-08-23 13:04       ` ✗ patchtest: failure for do_image: Implement IMAGE_ROOTFS_EXCLUDE_PATH feature. (rev6) Patchwork
2017-08-23 15:12         ` Leonardo Sandoval

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1493219031-12843-2-git-send-email-kristian.amlie@mender.io \
    --to=kristian.amlie@mender.io \
    --cc=openembedded-core@lists.openembedded.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.