How to create a file of the exact size of a directory (and its files), to hold a FAT32 filesystem?












0















I'm trying to create a file with the minimum-enough size to contain a FAT32 filesystem, which, in turn, mirrors the contents of a directory (an ESP structure). This is needed to create an UEFI-bootable ISO image.



I've managed to create the file successfully (the ISO image boots), but I've had to create it with a fixed size, and not the actual size of its contents.



Here's what I have done to achieve it:



BOOT_IMG_DATA=$(mktemp -d)
BOOT_IMG=$ISO_DIR/boot/efi.img

mkdir -p $(dirname $BOOT_IMG)

truncate -s 4M $BOOT_IMG
mkfs.vfat $BOOT_IMG
mount $BOOT_IMG $BOOT_IMG_DATA
mkdir -p $BOOT_IMG_DATA/efi/boot

grub-mkimage
-C xz
-O x86_64-efi
-p /boot/grub
-o $BOOT_IMG_DATA/efi/boot/bootx64.efi
boot linux search normal configfile
part_gpt btrfs fat iso9660 loopback
test keystatus gfxmenu regexp probe
efi_gop efi_uga all_video gfxterm font
echo read ls cat png jpeg halt reboot

umount $BOOT_IMG_DATA


That code is part of a script that generates an UEFI-bootable ISO image. The whole script is here: https://github.com/Nitrux/mkiso/blob/master/mkiso#L79-L100.



I need a way to create a file that will contain the ESP structure with the exact size to hold both the ESP data (directories and files) and the FAT32 metadada. How can I achieve this?










share|improve this question















migrated from stackoverflow.com Jan 4 at 3:28


This question came from our site for professional and enthusiast programmers.



















  • You mean like mkfs.vfat -C /path/to/image_to_create.img size where you specify the size you want for the image? You can then mount the image and copy the files to it as needed. You can do the same thing with dd and then call mkfs.vfat on the image itself.

    – David C. Rankin
    Jan 3 at 20:15











  • @DavidC.Rankin, what I need is the size parameter, in that case.

    – Luis Lavaire
    Jan 3 at 20:49











  • I would make a disk image, then shrink it after for archival.

    – juniorRubyist
    Jan 4 at 17:12
















0















I'm trying to create a file with the minimum-enough size to contain a FAT32 filesystem, which, in turn, mirrors the contents of a directory (an ESP structure). This is needed to create an UEFI-bootable ISO image.



I've managed to create the file successfully (the ISO image boots), but I've had to create it with a fixed size, and not the actual size of its contents.



Here's what I have done to achieve it:



BOOT_IMG_DATA=$(mktemp -d)
BOOT_IMG=$ISO_DIR/boot/efi.img

mkdir -p $(dirname $BOOT_IMG)

truncate -s 4M $BOOT_IMG
mkfs.vfat $BOOT_IMG
mount $BOOT_IMG $BOOT_IMG_DATA
mkdir -p $BOOT_IMG_DATA/efi/boot

grub-mkimage
-C xz
-O x86_64-efi
-p /boot/grub
-o $BOOT_IMG_DATA/efi/boot/bootx64.efi
boot linux search normal configfile
part_gpt btrfs fat iso9660 loopback
test keystatus gfxmenu regexp probe
efi_gop efi_uga all_video gfxterm font
echo read ls cat png jpeg halt reboot

umount $BOOT_IMG_DATA


That code is part of a script that generates an UEFI-bootable ISO image. The whole script is here: https://github.com/Nitrux/mkiso/blob/master/mkiso#L79-L100.



I need a way to create a file that will contain the ESP structure with the exact size to hold both the ESP data (directories and files) and the FAT32 metadada. How can I achieve this?










share|improve this question















migrated from stackoverflow.com Jan 4 at 3:28


