builder: disk: add image file supports

Signed-off-by: BigfootACA <bigfoot@classfun.cn>
This commit is contained in:
BigfootACA 2024-05-23 17:15:18 +08:00
parent 8baedd5358
commit 84d8a0c247
2 changed files with 29 additions and 0 deletions

27
builder/disk/file.py Normal file
View File

@ -0,0 +1,27 @@
import os
from logging import getLogger
from builder.disk.content import ImageContentBuilder
from builder.lib.config import ArchBuilderConfigError
log = getLogger(__name__)
class ImageFileBuilder(ImageContentBuilder):
def build(self):
cmds = ["dd"]
ctx = self.builder.ctx
cfg = self.builder.config
if "file" not in cfg:
raise ArchBuilderConfigError("file not set")
file: str = cfg["file"]
if file.startswith("/"): file = file[1:]
path = os.path.join(ctx.get_rootfs(), file)
if not os.path.exists(path):
raise FileNotFoundError(f"image file {path} not found")
cmds.append("status=progress")
cmds.append(f"if={path}")
cmds.append(f"of={self.builder.device}")
cmds.append(f"bs={self.builder.sector}")
log.info(f"start writing image file {path}")
ret = ctx.run_external(cmds)
if ret != 0: raise OSError("dd failed")
log.info(f"write image file {path} done")

View File

@ -2,6 +2,7 @@ from builder.disk.content import ImageContentBuilder
from builder.disk.layout.build import DiskLayoutBuilder
from builder.disk.filesystem.build import FileSystemBuilder
from builder.disk.abootimg import AndroidBootBuilder
from builder.disk.file import ImageFileBuilder
types: list[tuple[str, type[ImageContentBuilder]]] = [
@ -9,4 +10,5 @@ types: list[tuple[str, type[ImageContentBuilder]]] = [
("filesystem", FileSystemBuilder),
("aboot", AndroidBootBuilder),
("avndboot", AndroidBootBuilder),
("image", ImageFileBuilder),
]