grub2.inst: Add support for creating iso9660 boot image
Don't include the "location" offset, and use just a path for the prefix
section to set the path of the grub2 modules on the iso. eg.
{
"filename": "eltorito.img",
"platform": "i386-pc",
"core": {
"type": "mkimage",
"partlabel": "gpt",
"filesystem": "iso9660"
},
"prefix": {
"path": "/boot/grub2/"
}
}
This commit is contained in:
parent
41c0550a1f
commit
3bed7c7ace
3 changed files with 70 additions and 17 deletions
|
|
@ -70,11 +70,11 @@ def write_core_image(core_f, image_f, location, sector_size):
|
||||||
shutil.copyfileobj(core_f, image_f)
|
shutil.copyfileobj(core_f, image_f)
|
||||||
|
|
||||||
|
|
||||||
def core_mkimage(platform: str, prefix: str, options: Dict,):
|
def core_mkimage(platform: str, prefix: str, options: Dict):
|
||||||
pt_label = options["partlabel"]
|
pt_label = options["partlabel"]
|
||||||
fs_type = options["filesystem"]
|
fs_type = options["filesystem"]
|
||||||
|
|
||||||
os.makedirs("/var/tmp", exist_ok=True)
|
os.makedirs("/var/tmp/", exist_ok=True)
|
||||||
core_path = "/var/tmp/grub2-core.img"
|
core_path = "/var/tmp/grub2-core.img"
|
||||||
|
|
||||||
# Create the level-2 & 3 stages of the bootloader, aka the core
|
# Create the level-2 & 3 stages of the bootloader, aka the core
|
||||||
|
|
@ -88,8 +88,10 @@ def core_mkimage(platform: str, prefix: str, options: Dict,):
|
||||||
|
|
||||||
if platform == "i386-pc":
|
if platform == "i386-pc":
|
||||||
modules = ["biosdisk"]
|
modules = ["biosdisk"]
|
||||||
|
gformat = "i386-pc"
|
||||||
else:
|
else:
|
||||||
modules = []
|
modules = []
|
||||||
|
gformat = "i386-pc"
|
||||||
|
|
||||||
if pt_label in ["dos", "mbr"]:
|
if pt_label in ["dos", "mbr"]:
|
||||||
modules += ["part_msdos"]
|
modules += ["part_msdos"]
|
||||||
|
|
@ -102,6 +104,9 @@ def core_mkimage(platform: str, prefix: str, options: Dict,):
|
||||||
modules += ["xfs"]
|
modules += ["xfs"]
|
||||||
elif fs_type == "btrfs":
|
elif fs_type == "btrfs":
|
||||||
modules += ["btrfs"]
|
modules += ["btrfs"]
|
||||||
|
elif fs_type == "iso9660":
|
||||||
|
modules += ["iso9660"]
|
||||||
|
gformat = "i386-pc-eltorito"
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"unknown boot filesystem type: '{fs_type}'")
|
raise ValueError(f"unknown boot filesystem type: '{fs_type}'")
|
||||||
|
|
||||||
|
|
@ -110,7 +115,7 @@ def core_mkimage(platform: str, prefix: str, options: Dict,):
|
||||||
"--verbose",
|
"--verbose",
|
||||||
"--directory", f"/usr/lib/grub/{platform}",
|
"--directory", f"/usr/lib/grub/{platform}",
|
||||||
"--prefix", prefix,
|
"--prefix", prefix,
|
||||||
"--format", platform,
|
"--format", gformat,
|
||||||
"--compression", "auto",
|
"--compression", "auto",
|
||||||
"--output", core_path] +
|
"--output", core_path] +
|
||||||
modules,
|
modules,
|
||||||
|
|
@ -133,18 +138,7 @@ def prefix_partition(options: Dict):
|
||||||
return prefix
|
return prefix
|
||||||
|
|
||||||
|
|
||||||
def main(tree, options):
|
def patch_core(location, core_path, image, sector_size, platform):
|
||||||
filename = options["filename"]
|
|
||||||
platform = options["platform"]
|
|
||||||
location = options["location"]
|
|
||||||
sector_size = options.get("sector-size", 512)
|
|
||||||
|
|
||||||
image = os.path.join(tree, filename.lstrip("/"))
|
|
||||||
|
|
||||||
prefix = prefix_partition(options["prefix"])
|
|
||||||
print(f"prefix: {prefix}")
|
|
||||||
core_path = core_mkimage(platform, prefix, options["core"])
|
|
||||||
|
|
||||||
with open(image, "rb+") as image_f:
|
with open(image, "rb+") as image_f:
|
||||||
|
|
||||||
# Write the newly created grub2 core to the image
|
# Write the newly created grub2 core to the image
|
||||||
|
|
@ -168,6 +162,28 @@ def main(tree, options):
|
||||||
with open(boot_path, "rb") as boot_f:
|
with open(boot_path, "rb") as boot_f:
|
||||||
write_boot_image(boot_f, image_f, location)
|
write_boot_image(boot_f, image_f, location)
|
||||||
|
|
||||||
|
|
||||||
|
def main(tree, options):
|
||||||
|
filename = options["filename"]
|
||||||
|
platform = options["platform"]
|
||||||
|
sector_size = options.get("sector-size", 512)
|
||||||
|
|
||||||
|
image = os.path.join(tree, filename.lstrip("/"))
|
||||||
|
|
||||||
|
if "number" in options["prefix"]:
|
||||||
|
prefix = prefix_partition(options["prefix"])
|
||||||
|
else:
|
||||||
|
prefix = options["prefix"]["path"]
|
||||||
|
print(f"prefix: {prefix}")
|
||||||
|
core_path = core_mkimage(platform, prefix, options["core"])
|
||||||
|
|
||||||
|
location = options.get("location")
|
||||||
|
if location:
|
||||||
|
patch_core(location, core_path, image, sector_size, platform)
|
||||||
|
else:
|
||||||
|
# If location isn't set, use the image file as-is instead of with the MBR
|
||||||
|
shutil.copyfile(core_path, image)
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,8 @@
|
||||||
"enum": [
|
"enum": [
|
||||||
"ext4",
|
"ext4",
|
||||||
"xfs",
|
"xfs",
|
||||||
"btrfs"
|
"btrfs",
|
||||||
|
"iso9660"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"binary": {
|
"binary": {
|
||||||
|
|
@ -69,6 +70,21 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"prefix-path": {
|
||||||
|
"type": "object",
|
||||||
|
"description": "Grub2 config path on iso9660 eg. /boot/grub2",
|
||||||
|
"additionalProperties": false,
|
||||||
|
"required": [
|
||||||
|
"path"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"path": {
|
||||||
|
"description": "location of grub config inside the partition",
|
||||||
|
"type": "string",
|
||||||
|
"pattern": "/.*"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"prefix-partition": {
|
"prefix-partition": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"description": "Grub2 config on a specific partition, e.g. (,gpt3)/boot",
|
"description": "Grub2 config on a specific partition, e.g. (,gpt3)/boot",
|
||||||
|
|
@ -108,7 +124,6 @@
|
||||||
"required": [
|
"required": [
|
||||||
"filename",
|
"filename",
|
||||||
"platform",
|
"platform",
|
||||||
"location",
|
|
||||||
"core",
|
"core",
|
||||||
"prefix"
|
"prefix"
|
||||||
],
|
],
|
||||||
|
|
@ -138,6 +153,9 @@
|
||||||
"oneOf": [
|
"oneOf": [
|
||||||
{
|
{
|
||||||
"$ref": "#/definitions/prefix-partition"
|
"$ref": "#/definitions/prefix-partition"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"$ref": "#/definitions/prefix-path"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -31,3 +31,22 @@ def test_grub2_partition(tmp_path, stage_module):
|
||||||
with open(treedir / "disk.img", "rb") as f:
|
with open(treedir / "disk.img", "rb") as f:
|
||||||
msg = f.read(12)
|
msg = f.read(12)
|
||||||
assert msg != b"Just testing"
|
assert msg != b"Just testing"
|
||||||
|
|
||||||
|
def test_grub2_iso9660(tmp_path, stage_module):
|
||||||
|
treedir = tmp_path / "tree"
|
||||||
|
os.makedirs(treedir, exist_ok=True)
|
||||||
|
|
||||||
|
options = {
|
||||||
|
"filename": "eltorito.img",
|
||||||
|
"platform": "i386-pc",
|
||||||
|
"core": {
|
||||||
|
"type": "mkimage",
|
||||||
|
"partlabel": "gpt",
|
||||||
|
"filesystem": "iso9660",
|
||||||
|
},
|
||||||
|
"prefix": {
|
||||||
|
"path": "/boot/grub2/",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
stage_module.main(treedir, options)
|
||||||
|
assert os.path.exists(treedir / "eltorito.img")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue