Linux disk mount partition expansion operation implementation method

  • 2020-12-19 21:24:06
  • OfStack

The basic concept

Before you do this, you need to understand a few basic concepts

disk

All devices in the Linux system are stored as files. Device 1 is generally saved under /dev directory as sda, sda1, sda2... sdb, sdb1... , hdb hda. The current device 1 is usually named sd, whereas the old hard drives were named ha.

sda: the first hard disk, if the disk is partitioned sda1(the first partition), sda2, etc.

sdb: second hard disk, sdb1,sdb2, etc.

partition

The purpose of partitioning is to facilitate management. For example, in Windows system, we generally divide C disks, D disks, E disks and so on.

Linux can only create four primary partitions, and if you need to create more, you must create logical partitions, of which logical partitions need to occupy one primary partition.

The file system

The file system in Linux is the partition type. In Windows, there are NTEF,FAT32, etc. In linux, the common ones are Ext2, Ext3, Ext4, Linux swap, proc, sysfs, tmpfs, etc. The current mounted file system can be viewed by the mount name.

formatting

In the previous partition after the creation of a step is to format the partition, in fact, Windows system is also the same, after the creation of a partition also need to format the partition, only formatted into a specific file type can be used.

mount

Partition can be used after formatting in Windows, but must be mounted to a specific path in Linux.

Common commands

[

lsblk views the current disk status
df-lh View File system Situation-ES83en View mount points
parted-l lists file system types
fdisk-l View the hard disk that is not currently mounted

]

Mount the new hard disk

Mount a new hard disk basic idea is: create partition, create file system, mount.

1. Check your new hard drive

First, check your hard drive:


fdisk -l

Among them:

If there is something like: Disk /dev/sdc doesn't a valid partition table doesn contain a valid partition table sdb1 sdb2 indicates that the disk is not mounted

Assume that you see a hard disk named /dev/sdb

2. Create partitions


dfisk /dev/sdb

According to the prompts, enter "n", "p", "1", enter twice, "wq".

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

Note: For simplicity, just create 1 primary partition. In fact, a disk has a maximum of 4 primary partitions (including 1 extended partition), 1-4 are all primary partitions, we can also consider a partition as an extended partition (es137EN-ES138en is Extended)

At this point, the disk is partitioned, but there is no file system, and the disk is still not available

Write to the system


mkfs.ext4 /dev/sdb

This command formats the disk and writes to the file system

4. A mount

For example, mount it under /data


mkdir /data #  If this step omission exists 
mount /dev/sdb /data

5. Set automatic mount on startup

Above is only temporary mount, also need to be set to boot automatic mount


vim /etc/fstab


#  Then add at the end of the content 1 Line (note that the file type should correspond) : 

/dev/sdb  /data  ext4  defaults  0 0

capacity

About mounting to an existing directory

If the directory you want to mount is not empty, then after mounting the file system, the contents of the original directory will temporarily disappear. Instead of being overwritten, it is temporarily hidden, and when the new slot is removed, the original contents of the original directory will come out again.

If you want to mount an existing directory permanently, you can create a file system on your new hard drive, mount it to a temporary directory, then copy the directory you want to extend to that temporary directory, then delete the directory you want to extend, unmount the temporary mount point, and remount it to the directory you want to extend. For example:


#  Let's say you want to expand  /var

#  After the file system is created   Create a new temporary hardpoint  storage
mkdir /storage

#  will /dev/sdb1 Mount to the /storage Under the 
mount /dev/sdb1 /storage

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

#  If the disk is prompted to be busy during the process, use fuser Find the program that will be using the disk and end it. 

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

Related articles: