Returns a decompressed initial ramdisk. Parameters: architecture (str): Architecture to download image for. rootfs_format (str): Initrd format ('cpio' or 'ext4')
(
architecture: str,
rootfs_format: str = 'cpio',
gh_json_file: Path | None = None,
modules: Path | None = None,
)
| 181 | |
| 182 | |
| 183 | def prepare_initrd( |
| 184 | architecture: str, |
| 185 | rootfs_format: str = 'cpio', |
| 186 | gh_json_file: Path | None = None, |
| 187 | modules: Path | None = None, |
| 188 | ) -> Path: |
| 189 | """ |
| 190 | Returns a decompressed initial ramdisk. |
| 191 | |
| 192 | Parameters: |
| 193 | architecture (str): Architecture to download image for. |
| 194 | rootfs_format (str): Initrd format ('cpio' or 'ext4') |
| 195 | """ |
| 196 | src = Path(BOOT_UTILS, 'images', architecture, f"rootfs.{rootfs_format}.zst") |
| 197 | src.parent.mkdir(exist_ok=True, parents=True) |
| 198 | |
| 199 | # If the user supplied a GitHub release JSON file, we do not need to bother |
| 200 | # querying the GitHub API at all. |
| 201 | if gh_json_file and gh_json_file != UNINIT_PATH: |
| 202 | if not gh_json_file.exists(): |
| 203 | msg = f"Provided GitHub JSON file ('{gh_json_file}') does not exist!" |
| 204 | raise FileNotFoundError(msg) |
| 205 | gh_json_rel = json.loads(gh_json_file.read_text(encoding='utf-8')) |
| 206 | else: |
| 207 | # Make sure that the current user is not rate limited by GitHub, |
| 208 | # otherwise the next API call will not return valid information. |
| 209 | gh_json_rl = get_gh_json('https://api.github.com/rate_limit') |
| 210 | |
| 211 | # If we have API calls remaining or have already queried the API previously |
| 212 | # and cached the result, we can query for the latest release to make sure |
| 213 | # that we are up to date. |
| 214 | if (remaining := gh_json_rl['resources']['core']['remaining']) > 0: |
| 215 | gh_json_rel = get_gh_json(f"https://api.github.com/repos/{REPO}/releases/latest") |
| 216 | elif not src.exists(): |
| 217 | limit = gh_json_rl['resources']['core']['limit'] |
| 218 | msg = ( |
| 219 | f"Cannot query GitHub API for latest images release due to rate limit (remaining: {remaining}, limit: {limit}) and {src} does not exist already! " |
| 220 | 'Download it manually or supply a GitHub personal access token via the GITHUB_TOKEN environment variable to make an authenticated GitHub API request.' |
| 221 | ) |
| 222 | raise RuntimeError(msg) |
| 223 | |
| 224 | # Download the ramdisk if it is not already downloaded |
| 225 | if not src.exists(): |
| 226 | # gh_json_rel cannot be unset when used here because the elif condition |
| 227 | # above is the same as this one, which causes the script to exit. |
| 228 | # pylint: disable-next=possibly-used-before-assignment |
| 229 | download_initrd(gh_json_rel, src) |
| 230 | # If it is already downloaded, check that it is up to date and download |
| 231 | # an update only if necessary. |
| 232 | elif (rel_file := src.with_name('.release')).exists(): |
| 233 | cur_rel = rel_file.read_text(encoding='utf-8') |
| 234 | supplied_rel = gh_json_rel['tag_name'] |
| 235 | if cur_rel != supplied_rel: |
| 236 | download_initrd(gh_json_rel, src) |
| 237 | |
| 238 | check_cmd('zstd') |
| 239 | (dst := src.with_suffix('')).unlink(missing_ok=True) |
| 240 | subprocess.run(['zstd', '-d', src, '-o', dst, '-q'], check=True) |
nothing calls this directly
no test coverage detected