Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Show how to create an image in one command.
Source Link
Stephen Kitt
  • 482.3k
  • 60
  • 1.2k
  • 1.4k
(parted) unit s                                                           
(parted) print                                                            
Model:  (file)
Disk /home/steve/tmpdir/DISK1.img: 2457600s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start  End       Size      Type     File system  Flags
 1      2048s  2457599s  2455552s  primary  fat32        lba

(Note that since you’re working on your own disk image, you don’t need sudo, you only need to specify the full path to parted and mkfs.vfat since they are in /sbin or /usr/sbin and therefore not on your PATH.)

If you don’t need a partition table in your image, you might find it easier to work with a file system image rather than a full disk image; you can do everything in one command with mkfs.vfat:

$ /sbin/mkfs.vfat -v -C DISK2.img $((1200*1024))
mkfs.fat 4.2 (2021-01-31)
Auto-selecting FAT32 for large filesystem
DISK2.img has 64 heads and 63 sectors per track,
hidden sectors 0x0000;
logical sector size is 512,
using 0xf8 media descriptor, with 2457567 sectors;
drive number 0x80;
filesystem has 2 32-bit FATs and 8 sectors per cluster.
FAT size is 2400 sectors, and provides 306591 clusters.
There are 32 reserved sectors.
Volume ID is a452a0bf, no volume label.
(parted) unit s                                                           
(parted) print                                                            
Model:  (file)
Disk /home/steve/tmpdir/DISK1.img: 2457600s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start  End       Size      Type     File system  Flags
 1      2048s  2457599s  2455552s  primary  fat32        lba

(Note that since you’re working on your own disk image, you don’t need sudo, you only need to specify the full path to parted and mkfs.vfat since they are in /sbin or /usr/sbin and therefore not on your PATH.)

(parted) unit s                                                           
(parted) print                                                            
Model:  (file)
Disk /DISK1.img: 2457600s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start  End       Size      Type     File system  Flags
 1      2048s  2457599s  2455552s  primary  fat32        lba

(Note that since you’re working on your own disk image, you don’t need sudo, you only need to specify the full path to parted and mkfs.vfat since they are in /sbin or /usr/sbin and therefore not on your PATH.)

If you don’t need a partition table in your image, you might find it easier to work with a file system image rather than a full disk image; you can do everything in one command with mkfs.vfat:

$ /sbin/mkfs.vfat -v -C DISK2.img $((1200*1024))
mkfs.fat 4.2 (2021-01-31)
Auto-selecting FAT32 for large filesystem
DISK2.img has 64 heads and 63 sectors per track,
hidden sectors 0x0000;
logical sector size is 512,
using 0xf8 media descriptor, with 2457567 sectors;
drive number 0x80;
filesystem has 2 32-bit FATs and 8 sectors per cluster.
FAT size is 2400 sectors, and provides 306591 clusters.
There are 32 reserved sectors.
Volume ID is a452a0bf, no volume label.
Source Link
Stephen Kitt
  • 482.3k
  • 60
  • 1.2k
  • 1.4k

parted creates the partition table and the partition entry, but you also need to create the file system: exit parted after step 5, then create the file system with the appropriate offset (I’m using -v so that mkfs.vfat shows what it’s doing):

$ /sbin/mkfs.vfat -v --offset 2048 DISK1.img
mkfs.fat 4.2 (2021-01-31)
Auto-selecting FAT32 for large filesystem
DISK1.img has 64 heads and 63 sectors per track,
hidden sectors 0x0000;
logical sector size is 512,
using 0xf8 media descriptor, with 2455551 sectors;
drive number 0x80;
filesystem has 2 32-bit FATs and 8 sectors per cluster.
FAT size is 2400 sectors, and provides 306339 clusters.
There are 32 reserved sectors.
Volume ID is 97413b09, no volume label.

This will format the first partition (starting at sector 2048) as a FAT32 volume. When you reload the image in parted, it will show this:

$ /sbin/parted DISK1.img
…
(parted) print
Disk …/DISK1.img: 1258MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  1258MB  1257MB  primary  fat32        lba

In general, to determine the appropriate offset when working with images, you can change the units in parted to sectors:

(parted) unit s                                                           
(parted) print                                                            
Model:  (file)
Disk /home/steve/tmpdir/DISK1.img: 2457600s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start  End       Size      Type     File system  Flags
 1      2048s  2457599s  2455552s  primary  fat32        lba

(Note that since you’re working on your own disk image, you don’t need sudo, you only need to specify the full path to parted and mkfs.vfat since they are in /sbin or /usr/sbin and therefore not on your PATH.)