How to run mkfs on file image partitions without mounting?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
5
down vote

favorite












I am creating an empty file...



dd if=/dev/zero of=$SDCARD bs=1 count=0 seek=$(expr 1024 * $SDCARD_SIZE)


...then turning it into an drive image...



parted -s $SDCARD mklabel msdos


...and creating partitions on it



parted -s $SDCARD unit KiB mkpart primary fat32 $IMAGE_ROOTFS_ALIGNMENT $(expr $IMAGE_ROOTFS_ALIGNMENT + $BOOT_SPACE_ALIGNED)
parted -s $SDCARD unit KiB mkpart primary $(expr $IMAGE_ROOTFS_ALIGNMENT + $BOOT_SPACE_ALIGNED) $(expr $IMAGE_ROOTFS_ALIGNMENT + $BOOT_SPACE_ALIGNED + $ROOTFS_SIZE)


How do I use mkfs.ext and mkfs.vfat without mounting this image?










share|improve this question



























    up vote
    5
    down vote

    favorite












    I am creating an empty file...



    dd if=/dev/zero of=$SDCARD bs=1 count=0 seek=$(expr 1024 * $SDCARD_SIZE)


    ...then turning it into an drive image...



    parted -s $SDCARD mklabel msdos


    ...and creating partitions on it



    parted -s $SDCARD unit KiB mkpart primary fat32 $IMAGE_ROOTFS_ALIGNMENT $(expr $IMAGE_ROOTFS_ALIGNMENT + $BOOT_SPACE_ALIGNED)
    parted -s $SDCARD unit KiB mkpart primary $(expr $IMAGE_ROOTFS_ALIGNMENT + $BOOT_SPACE_ALIGNED) $(expr $IMAGE_ROOTFS_ALIGNMENT + $BOOT_SPACE_ALIGNED + $ROOTFS_SIZE)


    How do I use mkfs.ext and mkfs.vfat without mounting this image?










    share|improve this question

























      up vote
      5
      down vote

      favorite









      up vote
      5
      down vote

      favorite











      I am creating an empty file...



      dd if=/dev/zero of=$SDCARD bs=1 count=0 seek=$(expr 1024 * $SDCARD_SIZE)


      ...then turning it into an drive image...



      parted -s $SDCARD mklabel msdos


      ...and creating partitions on it



      parted -s $SDCARD unit KiB mkpart primary fat32 $IMAGE_ROOTFS_ALIGNMENT $(expr $IMAGE_ROOTFS_ALIGNMENT + $BOOT_SPACE_ALIGNED)
      parted -s $SDCARD unit KiB mkpart primary $(expr $IMAGE_ROOTFS_ALIGNMENT + $BOOT_SPACE_ALIGNED) $(expr $IMAGE_ROOTFS_ALIGNMENT + $BOOT_SPACE_ALIGNED + $ROOTFS_SIZE)


      How do I use mkfs.ext and mkfs.vfat without mounting this image?










      share|improve this question















      I am creating an empty file...



      dd if=/dev/zero of=$SDCARD bs=1 count=0 seek=$(expr 1024 * $SDCARD_SIZE)


      ...then turning it into an drive image...



      parted -s $SDCARD mklabel msdos


      ...and creating partitions on it



      parted -s $SDCARD unit KiB mkpart primary fat32 $IMAGE_ROOTFS_ALIGNMENT $(expr $IMAGE_ROOTFS_ALIGNMENT + $BOOT_SPACE_ALIGNED)
      parted -s $SDCARD unit KiB mkpart primary $(expr $IMAGE_ROOTFS_ALIGNMENT + $BOOT_SPACE_ALIGNED) $(expr $IMAGE_ROOTFS_ALIGNMENT + $BOOT_SPACE_ALIGNED + $ROOTFS_SIZE)


      How do I use mkfs.ext and mkfs.vfat without mounting this image?







      linux parted mkfs






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 11 mins ago









      Ciro Santilli 新疆改造中心 六四事件 法轮功

      4,46123938




      4,46123938










      asked May 6 '16 at 18:51









      Paul Knopf

      241310




      241310




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          You want to format a partition in a disk-image file, rather than the entire image file. In that case, you need to use losetup to tell linux to use the image file as a loopback device.



          NOTE: losetup requires root privileges, so must be run as root or with sudo. The /dev/loop* devices it uses/creates also require root privs to access and use.



          e.g (as root)



          # losetup /dev/loop0 ./sdcard.img

          # fdisk -l /dev/loop0
          Disk /dev/loop0: 1 MiB, 1048576 bytes, 2048 sectors
          Units: sectors of 1 * 512 = 512 bytes
          Sector size (logical/physical): 512 bytes / 512 bytes
          I/O size (minimum/optimal): 512 bytes / 512 bytes
          Disklabel type: dos
          Disk identifier: 0x54c246ab

          Device Boot Start End Sectors Size Id Type
          /dev/loop0p1 1 1023 1023 511.5K c W95 FAT32 (LBA)
          /dev/loop0p2 1024 2047 1024 512K 83 Linux

          # file -s /dev/loop0p1
          /dev/loop0p1: data

          # mkfs.vfat /dev/loop0p1
          mkfs.fat 3.0.28 (2015-05-16)
          Loop device does not match a floppy size, using default hd params

          # file -s /dev/loop0p1
          /dev/loop0p1: DOS/MBR boot sector, code offset 0x3c+2, OEM-ID "mkfs.fat", sectors/cluster 4, root entries 512, sectors 1023 (volumes <=32 MB) , Media descriptor 0xf8, sectors/FAT 1, sectors/track 32, heads 64, serial number 0xfa9e3726, unlabeled, FAT (12 bit)


          and, finally, detach the image from the loopback device:



          # losetup -d /dev/loop0


          See man losetup for more details.






          share|improve this answer





























            up vote
            4
            down vote













            To create an image with multiple partitions, a solution that doesn't require any fancy tools or root access is to first create the filesystems, then concatenate them.



            truncate -s $IMAGE_ROOTFS_ALIGNMENT disk
            truncate -s $BOOT_SPACE_ALIGNED part1
            mkfs.fat part1
            cat part1 >>disk
            truncate -s $ROOTFS_SIZE part2
            mkfs.ext4 part2
            cat part2 >>disk


            Then run parted or fdisk to create the partitions.



            This approach has the downside that the resulting image won't be sparse.






            share|improve this answer




















              Your Answer







              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "106"
              ;
              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',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f281589%2fhow-to-run-mkfs-on-file-image-partitions-without-mounting%23new-answer', 'question_page');

              );

              Post as a guest






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              4
              down vote



              accepted










              You want to format a partition in a disk-image file, rather than the entire image file. In that case, you need to use losetup to tell linux to use the image file as a loopback device.



              NOTE: losetup requires root privileges, so must be run as root or with sudo. The /dev/loop* devices it uses/creates also require root privs to access and use.



              e.g (as root)



              # losetup /dev/loop0 ./sdcard.img

              # fdisk -l /dev/loop0
              Disk /dev/loop0: 1 MiB, 1048576 bytes, 2048 sectors
              Units: sectors of 1 * 512 = 512 bytes
              Sector size (logical/physical): 512 bytes / 512 bytes
              I/O size (minimum/optimal): 512 bytes / 512 bytes
              Disklabel type: dos
              Disk identifier: 0x54c246ab

              Device Boot Start End Sectors Size Id Type
              /dev/loop0p1 1 1023 1023 511.5K c W95 FAT32 (LBA)
              /dev/loop0p2 1024 2047 1024 512K 83 Linux

              # file -s /dev/loop0p1
              /dev/loop0p1: data

              # mkfs.vfat /dev/loop0p1
              mkfs.fat 3.0.28 (2015-05-16)
              Loop device does not match a floppy size, using default hd params

              # file -s /dev/loop0p1
              /dev/loop0p1: DOS/MBR boot sector, code offset 0x3c+2, OEM-ID "mkfs.fat", sectors/cluster 4, root entries 512, sectors 1023 (volumes <=32 MB) , Media descriptor 0xf8, sectors/FAT 1, sectors/track 32, heads 64, serial number 0xfa9e3726, unlabeled, FAT (12 bit)


              and, finally, detach the image from the loopback device:



              # losetup -d /dev/loop0


              See man losetup for more details.






              share|improve this answer


























                up vote
                4
                down vote



                accepted










                You want to format a partition in a disk-image file, rather than the entire image file. In that case, you need to use losetup to tell linux to use the image file as a loopback device.



                NOTE: losetup requires root privileges, so must be run as root or with sudo. The /dev/loop* devices it uses/creates also require root privs to access and use.



                e.g (as root)



                # losetup /dev/loop0 ./sdcard.img

                # fdisk -l /dev/loop0
                Disk /dev/loop0: 1 MiB, 1048576 bytes, 2048 sectors
                Units: sectors of 1 * 512 = 512 bytes
                Sector size (logical/physical): 512 bytes / 512 bytes
                I/O size (minimum/optimal): 512 bytes / 512 bytes
                Disklabel type: dos
                Disk identifier: 0x54c246ab

                Device Boot Start End Sectors Size Id Type
                /dev/loop0p1 1 1023 1023 511.5K c W95 FAT32 (LBA)
                /dev/loop0p2 1024 2047 1024 512K 83 Linux

                # file -s /dev/loop0p1
                /dev/loop0p1: data

                # mkfs.vfat /dev/loop0p1
                mkfs.fat 3.0.28 (2015-05-16)
                Loop device does not match a floppy size, using default hd params

                # file -s /dev/loop0p1
                /dev/loop0p1: DOS/MBR boot sector, code offset 0x3c+2, OEM-ID "mkfs.fat", sectors/cluster 4, root entries 512, sectors 1023 (volumes <=32 MB) , Media descriptor 0xf8, sectors/FAT 1, sectors/track 32, heads 64, serial number 0xfa9e3726, unlabeled, FAT (12 bit)


                and, finally, detach the image from the loopback device:



                # losetup -d /dev/loop0


                See man losetup for more details.






                share|improve this answer
























                  up vote
                  4
                  down vote



                  accepted







                  up vote
                  4
                  down vote



                  accepted






                  You want to format a partition in a disk-image file, rather than the entire image file. In that case, you need to use losetup to tell linux to use the image file as a loopback device.



                  NOTE: losetup requires root privileges, so must be run as root or with sudo. The /dev/loop* devices it uses/creates also require root privs to access and use.



                  e.g (as root)



                  # losetup /dev/loop0 ./sdcard.img

                  # fdisk -l /dev/loop0
                  Disk /dev/loop0: 1 MiB, 1048576 bytes, 2048 sectors
                  Units: sectors of 1 * 512 = 512 bytes
                  Sector size (logical/physical): 512 bytes / 512 bytes
                  I/O size (minimum/optimal): 512 bytes / 512 bytes
                  Disklabel type: dos
                  Disk identifier: 0x54c246ab

                  Device Boot Start End Sectors Size Id Type
                  /dev/loop0p1 1 1023 1023 511.5K c W95 FAT32 (LBA)
                  /dev/loop0p2 1024 2047 1024 512K 83 Linux

                  # file -s /dev/loop0p1
                  /dev/loop0p1: data

                  # mkfs.vfat /dev/loop0p1
                  mkfs.fat 3.0.28 (2015-05-16)
                  Loop device does not match a floppy size, using default hd params

                  # file -s /dev/loop0p1
                  /dev/loop0p1: DOS/MBR boot sector, code offset 0x3c+2, OEM-ID "mkfs.fat", sectors/cluster 4, root entries 512, sectors 1023 (volumes <=32 MB) , Media descriptor 0xf8, sectors/FAT 1, sectors/track 32, heads 64, serial number 0xfa9e3726, unlabeled, FAT (12 bit)


                  and, finally, detach the image from the loopback device:



                  # losetup -d /dev/loop0


                  See man losetup for more details.






                  share|improve this answer














                  You want to format a partition in a disk-image file, rather than the entire image file. In that case, you need to use losetup to tell linux to use the image file as a loopback device.



                  NOTE: losetup requires root privileges, so must be run as root or with sudo. The /dev/loop* devices it uses/creates also require root privs to access and use.



                  e.g (as root)



                  # losetup /dev/loop0 ./sdcard.img

                  # fdisk -l /dev/loop0
                  Disk /dev/loop0: 1 MiB, 1048576 bytes, 2048 sectors
                  Units: sectors of 1 * 512 = 512 bytes
                  Sector size (logical/physical): 512 bytes / 512 bytes
                  I/O size (minimum/optimal): 512 bytes / 512 bytes
                  Disklabel type: dos
                  Disk identifier: 0x54c246ab

                  Device Boot Start End Sectors Size Id Type
                  /dev/loop0p1 1 1023 1023 511.5K c W95 FAT32 (LBA)
                  /dev/loop0p2 1024 2047 1024 512K 83 Linux

                  # file -s /dev/loop0p1
                  /dev/loop0p1: data

                  # mkfs.vfat /dev/loop0p1
                  mkfs.fat 3.0.28 (2015-05-16)
                  Loop device does not match a floppy size, using default hd params

                  # file -s /dev/loop0p1
                  /dev/loop0p1: DOS/MBR boot sector, code offset 0x3c+2, OEM-ID "mkfs.fat", sectors/cluster 4, root entries 512, sectors 1023 (volumes <=32 MB) , Media descriptor 0xf8, sectors/FAT 1, sectors/track 32, heads 64, serial number 0xfa9e3726, unlabeled, FAT (12 bit)


                  and, finally, detach the image from the loopback device:



                  # losetup -d /dev/loop0


                  See man losetup for more details.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited May 7 '16 at 2:31

























                  answered May 7 '16 at 2:23









                  cas

                  38k44495




                  38k44495






















                      up vote
                      4
                      down vote













                      To create an image with multiple partitions, a solution that doesn't require any fancy tools or root access is to first create the filesystems, then concatenate them.



                      truncate -s $IMAGE_ROOTFS_ALIGNMENT disk
                      truncate -s $BOOT_SPACE_ALIGNED part1
                      mkfs.fat part1
                      cat part1 >>disk
                      truncate -s $ROOTFS_SIZE part2
                      mkfs.ext4 part2
                      cat part2 >>disk


                      Then run parted or fdisk to create the partitions.



                      This approach has the downside that the resulting image won't be sparse.






                      share|improve this answer
























                        up vote
                        4
                        down vote













                        To create an image with multiple partitions, a solution that doesn't require any fancy tools or root access is to first create the filesystems, then concatenate them.



                        truncate -s $IMAGE_ROOTFS_ALIGNMENT disk
                        truncate -s $BOOT_SPACE_ALIGNED part1
                        mkfs.fat part1
                        cat part1 >>disk
                        truncate -s $ROOTFS_SIZE part2
                        mkfs.ext4 part2
                        cat part2 >>disk


                        Then run parted or fdisk to create the partitions.



                        This approach has the downside that the resulting image won't be sparse.






                        share|improve this answer






















                          up vote
                          4
                          down vote










                          up vote
                          4
                          down vote









                          To create an image with multiple partitions, a solution that doesn't require any fancy tools or root access is to first create the filesystems, then concatenate them.



                          truncate -s $IMAGE_ROOTFS_ALIGNMENT disk
                          truncate -s $BOOT_SPACE_ALIGNED part1
                          mkfs.fat part1
                          cat part1 >>disk
                          truncate -s $ROOTFS_SIZE part2
                          mkfs.ext4 part2
                          cat part2 >>disk


                          Then run parted or fdisk to create the partitions.



                          This approach has the downside that the resulting image won't be sparse.






                          share|improve this answer












                          To create an image with multiple partitions, a solution that doesn't require any fancy tools or root access is to first create the filesystems, then concatenate them.



                          truncate -s $IMAGE_ROOTFS_ALIGNMENT disk
                          truncate -s $BOOT_SPACE_ALIGNED part1
                          mkfs.fat part1
                          cat part1 >>disk
                          truncate -s $ROOTFS_SIZE part2
                          mkfs.ext4 part2
                          cat part2 >>disk


                          Then run parted or fdisk to create the partitions.



                          This approach has the downside that the resulting image won't be sparse.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered May 7 '16 at 19:16









                          Gilles

                          514k12110231550




                          514k12110231550



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f281589%2fhow-to-run-mkfs-on-file-image-partitions-without-mounting%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              How to check contact read email or not when send email to Individual?

                              Bahrain

                              Postfix configuration issue with fips on centos 7; mailgun relay