What is a block device and how do I resize it?

What is a block device

A block device is a device you can read blocks from. For example hard disks, cdrom drives and floppies are block devices, but not the keyboard. You can receive data from the keyboard and regard them as blocks, but you cannot seek on the keyboard. You can tell a hard disk “give me block 5433”, then block 7707, then block 1807 and you cannot do this with a keyboard, so, a keyboard is no block device.

Block sizes

It is important to understand the ideas behind the block sizes.

Disk block sizes

Typically, a hard disk cannot read less than 512 bytes, if you want to read less, read 512 bytes and discard the rest. This is why dd reads one block à 512 bytes in the following example:

tweedleburg:~ # dd if=/dev/sda1 of=/dev/null count=1
1+0 records in
1+0 records out
512 bytes (512 B) copied, 1.8977e-05 s, 27.0 MB/s

File system block sizes

On the other hand, every file system needs to split up a partition into blocks to store files and file parts. This is why there is a different block size for a file system as you can see here:

tweedleburg:/mnt/sdb2 # stat -f .
  File: "."
    ID: 236d62321492c2ce Namelen: 255     Type: ext2/ext3
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 76899893   Free: 8380419    Available: 4474114
Inodes: Total: 39075840   Free: 38013010

So, if you store a file in this file system, it will be stored in a 4096-byte-block, that means, even if your file only contains 5 bytes, it will take away 4096 bytes from your disk’s capacity:

tweedleburg:/mnt/sdb2 # df .
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sdb2            307599572 274077896  17896456  94% /mnt/sdb2
tweedleburg:/mnt/sdb2 # echo hallo>welt
tweedleburg:/mnt/sdb2 # df .
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sdb2            307599572 274077900  17896452  94% /mnt/sdb2
tweedleburg:/mnt/sdb2 # du -csh welt
4.0K    welt
4.0K    total

Kernel block size

Also the kernel has its own block size. This is relevant e.g. for vmstat. In the vmstat man page you find the statement

All linux blocks are currently 1024 bytes.

So, again another block size when you work with vmstat. This is the block size the Linux kernel uses internally for caching and buffering. It is the most prominent of all block sizes.

How do I set the Block Size?

You can set the block size on your linux machine using the steps provided below:

Check the block size of current device.

$ blockdev --getbsz /dev/vdb1
512

Unmount filesystem to change block size.

$ umount /dev/vdb1

Create filesystem to change new block size.

$ mkfs -t ext4 -b 4096 /dev/vdb1

or

$ blockdev --setbsz 1024 /dev/sda1 

Mount to check the changed block size.

$ mount /dev/vdb1 /test/
$ blockdev --getbsz /dev/vdb1
4096

 

Facebooktwittergoogle_plusredditpinterestlinkedinmail