Skip to content

Commit db2f873

Browse files
committed
Support resizing output image
When the final image does not contain the rootfs it must be grown before being able to boot it. Since the exact size to grow is not easily known to the user, it's best if the image is already grown to the right size. Also, when flashing to disk it helps to have the image be the minimum size needed because otherwise flashing on a too small disk succeeds but boot will fail. With the right image size it's easier for the user to know whether the disk is large enough.
1 parent 5901524 commit db2f873

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

mkosi/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4129,6 +4129,17 @@ def build_image(context: Context) -> None:
41294129
elif context.config.output_format == OutputFormat.directory:
41304130
context.root.rename(context.staging / context.config.output_with_format)
41314131

4132+
if context.config.output_size and context.config.output_format == OutputFormat.disk:
4133+
output = context.staging / context.config.output_with_format
4134+
current_size = output.stat().st_size
4135+
if context.config.output_size > current_size:
4136+
with complete_step(f"Resizing output image to {format_bytes(context.config.output_size)}…"):
4137+
os.truncate(output, context.config.output_size)
4138+
else:
4139+
log_notice(
4140+
f"Can't change the output size because the image is already larger ({format_bytes(current_size)}) than requested" # noqa: E501
4141+
)
4142+
41324143
if context.config.output_format.use_outer_compression():
41334144
maybe_compress(
41344145
context,

mkosi/config.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,6 +2023,7 @@ class Config:
20232023
manifest_format: list[ManifestFormat]
20242024
output: str
20252025
output_extension: str
2026+
output_size: Optional[int]
20262027
compress_output: Compression
20272028
compress_level: int
20282029
output_dir: Optional[Path]
@@ -2832,6 +2833,13 @@ def parse_kernel_module_filter_regexp(p: str) -> str:
28322833
default_factory=lambda ns: ns["output_format"].extension(),
28332834
default_factory_depends=("output_format",),
28342835
),
2836+
ConfigSetting(
2837+
dest="output_size",
2838+
metavar="SIZE",
2839+
section="Output",
2840+
parse=config_parse_bytes,
2841+
help="Resize the output image to this size",
2842+
),
28352843
ConfigSetting(
28362844
dest="compress_output",
28372845
metavar="ALG",
@@ -5627,6 +5635,7 @@ def summary(config: Config) -> str:
56275635
Output Format: {config.output_format}
56285636
Manifest Formats: {maniformats}
56295637
Output: {bold(config.output_with_compression)}
5638+
Output Size: {format_bytes_or_none(config.output_size)}
56305639
Compression: {config.compress_output}
56315640
Compression Level: {config.compress_level}
56325641
Output Directory: {config.output_dir_or_cwd()}

mkosi/resources/man/mkosi.1.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,12 @@ boolean argument: either `1`, `yes`, or `true` to enable, or `0`, `no`,
644644
any compression extension which will be appended to this extension if compression
645645
is enabled.
646646

647+
`OutputSize=`, `--output-size=`
648+
: If set, the output disk image is resized to the given size. Takes a size in
649+
bytes. The suffixes `K`, `M` and `G` can be used to specify the size in kilobytes,
650+
megabytes and gigabytes. This setting only grows the disk and is limited to full
651+
disk images and no other output format.
652+
647653
`CompressOutput=`, `--compress-output=`
648654
: Configure compression for the resulting image or archive. The argument can be
649655
either a boolean or a compression algorithm (**xz**, **zstd**). **zstd**

tests/test_json.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ def test_config() -> None:
268268
"OutputDirectory": "/your/output/here",
269269
"OutputExtension": "raw",
270270
"OutputMode": 83,
271+
"OutputSize": null,
271272
"Overlay": true,
272273
"PackageCacheDirectory": "/a/b/c",
273274
"PackageDirectories": [],
@@ -519,6 +520,7 @@ def test_config() -> None:
519520
openpgp_tool="gpg",
520521
output_dir=Path("/your/output/here"),
521522
output_extension="raw",
523+
output_size=None,
522524
output_format=OutputFormat.uki,
523525
output_mode=0o123,
524526
output="outfile",

0 commit comments

Comments
 (0)