AHdark

AHdark Blog

Senior high school student with a deep passion for coding. Driven by a love for problem-solving, I’m diving into algorithms while honing my skills in TypeScript, Rust, and Golang.
telegram
tg_channel
twitter
github

Linux Disk Partitioning

Introduction#

When purchasing a cloud server, the term "data disk" is usually familiar to us.

image

From Cloud Jun Data

As one of the few tangible options available to us, the data disk is something worth understanding, learning, and using. Separating the system disk from the data disk is also something many cloud computing solutions do.

As the name suggests, the "data disk" is the disk primarily responsible for storing data information on the server. Typically, when using cloud computing services, we separate the system disk and the data disk to make it more convenient for us to change the system environment, migrate data, or change server models. Additionally, separating system applications and software from data can prevent the system from freezing due to high traffic occupying disk bandwidth.

Our company, Cloud Jun Data, offers data disk options across multiple server products.

Initialization#

The system disk usually comes pre-installed with the system and does not require user reconfiguration. Due to the migratory nature of the data disk, cloud service providers almost never pre-configure it. This means that after purchasing a cloud server with a data disk, we need to mount it to the system and configure it ourselves before it can be used.

Check#

First, we need to check the current mounting situation to confirm whether we need to perform the data disk mounting operation.

# Check file system mount status
df -h

# Check disk information
fdisk -l

image

image

As shown, /dev/vda is my system disk, while /dev/vdb is my data disk. My data disk does not appear in the output of the df -h command, so I can determine that it is not mounted.

Partitioning#

Before mounting the disk, we need to perform partitioning operations on it.

fdisk /dev/vdb

After entering, we will enter an fdisk interactive session:

  1. Enter m to get the complete list.
  2. Enter n to prepare to create a new partition.
  3. Press Enter to select the default value primary and proceed to the next step.
  4. Press Enter to select the default value and proceed to the next step.
  5. Press Enter to select the default value and proceed to the next step.
  6. Enter p to view the partition status; if partition information appears in the corresponding disk section (e.g., /dev/vdb), it is successful.
  7. Enter w to save the partition table.

image

image

Interactive process

Determine File System Type#

Typically, the commonly used file system types in Linux systems are as follows:

  • EXT4[ref]Reference: https://en.wikipedia.org/wiki/Ext4
    [/ref]: The fourth extended file system, supporting up to 1EB file systems and 16TB files, with continuous writing to reduce file fragmentation. It has good compatibility (Linux / Windows / MacOS) and is often used for mobile devices and extended disks.

  • NFS[ref]Reference: https://en.wikipedia.org/wiki/Network_File_System
    [/ref]: Network File System, a distributed file system, is mostly suitable for cloud disks and storage arrays connected to internal networks.

  • XFS[ref]Reference: https://en.wikipedia.org/wiki/XFS
    [/ref]: A high-performance journaling file system, adept at handling large files while providing smooth data transfer.

In this example, we will use the XFS file system (the example machine is a physical machine, not using a storage array, and does not have migration conditions).

mkfs.xfs  -f /dev/vdb # For XFS file system
mkfs.nfs  -f /dev/vdb # For NFS file system
mkfs.ext4 -f /dev/vdb # For EXT4 file system

Configure File System

Configure file system

Temporary Mounting#

In this example, we will mount the /dev/vdb disk to the /mnt directory.

Note that all content in the mounted directory will be overwritten! Additionally, it needs to be remounted after the server restarts.

sudo rm -rf /mnt
sudo mkdir /mnt
sudo mount /dev/vdb /mnt

df -h

Mount Disk

Mount disk

At this point, the result of df -h shows that the disk has been successfully mounted.

Permanent Mounting#

By modifying /etc/fstab, we can achieve a permanent mount that can be used directly after the server restarts.

#
# /etc/fstab
# Created by anaconda on Mon Jun 10 14:18:53 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=8223c1d3-9644-4885-80b9-f24158ea6013 /boot xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/vdb/wwwxfsdefaults0 0

We just need to add according to the format: <disk> <mount point> <file system type> defaults 0 0

Migration#

Due to the characteristics of the data disk, its overall migration is relatively convenient.

For example, we want to migrate the directory /data from the original disk /dev/vda to the disk /dev/vdb:

  1. Mount the disk /dev/vdb to the /mnt directory.
  2. Shut down all running services in the /data directory.
  3. Copy all content from the /data directory to /mnt.
  4. Use rm -rf /data to delete the empty directory and create the mount point directory with mkdir /data.
  5. Use sudo umount /mnt to unmount, at which point the disk still contains data.
  6. Directly use sudo mount /dev/vdb /data to mount the disk (no need to reinitialize the disk).
  7. At this point, the data in /data remains intact. You can use the df -h command to check the mounting status.
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.