Summary Linux Disk Related Commands

Introduction

In this post we will look at commands that relate to disks in Linux.

Commands

  • Information display: lsblk , blkid
  • Partition creation: fdisk , cfdisk , gdisk, cgdisk, sgdisk, parted
  • Mount: mount , umount
  • File system: mkfs , fsck
  • File system (ext2 / 3/4): resize2fs, dumpe2fs, tune2fs
  • Swap: mkswap , swapon, swapoff
  • SMART Information display: smartctl
  • LVM: lvmdiskscan, pvdisplay, pvscan, lvdisplay, lvscan
  • Dealing with Bad sectors: badblocks, ddrescue
  • Finding out which directory is consuming space: ncdu

Information display

You can find almost all the information with fdisk -l You can tell whether it is MBR or GPT by looking at “Disk Label Type”. It doesn’t show where it’s mounted, so use the mount command.

Use Fdisk -l

root@master:/home/ozyrys# fdisk -l  -o +UUID
Disk /dev/loop0: 42.18 MiB, 44220416 bytes, 86368 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
Disk /dev/loop1: 55.5 MiB, 58183680 bytes, 113640 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
Disk /dev/loop2: 61.85 MiB, 64835584 bytes, 126632 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
Disk /dev/loop3: 67.25 MiB, 70508544 bytes, 137712 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
Disk /dev/loop4: 55.51 MiB, 58191872 bytes, 113656 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
Disk /dev/loop5: 32.42 MiB, 33980416 bytes, 66368 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
Disk /dev/loop7: 32.45 MiB, 34017280 bytes, 66440 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
Disk /dev/sda: 120 GiB, 128849018880 bytes, 251658240 sectors
Disk model: QEMU HARDDISK
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: gpt
Disk identifier: B1CCA9DF-FC34-4ACA-B803-2DB74127A5CA
Device Start End Sectors Size Type UUID
/dev/sda1 2048 4095 2048 1M BIOS boot EA26AEC2-6BB1-48C7-B146-9ABE263C225D
/dev/sda2 4096 2101247 2097152 1G Linux filesystem BD6CDC9C-1CBA-4DBC-8A51-6630061655D0
/dev/sda3 2101248 251656191 249554944 119G Linux filesystem D7D08B8C-33FA-4237-AFBC-55611FA30E3F
Disk /dev/mapper/ubuntu--vg-ubuntu--lv: 59.51 GiB, 63887638528 bytes, 124780544 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
Disk /dev/loop8: 67.24 MiB, 70504448 bytes, 137704 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
Disk /dev/loop9: 61.85 MiB, 64835584 bytes, 126632 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

he UUID displayed here is the UUID of the file system, and there is also a UUID of the disk ( /etc/fstab the UUID of the disk is specified in the first field of). You can see that with lsblk -O or ls -l /dev/disk/by-uuid

Use lsblk

You can display all columns with. lsblk -O

Use blkid

It seems that blkid displays UUIDs and labels, and searches by those conditions.

root@master:/home/ozyrys# blkid
/dev/sr0: UUID="2021-08-24-09-09-05-00" LABEL="Ubuntu-Server 20.04.3 LTS amd64" TYPE="iso9660" PTUUID="7fcaeaa1" PTTYPE="dos"
/dev/sda2: UUID="908f640f-a510-4e61-bf53-e1ca74717f1e" TYPE="ext4" PARTUUID="bd6cdc9c-1cba-4dbc-8a51-6630061655d0"
/dev/sda3: UUID="F4fpAv-YXiT-Fpdd-rUP3-vAbx-54US-eL9a2a" TYPE="LVM2_member" PARTUUID="d7d08b8c-33fa-4237-afbc-55611fa30e3f"
/dev/mapper/ubuntu--vg-ubuntu--lv: UUID="39ae7c14-82bf-4544-beb9-912514f34b0e" TYPE="ext4"
/dev/loop0: TYPE="squashfs"
/dev/loop1: TYPE="squashfs"
/dev/loop2: TYPE="squashfs"
/dev/loop3: TYPE="squashfs"
/dev/loop4: TYPE="squashfs"
/dev/loop5: TYPE="squashfs"
/dev/loop7: TYPE="squashfs"
/dev/sda1: PARTUUID="ea26aec2-6bb1-48c7-b146-9abe263c225d"
/dev/loop8: TYPE="squashfs"
/dev/loop9: TYPE="squashfs"
root@master:/home/ozyrys#