This question came from our site for professional and enthusiast programmers.



















  • You mean like mkfs.vfat -C /path/to/image_to_create.img size where you specify the size you want for the image? You can then mount the image and copy the files to it as needed. You can do the same thing with dd and then call mkfs.vfat on the image itself.

    – David C. Rankin
    Jan 3 at 20:15











  • @DavidC.Rankin, what I need is the size parameter, in that case.

    – Luis Lavaire
    Jan 3 at 20:49











  • I would make a disk image, then shrink it after for archival.

    – juniorRubyist
    Jan 4 at 17:12














0












0








0








I'm trying to create a file with the minimum-enough size to contain a FAT32 filesystem, which, in turn, mirrors the contents of a directory (an ESP structure). This is needed to create an UEFI-bootable ISO image.



I've managed to create the file successfully (the ISO image boots), but I've had to create it with a fixed size, and not the actual size of its contents.



Here's what I have done to achieve it:



BOOT_IMG_DATA=$(mktemp -d)
BOOT_IMG=$ISO_DIR/boot/efi.img

mkdir -p $(dirname $BOOT_IMG)

truncate -s 4M $BOOT_IMG
mkfs.vfat $BOOT_IMG
mount $BOOT_IMG $BOOT_IMG_DATA
mkdir -p $BOOT_IMG_DATA/efi/boot

grub-mkimage
-C xz
-O x86_64-efi
-p /boot/grub
-o $BOOT_IMG_DATA/efi/boot/bootx64.efi
boot linux search normal configfile
part_gpt btrfs fat iso9660 loopback
test keystatus gfxmenu regexp probe
efi_gop efi_uga all_video gfxterm font
echo read ls cat png jpeg halt reboot

umount $BOOT_IMG_DATA


That code is part of a script that generates an UEFI-bootable ISO image. The whole script is here: https://github.com/Nitrux/mkiso/blob/master/mkiso#L79-L100.



I need a way to create a file that will contain the ESP structure with the exact size to hold both the ESP data (directories and files) and the FAT32 metadada. How can I achieve this?










share|improve this question
















I'm trying to create a file with the minimum-enough size to contain a FAT32 filesystem, which, in turn, mirrors the contents of a directory (an ESP structure). This is needed to create an UEFI-bootable ISO image.



I've managed to create the file successfully (the ISO image boots), but I've had to create it with a fixed size, and not the actual size of its contents.



Here's what I have done to achieve it:



BOOT_IMG_DATA=$(mktemp -d)
BOOT_IMG=$ISO_DIR/boot/efi.img

mkdir -p $(dirname $BOOT_IMG)

truncate -s 4M $BOOT_IMG
mkfs.vfat $BOOT_IMG
mount $BOOT_IMG $BOOT_IMG_DATA
mkdir -p $BOOT_IMG_DATA/efi/boot

grub-mkimage
-C xz
-O x86_64-efi
-p /boot/grub
-o $BOOT_IMG_DATA/efi/boot/bootx64.efi
boot linux search normal configfile
part_gpt btrfs fat iso9660 loopback
test keystatus gfxmenu regexp probe
efi_gop efi_uga all_video gfxterm font
echo read ls cat png jpeg halt reboot

umount $BOOT_IMG_DATA


That code is part of a script that generates an UEFI-bootable ISO image. The whole script is here: https://github.com/Nitrux/mkiso/blob/master/mkiso#L79-L100.



I need a way to create a file that will contain the ESP structure with the exact size to hold both the ESP data (directories and files) and the FAT32 metadada. How can I achieve this?







linux uefi fat32 sh






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 5:34







Luis Lavaire

















asked Jan 3 at 20:07









Luis LavaireLuis Lavaire

216




216




migrated from stackoverflow.com Jan 4 at 3:28


This question came from our site for professional and enthusiast programmers.









migrated from stackoverflow.com Jan 4 at 3:28


