Serge Bazanski | 140bddc | 2020-06-05 21:01:19 +0200 | [diff] [blame] | 1 | # Copyright 2020 The Monogon Project Authors. |
| 2 | # |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | def _smalltown_initramfs_impl(ctx): |
| 18 | """ |
| 19 | Generate an lz4-compressed initramfs based on a label/file list. |
| 20 | """ |
| 21 | |
| 22 | # Generate config file for gen_init_cpio that describes the initramfs to build. |
| 23 | cpio_list_name = ctx.label.name + ".cpio_list" |
| 24 | cpio_list = ctx.actions.declare_file(cpio_list_name) |
| 25 | |
| 26 | # Start out with some standard initramfs device files. |
| 27 | cpio_list_content = [ |
| 28 | "dir /dev 0755 0 0", |
| 29 | "nod /dev/console 0600 0 0 c 5 1", |
| 30 | "nod /dev/null 0644 0 0 c 1 3", |
| 31 | "nod /dev/kmsg 0644 0 0 c 1 11", |
| 32 | "nod /dev/ptmx 0644 0 0 c 5 2", |
| 33 | ] |
| 34 | |
| 35 | # Find all directories that need to be created. |
| 36 | directories_needed = [] |
| 37 | for _, p in ctx.attr.files.items(): |
| 38 | if not p.startswith("/"): |
| 39 | fail("file {} invalid: must begin with /".format(p)) |
| 40 | |
| 41 | # Get all intermediate directories on path to file |
| 42 | parts = p.split("/")[1:-1] |
| 43 | directories_needed.append(parts) |
| 44 | |
| 45 | # Extend with extra directories defined by user. |
| 46 | for p in ctx.attr.extra_dirs: |
| 47 | if not p.startswith("/"): |
| 48 | fail("directory {} invalid: must begin with /".format(p)) |
| 49 | |
| 50 | parts = p.split("/")[1:] |
| 51 | directories_needed.append(parts) |
| 52 | |
| 53 | directories = [] |
| 54 | for parts in directories_needed: |
| 55 | # Turn directory parts [usr, local, bin] into successive subpaths [/usr, /usr/local, /usr/local/bin]. |
| 56 | last = "" |
| 57 | for part in parts: |
| 58 | last += "/" + part |
| 59 | |
| 60 | # TODO(q3k): this is slow - this should be a set instead, but starlark doesn't implement them. |
| 61 | # For the amount of files we're dealing with this doesn't matter, but all stars are pointing towards this |
| 62 | # becoming accidentally quadratic at some point in the future. |
| 63 | if last not in directories: |
| 64 | directories.append(last) |
| 65 | |
| 66 | # Append instructions to create directories. |
| 67 | # Serendipitously, the directories should already be in the right order due to us not using a set to create the |
| 68 | # list. They might not be in an elegant order (ie, if files [/foo/one/one, /bar, /foo/two/two] are request, the |
| 69 | # order will be [/foo, /foo/one, /bar, /foo/two]), but that's fine. |
| 70 | for d in directories: |
| 71 | cpio_list_content.append("dir {} 0755 0 0".format(d)) |
| 72 | |
| 73 | # Append instructions to add files. |
| 74 | inputs = [] |
| 75 | for label, p in ctx.attr.files.items(): |
| 76 | # Figure out if this is an executable. |
| 77 | is_executable = True |
| 78 | |
| 79 | di = label[DefaultInfo] |
| 80 | if di.files_to_run.executable == None: |
| 81 | # Generated non-executable files will have DefaultInfo.files_to_run.executable == None |
| 82 | is_executable = False |
| 83 | elif di.files_to_run.executable.is_source: |
| 84 | # Source files will have executable.is_source == True |
| 85 | is_executable = False |
| 86 | |
| 87 | # Ensure only single output is declared. |
| 88 | # If you hit this error, figure out a better logic to find what file you need, maybe looking at providers other |
| 89 | # than DefaultInfo. |
| 90 | files = di.files.to_list() |
| 91 | if len(files) > 1: |
| 92 | fail("file {} has more than one output: {}", p, files) |
| 93 | src = files[0] |
| 94 | inputs.append(src) |
| 95 | |
| 96 | mode = "0755" if is_executable else "0444" |
| 97 | |
| 98 | cpio_list_content.append("file {} {} {} 0 0".format(p, src.path, mode)) |
| 99 | |
| 100 | # Write cpio_list. |
| 101 | ctx.actions.write(cpio_list, "\n".join(cpio_list_content)) |
| 102 | |
| 103 | gen_init_cpio = ctx.executable._gen_init_cpio |
| 104 | savestdout = ctx.executable._savestdout |
| 105 | lz4 = ctx.executable._lz4 |
| 106 | |
| 107 | # Generate 'raw' (uncompressed) initramfs |
| 108 | initramfs_raw_name = ctx.label.name |
| 109 | initramfs_raw = ctx.actions.declare_file(initramfs_raw_name) |
| 110 | ctx.actions.run( |
| 111 | outputs = [initramfs_raw], |
| 112 | inputs = [cpio_list] + inputs, |
| 113 | tools = [savestdout, gen_init_cpio], |
| 114 | executable = savestdout, |
| 115 | arguments = [initramfs_raw.path, gen_init_cpio.path, cpio_list.path], |
| 116 | ) |
| 117 | |
| 118 | # Compress raw initramfs using lz4c. |
| 119 | initramfs_name = ctx.label.name + ".lz4" |
| 120 | initramfs = ctx.actions.declare_file(initramfs_name) |
| 121 | ctx.actions.run( |
| 122 | outputs = [initramfs], |
| 123 | inputs = [initramfs_raw], |
| 124 | tools = [savestdout, lz4], |
| 125 | executable = lz4.path, |
| 126 | arguments = ["-l", initramfs_raw.path, initramfs.path], |
| 127 | ) |
| 128 | |
| 129 | return [DefaultInfo(files = depset([initramfs]))] |
| 130 | |
| 131 | smalltown_initramfs = rule( |
| 132 | implementation = _smalltown_initramfs_impl, |
| 133 | doc = """ |
| 134 | Build a Smalltown initramfs. The initramfs will contain a basic /dev directory and all the files specified by the |
| 135 | `files` attribute. Executable files will have their permissions set to 0755, non-executable files will have |
| 136 | their permissions set to 0444. All parent directories will be created with 0755 permissions. |
| 137 | """, |
| 138 | attrs = { |
| 139 | "files": attr.label_keyed_string_dict( |
| 140 | mandatory = True, |
| 141 | allow_files = True, |
| 142 | doc = """ |
| 143 | Dictionary of Labels to String, placing a given Label's output file in the initramfs at the location |
| 144 | specified by the String value. The specified labels must only have a single output. |
| 145 | """, |
| 146 | ), |
| 147 | "extra_dirs": attr.string_list( |
| 148 | default = [], |
| 149 | doc = """ |
| 150 | Extra directories to create. These will be created in addition to all the directories required to |
| 151 | contain the files specified in the `files` attribute. |
| 152 | """, |
| 153 | ), |
| 154 | |
| 155 | # Tools, implicit dependencies. |
| 156 | "_gen_init_cpio": attr.label( |
| 157 | default = Label("@linux//:gen_init_cpio"), |
| 158 | executable = True, |
| 159 | cfg = "host", |
| 160 | ), |
| 161 | "_lz4": attr.label( |
| 162 | default = Label("@com_github_lz4_lz4//programs:lz4"), |
| 163 | executable = True, |
| 164 | cfg = "host", |
| 165 | ), |
| 166 | "_savestdout": attr.label( |
| 167 | default = Label("//build/savestdout"), |
| 168 | executable = True, |
| 169 | cfg = "host", |
| 170 | ), |
| 171 | }, |
| 172 | ) |