logo

Device

Last Updated: 2023-02-14

In some Unix-like systems, most device files are managed as part of a virtual file system traditionally mounted at /dev.

Devices

Normal Disks (/dev/hda1)

By convention:

  • /dev/hd{x}: IDE controllers devices. E.g. /dev/hda, /dev/hdb, /dev/hdc.
  • /dev/sd{x}: SCSI and SATA controllers devices. (Hard disk and SATA SSD) E.g. /dev/sda, /dev/sdb, /dev/sdc.
  • /dev/nvme{disk}n{namespace}p{partition}: NVMe SSD. E.g. /dev/nvme0n1p6 equivalent to /dev/sda6

Configurable: The device names are determined by the udev configuration /lib/udev/rules.d/60-persistent-storage.rules.

Diskless instances: use local ram (tmpfs) or remote HDD / SSD (accessible via iscsi).

Network Block Device (/dev/nb0)

Every time the client computer wants to read, e.g., /dev/nb0, it sends a request over TCP to the server, which will reply with the data read.

This can be used for stations with low disk space (or even diskless) to borrow disk space from another computer.

Unlike NFS, it is possible to put any filesystem on it, etc. It should even be possible to use NBD as a root filesystem, but it requires a user-level program to be in the initrd to start. It also allows you to run block-device in user land (making server and client physically the same computer, communicating using loopback).

Loopback (/dev/loop1)

/dev/loop* are loop devices making plain files accessible as block devices.

Loopback device: a virtual device that can be used like any other media device. The loopback filesystem associates a file on another filesystem as a complete device.

LVM / Device Mapper (/dev/mapper)

Logical Volume Manager (LVM) is a device mapper framework; can have their root file systems on a logical volume.

device-mapper is in the kernel, LVM2 tools are in userspace.

The device mapper is a framework provided by the Linux kernel for mapping physical block devices onto higher-level virtual block devices. It forms the foundation of the logical volume manager (LVM), software RAIDs and dm-crypt disk encryption, and offers additional features such as file system snapshots.

The entries in /dev/mapper are LVM logical volumes. You can think of these as Linux's native partition type. Linux can also use other partition types, such as PC (MBR or GPT) partitions.

Note that the Linux device mapper is used for other things besides LVM (such as dm-crypt disk encryption), so files in /dev/mapper aren't necessarily LVM logical volumes.

Ramdisk (e.g. /dev/ram0)

The device does not refer to any physical hardware, but to a portion of memory that is set aside for the purpose.

/boot/efi

The /boot/efi system partition is simply the boot partition created when the computers mother board runs UEFI rather than BIOS.

Controlling daemon

Monitors hardware addition and removal at run time, making corresponding changes to the device file system

  • BSD: devfs, in kernel space
  • Linux: udev, in user space

Check by df:

Linux:

Filesystem ... Mounted on
udev       ... /dev

BSD:

Filesystem ... Mounted on
devfs          /dev

udev

udev (userspace /dev)

/dev is static, udev is for plugable devices running in userspace.

To be able to deal with peripheral devices that are hotplug-capable in a user-friendly way, a part of handling all of these hotplug-capable hardware devices was handed over from the kernel to a daemon running in user-space. Running in user space serves security and stability purposes.

udev primarily manages device nodes in the /dev directory, Unlike traditional Unix systems, where the device nodes in the /dev directory have been a static set of files, the Linux udev device manager dynamically provides only the nodes for the devices actually present on a system.

$ ps -e | grep udevd
    304 ?        00:00:00 systemd-udevd

Check attacked EBS(sda/sdf may be renamed to xvda/xvdf)

$ sudo fdisk -l
Disk /dev/xvda1: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/xvda1 doesn't contain a valid partition table

Disk /dev/xvdf: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Format EBS

$ sudo mkfs -t ext4 /dev/xvdf

Edit fstab and reboot

$ vi /etc/fstab

or manually mount

# mkdir /mnt/tmp
# mount /dev/xvdf /mnt/tmp
# umount /dev/xvdf

XFS

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

Mount / Umount

Unix systems have a single directory tree. Mounting is the act of associating a storage device to a particular location in the directory tree, which makes a filesystem available to the system.

Assume a device is added as /mnt/vdc. Create a new folder as /dev/vdc

$ sudo mkdir /dev/vdc

Edit /etc/fstab.

# <file system> <mount point>   <type>  <options>       <dump>  <pass>
/dev/vdc        /mnt/vdc        auto    defaults        0       0

Mount all

$ sudo mount -a

Check by df

$ df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vdc           65390      36     65354   1% /mnt/vdc

To remount

$ mount -o remount /dev/vdc

Mount disk

$ mount /dev/sdf /mnt/workspace

umount: it seems [device] is mounted multiple times

Error:

$ umount /dev/[device]
umount: it seems /dev/<device> is mounted multiple times

Solution: use sudo

$ sudo umount /dev/[device]

Format a Device

Use mkfs.ext2, mkfs.ext3, mkfs.ext4 etc

$ sudo mkfs.ext3 /dev/<device>

Check FS

# 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

cmd

  • mkfs -t ext2 is equivalent to mke2fs
  • mke2fs
  • tune2fs
  • mkswap
mke2fs -F -j /dev/sdh
mkdir /ebs
mount /dev/sdh /ebs

copy to ebs:

dd bs=65536 if=/dev/sda1 of=/dev/sdh
fsck /dev/sdh

check

df -h

fsck /dev/sdh