This question came from our site for professional and enthusiast programmers.















  • You mean like mkfs.vfat -C /path/to/image_to_create.img size where you specify the size you want for the image? You can then mount the image and copy the files to it as needed. You can do the same thing with dd and then call mkfs.vfat on the image itself.

    – David C. Rankin
    Jan 3 at 20:15











  • @DavidC.Rankin, what I need is the size parameter, in that case.

    – Luis Lavaire
    Jan 3 at 20:49











  • I would make a disk image, then shrink it after for archival.

    – juniorRubyist
    Jan 4 at 17:12



















  • You mean like mkfs.vfat -C /path/to/image_to_create.img size where you specify the size you want for the image? You can then mount the image and copy the files to it as needed. You can do the same thing with dd and then call mkfs.vfat on the image itself.

    – David C. Rankin
    Jan 3 at 20:15











  • @DavidC.Rankin, what I need is the size parameter, in that case.

    – Luis Lavaire
    Jan 3 at 20:49











  • I would make a disk image, then shrink it after for archival.

    – juniorRubyist
    Jan 4 at 17:12

















You mean like mkfs.vfat -C /path/to/image_to_create.img size where you specify the size you want for the image? You can then mount the image and copy the files to it as needed. You can do the same thing with dd and then call mkfs.vfat on the image itself.

– David C. Rankin
Jan 3 at 20:15





You mean like mkfs.vfat -C /path/to/image_to_create.img size where you specify the size you want for the image? You can then mount the image and copy the files to it as needed. You can do the same thing with dd and then call mkfs.vfat on the image itself.

– David C. Rankin
Jan 3 at 20:15













@DavidC.Rankin, what I need is the size parameter, in that case.

– Luis Lavaire
Jan 3 at 20:49





@DavidC.Rankin, what I need is the size parameter, in that case.

– Luis Lavaire
Jan 3 at 20:49













I would make a disk image, then shrink it after for archival.

– juniorRubyist
Jan 4 at 17:12





I would make a disk image, then shrink it after for archival.

– juniorRubyist
Jan 4 at 17:12










1 Answer
1






active

oldest

votes


















0














The right tool for this task is du.



mkfs.vfat -C $BOOT_IMG 
$(( ($(du -b $BOOT_IMG_DATA | tail -n 1 | awk '{ print $1 }') / 1024 + 511) / 1024 ))


That command will create the file $BOOT_IMG, which will have enough size to keep the contents of $BOOT_IMG_DATA.






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1390369%2fhow-to-create-a-file-of-the-exact-size-of-a-directory-and-its-files-to-hold-a%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The right tool for this task is du.



    mkfs.vfat -C $BOOT_IMG 
    $(( ($(du -b $BOOT_IMG_DATA | tail -n 1 | awk '{ print $1 }') / 1024 + 511) / 1024 ))


    That command will create the file $BOOT_IMG, which will have enough size to keep the contents of $BOOT_IMG_DATA.






    share|improve this answer




























      0














      The right tool for this task is du.



      mkfs.vfat -C $BOOT_IMG 
      $(( ($(du -b $BOOT_IMG_DATA | tail -n 1 | awk '{ print $1 }') / 1024 + 511) / 1024 ))


      That command will create the file $BOOT_IMG, which will have enough size to keep the contents of $BOOT_IMG_DATA.






      share|improve this answer


























        0












        0








        0







        The right tool for this task is du.



        mkfs.vfat -C $BOOT_IMG 
        $(( ($(du -b $BOOT_IMG_DATA | tail -n 1 | awk '{ print $1 }') / 1024 + 511) / 1024 ))


        That command will create the file $BOOT_IMG, which will have enough size to keep the contents of $BOOT_IMG_DATA.






        share|improve this answer













        The right tool for this task is du.



        mkfs.vfat -C $BOOT_IMG 
        $(( ($(du -b $BOOT_IMG_DATA | tail -n 1 | awk '{ print $1 }') / 1024 + 511) / 1024 ))


        That command will create the file $BOOT_IMG, which will have enough size to keep the contents of $BOOT_IMG_DATA.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 5 hours ago









        Luis LavaireLuis Lavaire

        216




        216






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1390369%2fhow-to-create-a-file-of-the-exact-size-of-a-directory-and-its-files-to-hold-a%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Plaza Victoria

            Brian Clough

            Cáceres