The file system manages how storage media is organized, so different operating systems support different file systems. Each file system has its own advantages and disadvantages. For example, when formatting a USB flash drive in Windows 7/Windows 10, options may include FAT, FAT32, NTFS, exFAT, and others.
If an SD card is formatted with Windows' native "NTFS" file system, some digital cameras or phones may not be able to read it. Formatting it as "FAT" limits single file size to 2GB, while "FAT32" allows files up to 4GB. Filesystems affect the handling capabilities of files, such as copying files from a Linux filesystem to a USB flash drive (usually formatted with FAT32), which may result in the loss of permissions and other information.
Windows can only be installed on the NTFS file system, leaving no other choices, but Linux offers more flexibility and can be installed on native ext2/ext3/ext4 or other contributed file systems like ReiserFS/xfs/Btrfs, among others. Some file systems are better suited for handling large files, while others are more efficient with small files or specifically designed for databases, giving users more choices and, at the same time, more decisions to make.
The file system is an essential element of a PC's operating system. For example, Microsoft's early operating systems mainly provided file system support and were named "Disk Operating System" (DOS). Linux's native file system is ext2 or ext3/ext4.
The ext2 file system is composed of two main components: "inodes" and "blocks." Inodes store metadata about files, while blocks contain the actual file content. Their functions are as follows:
The size of blocks determines the maximum file size that ext2 can handle, as shown in the table below:
ext2 limits @ Linux | ||||
block size | 1 KiB | 2 KiB | 4 KiB | 8 KiB (only supported by some Linux distributions) |
max file size | 16 GiB | 256 GiB | 2 TiB | 2 TiB |
max filesystem size | 4 TiB | 8 TiB | 16 TiB | 32 TiB |
However, it's not necessarily better to plan for larger blocks. Larger blocks may lead to wasted disk space because blocks are the smallest units that can be filled. If a block isn't fully utilized, the remaining space will be wasted. For example, if a block is set to 4K but only 1K of data is written, the remaining 3K will be unused.
Furthermore, when formatting advanced format drives, the block size should be set as a multiple of 4k; otherwise, it can severely impact performance.
These are just a few examples of device files in Linux. The /dev directory contains many other device files, each representing a different hardware device or interface in the system.
In the above table, the focus is on item 1 "/dev/sda, /dev/sdb, ..." which represents block device files in Linux. These device files are crucial for later operation examples as they allow interaction with physical storage devices such as hard drives and USB drives. When performing tasks like formatting, partitioning, or managing storage devices in Linux, it is essential to work with the appropriate device file, such as "/dev/sda" or "/dev/sdb." Caution should be exercised while using these device files, as any actions performed on them directly affect the data on the associated storage devices. Always verify the device you are working with to avoid unintended consequences.
$ df Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda5 13172924 3837376 8655600 31% / /dev/sda2 4956316 154760 4545724 4% /home /dev/sda1 194442 12060 172343 7% /boot tmpfs 514304 12 514292 1% /dev/shm /dev/sdb1 31210416 88736 31121680 1% /media/disk |
In the above example, the "Filesystem" column shows two devices, "/dev/sda#" (# being a number) and "/dev/sdb#", indicating the presence of two hard drives. For instance, "dev/sda" has four partitions: "/dev/sda5", "/dev/sda2", and "/dev/sda1", mounted at "/", "/home", and "/boot", respectively.
The "tmpfs" ("/dev/shm") is not a physical disk but rather a type of RAM Disk. It is like a virtual disk in memory and is present in most Linux distributions by default. Data stored in "/dev/shm" operates at RAM speed, making it useful for temporary file operations. However, remember that data in "/dev/shm" is volatile and will be lost upon reboot.
The main options are:
To check the location of a specific file, append the file path to df:
Example: $ df -T ←Displays the filesystem type of each partition
Filesystem Filesystem Type 1K-blocks Used Available Use% Mounted on /dev/sda5 ext3 13172924 3876612 8616364 32% / /dev/sda2 ext3 4956316 154656 4545828 4% /home /dev/sda1 ext3 194442 12060 172343 7% /boot tmpfs tmpfs 514304 12 514292 1% /dev/shm /dev/sdb1 ext2 20635700 44992 19542472 1% /media/DB /dev/sdc1 vfat 31195120 16 31195104 1% /media/disk $ df -t ext3 -t vfat ←Displays only "ext2" and "vfat" Filesystem Type 1K-blocks Used Available Use% Mounted on /dev/sdb1 ext2 20635700 44992 19542472 1% /media/DB /dev/sdc1 vfat 31195120 16 31195104 1% /media/disk |
$ df -h /bin/bash ←Check the location of the file "/bin/bash"
Filesystem Size Used Avail Use% Mounted on Filesystem Size Used Avail Use% Mounted on /dev/sda5 3.9G 3.6G 173M 96% / |
$ du /etc ←Displays the size of subdirectories under the directory "/etc" 40 /etc/redhat-lsb 8 /etc/yum/pluginconf.d 24 /etc/yum du: `/etc/audisp': Permission denied 32 /etc/samba 8 /etc/sysconfig/modules 388 /etc/sysconfig/network-scripts ... (continued) $ du /etc | sort -n ←Sort by directory size |
By default, the sizes are displayed in 1024 bytes. The option du -h is used to display sizes in KB, MB, or GB.
Other commonly used options include:$ du -sh ~/←Displays the total size of the home directory 16M /home/aaa/ 16M /home/aaa/ |
blkid is a useful tool to gather information about hard drives, and its usage is straightforward, as shown in the following example.
Example:# blkid ←Displays disk information /dev/sda5: LABEL="/" UUID="24328158-f8c3-4c5e-9be0-cbd1ceee581f" SEC_TYPE="ext2" TYPE="ext3" /dev/sda1: LABEL="/boot" UUID="8ab1dede-36dd-4469-a174-ffb04f9ee6df" SEC_TYPE="ext2" TYPE="ext3" ... (continued) |
The main information provided includes the device file, label name, UUID, and filesystem type. The UUID (Universally Unique Identifier) is a unique number generated during formatting, and it can be used for mounting, LVM, or RAID to identify the hard drive or partition. It remains constant even if the Linux kernel is upgraded, BIOS settings are changed, or a different Linux distribution is used.
To display specific information, you can use the "-s" option followed by [LABEL], [UUID], or [TYPE]. For example:
Example:# blkid -s TYPE ←Displays the filesystem type for each partition /dev/sda5: TYPE="ext3" /dev/sda1: TYPE="ext3" /dev/sda2: TYPE="ext3" /dev/sda3: TYPE="swap" /dev/sdb1: TYPE="ext3" /dev/sdc1: TYPE="vfat" # blkid -s LABEL ←Displays the label name for each filesystem (only if labeled) /dev/sda5: LABEL="/" /dev/sda1: LABEL="/boot" /dev/sda2: LABEL="/home" /dev/sda3: LABEL="SWAP-sda3" /dev/sdb1: LABEL="data" /dev/sdc1: LABEL="PEN_FLASH" # blkid -s UUID ←Displays the UUID for each partition /dev/sda1: UUID="9da5bd9a-300f-42f8-8d32-49af244a4c4f" /dev/sda2: UUID="374b2969-3c63-438f-8762-3db53f96f409" /dev/sda3: UUID="a2e9407d-8a07-4d68-8a4a-ebd712280f10" |
To customize the output related to devices, you can use the "-o" option followed by [device], [full], or [value]. For example:
Example:# blkid -o device ← Displays the device files for each device /dev/sda5 /dev/sda1 /dev/sda2 /dev/sda3 /dev/sdb1 /dev/sdc1 |
The benefits of partitioning a hard disk into multiple partitions include:
However, the main drawback of partitioning is that if the physical hard disk fails, all the partitions on that disk may become inaccessible. To mitigate this risk, one can consider distributing data across partitions on separate physical disks.
The concept of disk partitioning is not specific to any particular operating system. Different operating systems may have varied terminology and support different partitioning tables. Two commonly used partitioning tables are the traditional Master Boot Record (MBR) and the newer GUID Partition Table (GPT), both of which are well-supported by Linux.
SATA Hard Disk | ||||
SATA Hard Disk @port 1 | /dev/sda1 | /dev/sda2 | /dev/sda3 | /dev/sda4 |
SATA Hard Disk @port 2 | /dev/sdb1 | /dev/sdb2 | /dev/sdb3 | /dev/sdb4 |
Partitioning of the Hard Disk:2 Primary Partitions+3 Logical Partitions | |||||||
Primary Partition 1 |
Primary Partition 2 |
Extended | → | Logical Partition 1 |
Logical Partition 2 |
Logical Partition 3 |
|
/dev/sda1 | /dev/sda2 | Extended | → | /dev/sda5 | /dev/sda6 | /dev/sda7 |
$ df Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda5 13172924 3837376 8655600 31% / ←logical partition /dev/sda2 4956316 154760 4545724 4% /home /dev/sda1 194442 12060 172343 7% /boot tmpfs 514304 12 514292 1% /dev/shm /dev/sdb1 31210416 88736 31121680 1% /media/disk |
The extended partition cannot be directly used for data storage and must be further divided into logical partitions. Logical partitions are numbered starting from 5.
In summary, regardless of the number of partitions on a hard disk, only primary partitions can be set as the "Active Partition," which is the partition from which the system boots. Logical partitions cannot be set as the active partition.
Ordinary users are not allowed to perform dangerous fdisk operations. Therefore, it is necessary to log in as the Superuser (root) to carry out partitioning. If the physical machine is not accessible, or BIOS information is unavailable, sometimes it is not known how many hard drives are present inside the PC. In such cases, the fdisk -l option can be used to list the hard drive information.
Example: (logged in as root)# fdisk -l ←List hard drive information Disk /dev/sda: 21.4 GB, 21474836480 bytes ←First hard drive /dev/sda 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0x000243c8 Device Boot Start End Blocks Id System /dev/sda1 * 1 25 200781 83 Linux ←* denotes the boot partition /dev/sda2 26 662 5116702+ 83 Linux /dev/sda3 663 789 1020127+ 82 Linux swap / Solaris /dev/sda4 790 2610 14627182+ 5 Extended /dev/sda5 790 2610 14627151 83 Linux Disk /dev/sdb: 21.4 GB, 21474836480 bytes ←Second hard drive /dev/sdb 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0x00000000 Disk /dev/sdb doesn't contain a valid partition table ←/dev/sdb is a new, unpartitioned hard drive |
The fdisk command can list how many partitions are created on a hard drive. In the example above, the first hard drive "/dev/sda" is divided into five partitions, named "/dev/sda1" to "/dev/sda5". However, fdisk does not provide information about the mounted partitions. To identify the mount points of "/dev/sda1" to "/dev/sda5", one needs to cross-reference with the information provided by the df command.
If a hard drive is new and unpartitioned, like the second hard drive "/dev/sdb" in the example, it does not have any partition numbers (e.g., "/dev/sdb1"). Next, let's demonstrate how to partition this hard drive.
To operate on a specific hard drive, you can add the hard drive device file after the "fdisk" command. If you are unsure about the device file name for your Linux system, whether it is "/dev/sda" or "/dev/hda," you can use either the fdisk -l or ls /dev/[sh]d command to confirm.[Note]
# ls /dev/[sh]d* ←List all available hard drives or partitions /dev/sda /dev/sda1 /dev/sda2 /dev/sda3 /dev/sda4 /dev/sda5 /dev/sdb |
In the example provided, the output lists available hard drives and partitions, such as "/dev/sda", "/dev/sda1", "/dev/sda2" and so on. For instance, "/dev/sda" refers to the whole hard drive, while "/dev/sda1", "/dev/sda2" etc., are individual partitions on that hard drive.
The output also includes "/dev/sdb," which has no number appended (e.g., "/dev/sdb1"). This indicates that no partition has been created on this hard drive yet. To partition this hard drive, you can proceed with the following steps:
Example: (logged in as root)# fdisk /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x9939c1b3. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. The number of cylinders for this disk is set to 2610. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with: 1) software that runs at boot time (e.g., old versions of LILO) 2) booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK) Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) Command (m for help): ←For a list of available commands, press <m> |
Command (m for help): m Command action a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition l list known partition types m print this menu n add a new partition o create a new empty DOS partition table p print the partition table q quit without saving changes s create a new empty Sun disklabel t change a partition's system id u change display/entry units v verify the partition table w write table to disk and exit x extra functionality (experts only) |
Command (m for help): n ←Create a new partition Command action e extended ←Create an extended partition p primary partition (1-4) ←Create a primary partition p ←Press <p> to create a primary partition Partition number (1-4): 1 ←Enter the number for the new primary partition First cylinder (1-2610, default 1):↵ Enter ←Press <Enter> to use the default value for the starting cylinder Using default value 1 Last cylinder or +size or +sizeM or +sizeK (1-2610, default 2610): +10000M ←Enter the ending cylinder or size for this partition (you can input "+xxxG" or "+xxxM" for size) or press <Enter> to allocate all remaining space. Command (m for help): p ←Display the current partition table to confirm Disk /dev/sdb: 21.4 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0x378a1ba0 Device Boot Start End Blocks Id System /dev/sdb1 1 1217 9775521 83 Linux Command (m for help): w ←Write the partition table and exit The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. You have new mail in /var/spool/mail/root |
The size of a partition on a hard drive is calculated as the product of the number of cylinders, heads, sectors, and 512 bytes. However, you don't need to calculate it manually while partitioning; let fdisk handle the conversion for you. For example, when you use 'n' to create a new partition, you'll be prompted to enter the starting cylinder. Pressing <Enter> will use the default value, and then you can input "+sizeG" or "+sizeM" to specify the size of the partition, or simply press <Enter> to allocate all the remaining space to this partition.
If you make any changes to the partition, you'll need to restart the system, as Linux kernel loads the partition table only during boot-up. While you can use the partprobe command to force the kernel to update the partition table, it's safer to restart the system after any changes to the partitions.
The example only demonstrates creating one primary partition, but if you're interested, you can experiment with deleting partitions or adding more primary/extended partitions to become more familiar with the process.
During the partitioning process, you can press 'p' to display the current partition table, and if you want to create non-Linux partitions, you can use 't' to change the partition ID. Pressing 'l' (lowercase L) will list all the supported partition types and their IDs.
Traditional BIOS reads the MBR (Master Boot Record) of the disk during boot-up, and due to the limitations of MBR, partitions cannot exceed 2 TB, which becomes inadequate for modern requirements.
To replace the old BIOS, Intel led the development of a new interface called "EFI" (Extensible Firmware Interface), also known as UEFI (Unified Extensible Firmware Interface). EFI redefines the disk partition table and boot process.
One of the advantages of GPT is that it eliminates the limitations of MBR's maximum four primary partitions. As a result, there is no need for extended partitions and logical partitions. Thus, a single hard drive can have more than four operating systems installed as bootable partitions, and each partition can exceed 2.19 TB in size.
As hard drives continue to grow in size, using GPT for partitioning and adopting advanced formatting for hard drives and SSDs has become an irreversible trend. parted provides more support than fdisk and is recommended to replace the outdated fdisk.
Parted can be used in interactive mode and command-line mode. The interactive mode is more user-friendly and less error-prone, while the command-line mode is faster and suitable for bulk and quick operations.
# parted GNU Parted 3.1 ← Displays the version Using /dev/sda← Default disk for operations Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) ← Interactive mode prompt |
(parted) print Model: ATA ST3320813AS (scsi) Disk /dev/sda: 320GB Sector size (logical/physical): 512B/512B Partition Table: gpt Number Start End Size Type File system Flags 1 32.3kB 206MB 206MB primary ext3 boot 2 206MB 5445MB 5240MB primary ext3 3 5445MB 6490MB 1045MB primary linux-swap 4 6490MB 21.5GB 15.0GB extended 5 6490MB 21.5GB 15.0GB logical ext3 |
(parted) print devices /dev/sda (320GB) /dev/sdb (8070B) |
(parted) select /dev/sdb ← Selecting "/dev/sdb" for operation using /dev/sdb |
(parted) mklabel gpt ← Using GPT partition to partition the disk Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to continue? ← If there are existing partitions on the disk, there will be a warning Yes/No? ← Press <y> to confirm, <n> to cancel |
(parted) select /dev/sdb ← Select the device "/dev/sdb" (use "print all" to confirm the device if unsure)) Using /dev/sdb (parted) mklabel gpt ←Use GPT partition to partition the disk (parted) mkpart data1 ext2 0 5000 ←Create an ext2 partition named "data1" with a size of 5GB (parted) mkpart data2 ext2 5000 100% ←Create another partition with a size of 5GB to use the remaining space (parted) print free ← List the newly created partitions to verify Model: ATA FUJITSU MHR2020A (scsi) Disk /dev/sdb: 20.0GB Sector size (logical/physical): 512B/512B Partition Table: gpt Number Start End Size File system Name Flags 1 17.4kB 5000MB 5000MB data1 2 5000MB 20.5GB 15.5GB data2 |
(parted) mkpart primary linux-swap 0 1G ←Create a primary 1GB swap partition |
(parted) mkpart Partition type? primary/extended? extend Start? 1000 100% (parted) mkpart Partition type? primary/logical? logical File system type? [ext2]? ext2 Start? 1000 End? 30% |
Supported flag types for Linux include "boot," "hidden", "raid", "lvm" and more. However, for Linux, the valid flag types are "boot", "raid" and "lvm" used to set the boot partition and LVM / RAID partitions.
(parted operation)(parted) set 1 boot on ← Set partition 1 as the boot partition |
(parted) help mkpart ← View detailed instructions for the "mkpart" command |
(parted) name 2 "DB data" ←Set partition 2 name as "DB data" |
(parted) rm 2 ←Delete partition 2 |
(parted) unit GB ← Set the unit to GB |
(parted) align-check optimal 1 ←Check if partition 1 is aligned 1 aligned |
In the "command-line mode", if you are familiar with the interactive mode of parted, the command mode will be easy. Simply chain the commands and parameters used in the interactive mode after parted.
For example, to partition an empty hard disk "/dev/sdc" with a GPT table into four equally sized partitions, where partition 3 is a swap partition, and then verify the partitions, you can use the following commands:
# parted /dev/sdc mklabel gpt # parted /dev/sdc mkpart p1 ext2 0% 25% # parted /dev/sdc mkpart p2 ext2 25% 50% # parted /dev/sdc mkpart p3 linux-swap 50% 75% # parted /dev/sdc mkpart p4 ext2 75% 100% # parted /dev/sdc print ←View partition table Model: ST964032 2AS (scsi) Disk /dev/sdc: 640GB Sector size (logical/physical): 512B/512B Partition Table: gpt Number Start End Size File system Name Flags 1 17.4kB 160GB 160GB p1 2 160GB 320GB 160GB p2 3 320GB 480GB 160GB p3 4 480GB 640GB 160GB p4 |
A common option is "-s" to display a summary of partition content.
Example: # partprobe -s ←Display a summary of partition content /dev/sda: gpt partitions 1 2 3 4 5 /dev/sdb: gpt partitions 1 2 3 5 6 7 8 9 4 |
After partitioning with tools like fdisk or parted, partitions need to be formatted before they can be used properly. The file system used for formatting should match the filesystem type (ID) set for the partition during partitioning. For example, if parted is used to set the filesystem type to ext2, the partition can be formatted as ext2, ext3, or ext4, but not as FAT32 or any other file system, as compatibility issues may arise.
The reason for formatting partitions with ext2/ext3/ext4 file systems is that formatting involves allocating inodes and specifying block sizes. If there were data in the original filesystem, it will be erased after formatting. Therefore, caution should be exercised during formatting, and only the superuser (root) can perform this operation. The commonly used formatting tool in Linux is mkfs.
Please note that you cannot format a partition that is already mounted.
Example:# ls /sbin/mkfs.* ←heck which file systems are supported for formatting /sbin/mkfs.btrfs /sbin/mkfs.ext3 /sbin/mkfs.minix /sbin/mkfs.xfs /sbin/mkfs.cramfs /sbin/mkfs.ext4 /sbin/mkfs.msdos /sbin/mkfs.ext2 /sbin/mkfs.fat /sbin/mkfs.vfat /sbin/mkfs.exfat |
Syntax: mkfs [-otpiton] [fs][-option] device | ||
Command name/function/command user | Options | Function |
mkfs/ format filesystem/ root |
-t | Specifies the formatted file system, commonly used as follows: ext2, ext3 or ext4: Linux native file system. ntfs: Windows NT, the file system above XP. vfat: Supports FAT with long file names (FAT32). exfat: exFAT xfs: a log-type high-efficiency file system msdos: only supports FAT with 8.3 file names. |
-b | Specifies the block size (not applicable to vfat/msdos/ntfs). |
|
-L | Set the partition label (not applicable for vfat/msdos/ntfs). | |
fs | The options that can be connected after "fs" are as follows: -c: Check to see if there is any bad track and then format. -l: (lowercase L) read the file output by badblocks . -v: Display the formatting process |
# mkfs /dev/sdb1 ←If no options are specified, the default formatting filesystem is ext2 # mkfs -t ext4 /dev/sdb ←Format as ext4 # mkfs -t ext3 -b 2048 -L DB /dev/sdb2 ←Format as ext3 with a block size of 2048, and label it as "DB" # mkfs fs -c /dev/sdb1 ←Check for bad blocks before formatting # mkfs -t vfat /dev/sdc1 ←Format as FAT32 # mkfd -t msdos /dev/fd0 ←Format the floppy disk as msdos |
# tune2fs -j /dev/sdb1 ←Upgrades /dev/sdb1 from ext2 to ext3 |
Syntax: tune2fs [-otpiton] device | ||
Command name/function/command user | Options | Function |
tune2fs/ tune to filesystem/ root |
-l: (lowercase L) | Lists filesystem superblock and other information. |
-c # | Sets the number of mounts before an automatic filesystem check (# is a number; set to 0 or -l to disable). | |
-i#[dmw] | Sets the time interval for automatic filesystem checks (# is a number; set to 0 or -l to disable; d for days, m for months, w for weeks). | |
-m # | Reserves a percentage of blocks for the system (# is a number; the default is usually 5%). |
|
-r # | Sets the number of reserved blocks (# is a number). |
|
-j | Converts ext2 to ext3. | |
-L | Sets the partition label (similar to e2label or mkfs -L). |
|
-o | Additional mount options. |
# tune2fs -L "share" /dev/sdb1 ← Sets the label of /dev/sdb1 to "share" # tune2fs -l /dev/sda2 | grep "Last mount" ← Checks the last mount time # tune2fs -m 10 /dev/sdb1 ← Reserves 10% of blocks # tune2fs -i 3m /dev/sdb1 ← Checks the filesystem after three months # tune2fs -c 100 /dev/sdb1 ← Sets automatic check after 100 mounts # tune2fs -c -1 /dev/sdb1 ← Disables automatic check on mounts |
# e2label /dev/sdc1 ← Display the current label lab_data # e2label /dev/sdc1 "hello world" ← Set the label of /dev/sdc1 to "hello world" |
fsck is a front-end program similar to mkfs and performs file system checks and repairs specific to different file systems like ext2, ext3, ext4, vfat, or e2fsck. You can check the supported file systems by running the command ls /sbin/fsck.*.
During startup, the system may automatically run fsck based on mount counts or mount intervals to determine whether file system checks and repairs are needed. This information can be displayed using the tune2fs -l DEVICE command.
It's important to note that running fsck on a mounted file system can result in data corruption. Therefore, it's essential to unmount the file system before using fsck for checking and repair.
You can specify the file system to be checked by using either the device file (e.g., fsck /dev/sda2) or the mount point (e.g., fsck /home).
For automatic checks on partitions listed in "/etc/fstab", you can use fsck -A. The "-AR" option is similar to "-A" but excludes the root directory, often used to check all partitions except the root directory.
To illustrate further, here are some examples:# umount /home # fsck /home |
Running "fsck" on partitions listed in "/etc/fstab":
# init 1 # umount -a # fsck -AR |
UNIX directory structure observation reveals that crucial directories like "/bin" and "/dev" are on the same partition as the root "/", making it impossible to unmount the root directory. To address this, the highest priority for checking is assigned to the root partition in "/etc/fstab". If needed, a forced fsck check can be triggered using shutdown -rF now.
Please note that these commands and instructions are based on Linux file system maintenance.
When working with Windows, connecting recognized filesystems like hard drives, CDs, floppies, or USB flash drives automatically results in their immediate "mounting," making them readily accessible. This behavior seems natural and expected. However, in the UNIX world, the process is quite different. Adding new storage media requires system administrator intervention to "mount" them before they can be accessed. (If authorized, regular users can also perform mount and umount operations by setting the "user" option in the "/etc/fstab" file.)
In modern Linux distributions, such as Fedora or CentOS, the approach to mounting storage media falls between that of Windows and UNIX. For instance, when running graphical interfaces (e.g., GNOME desktop environment), recognized filesystems on CD-ROMs, USB flash drives, or hard drives are automatically mounted. However, if you log in through a text interface, the UNIX tradition of not automatically mounting new filesystems is retained.
While automated mounting of storage media offers convenience, it also poses security risks. For example, if sensitive data is on a disk and it's automatically mounted, anyone can access its contents, undermining security. As a result, many Linux servers are configured to boot and operate in runlevel 3 (init = 3), a text-based mode. In this mode, filesystems need to be manually "mounted" by a system administrator before their contents can be accessed.
Due to the interference caused by the "gnome-volume-manager," which automatically mounts new storage media in the Fedora/CentOS GNOME desktop environment, the subsequent explanations and operations will revert to a more basic text-based approach. To achieve this, edit the "/etc/inittab" file and locate the line "id:5:initdefault:". Change it to "id:3:initdefault:" and then restart. This change ensures the system logs in to runlevel 3, the text-based interface, on reboot. This approach aims to closely replicate the original UNIX operations.
(Note: This information was last updated on June 17, 2019.)
In newer versions of Fedora/CentOS (like CentOS 7 or later), the concept of "target" is introduced to replace the use of the configuration file "/etc/inittab" for setting the runlevel during boot. Different boot modes (targets) can be configured using systemctl.
Example: (For CentOS 7/CentOS 8)# systemctl set-default graphical.target ← Set graphical mode boot (default runlevel = 5) # systemctl set-default multi-user.target ← Set text-based boot (runlevel = 3, requires a reboot) # # systemctl get-default ← View the current default mode # systemctl list-units --type=target ← View available targets |
# mount ← Display the current mounting status /dev/sda2 on / type ext3 (rw) /dev/sda3 on /home type ext3 (rw) /dev/sda1 on /boot type ext3 (rw) |
The "-t" option specifies the filesystem type("FS_TYPE") to mount (ext2, vfat, auto, etc.). The "DEVICE" refers to the device file or UUID/PARTUUID or label of the partition to be mounted, while "DIR" is the path of the mount point.
For instance, to mount the filesystem on the partition "/dev/sdb1" from a hard drive, you first need a mount point. In the following example, we create a mount point at /mnt/db to mount the filesystem from "/dev/sdb1".
Example:# mkdir -p /mnt/db ←Create a directory `/mnt/db` as the mount point # echo > /mnt/db/test_file ←Create a file `test_file` in the mount point directory (used for testing if the contents are temporarily hidden after mounting) # mount -t ext2 /dev/sdb1 /mnt/db ← Mount the filesystem from `/dev/sdb1` to the mount point `/mnt/db` # ls /mnt/db/test_file ←Test if the newly created file `test_file` is present after mounting the filesystem ls: cannot acces/mnt/db/testfile: No such file or directory ←The file is temporarily hidden within the directory when mounted # chmod o+rwx /mnt/db ←Change the permissions of the mount point directory to allow others to read and write to this filesystem |
In this example, successful mounting results in accessing the directory "/mnt/db", which effectively accesses the filesystem on "/dev/sdb1". Consequently, the original files within the directory become temporarily invisible. To verify the mounting status, you can enter mount or df to display the status of various filesystems.
For ext2/ext3/ext4/Btrfs or xfs filesystems, after mounting, other users may be unable to write to the filesystem. This is because, regardless of the original mount point owner, the owner is changed to the person performing the mount (e.g., the owner becomes root when mounted by the root user). To allow others to write to the newly mounted filesystem, you can use chmod after mounting (as shown in the last step of the example). This change must be performed after mounting, as the mount operation adjusts the mount point's permissions based on umask.
If the "-t" option is omitted, mount will attempt to mount using various "FS_TYPE" values listed in "/etc/filesystems". However, this approach can sometimes result in misjudgments. It is best used when unsure of the filesystem type on a USB flash drive/CD-ROM or floppy, or when the "FS_TYPE" is specified in the third column of "/etc/fstab".
Omitting the "DEVICE" can only be done when the first column of "/etc/fstab" already contains the" DEVICE", or when remounting (e.g., mount -o remount,rw /home).
For example, when "/etc/fstab" lists the "DEVICE" as "/dev/sdc2" and the mounted filesystem is "ext3", you can mount the partition with only the mount point specified.
Example:# cat /etc/fstab ←List the contents of `/etc/fstab` | |||
/dev/sdc2 | /mnt/pub | ext3 | noauto,user |
↑ | ↑ | ||
The first column lists the `DEVICE` as `/dev/sdc2` | The third column lists the filesystem as `ext3` | ||
# | |||
# mount /mnt/pub ←Mount the partition listed in `/etc/fstab` with only the mount point specified |
The "-o" option allows additional control over the mounting settings. The parameters that "-o" accepts can be referenced from the fourth column of "/etc/fstab" as mount options (e.g., mount -o noatime /dev/sda1 /media mounts the filesystem without updating access times).
Examples:# mount -t ext2 -o ro /dev/sdc1 /mnt/web ←Mount the filesystem as read-only # mount -t ext3 -o data=journal /dev/sda2 /home ←Specify the journaling level for ext3 as `journal` |
Many tools heavily rely on "/etc/mtab" to determine mounting status, such as for file capacity quotas.
# mount UUID=24328158-f8c3-4c5e-9be0-cbd1ceee581f /media/vol1 ←Mount using UUID # mount -U 24328158-f8c3-4c5e-9be0-cbd1ceee581f /media/vol1 ←Mount using UUID (same as above) # mount PARTUUID=dad0c110-0a71-4e46-935a-304969ea36ae /media/vol3 ←Mount using PARTUUID |
# mount -t vfat /dev/sdc1 /media/flash ←Mount a USB flash drive (FAT filesystem) # mount -t exfat /dev/sdd /media/flash ←Mount a USB flash drive (exFAT filesystem) # mount /dev/floppy /media/floppy1 ←Mount a floppy disk |
However, note that for FAT filesystems, the chmod command doesn't work due to the lack of inode information. You can use the "-o" umask option to change permissions.
Example:# mount -t vfat -o umask=0 /dev/sdc1 /media/flash ←Mount a USB flash drive with full access for all users (FAT filesystem) |
Most Linux distributions generate a device file "/etc/cdrom" or similar when a CD-ROM drive is detected. You can use this device file to mount CDs.
Example: # ls -l /dev/cdrom ←Check the CD-ROM device file # mount /dev/sr0 /media/cdrom ←Mount a CD-ROM using the actual device file # mount /dev/cdrom /media/cdrom ←Mount a CD-ROM using the symlink /etc/cdrom # mount -t iso9660,udf /dev/cdrom /media/cdrom ← Mount a CD-ROM with possible filesystems (iso9660 and udf) |
To automatically mount filesystems at system startup, you can configure the "/etc/fstab" file. This file contains information about filesystems that need to be mounted at boot time. You can also use the mount -a command to remount filesystems listed in "/etc/fstab" after making changes to the file.
Here is an example of an "/etc/fstab" file:# cat /etc/fstab | ||||||
LABEL=/ | / | ext3 | defaults | 1 | 1 | # Mount by label |
UUID=7f382385-308c-4684-a8d3-651ae16836f4 | /home | ext3 | defaults | 1 | 2 | # Mount by UUID |
LABEL=/boot | /boot | ext3 | defaults | 1 | 2 | |
tmpfs | /dev/shm | tmpfs | defaults | 0 | 0 | |
devpts | /dev/pts | devpts | gid=5,mode=620 | 0 | 0 | |
sysfs | /sys | sysfs | defaults | 0 | 0 | |
proc | /proc | proc | defaults | 0 | 0 | |
LABEL=SWAP-sda3 | swap | swap | defaults | 0 | 0 | |
LABEL=data | /mnt/doc | ext3 | noauto,user | 0 | 0 | |
/dev/sdc1 | /media/usb | auto | noauto,user,umask=0 | 0 | 0 | # Mount by device name |
1 | 2 | 3 | 4 | 5 | 6 | ←Number of fields |
device or label or UUID name | mount point | fs-type | options | dump | pass | ← meaning |
Using the command ls -l /bin/mount, you can observe that the mount utility possesses the special suid special permissions, indicating that non-root users can also utilize the mount command. But how can they use it? For regular users to operate the mount command, relevant information needs to be written into the "/etc/fstab" file.
For instance, in the author's "/etc/fstab" file, the mounting options (options) in the second-to-last line are as follows:# cat /etc/fstab | tail -n2 | head -n1 ←lists the second-to-last line of the "/etc/fstab" file | |||||
LABEL=data | /mnt/doc | ext3 | noauto,user | 0 | 0 |
↑ | |||||
← Not automatically mounted at boot (noauto) & allowed for any user to mount (user) |
The use of the "noauto" option ensures that the filesystem won't be automatically mounted during system startup, contributing to enhanced security. With the "user" option in place, regular users are empowered to manually mount the filesystem labeled as "data" (as indicated by "LABEL=data") whenever they require read and write access.
When the content is recorded in the "/etc/fstab" file, you can mount it simply by entering the mount point. Here is an example of mounting the filesystem as a regular user:
Example (Tested with a regular user login):$ mount /mnt/doc |
# tail -n1 /etc/fstab ←Display the last line of "/etc/fstab" | |||||
/dev/sdc1 | /media/usb | auto | noauto,user,umask=0 | 0 | 0 |
↑ | ↑ | ||||
When unsure about the filesystem of the USB flash drive, set it to auto | To allow general users to read/write the FAT filesystem of the USB flash drive, set umask=0 |
# umount /mnt/doc ←Unmount using the mount point (recommended) # umount /dev/sdc1 ←Unmount using the device file (not recommended, as it might mistakenly unmount a different filesystem when using UUID or label to mount) |
For devices like CDs, USB flash drives, or floppies, it's necessary to unmount them before removing. If a filesystem is currently in use, the unmount operation will not proceed. In such cases, umount -l can be used, which will wait until no one is using the filesystem before unmounting. On the other hand, umount -f is a forceful unmount and is recommended only for NFS (Network File System) to allow administrators to forcibly disconnect.
Furthermore, umount -a unmounts all filesystems according to the mount settings specified in "/etc/fstab."
To mount virtual devices, the "-o loop" option is used.
Example:# mount -t iso9660 -o loop ~/Fedora-DVD.iso /media/cdrom/ ←Mount an ISO image of a CD # mount -o loop floppy1.img /media/floppy/ ←Mount an image of a floppy disk |
To perform the upgrade, follow these steps:
Backup your data: Before making any changes, it is always recommended to back up your data to prevent accidental data loss.
Unmount the ext3 file system: Make sure the ext3 file system is not mounted. You can use the umount command to unmount it if it's currently mounted.
Check the file system: Before upgrading, you should run a file system check on the ext3 file system to ensure there are no errors. You can use the e2fsck command for this:
e2fsck -f /dev/your_ext3_partitionOnce the conversion is complete, your ext3 file system will be using the ext4 features, such as extents and directory indexing. However, it's essential to note that converting from ext3 to ext4 does not reclaim the space used by the old inode table. If you want to make the most of the ext4 file system, it is recommended to create a new file system from scratch (after backing up your data) rather than converting from ext3.