1

In question "Parted command line not get the same result", the answer was selected as correct (to create IMG file systems and partitions with "parted") is:

# parted MyDrive.img \
    mklabel msdos \
    mkpart primary NTFS 1 1024 \
    set 1 lba on \
    align-check optimal 1 \
    print

Model:  (file)
Disk /dev/shm/MyDrive.img: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
  1      1049kB  1074MB  1073MB  primary  ntfs         lba

The same for fat32/ext4. However when I mount image in /dev/loop (sudo losetup loop1 MyDrive.img) it's doesn't work (unknown partition).

So the secuence is incomplete.

Somebody can help me to complete the secuence to create .img for ext4/ntfs/fat32 (GPT and MSDOS) to recognize when mounting it in a loop (ready to work)

Thanks!

6
  • did you create a filesystem with mkfs? Mar 25, 2018 at 18:36
  • no. The idea is that the parted sequence creates the whole structure. Then the sequence is incomplete
    – acgbox
    Mar 25, 2018 at 18:39
  • No idea why you are running mkfs on the /dev/loop device, you want to run it on your image file Mar 25, 2018 at 18:52
  • the same result if i run in /dev/loop1 or into MyDrive.img
    – acgbox
    Mar 25, 2018 at 19:01
  • Now I have no idea what the question is. You asked about why executing your parted script did not create a recognizable partition, that was fixed by running mkfs. Now you say the partition does not allow writing, but you do not show how you are mounting the image. Mar 25, 2018 at 19:05

1 Answer 1

2

I'll provide the method you asked for and also a much simpler method if partitions are not required. I will also only do ext4 examples, it should be possible to derive the rest:

Image file with partitions:

#!/bin/sh

FILE=MyDrive.img

# create new 2Gb image file, will overwrite $FILE if it already exists
dd if=/dev/zero of=$FILE bs=1M count=2048

# make two 1Gb partitions and record the offsets
offset1=$(parted $FILE \
    mklabel msdos \
    mkpart primary ext2 1 1024 \
    unit B \
    print | awk '$1 == 1 {gsub("B","",$2); print $2}')
offset2=$(parted $FILE \
    mkpart primary ext2 1024 2048 \
    unit B \
    print | awk '$1 == 2 {gsub("B","",$2); print $2}')

# loop mount the partitions and record the device
loop1=$(losetup -o $offset1 -f $FILE --show)
loop2=$(losetup -o $offset2 -f $FILE --show)

# create and mount the filesystems
mkdir -p /tmp/mnt{1,2}
mkfs.ext4 $loop1
mount $loop1 /tmp/mnt1
mkfs.ext4 $loop2
mount $loop2 /tmp/mnt2

# file write test
touch /tmp/mnt1/file_on_partition_1
touch /tmp/mnt2/file_on_partition_2

# cleanup
umount /tmp/mnt1 /tmp/mnt2
losetup -d $loop1 $loop2

Image file without partitions:

#!/bin/sh

FILE=MyDrive.img

# create new 2Gb image file, will overwrite $FILE if it already exists
dd if=/dev/zero of=$FILE bs=1M count=2048

# create and mount filesystem
mkfs.ext4 -F $FILE
mount $FILE /mnt

# file write test
touch /tmp/mnt/file_in_imagefile

# cleanup
umount /mnt

Hopefully that is self explanatory, was easier to express this answer in a shell script.

8
  • You tried those scripts ?. They do not seem to work. Please check them. And thanks for your effort
    – acgbox
    Mar 25, 2018 at 23:51
  • they work for me on a stock centos 7 install using /bin/sh Mar 26, 2018 at 0:37
  • Why do you run "mkpart primary ext2" and then run "mkfs.ext4 $ loop1" ?? (ext2 vs ext4)
    – acgbox
    Mar 26, 2018 at 1:02
  • You should read up on what those parted options are in the official documentation. In summary, that is the parted mkpart fs-type option and generally is only to help identify what filesystem will be on that partition. There are only a handful of possible choices you can use there, ext2 is the option that represents all extX filesystems. The mkfs command is what actually makes the filesystem. Mar 26, 2018 at 17:44
  • then I can remove ext2 (mkpart primary 1 1024 ). And where do I insert the alignment (align-check optimal 1)?
    – acgbox
    Mar 26, 2018 at 23:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .