Disk operations based on Centos 7 virtual machine (add disk, partition, format partition, mount)

Table of contents

1. Add a hard drive

2. Check the new disk

3. Disk partition

3.1 Create a new partition

 3.2 Format partition

 3.3 Mount partition

 3.4 Permanently mount the new partition

 3.5 Unmount partition


1. Add a hard drive

1. Select Edit Virtual Machine Settings at the virtual machine, and then select Add

 2. Select the hard drive and then select Next

 3. Just leave it as default, next step

 4. Select Create a new virtual disk and click Next

 5. Set the disk size you need, then select the option to store the virtual disk as a single file, next step

 6. Set the new disk name. You can select the storage location, or you can directly select Complete by default. The disk will be stored in the same level directory of the virtual machine system.

 7. After the setting is completed, you can see your newly added disk.

2. Check the new disk

1. Start or restart the virtual machine after adding the new disk

2. Use the command lsblk to check the size and mounting status of the disk.

 3. You can also use the command lsblk -f to check the disk format.

 4. The two commands can be viewed together

3. Disk partition

3.1 Create a new partition

1. Create a RepositoryDisk folder in the /root directory to mount the new disk. The directory and folder can be arbitrary. You can mount it to any directory according to your own needs.

 2. The newly added disk is in /dev/. The new disk name can be seen in the /dev/ directory.

 3. Use the command fdisk /dev/disk name to partition, such as fdisk /dev/sdb

 4. After executing the above command, you will be prompted to enter m to get help, enter m and press Enter [If you do not want to view the help, you can ignore this step]

 5. There are 5 steps in total to add a new partition. Each step is introduced below. Each step ends with Enter.

  • Enter n to add a new partition
  • Enter p to create a new primary partition
  • Specify the partition number and enter 1
  • Specify the starting sector. The default is sufficient. It is not recommended to change it.
  • Specify the end sector, which represents the partition size. You can use K, M, G to specify the partition size. For example, if 50G is allocated, enter 50G.

 6. After performing the above operations, you need to enter w to confirm the partition, otherwise it will not take effect.

 7. If you are deleting a partition, after executing fdisk /dev/sdb, enter d to delete the partition, and then enter w to confirm the change.

 8. Through lsblk and lsblk -f, you can see that the newly created partition has taken effect, but it has not been formatted and mounted, and it still cannot be used at this time.

 3.2 Format partition

 1. Command: mkfs -t partition system format disk partition

  • mkfs -t xfs /dev/sdb1 (sdb1 is obtained after partitioning the sdb disk) 

 2. Use lsblk -f again to see that the format partition is successful, but there is no mount point yet.

 3.3 Mount partition

1. This method of mounting is only temporary and needs to be remounted after the system is restarted.

2. Now mount sdb1 to the /root/RepositoryDisk directory created previously

3. Use the mount command to mount the partition: mount the partition mounting directory. After mounting, you can use lsblk -f to check whether the mounting is successful.

  • mount /dev/sdb1 /root/RepositoryDisk
  • lsblk -f

 4. Use the df -h command to check whether it is really successful. You can see that the system has been able to load the new disk correctly.

 3.4 Permanently mount the new partition

1. The mounting in the above steps is only temporary. It needs to be remounted after the system is restarted.

2. It will be very troublesome if you need to remount it every time you use it, so we can mount the partition permanently and automatically mount it when you turn on the computer.

3. To take effect permanently, you need to write the disk information to /etc/fstab so that it can be automatically loaded after booting.

 4. To write the newly created disk information, we need to configure 5 pieces of information, which represent the following information:

  • The first partition that needs to be mounted, such as /dev/sdb1
  • The second partition mount point, such as mounting to /root/RepositoryDisk
  • The third partition format is consistent with the format specified when formatting the partition yourself.
  • The fourth one can be directly defaults, consistent with the system.
  • The fifth one is consistent with the system, just enter 0
  • The sixth one is consistent with the system, just enter 0

 5. After completing the input, save and exit, then restart the virtual machine and check whether it is automatically loaded. After restarting, you can see that it is successfully loaded automatically.

 3.5 Unmount partition

1. If you do not want to mount this partition at this time, you can use the command: umount partition or umount mount directory. Both of the following methods are available.

  • umount /dev/sdb1
  • umount /root/RepositoryDisk

2. Note that when using the umount command to unmount a partition, do not use it in the partition mounting directory or the disk is not in use, otherwise the prompt: device is busy will appear.

3. After unmounting the partition, you can mount it to another partition. You can mount it to any directory you need, but it is recommended not to mount it in the system directory.

4. Note: Please do not mount the hard disk directly to the original directory of the system such as /home, /root, etc., as it may easily cause problems.

  • Directly mounting to the system directory will replace the existing contents of the original system directory, and the original mounted disk will be replaced.
  • If mounted to the user's home directory, it will cause a -bash problem because the original .bashrc file cannot be found.
  • If you accidentally mount the wrong directory to the above directory, cancel the mount and remount to the directory you need.

https://juejin.cn/post/6987200157733371935https://juejin.cn/post/6987200157733371935

https://blog.csdn.net/sinat_34104446/article/details/84637590https://blog.csdn.net/sinat_34104446/article/details/84637590

Guess you like

Origin blog.csdn.net/weixin_43200943/article/details/131910458