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

How to use IDEA to create a web project and publish it to tomcat

Table of contents Web Development 1. Overview of ...

Solution to the horizontal scroll bar in iframe under IE6

The situation is as follows: (PS: The red box repr...

Detailed steps for deepin20 to install NVIDIA closed-source drivers

Step 1: Install the deep "graphics driver&qu...

...

MySQL 5.7.17 installation and configuration method graphic tutorial (windows10)

MySQL 5.7.17 installation and configuration metho...

Example of how to upload a Docker image to a private repository

The image can be easily pushed directly to the Do...

HTML meta viewport attribute description

What is a Viewport Mobile browsers place web page...

Tutorial on installing mysql5.7.18 on mac os10.12

I searched the entire web and found all kinds of ...

Example code for implementing WeChat account splitting with Nodejs

The company's business scenario requires the ...

How to export mysql query results to csv

To export MySQL query results to csv , you usuall...

JavaScript to achieve dynamic color change of table

This article shares the specific code for JavaScr...

Using CSS3's 3D effects to create a cube

Learning to use CSS3's 3D effects to create a...

Users need to know why

When I was in the securities company, because the ...