Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
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. –  Anthon Dec 9 '13 at 9:19

1 Answer 1

up vote 2 down vote accepted

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.