Linux series installs a new hard disk on the server and mounts it to the specified directory

edge

Recently, the server's hard disk has reached 91. In order to prevent the disk from hanging up due to insufficient storage, we plan to mount some things that are not commonly used and occupy more memory elsewhere. In order to keep consistent with the original path, you may need to add a soft connection or hard connection. In order to prevent problems, now test on the virtual machine.

Mount hard disk

Add hard disk

Adding a hard disk to a virtual machine is very simple. I didn't want to say more. I'd like to post a picture so that people who can't see how to add it. As follows: click add in the setting, select the hard disk, and then go to the next step to get a hard disk named disk 2.

Initialize partition

Check the hard disk partition and command fdisk -l. for convenience of reading, the following contents only show the main partition information:

[root@localhost ~]# fdisk -l

disk /dev/sda: 21.5 GB, 21474836480 Bytes, 41943040 sectors
Units = a sector of 1 * 512 = 512 bytes
 Sector Size (logic/Physics): 512 byte / 512 byte
I/O size(minimum/optimum): 512 byte / 512 byte
 Disk label type: dos
 Disk identifier: 0 x000ccdc3

   equipment Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM

disk /dev/sdb: 21.5 GB, 21474836480 Bytes, 41943040 sectors
Units = a sector of 1 * 512 = 512 bytes
 Sector Size (logic/Physics): 512 byte / 512 byte
I/O size(minimum/optimum): 512 byte / 512 byte
 Disk label type: dos
 Disk identifier: 0 x881d3f29

   equipment Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41943039    20970496   83  Linux

...

Disk / dev/sda is the original disk. There are two partitions, / dev/sda1 and / dev/sda2
Disk / dev/sdb is a new disk. There is a partition, which is / dev/sdb1

delete a partition

To start from scratch, delete the existing partition / dev/sdb1. The command is as follows:

#Enter the partition operation of new disk sdb
[root@localhost ~]# fdisk /dev/sdb
 Welcome fdisk (util-linux 2.23.2). 

The changes remain in memory until you decide to write the changes to disk.
Think twice before using the write command.

#Enter m here to obtain relevant instruction information
 command(input m get help): m
 Command operation
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

#delete a partition
 command(input m get help): d
 Partition 1 Selected
 Partition 1 deleted

#Save changes to the partition
 command(input m get help): w
The partition table has been altered!

To operate a new partition on a new hard disk, you only need the above four instructions. Explain the meaning of each instruction:

   d   delete a partition(Delete a partition)
   n   add a new partition(Add a partition)
   q   quit without saving changes(Exit without saving the changes)
   w   write table to disk and exit((save and exit)

New partition

Add partition command:

[root@localhost ~]# fdisk /dev/sdb
 Welcome fdisk (util-linux 2.23.2). 

The changes remain in memory until you decide to write the changes to disk.
Think twice before using the write command.

# Create a new partition
 command(input m get help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
   
#Set primary partition attribute
Select (default p): p
#The partition number is set to 1
 Partition number (1-4,Default 1): 1
#Set the start position and end position of the sector
 Start sector (2048-41943039,The default is 2048): 
The default value 2048 will be used
Last a sector, +a sector or +size{K,M,G} (2048-41943039,The default is 41943039): 
The default value 41943039 will be used
 Partition 1 is set to Linux Type, size set to 20 GiB
#Save changes and exit
 command(input m get help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Synchronizing disks.

Note: since I use the whole disk as a partition, the default setting for sectors here is that all sectors are assigned to partition 1. If you want to divide multiple partitions, you can write different sector positions or set specific sizes (unit: K/M/G)

View hard disk partitions

Use the command to view the partition. The command is as follows:

[root@localhost ~]# fdisk -l

disk /dev/sda: 21.5 GB, 21474836480 Bytes, 41943040 sectors
Units = a sector of 1 * 512 = 512 bytes
 Sector Size (logic/Physics): 512 byte / 512 byte
I/O size(minimum/optimum): 512 byte / 512 byte
 Disk label type: dos
 Disk identifier: 0 x000ccdc3

   equipment Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM

disk /dev/sdb: 21.5 GB, 21474836480 Bytes, 41943040 sectors
Units = a sector of 1 * 512 = 512 bytes
 Sector Size (logic/Physics): 512 byte / 512 byte
I/O size(minimum/optimum): 512 byte / 512 byte
 Disk label type: dos
 Disk identifier: 0 x881d3f29

   equipment Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41943039    20970496   83  Linux

As can be seen from the above output, we have successfully added a partition / dev/sdb1.

Partition mount to path

Now that the partition is added, the next thing we need to do is associate the partition with the corresponding directory. The permanent setting method is to edit the / etc/fstab file:

[root@localhost /]# vim /etc/fstab 
#Add on the last line
 /dev/sdb1       /home/iceter      ext3   defaults   1 1

Parameter Description: / dev / sdb1 / home / iceter ext3 defaults 1

parameterexplain
/dev/sdb1partition
/home/iceterCorresponding folder
ext3Mount type

The mount and uninstall commands are as follows:

#mount 
mount /dev/sdb1 /home/iceter
#uninstall
umount /dev/sdb1 /home/iceter

Then, when you want to make a soft and hard connection, if you find that it is not applicable, you should give it up, or put the command for emergency needs, as follows:

#Hard connection
[root@localhost iceter]# ln iceter.out iceter
#Soft connection
[root@localhost iceter]# ln -s iceter iceters
[root@localhost iceter]# ls -li
 Total consumption 2050008
11 -rw-r--r--. 2 root root 1048576000 2 June 23-17:07 iceter
11 -rw-r--r--. 2 root root 1048576000 2 June 23-17:07 iceter.out
12 lrwxrwxrwx. 1 root root          6 2 June 23-17:16 iceters -> iceter

Note: hard and soft connections can only be used for files, not folders.

Keywords: Linux CentOS server

Added by Scotty2024 on Wed, 23 Feb 2022 12:45:11 +0200