-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support VirtualBox: Ensure that binary size is a multiple of 512 bytes #35
Comments
@phil-opp What needs to be changed? P.S. I think it is reasonable to expect the size of a disk image to be a multiple of the minimum block size. |
Yes, it's absolutely reasonable, it's just not what Lines 283 to 298 in c2ce530
To fix this we need to either find some |
There are 2 options for |
Hmm, Given that the result is a binary file, it's probably easier to just open it afterwards and using |
@phil-opp Would something like this work? I can't test it now. // Pad to nearest block size
{
// BLOCK_SIZE must be a power of 2
const BLOCK_SIZE = 512;
use std::fs::{OpenOptions, File};
let mut file = OpenOptions::new().append(true).open(&output_bin_path).map_err(|err| CreateBootimageError::Io {
message: "failed to open output file",
error: err,
})?;
let file_size = file.metadata().map_err(|err| CreateBootimageError::Io {
message: "failed to get size of output file",
error: err,
})?.len();
file.set_len((file_size + BLOCK_SIZE - 1) & -BLOCK_SIZE).map_err(|err| CreateBootimageError::Io {
message: "failed to pad output file to a multiple of the block size",
error: err,
})?;
} |
Yes, I think so. One nit: I would prefer a different approach for aligning so that it becomes more apparent that we never make the file length smaller. Maybe something like |
@phil-opp How about this? let padding = file_size % BLOCK_SIZE;
let padding = if padding > 0 { BLOCK_SIZE - padding } else { 0 };
file.set_len(file_size + padding).map_err(_)?; Actually, since let padding = file_size & -BLOCK_SIZE; |
Looks good! Do you mind opening a pull request?
I think the compiler should be able to perform this optimization automatically. I like the first variant more because it is more obvious what happens. (Performance shouldn't be relevant at this place anyway since it's clearly IO-bound. |
@phil-opp I created a pull request. |
For some reason VirtualBox expects that disk images are a multiple of 512 bytes. We had an option to add the required padding in an old version of
bootimage
but we haven't ported that option to the new build system yet.Reported in phil-opp/blog_os#403 (comment)
The text was updated successfully, but these errors were encountered: