Block devices

one simple building stone

© 2010, 2011, 2012 Dennis Leeuw dleeuw at made-it dot com
License: GPLv2 or later


    1. Introduction
    2. Working with block devices


A block device is a device where you can read blocks from and write blocks to. Harddisks, DVD-drives, and USB-sticks are all block devices. Block devices are able to read block 12 and then block 5709, so it does not have to be insequence.

Probably the only character device you may come across when dealing with storage is a tape unit.

A block is a size unit. The size of a block on a harddisk is 512, 1024, 2048 or 4096 bytes. More is also possible. The block size means that when you write a file of 12 bytes to a disk that is formatted with a blocksize of 2048, the file occupies 2048 bytes. You might think that it is then best to always use the smallest blocksize possible. This is not true. The smaller the blocksize the more blocks are needed on a disk to hold the data, which means that fragmentation chances get higher, which degrades performance.

Larger block sizes will help disk I/O performance when using large files, such as databases. The larger block can be read at once, there are less blocks to read, so there is less searching done.

There of course also needs to be some table that tells which blocks belong to which file. The more entries in this table the lomnger it takes to retrieve the entire file.

A harddisk can not read blocksizes smaller then 512 bytes. So smaller files can only be read by reading 512 bytes and discarding the rest.

Each block device has an entry in /dev. IDE-disks start with hd and SCSI-devices start with sd. The disk are numbered with letters of the alphabet, and partitions are numbered with numbers. So the first partition on the first IDE-disk is called /dev/hda1.

Working with block devices

Use ls -l on /dev/disk/ to see more information about how everything is interrelated. All files are symlinks in the directories below /dev/disk, so you can see which id, path, label or uuid is mapped to which block device.

ls -l /dev/disk/by-id/
total 0
lrwxrwxrwx 1 root root  9 2009-11-16 09:46 ata-WDC_WD800JD-75MSA3_WD-WMAM9ARM8462 -> ../../sda
lrwxrwxrwx 1 root root 10 2009-11-16 09:46 ata-WDC_WD800JD-75MSA3_WD-WMAM9ARM8462-part1 -> ../../sda1
lrwxrwxrwx 1 root root 10 2009-11-16 09:46 ata-WDC_WD800JD-75MSA3_WD-WMAM9ARM8462-part2 -> ../../sda2
lrwxrwxrwx 1 root root  9 2009-11-16 09:46 scsi-SATA_WDC_WD800JD-75M_WD-WMAM9ARM8462 -> ../../sda
lrwxrwxrwx 1 root root 10 2009-11-16 09:46 scsi-SATA_WDC_WD800JD-75M_WD-WMAM9ARM8462-part1 -> ../../sda1
lrwxrwxrwx 1 root root 10 2009-11-16 09:46 scsi-SATA_WDC_WD800JD-75M_WD-WMAM9ARM8462-part2 -> ../../sda2

ls -l /dev/hd*

ls -l /dev/sd*


mknod -m 666 hda b 3 0
mknod -m 666 hda1 b 3 1
mknod -m 666 hda2 b 3 2
mknod -m 666 hda3 b 3 3

To optimize the settings SATA and IDE devices there is the hdparm tool. The SAS and SCSI counter part is called sdparm.

Another tool that you might want to look into is blockdev