Linux Disk Management
The quality of Linux disk management directly affects the performance of the entire system.
Commonly used Linux disk management commands are df
, du
, and fdisk
.
df (Full name: disk free): Lists the overall disk usage of the file system.
du (Full name: disk used): Checks the disk space usage.
fdisk: Used for disk partitioning.
df
The df command function: Checks the disk space usage of the file system. This command can be used to obtain information about how much space is occupied on the hard drive and how much space is still available.
Syntax:
df [-ahikHTm] [directory or filename]
Options and parameters:
-a: Lists all file systems, including system-specific file systems like /proc.
-k: Displays the capacity of each file system in KBytes.
-m: Displays the capacity of each file system in MBytes.
-h: Displays in a more human-readable format such as GBytes, MBytes, KBytes.
-H: Uses a M=1000K instead of M=1024K conversion.
-T: Displays the file system type, including the filesystem name (e.g., ext3) of the partition.
-i: Displays the number of inodes instead of disk capacity.
Example 1
Lists all file systems in the system!
[root@www ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/hdc2 9920624 3823112 5585444 41% /
/dev/hdc3 4956316 141376 4559108 4% /home
/dev/hdc1 101086 11126 84741 12% /boot
tmpfs 371332 0 371332 0% /dev/shm
In Linux, if df is used without any options, it will list all file systems (excluding special memory-based file systems and swap) with a capacity of 1 Kbytes by default!
Example 2
Displays the capacity results in a human-readable format.
[root@www ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/hdc2 9.5G 3.7G 5.4G 41% /
/dev/hdc3 4.8G 139M 4.4G 4% /home
/dev/hdc1 99M 11M 83M 12% /boot
tmpfs 363M 0 363M 0% /dev/shm
Example 3
Lists all special file formats and names in the system.
[root@www ~]# df -aT
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/hdc2 ext3 9920624 3823112 5585444 41% /
proc proc 0 0 0 - /proc
sysfs sysfs 0 0 0 - /sys
devpts devpts 0 0 0 - /dev/pts
/dev/hdc3 ext3 4956316 141376 4559108 4% /home
/dev/hdc1 ext3 101086 11126 84741 12% /boot
tmpfs tmpfs 371332 0 371332 0% /dev/shm
none binfmt_misc 0 0 0 - /proc/sys/fs/binfmt_misc
sunrpc rpc_pipefs 0 0 0 - /var/lib/nfs/rpc_pipefs
Example 4
Displays the available disk capacity of the /etc directory in a human-readable format.
[root@www ~]# df -h /etc
Filesystem Size Used Avail Use% Mounted on /dev/hdc2 9.5G 3.7G 5.4G 41% / Device Boot Start End Blocks Id System /dev/xvdb2 1 2610 20964793+ 83 Linux
Example 2
Find the disk where the root directory of your system is located, and check the relevant information within that disk.
[root@www ~]# df /
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/hdc2 9920624 3823168 5585388 41% /
[root@www ~]# fdisk /dev/hdc
The number of cylinders for this disk is set to 5005.
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)
Command (m for help):
After entering m
, you will see the following command introductions:
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)
Pressing q
when exiting fdisk will not save any actions! Conversely, pressing w
means the actions will take effect.
Command (m for help): p
Disk /dev/hdc: 41.1 GB, 41174138880 bytes
255 heads, 63 sectors/track, 5005 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/hdc1 * 1 13 104391 83 Linux
/dev/hdc2 14 1288 10241437+ 83 Linux
/dev/hdc3 1289 1925 5116702+ 83 Linux
/dev/hdc4 1926 5005 24740100 5 Extended /dev/hdc5 1926 2052 1020096 82 Linux swap / Solaris
Device file name Bootable Start cylinder End cylinder 1K block size System within the disk partition
Command (m for help): q
Want to exit without saving? Press q and that's it! Don't press w randomly!
Using `p` can list the partition table information of the current disk, the upper part of this information displays the overall status of the disk.
---
### Disk Formatting
After partitioning the disk, the next step is to format the file system. The formatting command is very simple, using the `mkfs` (make filesystem) command.
Syntax:
mkfs [-t file system format] device file name
Options and parameters:
- -t : Can specify the file system format, such as ext3, ext2, vfat, etc. (only if supported by the system)
### Example 1
View the file formats supported by mkfs
[root@www ~]# mkfs[tab][tab] mkfs mkfs.cramfs mkfs.ext2 mkfs.ext3 mkfs.msdos mkfs.vfat
Pressing two [tab] keys will reveal the file formats supported by mkfs as shown above.
### Example 2
Format the partition /dev/hdc6 (or specify your own partition) as an ext3 file system:
[root@www ~]# mkfs -t ext3 /dev/hdc6 mke2fs 1.39 (29-May-2006) Filesystem label= <== This refers to the partition name (label) OS type: Linux Block size=4096 (log=2) <== Block size configured as 4K Fragment size=4096 (log=2) 251392 inodes, 502023 blocks <== inode/block numbers determined by this configuration 25101 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=515899392 16 block groups 32768 blocks per group, 32768 fragments per group 15712 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912
Writing inode tables: done Creating journal (8192 blocks): done <== Journal logging enabled Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 34 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
This creates the Ext3 file system we need! Simple and clear!
---
## Disk Check
fsck (file system check) is used to check and maintain inconsistent file systems.
If the system crashes or there is a disk issue, you can use the fsck command to check the file system.
Syntax:
fsck [-t file system] [-ACay] device name
Options and parameters:
- -t : Specifies the type of file system. No need to add this parameter if it is already defined in /etc/fstab or supported by the kernel.
- -s : Checks one by one in sequence using the fsck command.
- -A : Checks all partitions listed in /etc/fstab.
- -C : Displays the full progress of the check.
- -d : Prints the debug results of e2fsck.
- -p : With the -A condition, executes multiple fsck checks simultaneously.
- -R : With the -A condition, omits checking /.
- -V : Detailed display mode.
- -a : Automatically repairs if there are errors.
- -r : Prompts the user for confirmation to repair if there are errors.
- -y : Automatically inputs yes for each file check, useful when unsure which files are abnormal, can execute # fsck -y to check and repair all.
### Example 1
View how many file systems the fsck command supports:
[root@www ~]# fsck[tab][tab]
fsck fsck.cramfs fsck.ext2 fsck.ext3 fsck.msdos fsck.vfat
Example 2
Force check on partition /dev/hdc6:
[root@www ~]# fsck -C -f -t ext3 /dev/hdc6
fsck 1.39 (29-May-2006)
e2fsck 1.39 (29-May-2006)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
vbird_logical: 11/251968 files (9.1% non-contiguous), 36926/1004046 blocks
If the -f option is not added, the check process is very quick because the file system has no issues. Only with the -f option to force the check will it display each step.
Disk Mounting and Unmounting
In Linux, disk mounting is done using the mount
command, and unmounting is done using the umount
command.
Syntax for disk mounting:
mount [-t filesystem] [-L Label] [-o additional options] [-n] device filename mount point
Example 1
Mount /dev/hdc6 to /mnt/hdc6 using the default method:
[root@www ~]# mkdir /mnt/hdc6
[root@www ~]# mount /dev/hdc6 /mnt/hdc6
[root@www ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
.....omitted.....
/dev/hdc6 1976312 42072 1833836 3% /mnt/hdc6
Syntax for disk unmounting using umount
:
umount [-fn] device filename or mount point
Options and parameters:
- -f: Force unmount, usable in cases like network file systems (NFS) that cannot be read;
- -n: Unmount without updating /etc/mtab.
Unmount /dev/hdc6:
[root@www ~]# umount /dev/hdc6