MCPcopy Create free account
hub / github.com/ClangBuiltLinux/boot-utils / prepare_initrd

Function prepare_initrd

utils.py:183–263  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 4

get_gh_jsonFunction · 0.85
download_initrdFunction · 0.85
check_cmdFunction · 0.85
runMethod · 0.45

Tested by

no test coverage detected