All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] devtool: deploy-target: Support stripped libs and execs
@ 2017-06-14  6:24 Tobias Hagelborn
  2017-06-14  9:28 ` Richard Purdie
  2017-06-14 17:33 ` Leonardo Sandoval
  0 siblings, 2 replies; 5+ messages in thread
From: Tobias Hagelborn @ 2017-06-14  6:24 UTC (permalink / raw)
  To: openembedded-core; +Cc: Tobias Hagelborn

New devtool deploy-target option --strip which enables deploying
stripped binaries, saving some space on target.

* Copies the files of ${D} into a new directory and strips them in place
* Based on sysroot_strip from staging.bbclass
* Added devtool.conf option "strip" for changing default behavior
* Added .ko strip support compared to original function sysroot_strip

Config example:
[Deploy]
strip = true

[YOCTO #11227]

Signed-off-by: Tobias Hagelborn <tobiasha@axis.com>
---
 scripts/lib/devtool/deploy.py | 134 ++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 130 insertions(+), 4 deletions(-)

diff --git a/scripts/lib/devtool/deploy.py b/scripts/lib/devtool/deploy.py
index 04c34cb..9547018 100644
--- a/scripts/lib/devtool/deploy.py
+++ b/scripts/lib/devtool/deploy.py
@@ -16,12 +16,16 @@
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 """Devtool plugin containing the deploy subcommands"""
 
+import logging
 import os
+import shutil
 import subprocess
-import logging
 import tempfile
-import shutil
+
+import bb.utils
 import argparse_oe
+import oe
+
 from devtool import exec_fakeroot, setup_tinfoil, check_workspace_recipe, DevtoolError
 
 logger = logging.getLogger('devtool')
@@ -140,9 +144,114 @@ def _prepare_remote_script(deploy, verbose=False, dryrun=False, undeployall=Fals
     return '\n'.join(lines)
 
 
+def strip_execs(dstdir, strip_cmd, libdir, base_libdir):
+    """
+    Strip executable code (like executables, shared libraries, kernel modules) _in_place_
+    - Based on sysroot_strip in staging.bbclass
+    :param dstdir: directory in which to strip files
+    :param strip_cmd:
+    """
+    import stat, errno, mmap
+
+    # Detect .ko module by searching for "vermagic=" string
+    def is_kernel_module(path):
+        with open(path) as f:
+            return mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ).find(b"vermagic=") >= 0
+
+    # Return type (bits):
+    # 0 - not elf
+    # 1 - ELF
+    # 2 - stripped
+    # 4 - executable
+    # 8 - shared library
+    # 16 - kernel module
+    def is_elf(path):
+        exec_type = 0
+        ret, result = oe.utils.getstatusoutput(
+            "file \"%s\"" % path.replace("\"", "\\\""))
+
+        if ret:
+            logger.error("split_and_strip_files: 'file %s' failed", path)
+            return exec_type
+
+        # Not stripped
+        if "ELF" in result:
+            exec_type |= 1
+            if "not stripped" not in result:
+                exec_type |= 2
+            if "executable" in result:
+                exec_type |= 4
+            if "shared" in result:
+                exec_type |= 8
+            if "relocatable" in result and is_kernel_module(path):
+                exec_type |= 16
+        return exec_type
+
+    elffiles = {}
+    inodes = {}
+    libdir = os.path.abspath(os.path.join(dstdir, libdir.lstrip(os.sep)))
+    base_libdir = os.path.abspath(os.path.join(dstdir, base_libdir.lstrip(os.sep)))
+    exec_mask = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
+    #
+    # First lets figure out all of the files we may have to process
+    #
+    for root, dirs, files in os.walk(dstdir):
+        for f in files:
+            file = os.path.join(root, f)
+
+            try:
+                ltarget = oe.path.realpath(file, dstdir, False)
+                s = os.lstat(ltarget)
+            except OSError as e:
+                (err, strerror) = e.args
+                if err != errno.ENOENT:
+                    raise
+                # Skip broken symlinks
+                continue
+            if not s:
+                continue
+            # Check its an excutable
+            if s[stat.ST_MODE] & exec_mask \
+                    or ((file.startswith(libdir) or file.startswith(base_libdir)) and ".so" in f) \
+                    or file.endswith('.ko'):
+                # If it's a symlink, and points to an ELF file, we capture the readlink target
+                if os.path.islink(file):
+                    continue
+
+                # It's a file (or hardlink), not a link
+                # ...but is it ELF, and is it already stripped?
+                elf_file = is_elf(file)
+                if elf_file & 1:
+                    if elf_file & 2:
+                        logger.warn(
+                            "File '%s' is unexpectedly already stripped.",
+                            file[len(dstdir):])
+                        continue
+
+                    if s.st_ino in inodes:
+                        os.unlink(file)
+                        os.link(inodes[s.st_ino], file)
+                    else:
+                        # break hardlinks so that we do not strip the original.
+                        inodes[s.st_ino] = file
+                        # break hardlink
+                        bb.utils.copyfile(file, file)
+                        elffiles[file] = elf_file
+
+    #
+    # Now strip them (in parallel)
+    #
+    sfiles = []
+    for file in elffiles:
+        elf_file = int(elffiles[file])
+        logger.debug("Strip %s",file)
+        sfiles.append((file, elf_file, strip_cmd))
+
+    oe.utils.multiprocess_exec(sfiles, oe.package.runstrip)
+
+
 def deploy(args, config, basepath, workspace):
     """Entry point for the devtool 'deploy' subcommand"""
-    import re
     import math
     import oe.recipeutils
 
@@ -170,6 +279,17 @@ def deploy(args, config, basepath, workspace):
                             'recipe? If so, the install step has not installed '
                             'any files.' % args.recipename)
 
