logo

Shell Cheatsheet - Devices and File Systems

List available/supported filesystems

$ cat /proc/filesystems
nodev   sysfs
nodev   tmpfs
nodev   bdev
nodev   proc
nodev   cgroup
nodev   cgroup2
...

List mounted filesystems

$ mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=123602848k,nr_inodes=30900712,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,noexec,relatime,seclabel)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,seclabel,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,seclabel,mode=755)

In a tree view

$ findmnt

Narrow down to ext4

$ findmnt -t ext4
$ mount -l -t ext4

List block devices

# List block devices
$ lsblk

# Locate/print block device attributes (the UUID)
$ blkid

# lsblk is not available on macOS, use diskutil instead.
$ diskutil list

What is MAJ:MIN in lsblk result?

MAJ:MIN column shows the Major and the Minor numbers.

e.g. if you have several SCSI disks, they would all have major number 8, but with different minor numbers.

Common major numbers:

  • 2: floppies
  • 3: IDE disks
  • 8: SCSI disks
  • 11: the optical drivess

To check the major numbers on your system:

$ cat /proc/devices
Block devices:
  7 loop
  8 sd
  9 md
...
253 device-mapper
254 mdp
259 blkext

Get Info about devices / logical volumes

# get info about dm-0 device
$ sudo dmsetup info /dev/dm-0
Name:              rocky-rl--root
State:             ACTIVE
Read Ahead:        256
Tables present:    LIVE
Open count:        1
Event number:      0
Major, minor:      253, 0

# get info about logical volumes
$ lvdisplay

How to check / repair a file system?

How to check

fsck (file system consistency check) is a tool for checking the consistency of a file system. E.g.

$ fsck /boot/efi
0x41: Dirty bit is set. Fs was not properly unmounted and some data may be corrupt.
1) Remove dirty bit
2) No action

How to repair

In general it is not safe to run fsck on mounted filesystems.

# unmount the disk
$ umount /dev/sda1

# clear the dirty bit and repair the disk:
# -a: automatic repair; -w: write the changes immediately.
$ fsck /dev/sda1 -a -w

# mount the disk again
$ mount /dev/sda1

fsck vs e2fsck

fsck is a wrapper for the filesystem-specific fsck.* family of tools. e2fsck is specific for ext2/ext3/ext4. They can be used interchangeably.

How to mount and unmount a device

To mount

$ mount -t type DEVICE DIR

# e.g. mount the device /dev/sdf to folder /mnt/workspace
$ mount /dev/sdf /mnt/workspace

If you leave the DIR part of syntax, it looks for a mount point in /etc/fstab.

# mount all according to /etc/fstab
$ sudo mount -a

To remount

$ mount -o remount /dev/DEVICE

To unmount

Use umount, note that is it "umount" instead of "unmount".

$ umount /dev/DEVICE

fstab

$ cat /etc/fstab

6 colums:

  • Device: the given name or UUID of the mounted device (sda1/sda2/etc).
  • Mount Point: the directory where the device should be mounted.
  • File System Type: the type of filesystem in use.
  • Options: lists any active mount options. If using multiple options they must be separated by commas.
  • Backup Operation:
    • 0: no backup.
    • 1: dump utility backup of a partition.
  • File System Check Order:
    • 0: fsck will not check the filesystem.
    • 0: represent the check order. The root filesystem should be set to 1 and other partitions set to 2.

A line may look like this:

/dev/mapper/rocky-rl--root /                       xfs     defaults        0 0

mount point is /, fs type is xfs.

fdisk Commands

enter the command mode

$ sudo fdisk /dev/sdb

m to show all the commands.

E.g. to delete a partition:

  1. d for deletion
  2. select partition number
  3. w for writing the changes

Check lsblk, the selected partition should be gone.

List all partitions

$ fdisk -l

Check File System Types

$ df -Th

You may find other filesystem like:

  • tmpfs: a common name for a mounted file system, but stored in volatile memory instead of a persistent storage device.
  • drvfs: mounted Windows disks, seen in Windows Subsystem for Linux (WSL). e.g. C:\ will be /mnt/c, and D:\ will be /mnt/d

To only show devices under /dev:

$ df -hT | awk '{print $1,$2,$NF}' | grep "^/dev"
/dev/sda3 ext4 /
/dev/sda2 ext2 /boot
/dev/sda6 ext4 /tmp
/dev/sdb1 ext4 /data1
/dev/sdc1 ext4 /data2
/dev/sdd1 ext4 /data3

Use diskutil

$ diskutil list
$ diskutil info -all
$ diskutil info /

List All Block Devices and Partitions

$ cat /proc/partitions
major minor  #blocks  name

253        0   31457280 vda
253       16   62914560 vdb
253       32      65536 vdc
253       48  251658240 vdd

Use file -s DEVICE to check type

$ sudo file -s /dev/vda
/dev/vda: Linux rev 1.0 ext4 filesystem data, UUID=6f54f78f-3f47-488c-ab1b-b1b8a596c2d3, volume name "c3image-rootfs" (needs journal recovery) (extents) (large files) (huge files)

What is a superblock?

The superblock is essentially file system metadata and defines the file system type, size, status, and information about other metadata structures (metadata of metadata).

There are multiple copies of the superblock. You can find the locations of the backups by:

$ dumpe2fs /dev/foo | grep -i superblock

What is errors=remount-ro?

From man page:

errors={continue|remount-ro|panic}
    Define the behavior  when  an  error  is  encountered.   (Either
    ignore  errors  and  just  mark  the  filesystem  erroneous  and
    continue, or remount the filesystem read-only, or panic and halt
    the  system.)   The default is set in the filesystem superblock,
    and can be changed using tune2fs(8).

Basically it will be remounted as read-only upon errors.

How to get udev device info

Print the SCSI device information for all /dev/sd devices

for d in /dev/sd*;
  do echo $d
  sudo /lib/udev/scsi_id -g -d $d;
done

Print the udev information for all /dev/ devices

$ udevadm info --export-db

How to format a device?

Use mke2fs, mkfs.ext2, mkfs.ext3, mkfs.ext4 etc. mkfs -t ext2 is equivalent to mke2fs

$ sudo mkfs.ext3 /dev/DEVICE

# Format and mount
$ mke2fs -F -j /dev/sdh
$ mkdir /ebs
$ mount /dev/sdh /ebs
# Copy data
$ dd bs=65536 if=/dev/sda1 of=/dev/sdh
$ fsck /dev/sdh

Troubleshooting

df not responding

Try df -l, -l for local only.

umount: it seems /dev/DEVICE is mounted multiple times

If you see this error:

$ umount /dev/DEVICE
umount: it seems /dev/DEVICE is mounted multiple times

Solution: use sudo

$ sudo umount /dev/DEVICE

Other commands

  • tune2fs: adjust tunable filesystem parameters on ext2/ext3/ext4 filesystems
  • mkswap: set up a Linux swap area

To work with XFS: mkfs.xfs

$ sudo apt-get install xfsprogs
$ sudo mkfs.xfs /dev/sdf
$ sudo mount -t xfs -o defaults /dev/sdf /var/www