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

CSS solves the misalignment problem of inline-block

No more nonsense, post code HTML part <div cla...

Example of how to identify the user using a linux Bash script

It is often necessary to run commands with sudo i...

A Brief Analysis of MySQL Connections and Collections

Join query A join query refers to a matching quer...

The textarea tag cannot be resized and cannot be dragged with the mouse

The textarea tag size is immutable Copy code The c...

Eight rules for effective web forms

If you're collecting information from your us...

Detailed explanation of Mysql self-join query example

This article describes the Mysql self-join query....

Mysql8.0 uses window functions to solve sorting problems

Introduction to MySQL Window Functions MySQL has ...

React handwriting tab switching problem

Parent File import React, { useState } from '...

Introduction to JavaScript conditional access attributes and arrow functions

Table of contents 1. Conditional access attribute...

Common naming rules for CSS classes and ids

Public name of the page: #wrapper - - The outer e...

How to view and close background running programs in Linux

1. Run the .sh file You can run it directly using...

Tutorial on installing MYSQL8.X on Centos

MySQL installation (4, 5, 6 can be omitted) State...

MySQL SQL statement analysis and query optimization detailed explanation

How to obtain SQL statements with performance iss...