+        if args.strip and not args.dry_run:
+            # Fakeroot copy to new destination
+            srcdir = recipe_outdir
+            recipe_outdir = os.path.join(rd.getVar('WORKDIR', True), 'deploy-target-stripped')
+            if os.path.isdir(recipe_outdir):
+                bb.utils.remove(recipe_outdir, True)
+            exec_fakeroot(rd, "cp -af %s %s" % (os.path.join(srcdir, '.'), recipe_outdir), shell=True)
+            os.environ['PATH'] = ':'.join([os.environ['PATH'], rd.getVar('PATH', True) or ''])
+            strip_execs(recipe_outdir, rd.getVar('STRIP', True), rd.getVar('libdir', True),
+                        rd.getVar('base_libdir', True))
+
         filelist = []
         ftotalsize = 0
         for root, _, files in os.walk(recipe_outdir):
@@ -189,7 +309,6 @@ def deploy(args, config, basepath, workspace):
                 print('  %s' % item)
             return 0
 
-
         extraoptions = ''
         if args.no_host_check:
             extraoptions += '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
@@ -301,6 +420,7 @@ def undeploy(args, config, basepath, workspace):
 
 def register_commands(subparsers, context):
     """Register devtool subcommands from the deploy plugin"""
+
     parser_deploy = subparsers.add_parser('deploy-target',
                                           help='Deploy recipe output files to live target machine',
                                           description='Deploys a recipe\'s build output (i.e. the output of the do_install task) to a live target machine over ssh. By default, any existing files will be preserved instead of being overwritten and will be restored if you run devtool undeploy-target. Note: this only deploys the recipe itself and not any runtime dependencies, so it is assumed that those have been installed on the target beforehand.',
@@ -313,6 +433,12 @@ def register_commands(subparsers, context):
     parser_deploy.add_argument('-p', '--no-preserve', help='Do not preserve existing files', action='store_true')
     parser_deploy.add_argument('--no-check-space', help='Do not check for available space before deploying', action='store_true')
     parser_deploy.add_argument('-P', '--port', default='22', help='Port to use for connection to the target')
+    parser_deploy.add_argument('-S', '--strip',
+                               help='Strip executables prior to deploying (default: %(default)s). '
+                                    'The default value of this option can be controlled by setting the strip option in the [Deploy] section to True or False.',
+                               default=oe.types.boolean(context.config.get('Deploy', 'strip', default='0')),
+                               action='store_true')
+    parser_deploy.add_argument('--no-strip', help='Do not strip executables prior to deploy', dest='strip', action='store_false')
     parser_deploy.set_defaults(func=deploy)
 
     parser_undeploy = subparsers.add_parser('undeploy-target',
-- 
2.1.4



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] devtool: deploy-target: Support stripped libs and execs
  2017-06-14  6:24 [PATCH] devtool: deploy-target: Support stripped libs and execs Tobias Hagelborn
@ 2017-06-14  9:28 ` Richard Purdie
  2017-06-14 11:35   ` Tobias Hagelborn
  2017-06-14 17:33 ` Leonardo Sandoval
  1 sibling, 1 reply; 5+ messages in thread
From: Richard Purdie @ 2017-06-14  9:28 UTC (permalink / raw)
  To: Tobias Hagelborn, openembedded-core; +Cc: Tobias Hagelborn

On Wed, 2017-06-14 at 08:24 +0200, Tobias Hagelborn wrote:
> New devtool deploy-target option --strip which enables deploying
> stripped binaries, saving some space on target.
> 
> * Copies the files of ${D} into a new directory and strips them in
> place
> * Based on sysroot_strip from staging.bbclass
> * Added devtool.conf option "strip" for changing default behavior
> * Added .ko strip support compared to original function sysroot_strip

This looks very like the original strip function. Would it be possible
to create one generic strip function in lib/oe which we could call from
the various places rather than duplicating this again?

(I think staging.bbclass and lib/oe/package.py:runstrip() are already
similar code).

Cheers,

Richard


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] devtool: deploy-target: Support stripped libs and execs
  2017-06-14  9:28 ` Richard Purdie
@ 2017-06-14 11:35   ` Tobias Hagelborn
  2017-06-16  5:12     ` Tobias Hagelborn
  0 siblings, 1 reply; 5+ messages in thread
From: Tobias Hagelborn @ 2017-06-14 11:35 UTC (permalink / raw)
  To: Richard Purdie, Tobias Hagelborn, openembedded-core

On 06/14/2017 11:28 AM, Richard Purdie wrote:
> On Wed, 2017-06-14 at 08:24 +0200, Tobias Hagelborn wrote:
>> New devtool deploy-target option --strip which enables deploying
>> stripped binaries, saving some space on target.
>>
>> * Copies the files of ${D} into a new directory and strips them in
>> place
>> * Based on sysroot_strip from staging.bbclass
>> * Added devtool.conf option "strip" for changing default behavior
>> * Added .ko strip support compared to original function sysroot_strip
>
> This looks very like the original strip function. Would it be possible
> to create one generic strip function in lib/oe which we could call from
> the various places rather than duplicating this again?
>
> (I think staging.bbclass and lib/oe/package.py:runstrip() are already
> similar code).
>
> Cheers,
>
> Richard
>
I did think if this but I felt these are quite central functions and I 
didn't have enough tests locally to cover for those classes (staging / 
package)

