How to implement Linux disk mounting, partitioning, and capacity expansion operations

How to implement Linux disk mounting, partitioning, and capacity expansion operations

Basic Concepts

Before operation, you must first understand some basic concepts

disk

In the Linux system, all devices are stored in the form of files. Devices are generally stored in the /dev directory, in the form of sda, sda1, sda2…, sdb, sdb1…, hda, hdb. Nowadays, most devices are named "sd", while very old hard drives were named "ha".

sda: The first hard disk. If the disk is partitioned, there will be sda1 (the first partition), sda2, and so on.

sdb: The second hard disk, after partitioning, there are sdb1, sdb2, etc.

Partition

The purpose of partitioning is to facilitate management. For example, in Windows systems we usually divide them into C drive, D drive, E drive, etc.

Linux can only create 4 primary partitions. If you need to create more partitions, you must create logical partitions, and the logical partitions need to occupy one primary partition.

File System

The file system in Linux is the partition type. In Windows, there are NTEF, FAT32, etc. In Linux, there are Ext2, Ext3, Ext4, Linux swap, proc, sysfs, tmpfs, etc. You can view the currently mounted file system through the mount name.

format

After creating the partition, one step is to format the partition. In fact, it is the same in Windows system. After creating a partition, you also need to format the partition. It can only be used after it is formatted into a specific file type.

Mount

In Windows, the partition can be used after it is formatted, but in Linux system, the partition must be mounted to a specific path.

Common commands

lsblk View the current disk status
df -lh View the file system status -l View the mount point
parted -l will list the file system type
fdisk -l View currently unmounted hard disks

Mount the new hard drive

The basic idea of ​​mounting a new hard disk is: create partitions, create file systems, and mount.

1. Check the new hard drive

First, check the hard disk status:

fdisk -l

in:

If there is a message like: Disk /dev/sdc doesn't contain a valid partition table under the disk; or there is no message like: sdb1 sdb2 under the disk, it means that the disk is not mounted.

Here we assume that the hard disk name is /dev/sdb

2. Create partitions

dfisk /dev/sdb

According to the prompts, enter "n", "p", "1" in sequence, press Enter twice, and "wq"

This means creating a new primary partition (1) the size of the entire sdb disk, and then writing to it.

Note: For simplicity, the above operation only creates one primary partition. In fact, a disk can have up to four primary partitions (including one extended partition). 1-4 are all primary partitions. We can also use a partition as an extended partition (the system displayed by df -lh is Extended).

At this point the disk has been partitioned, but there is no file system yet, and the disk is still unusable.

3. Write to the system

mkfs.ext4 /dev/sdb

This command will format the disk and write the file system

4. Mount

For example, mount it under /data

mkdir /data # If this step exists, skip mount /dev/sdb /data

5. Set up automatic mounting at startup

The above is just a temporary mount, and you need to set it to automatically mount when you boot.

vim /etc/fstab


# Then add a line at the end of the content (note that the file type must correspond):

/dev/sdb /data ext4 defaults 0 0

Scaling

About mounting to an existing directory

If the directory you want to mount is not empty, then after the file system is mounted, the contents of the original directory will temporarily disappear. It is not overwritten, but temporarily hidden. After the new partition is unmounted, the original contents of the original directory will appear again.

If you want to permanently mount an existing directory, you can mount it to a temporary directory after creating a file system on the new hard disk, then copy the directory to be expanded to this temporary directory, then delete the directory to be expanded, unmount the temporary mount point, and remount it to the directory to be expanded. Example:

# For example, to expand /var

# After creating the file system, create a temporary mount point storage
mkdir /storage

# Mount /dev/sdb1 to /storage mount /dev/sdb1 /storage

# Copy all contents under /var to the new hard disk cp -pdr /var /storage
# Or execute in the /var directory: find . -depth -print | cpio - pldvm /temp
# Delete the contents of the current /var directory rm -rf /var/*
# Remount the hard disk to the /var directory umount /dev/sdb1
mount /dev/sdb1 /var

# If the disk is busy, use fuser to find out the program that is using the disk and terminate it;

fuser -m -v /var
fuser -m -v -i -k /var

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Analysis of the Principle and Method of Implementing Linux Disk Partition
  • An article to understand Linux disks and disk partitions
  • Linux system disk formatting and manually adding swap partition
  • Detailed explanation of Linux virtual machine root partition disk expansion space record
  • Linux disk partitioning practical examples (must read)
  • Detailed process of LINUX disk partitioning, formatting, mounting and uninstalling
  • How to use GPT partitioning on Linux disks larger than 2T
  • Linux parted disk partition implementation steps analysis

<<:  VMWare Linux MySQL 5.7.13 installation and configuration tutorial

>>:  How to use less in WeChat applet (optimal method)

Recommend

Detailed tutorial on building Gitlab server on CentOS8.1

There is no need to say much about the difference...

Where is mysql data stored?

MySQL database storage location: 1. If MySQL uses...

Detailed explanation of browser negotiation cache process based on nginx

This article mainly introduces the detailed proce...

Detailed explanation of docker's high availability configuration

Docker Compose Docker Compose divides the managed...

Notes on upgrading to mysql-connector-java8.0.27

Recently, an online security scan found a vulnera...

Distributed monitoring system Zabbix uses SNMP and JMX channels to collect data

In the previous article, we learned about the pas...

How to implement scheduled backup of MySQL database

1. Create a shell script vim backupdb.sh Create t...

Solution to the img tag problem below IE10

Find the problem I wrote a simple demo before, bu...

MySQL changes the default engine and character set details

Table of contents 1. Database Engine 1.1 View dat...

Detailed explanation of CSS3 elastic expansion box

use Flexible boxes play a vital role in front-end...

Docker container from entry to obsession (recommended)

1. What is Docker? Everyone knows about virtual m...

Detailed steps for installing Harbor, a private Docker repository

The installation of Harbor is pretty simple, but ...