Documentation of blkid it says that lsblk is recommended over blkid.

Partition creation

  • fdisk → An ancient command. Interactive. Not suitable for batch processing. Included in util-linux.
  • cfdisk → Curses-based fdisk. Interactive and Included in util-linux.
  • gdisk → GPT version fdisk. The developer is different from fdisk, and it is not included in util-linux.
  • cgdisk → Curses-based gdisk. Interactive. The author is Roderick W. Smith, the same as gdisk.
  • sgdisk → Script-based gdisk. Non-interactive. The author is Roderick W. Smith, the same as gdisk.
  • parted → Made by GNU.

File system

File system creation (mkfs)

root@master:/home/ozyrys# mkfs -t ext4 /dev/device

Identify the file system

root@master:/home/ozyrys# file -s /dev/sda
/dev/sda: DOS/MBR boot sector, extended partition table (last)
root@master:/home/ozyrys#

File system (ext2 / 3/4)

Resize ext2 /3/4 file system (resize2fs)

Resize the file system if the partition size changes. In the case of AWS Linux, resize2fs is automatically executed at startup, so it seems that you do not have to execute it manually.

Display information about the ext2 /3/4 file system (dumpe2fs)

Detailed information can be obtained. However, the number of used inodes displayed here is not real-time data, and is correct only in the unmounted state. It df -i is better to use to know it . To get all the information with df command.

Reduce the size of the spare area and squeeze out the free space (tune2fs)

Reserve area just in case (5% by default, but not on AWS Linux). First we need to check current situation.

root@master:/home/ozyrys# tune2fs -l /dev/sda2 | grep Reserved
Reserved block count: 13107
Reserved GDT blocks: 127
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
root@master:/home/ozyrys#

Set the spare area to 5%.

root@master:/home/ozyrys# tune2fs -m 5 /dev/sda2

Mount

Typical use case

root@master:/home/ozyrys# mount /dev/device /mnt/mount_point

Specify the UUID

root@master:/home/ozyrys# mount UUID=XXXXXXXX-XXXX-XXXX-XXXXXXXXXX /mnt/mount_point

Specify mount options

root@master:/home/ozyrys# mount /dev/device /mnt/mount_point -o rw,relatime

Explanation:

  • relatime Updates the access date and time (atime) only when writing.
  • noatime Never updates the access date and time (atime)
  • Both improve the performance, noatime and to mutt have a program that no longer work properly, such as.
  • See for other options in man mount.
  • Some options can only be set with /etc/fstab

Verification

root@master:/home/ozyrys# mount

Unmount

root@master:/home/ozyrys# umount /mnt/mount_point

Setup it permanent

Edit /etc/fstab file and paste

/dev/device /mnt/mount_point ext4 defaults 0 0#Or use UUID
UUID = XXXXXXXX-XXXX-XXXX-XXXXXXXXXX /mnt/mount_point ext4 defaults 0 0

Swap

Swap file creation

 fallocate -l 512M /swapfile

Swap space creation

If you want the device to be swap space:

root@master:/home/ozyrys# mkswap /dev/device

When using a swap file

root@master:/home/ozyrys# mkswap /swapfile

Swap set ( — verbose can be omitted)

root@master:/home/ozyrys# swapon --verbose /dev/device

Swap confirmation

root@master:/home/ozyrys# swapon -s

Unswap ( — verbose can be omitted)

root@master:/home/ozyrys# swapoff --verbose /dev/device

SMART Information display

root@master:/home/ozyrys# smartctl --all /dev/sda

LVM

  • PV (Physical Volume) = Physical disk (or partition)
  • VG (Volume Group) = A collection of PV
  • LV (Logical Volume) = Logical disk created in PV. You can mount LV and mkfs.

Information we can display with this commands:

  • lvmdiskscan,
  • lvscan,
  • lvdisplay

Dealing with Bad sectors

root@master:/home/ozyrys# badblocks
root@master:/home/ozyrys# ddrescue

Finding out which directory is consuming space

 by the author.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Maciej

Maciej

DevOps Consultant. I’m strongly focused on automation, security, and reliability.