I can supply another version of this patch where the strip function is 
merged into lib/oe.

//Tobias


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] devtool: deploy-target: Support stripped libs and execs
  2017-06-14  6:24 [PATCH] devtool: deploy-target: Support stripped libs and execs Tobias Hagelborn
  2017-06-14  9:28 ` Richard Purdie
@ 2017-06-14 17:33 ` Leonardo Sandoval
  1 sibling, 0 replies; 5+ messages in thread
From: Leonardo Sandoval @ 2017-06-14 17:33 UTC (permalink / raw)
  To: Tobias Hagelborn; +Cc: Tobias Hagelborn, openembedded-core


>  
>  def register_commands(subparsers, context):
>      """Register devtool subcommands from the deploy plugin"""
> +
>      parser_deploy = subparsers.add_parser('deploy-target',
>                                            help='Deploy recipe output files to live target machine',
>                                            description='Deploys a recipe\'s build output (i.e. the output of the do_install task) to a live target machine over ssh. By default, any existing files will be preserved instead of being overwritten and will be restored if you run devtool undeploy-target. Note: this only deploys the recipe itself and not any runtime dependencies, so it is assumed that those have been installed on the target beforehand.',
> @@ -313,6 +433,12 @@ def register_commands(subparsers, context):
>      parser_deploy.add_argument('-p', '--no-preserve', help='Do not preserve existing files', action='store_true')
>      parser_deploy.add_argument('--no-check-space', help='Do not check for available space before deploying', action='store_true')
>      parser_deploy.add_argument('-P', '--port', default='22', help='Port to use for connection to the target')
> +    parser_deploy.add_argument('-S', '--strip',
> +                               help='Strip executables prior to deploying (default: %(default)s). '
> +                                    'The default value of this option can be controlled by setting the strip option in the [Deploy] section to True or False.',
> +                               default=oe.types.boolean(context.config.get('Deploy', 'strip', default='0')),
> +                               action='store_true')
> +    parser_deploy.add_argument('--no-strip', help='Do not strip executables prior to deploy', dest='strip', action='store_false')
>      parser_deploy.set_defaults(func=deploy)


sounds like these two new parameters mutual exclude, so something like
this may be useful

https://docs.python.org/3/library/argparse.html#mutual-exclusion

>  
>      parser_undeploy = subparsers.add_parser('undeploy-target',
> -- 
> 2.1.4
> 




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] devtool: deploy-target: Support stripped libs and execs
  2017-06-14 11:35   ` Tobias Hagelborn
@ 2017-06-16  5:12     ` Tobias Hagelborn
  0 siblings, 0 replies; 5+ messages in thread
From: Tobias Hagelborn @ 2017-06-16  5:12 UTC (permalink / raw)
  To: openembedded-core

On 06/14/2017 01:35 PM, Tobias Hagelborn wrote:
> On 06/14/2017 11:28 AM, Richard Purdie wrote:
>> On Wed, 2017-06-14 at 08:24 +0200, Tobias Hagelborn wrote:
>>> New devtool deploy-target option --strip which enables deploying
>>> stripped binaries, saving some space on target.
>>>
>>> * Copies the files of ${D} into a new directory and strips them in
>>> place
>>> * Based on sysroot_strip from staging.bbclass
>>> * Added devtool.conf option "strip" for changing default behavior
>>> * Added .ko strip support compared to original function sysroot_strip
>>
>> This looks very like the original strip function. Would it be possible
>> to create one generic strip function in lib/oe which we could call from
>> the various places rather than duplicating this again?
>>
>> (I think staging.bbclass and lib/oe/package.py:runstrip() are already
>> similar code).
>>
>> Cheers,
>>
>> Richard
>>
> I did think if this but I felt these are quite central functions and I
> didn't have enough tests locally to cover for those classes (staging /
> package)
>
> I can supply another version of this patch where the strip function is
> merged into lib/oe.
>
> //Tobias

It should be fairly straightforward to do for staging.bbclass.
I also looked into package.bbclass which has a similar one 
(split_and_strip_files) but that one is very convoluted with the package 
splitting so I want to leave that one for now.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-06-16  5:12 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-06-14  6:24 [PATCH] devtool: deploy-target: Support stripped libs and execs Tobias Hagelborn
2017-06-14  9:28 ` Richard Purdie
2017-06-14 11:35   ` Tobias Hagelborn
2017-06-16  5:12     ` Tobias Hagelborn
2017-06-14 17:33 ` Leonardo Sandoval

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.