Skip to content

Commit 5e11225

Browse files
committed
grubify: automatically determine appropriate image size
1 parent 7b6ea22 commit 5e11225

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

etc/scripts/grubify.sh

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
KERNEL=${1-build/test_grub}
33
DISK=${DISK-$KERNEL.grub.img}
44
MOUNTDIR=${MOUNTDIR-/mnt}
5-
MOUNT_OPTS="sync,rw"
6-
BLOCKCCOUNT=10000
5+
MOUNT_OPTS="rw"
6+
7+
# GRUB uses roughly 4.6 Mb of disk
8+
GRUB_KB=5000
9+
710
set -e
811

912
function unmount {
@@ -23,18 +26,25 @@ function clean {
2326

2427

2528
function create_disk {
26-
echo -e ">>> Creating FAT file system on $DISK with $BLOCKSIZE blocks"
27-
# NOTE: mkfs with the '-C' option creates the disk before creating FS
28-
# fallocate -l 10MiB $DISK
2929

3030
if [ -f $DISK ]
3131
then
3232
echo -e ">>> $DISK allready exists. Preserving existing image as $DISK.bak"
3333
mv $DISK $DISK.bak
3434
fi
3535

36+
# Kernel size in Kb
37+
KERN_KB=$(( ($(stat -c%s "$KERNEL") / 1024) ))
38+
39+
# Estimate some overhead for the FAT
40+
FAT_KB=$(( ($KERN_KB + $GRUB_KB) / 10 ))
3641

37-
mkfs.fat -C $DISK $BLOCKCCOUNT
42+
DISK_KB=$(( $KERN_KB + $GRUB_KB + $FAT_KB ))
43+
44+
echo ">>> Estimated disk size: $GRUB_KB Kb GRUB + $KERN_KB Kb kernel + $FAT_KB Kb FAT = $DISK_KB Kb"
45+
echo ">>> Creating FAT file system on $DISK"
46+
47+
mkfs.fat -C $DISK $DISK_KB
3848
}
3949

4050
function mount_loopback {
@@ -58,6 +68,7 @@ function mount_loopback {
5868
function copy_kernel {
5969
echo ">>> Copying kernel '$KERNEL' to $MOUNTDIR/boot/includeos_service"
6070
sudo cp $KERNEL $MOUNTDIR/boot/includeos_service
71+
sync
6172
}
6273

6374
function build {
@@ -130,6 +141,9 @@ copy_kernel
130141
echo -e ">>> Running grub install"
131142
sudo grub-install --target=i386-pc --force --boot-directory $MOUNTDIR/boot/ $LOOP
132143

144+
echo -e ">>> Synchronize file cache"
145+
sync
146+
133147
unmount
134148

135149
echo -e ">>> Done. You can now boot $DISK"

0 commit comments

Comments
 (0)