Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # ex:ts=4:sw=4:sts=4:et |
| 2 | # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- |
| 3 | # |
| 4 | # Copyright (c) 2014, Intel Corporation. |
| 5 | # All rights reserved. |
| 6 | # |
| 7 | # This program is free software; you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License version 2 as |
| 9 | # published by the Free Software Foundation. |
| 10 | # |
| 11 | # This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | # GNU General Public License for more details. |
| 15 | # |
| 16 | # You should have received a copy of the GNU General Public License along |
| 17 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | # |
| 20 | # DESCRIPTION |
| 21 | # This implements the 'rootfs' source plugin class for 'wic' |
| 22 | # |
| 23 | # AUTHORS |
| 24 | # Tom Zanussi <tom.zanussi (at] linux.intel.com> |
| 25 | # Joao Henrique Ferreira de Freitas <joaohf (at] gmail.com> |
| 26 | # |
| 27 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 28 | import logging |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 29 | import os |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 30 | import shutil |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame^] | 31 | import sys |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 32 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 33 | from oe.path import copyhardlinktree |
| 34 | |
| 35 | from wic import WicError |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 36 | from wic.pluginbase import SourcePlugin |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame^] | 37 | from wic.misc import get_bitbake_var |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 38 | |
| 39 | logger = logging.getLogger('wic') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 40 | |
| 41 | class RootfsPlugin(SourcePlugin): |
| 42 | """ |
| 43 | Populate partition content from a rootfs directory. |
| 44 | """ |
| 45 | |
| 46 | name = 'rootfs' |
| 47 | |
| 48 | @staticmethod |
| 49 | def __get_rootfs_dir(rootfs_dir): |
| 50 | if os.path.isdir(rootfs_dir): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame^] | 51 | return os.path.realpath(rootfs_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 52 | |
| 53 | image_rootfs_dir = get_bitbake_var("IMAGE_ROOTFS", rootfs_dir) |
| 54 | if not os.path.isdir(image_rootfs_dir): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 55 | raise WicError("No valid artifact IMAGE_ROOTFS from image " |
| 56 | "named %s has been found at %s, exiting." % |
| 57 | (rootfs_dir, image_rootfs_dir)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 58 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame^] | 59 | return os.path.realpath(image_rootfs_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 60 | |
| 61 | @classmethod |
| 62 | def do_prepare_partition(cls, part, source_params, cr, cr_workdir, |
| 63 | oe_builddir, bootimg_dir, kernel_dir, |
| 64 | krootfs_dir, native_sysroot): |
| 65 | """ |
| 66 | Called to do the actual content population for a partition i.e. it |
| 67 | 'prepares' the partition to be incorporated into the image. |
| 68 | In this case, prepare content for legacy bios boot partition. |
| 69 | """ |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 70 | if part.rootfs_dir is None: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 71 | if not 'ROOTFS_DIR' in krootfs_dir: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 72 | raise WicError("Couldn't find --rootfs-dir, exiting") |
| 73 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 74 | rootfs_dir = krootfs_dir['ROOTFS_DIR'] |
| 75 | else: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 76 | if part.rootfs_dir in krootfs_dir: |
| 77 | rootfs_dir = krootfs_dir[part.rootfs_dir] |
| 78 | elif part.rootfs_dir: |
| 79 | rootfs_dir = part.rootfs_dir |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 80 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 81 | raise WicError("Couldn't find --rootfs-dir=%s connection or " |
| 82 | "it is not a valid path, exiting" % part.rootfs_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 83 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame^] | 84 | part.rootfs_dir = cls.__get_rootfs_dir(rootfs_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 85 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame^] | 86 | new_rootfs = None |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 87 | # Handle excluded paths. |
| 88 | if part.exclude_path is not None: |
| 89 | # We need a new rootfs directory we can delete files from. Copy to |
| 90 | # workdir. |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame^] | 91 | new_rootfs = os.path.realpath(os.path.join(cr_workdir, "rootfs%d" % part.lineno)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 92 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 93 | if os.path.lexists(new_rootfs): |
| 94 | shutil.rmtree(os.path.join(new_rootfs)) |
| 95 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame^] | 96 | copyhardlinktree(part.rootfs_dir, new_rootfs) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 97 | |
| 98 | for orig_path in part.exclude_path: |
| 99 | path = orig_path |
| 100 | if os.path.isabs(path): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame^] | 101 | logger.error("Must be relative: --exclude-path=%s" % orig_path) |
| 102 | sys.exit(1) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 103 | |
| 104 | full_path = os.path.realpath(os.path.join(new_rootfs, path)) |
| 105 | |
| 106 | # Disallow climbing outside of parent directory using '..', |
| 107 | # because doing so could be quite disastrous (we will delete the |
| 108 | # directory). |
| 109 | if not full_path.startswith(new_rootfs): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame^] | 110 | logger.error("'%s' points to a path outside the rootfs" % orig_path) |
| 111 | sys.exit(1) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 112 | |
| 113 | if path.endswith(os.sep): |
| 114 | # Delete content only. |
| 115 | for entry in os.listdir(full_path): |
| 116 | full_entry = os.path.join(full_path, entry) |
| 117 | if os.path.isdir(full_entry) and not os.path.islink(full_entry): |
| 118 | shutil.rmtree(full_entry) |
| 119 | else: |
| 120 | os.remove(full_entry) |
| 121 | else: |
| 122 | # Delete whole directory. |
| 123 | shutil.rmtree(full_path) |
| 124 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 125 | part.prepare_rootfs(cr_workdir, oe_builddir, |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame^] | 126 | new_rootfs or part.rootfs_dir, native_sysroot) |