[Linux operating system] In-depth understanding of Linux disk partitioning and mounting

Linux disk partitioning and mounting are a very important part of system management. They can help us better manage storage space and file systems. This article will introduce the concepts, principles and practical operations of Linux disk partitioning and mounting in detail, and provide corresponding examples, codes and instructions to help readers fully understand and master these two key concepts.

1. Disk Partitioning

Disk partitioning divides a physical hard disk into different logical parts, and each partition can independently store data and file systems. In Linux, commonly used disk partitioning tools are fdisk and parted.

1. Use fdisk to create a partition:

a. Open the terminal and log in to the system with root privileges.
b. Run the command fdisk -lto check the current hard disk partition status.
c. Run the command fdisk /dev/sdato enter the fdisk interactive interface.
d. Press nthe key to create a new partition.
e. Select the partition type, such as primary or extended.
f. Specify the starting and ending positions of the partition.
g. Repeat steps d to f to create more partitions.
h. Press wthe key to save and exit fdisk.

2. Use parted to create a partition:

a. Open the terminal and log in to the system with root privileges.
b. Run the command parted /dev/sdato enter the parted interactive interface.
c. Run the command mklabel gptto create a GPT partition table (optional).
d. Run the command mkpart primary ext4 1MiB 100GiBto create a new partition.
e. Repeat step d to create more partitions.
f. Run the command quitto exit parted.

2. File System

A file system is a way that an operating system uses to manage and organize files. In Linux, commonly used file systems include ext4, XFS, Btrfs, etc.

1. Format the partition:

a. Open the terminal and log in to the system with root privileges.
b. Run the command mkfs.ext4 /dev/sda1to format the partition into the ext4 file system.
c. Run the command mkfs.xfs /dev/sdb1to format the partition into the XFS file system.
d. Run the command mkfs.btrfs /dev/sdc1to format the partition into the Btrfs file system.

2. Mount the partition:

a. Create a mount point (Mount Point), such as /mnt/data.
b. Run the command mount /dev/sda1 /mnt/datato mount the partition to the specified mount point.
c. Run the command df -hto view the mounted partitions.

3. Automatic mounting (Automount)

In order to facilitate management, we can configure the system to automatically mount the partition at startup. In Linux, you can use the fstab file to implement automatic mounting.

  1. Open a terminal and log in to the system with root privileges.
  2. Edit the fstab file and run the command vi /etc/fstab.
  3. Add a line to the end of the file specifying the partition's device path, mount point, file system type, and mount options.
    For example: /dev/sda1 /mnt/data ext4 defaults 0 0.
  4. Save and exit the fstab file.
  5. Run the command mount -ato reload the fstab file to make the configuration take effect.

4. Other common commands related to disks

  1. mount: View the mounted file systems in the current system.

  2. df: Displays disk partition usage, including used space, free space and file system type.

    • df -h: Displays disk space usage in a human readable format.

    • df -T: Display the file system type.

  3. du: Display the disk usage of the specified directory or file.

    • du -h: Displays disk usage in a human readable format.

    • du -sh /path/to/directory: Displays the total disk usage of the specified directory.

  4. lsblk: List block device information, including disks, partitions, and mount points.

    • lsblk -f: Display the file system type.

    • lsblk -m: Displayed in machine-readable format.

  5. fdisk -l: List the disk partition information in the system.

  6. parted /dev/sda print: Display the partition information of the specified disk.

  7. blkid: Displays the UUID and file system type of the block device.

  8. cat /etc/fstab: Display the system’s mounting configuration information.

    • grep /mnt/data /etc/fstab: Find the configuration information of the specified mount point.

in conclusion:

Through the introduction of this article, we have a detailed understanding of the concepts, principles and practical operations of Linux disk partitioning and mounting. Disk partitions can help us better manage storage space, while mounting associates partitions with file systems so that they can be accessed and used.

Guess you like

Origin blog.csdn.net/Goforyouqp/article/details/132052060