2

first, i create a disk image use dd:

> dd if=/dev/zero of=disk bs=512 count=2097152 > du -h disk 1.1G disk 

second, fdisk disk create a partion:

Command (m for help): p Disk disk: 1 GiB, 1073741824 bytes, 2097152 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: 0x40c73f2f Device Boot Start End Blocks Id System disk1 2048 2097151 1047552 83 Linux 

blocks column: 1047552, so sectors is : 1024(can fdisk set sector size?).

third, losetup:

 losetup /dev/loop0 -o 2097152 --sizelimit=1072693248 disk 

2048*1024 = 2097152 1047552*1024 = 1072693248.

at this time the size of disk is 1G:

> du -h disk 1.1G disk 

forth, mkfs.ext2:

> mkfs.ext2 /dev/loop0 mke2fs 1.42.8 (20-Jun-2013) Discarding device blocks: done Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 65408 inodes, 261632 blocks 13081 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=268435456 8 block groups 32768 blocks per group, 32768 fragments per group 8176 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376 Allocating group tables: done Writing inode tables: done Writing superblocks and filesystem accounting information: done 

and disk image size is decreased:

> du -h disk 19M disk 

You know filesystem block size is 4096, and have 8 block groups, 32768 blocks per group.

4096 * 8 * 32768 = 1G.

1
  • You get the size of the disk image by doing ls -l disk not by any multiplication of sectors and size and then rounding to the nearest 1G (or have a program do that). Please give the exact values of ls -l disk after dd and after mkfs.ext2 on the loop device. Commented Dec 9, 2013 at 9:19

1 Answer 1

3

That's because the discard option in mke2fs is on by default. In newer kernels, doing a BLKDISCARD on a loop device causes a hole to be punched on the underlying file, so that that file becomes sparse.

On an ATA drive, the BLKDISCARD would translate to a TRIM, the idea being that mke2fs tells the layer underneath that the data is unallocated so it can do optimisations based on that information (unallocate the space in the file for loop devices, recycle flash cells for SSDs...).

If you want to disable that, just do:

mkfs.ext2 -E nodiscard /dev/loop0p1 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.