-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
virtme-ng: use pre-compiled mainline kernels
Allow to download precompiled mainline kernels from the Ubuntu mainline kernel repository (https://kernel.ubuntu.com/mainline). Mainline builds are provided by Ubuntu for debugging purposes, they are basically vanilla kernels (no additional patches applied), built with the generic Ubuntu .config and packaged as deb. These precompiled kernels can be used by virtme-ng to test specific mainline tags, without having to rebuild the kernel from source. To do so the option `--run` can now accept a Linux tag (i.e., v6.6-rc2). When a tag is specified, virtme-ng will search in the Ubuntu mainline repository and fetch the corresponding packages, if available. Packages are then cached and extracted inside $HOME/.cache/virtme-ng, so they just need to be downloaded the first time that a mainline tag is requested. Example usage: $ vng -r v6.6-rc2 -- uname -r 6.6.0-060600rc2-generic This allows to save even more time (and energy) when testing mainline kernel versions, completely cutting out the kernel rebuild time. Signed-off-by: Andrea Righi <andrea.righi@canonical.com>
- Loading branch information
Andrea Righi
committed
Nov 19, 2023
1 parent
4c65839
commit 6fec168
Showing
3 changed files
with
94 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# -*- mode: python -*- | ||
# Copyright 2023 Andrea Righi | ||
|
||
"""virtme-ng: mainline kernel downloader.""" | ||
|
||
import os | ||
import re | ||
import sys | ||
import subprocess | ||
from glob import glob | ||
import requests | ||
from virtme_ng.utils import CACHE_DIR, spinner_decorator | ||
|
||
BASE_URL = "https://kernel.ubuntu.com/mainline" | ||
|
||
HTTP_CHUNK = 4096 | ||
HTTP_TIMEOUT = 30 | ||
|
||
|
||
class KernelDownloader: | ||
def __init__(self, version, arch="amd64", verbose=False): | ||
# Initialize cache directory | ||
self.kernel_dir = f"{CACHE_DIR}/{version}/{arch}" | ||
os.makedirs(self.kernel_dir, exist_ok=True) | ||
|
||
# Fetch and extract precompiled mainline kernel | ||
self.target = f"{CACHE_DIR}/{version}/{arch}/boot/vmlinuz-*" | ||
if not glob(self.target): | ||
url = BASE_URL + "/" + version + "/" + arch | ||
if verbose: | ||
sys.stderr.write(f"use {version}/{arch} pre-compiled kernel from {url}\n") | ||
self._fetch_kernel(url) | ||
|
||
def _download_file(self, url, destination): | ||
response = requests.get(url, stream=True, timeout=HTTP_TIMEOUT) | ||
if response.status_code == 200: | ||
with open(destination, 'wb') as file: | ||
for chunk in response.iter_content(chunk_size=HTTP_CHUNK): | ||
file.write(chunk) | ||
else: | ||
raise FileNotFoundError(f"failed to download {url}, error: {response.status_code}") | ||
|
||
@spinner_decorator(message="📥 downloading kernel") | ||
def _fetch_kernel(self, url): | ||
response = requests.get(url, timeout=HTTP_TIMEOUT) | ||
if response.status_code == 200: | ||
href_pattern = re.compile(r'href=["\']([^\s"\']+.deb)["\']') | ||
matches = href_pattern.findall(response.text) | ||
for match in matches: | ||
# Skip headers packages | ||
if 'headers' in match: | ||
continue | ||
# Skip if package is already downloaded | ||
deb_file = f"{self.kernel_dir}/{match}" | ||
if os.path.exists(deb_file): | ||
continue | ||
self._download_file(url + "/" + match, deb_file) | ||
subprocess.check_call(['dpkg', '-x', deb_file, self.kernel_dir]) | ||
if not glob(f"{self.kernel_dir}/*.deb"): | ||
raise FileNotFoundError(f"could not find kernel packages at {url}") | ||
else: | ||
raise FileNotFoundError(f"failed to retrieve content, error: {response.status_code}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters