Skip to content

Commit 584840a

Browse files
committed
towbootctl: Rework calculation of image size
The image size is now a multiple of 512 bytes and there is slightly less empty space.
1 parent 29f18cb commit 584840a

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

towbootctl/src/image.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@ impl Image {
2626
.create(true)
2727
.truncate(true)
2828
.open(path)?);
29+
// make sure the image size is a multiple of 512
30+
// gdisk warns otherwise and OVMF seems to ignore the disk in some cases
31+
// also, keep a spare megabyte for the partition table and alignment
32+
let size = size.next_multiple_of(512) + 1024 * 1024;
2933
file.set_len(size)?;
3034
// protective MBR
3135
let mbr = ProtectiveMBR::with_lb_size(
32-
u32::try_from((size / 512) - 1).unwrap_or(0xFF_FF_FF_FF)
36+
u32::try_from((size / 512) - 1)?
3337
);
3438
mbr.overwrite_lba0(&mut file)?;
3539
let mut disk = GptConfig::new()
@@ -38,7 +42,10 @@ impl Image {
3842
.create_from_device(file, None)?;
3943
disk.update_partitions(BTreeMap::new())?;
4044
debug!("creating partition");
41-
disk.add_partition("towboot", size - 1024 * 1024, partition_types::EFI, 0, None)?;
45+
// the partition needs to be slightly smaller than the disk image
46+
disk.add_partition(
47+
"towboot", size - 64 * 1024, partition_types::EFI, 0, None,
48+
)?;
4249
let partitions = disk.partitions().clone();
4350
let (_, partition) = partitions.iter().next().unwrap();
4451
let file = disk.write()?;

towbootctl/src/lib.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ mod image;
2121
use bochs::bochsrc;
2222
use image::Image;
2323

24-
/// How big the image should be
25-
pub const DEFAULT_IMAGE_SIZE: u64 = 50*1024*1024;
26-
2724
/// Where to place the 32-bit EFI file
2825
pub const IA32_BOOT_PATH: &str = "EFI/Boot/bootia32.efi";
2926

@@ -98,15 +95,19 @@ pub fn create_image(
9895
paths.push((PathBuf::from(src), PathBuf::from(X64_BOOT_PATH)));
9996
}
10097

101-
let mut image_size = 0x00_20_00_00;
98+
let mut image_size = 0;
10299
for pair in paths.iter() {
103100
let file = OpenOptions::new()
104101
.read(true)
105102
.open(PathBuf::from(&pair.0))?;
106103
image_size += file.metadata()?.len();
107104
}
108105

109-
info!("creating image at {} (size: {} MiB)", target.display(), image_size / 1024 / 1024);
106+
info!(
107+
"creating image at {} (size: {} MiB)",
108+
target.display(),
109+
image_size.div_ceil(1024).div_ceil(1024),
110+
);
110111
let mut image = Image::new(target, image_size)?;
111112
for pair in paths {
112113
image.add_file(pair.0.as_path(), pair.1.as_path())?

0 commit comments

Comments
 (0)