# mkfs [-t type] [fsoptions] device # mke2fs [-t type] device
# mkfs
-t
# mke2fs
Formats a block device or file into a filesystem.
-m
-i
-j
-E
-c
FS type Description ext2 Original Linux file-system, lacks many modern features ext3 Extension of ext2 which added Journaling (method of preventing data loss on power-failures) ext4 Further extension of ext3 with proper 64 bit support btrfs "Better FS", slow but lost of new features. xfs Default for Red Hat, reasonably fast and efficient, but a bit old now jfs IBM journaled file-system Other OS file-systems vfat Virtual FAT, extends FAT (MS DOS) file-systems with long filenames, supports all FAT through FAT32 ntfs Windows NTFS Virtual file-systems tmpfs Temporary ram drive file-system proc Proc virtual filesystem (/proc) sysfs System virtual filesystem (/sys) devpts Virtual pseudo-terminal device file-system (/dev/pts)
# fsck [-a] device
# fsck
-a
# e2fsck device
# e2fsck
> yes [string]
> yes
# badblocks [-svw] [-o out] device
# badblocks
-svw
-o
Check device for bad blocks. Mostly useful for checking old style spinning disk drives.
-s
-v
-w
# e2label <device>
# tune2fs [-l] device
# tune2fs
-l
# /sbin/mkfs.* # /sbin/fsck.*
# /sbin/mkfs.*
# /sbin/fsck.*
/sbin/mkfs.ext4 -> mke2fs
/dev/sd
/dev/sd[a-z][0+] │ │ └── Partition number │ └────── Drive enumeration └────────── Block device driver type
Old IDE devices used /dev/hd* device names, however those device names may be deprecated in favor of /dev/sd*.
/dev/hd
/dev/sda1 Partition 1 on the first SATA drive (sda) /dev/sdc3 Partition 3 on the third SATA drive (sdc)
/dev/sda1
/dev/sdc3
/dev/md[0+] /dev/md/[0+]
/dev/md
/dev/md/
/tmp
/var/run
/dev/ram[0+]
/dev/ram
/dev/loop[0+]
/dev/loop
# mount -o loop ./cdrom.iso /mnt/cdrom Mounts the cdrom.iso image at /mnt/cdrom, allocating /dev/loop0 automatically for us.
# mount -o loop ./cdrom.iso /mnt/cdrom
cdrom.iso
/mnt/cdrom
/dev/loop0
/dev/nvme[0-9]n[0-9]p[0-9] │ │ └── Partition number │ └──────── Drive enumeration └────────────── NVMe Bus enumeration
Sometimes the kernel may for some reason re-order physical devices (particularly when adding/removing other devices,) which would prevent the system from mounting the requestd file-system. To overcome this problem, many file-systems allow the option of specifying a user-defined label for the file-system or may be referred to by a UUID (Universally Unique ID - auto-generated when the file-system is made.) Theses labels or UUID's can be displayed by special /dev/disk entries, such as:
/dev/disk
/dev/disk/by-uuid/*
/etc/fstab
UUID=6d65a372-a94b-4fd9-a5d3-1c14b2cb5a1e
/dev/disk/by-label/*
LABEL=root
I personally prefer file-system labels as they are easier to use and remember and can provide meaningful information about what is contained on the file-system.
> lsblk
# blkid
> dd if=/dev/zero of=fsimage bs=1k count=64 Makes a 64K blank file called fsimage
> dd if=/dev/zero of=fsimage bs=1k count=64
fsimage
> mkfs -t ext4 ./fsimage Formats it to be an ext4 filesystem
> mkfs -t ext4 ./fsimage
> mount -o loop ./fsimage /mnt
> cd /mnt
We've been discussing a lot of device files, and in Unix things should present as much as possible as a file. There are two types of device files in Unix, those being character special and block special. A character special device is like a serial device, where data is read from it, but cannot be re-wound, i.e. there is no seeking to different points of the input. A block special device on the other hand is like a linear array of data, any point of which can be seeked to and read/written. In this way a block device is like a regular file with its data.
Normally device files are created for us automatically as required by the system, normally the udev system, and /dev itself is a special devtmpfs filesystem (like a tmpfs ram-drive.)
/dev
We can however create device files in /dev or any other file-system ourselves if need by using the mknod program.
mknod
# mknod name type [major minor]
# mknod
A normal user can use mknod to create FIFO (named pipes) files (it is probably easier to just use mkfifo which is specifically made for that task,) but to create character or block special devices requires super-user privileges.
mkfifo
The type can be 'c' or 'u' for a character special device, 'b' for a block special or 'p' for a named pipe. Named pipes do not require a major or minor number.
c
u
b
p
Major/minor numbers are used by the kernel to reference a specific device in the system and can be viewed on a block or character device using the ls command. They appear as comma separated numbers in the place a file-size would normally appear:
~> ls -la /dev/null /dev/sda1 crw-rw-rw- 1 root root 1, 3 Mar 16 10:49 /dev/null brw-rw---- 1 root disk 8, 1 Mar 16 10:49 /dev/sda1
Here we see that the /dev/null device has major #1 and minor #3 and the first partition on /dev/sda has major #8 and minor #1.
/dev/null
/dev/sda
You will likely rarely need to worry about device files, but you should be aware of the mknod command and how the kernel keeps track of what device is which via the major/minor number system.
man 4 fstab
The system file-system table, lists file-systems that the system should know about, including those devices that should be mounted at boot time. Even those devices that you don't want to mount at boot (mount option: noauto) should be added if they are periodically mounted so that they can be mounted by their mount point, rather than device name.
noauto
Format (by field):
The device or file (such as a swap file), may be a LABEL=X or UUID=X entry instead.
The mount point, where on the file-system the device should be mounted, if the device has no mount-point you can use anything here, such as none.
none
Type of FS, use auto if you want the kernel to try to automatically determine the file-system type being used (often used for removable media devices.)
auto
Mount options (comma separated)
Option What it does * defaults Mount with default options (rw,suid,dev,exec,auto,nouser,async) * noatime Don't update atime records on files * noauto Don't mount it explicitly (mostly used in /etc/fstab) nodev Don't interpret device files on the file-system * noexec Don't execute binaries on the file-system nosuid Don't allow setuid binaries on this file-system * remount Remounts the file-system with the new options * ro Mount file-system in read-only mode rw Mount FS in read-write mode (default) user Allow a user to mount owner Allow the device owner to mount * = remember these (may be useful in the future)
* = remember these (may be useful in the future)
Historically used by the "dump" command (used to backup file-systems to tape archival devices.) May be used by other programs.
"fsck" pass number for file-systems that need to be checked. Should generally be 1 for the first partition that should be checked on each device and 2 for the second etc.
# Lines starting with # are comments: # Device mnt-point fs-type mount-options dmp fsck-pass /dev/nvme0n1p2 swap swap defaults 0 0 /dev/nvme0n1p3 / ext4 defaults 1 1 /dev/nvme0n1p1 /boot/efi vfat defaults 1 0 #/dev/cdrom /mnt/cdrom auto noauto,owner,ro,comment=x-gvfs-show 0 0 /dev/fd0 /mnt/floppy auto noauto,owner 0 0 # These are "virtual" devices: devpts /dev/pts devpts gid=5,mode=620 0 0 proc /proc proc defaults 0 0 tmpfs /dev/shm tmpfs nosuid,nodev,noexec 0 0
/etc/mtab /proc/mounts
/etc/mtab
/proc/mounts
mount
# iostat [interval [count]]
# iostat
# vmstat [interval [count]]
# vmstat
Command What it does mkfs.* Make a file-system fsck.* Check/repair a file-system lsblk List block devices and partitions mknod Make a device file iostat View current I/O usage vmstat View current memory usage
mkfs.*
fsck.*
lsblk
iostat
vmstat
File/dir What it's for /dev The device files directory /dev/disk/by-uuid/* Storage devices listed by their UUID /dev/disk/by-label/* Storage devices listed by their label /etc/fstab The file-systems and mount-points registry /etc/mtab The mounted file-systems (maintained by mount) /proc/mounts Mounted file-systems (maintained by the kernel)