How to add a new disk to a virtual machine and mount it to a specific directory?

Requirements: Add a 2GB disk to the VMware virtual machine and mount it under the /home/newdisk directory. The
whole process is divided into 5 steps as follows:

  1. Set up a virtual machine to add a new disk
  2. Will add new disk partition fdisk
  3. Format mkfs
  4. Mount
  5. Set permanent mount /etc/fstab

步骤一:虚拟机增加磁盘

Open the virtual machine settings, and then add a disk, as long as you select the size, click Next for everything else.
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

步骤二:分区

The command is fdisk /dev/sdb
and then enter n to add a new partition, then select p, the partition type is primary partition, and then select 1 to be the first primary partition. Then write all the remaining space twice by default, and finally enter w to write the modification. Or type q to exit.

[root@localhost Desktop]fdisk  /dev/sdb 
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n  #  输入n , 新增分区
Command action
   e   extended
   p   primary partition (1-4)
p                        #  输入p , 设定为主分区
Partition number (1-4): 1  # 选择1 ,为第1分区
First cylinder (1-261, default 1):   # enter ,选择默认
Using default value 1
Last cylinder, +cylinders or +size{
    
    K,M,G} (1-261, default 261): # enter ,选择默认
Using default value 261
Command (m for help): w  #  输入w ,写入修改 
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

步骤三:格式化

The command is: mkfs -t ext4 /dev/sdb1

[root@localhost Desktop] mkfs -t  ext4  /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 524112 blocks
26205 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 22 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

步骤四:手动挂载

Command: mount disk file directory# Mount the disk on the directory

root@localhost Desktop]# mount  /dev/sdb1  /home/newdisk 
[root@localhost Desktop]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0     11:0    1 1024M  0 rom  
sda      8:0    0   40G  0 disk 
├─sda1   8:1    0  300M  0 part /boot
├─sda2   8:2    0  3.9G  0 part [SWAP]
└─sda3   8:3    0 35.8G  0 part /
sdb      8:16   0    2G  0 disk 
└─sdb1   8:17   0    2G  0 part /home/newdisk    # 挂载成功

But this is a single mount, and it will fail after restarting.

步骤五:永久自动挂载

The method of permanent automatic modification is to modify the /dev/fstab configuration file, write the disk and file mounting information into /etc/fstab,
/dev/sdb1 /home/newdisk ext4 defaults 0 0
and then execute mount -a.

[root@localhost Desktop]# vim /etc/fstab 
#
# /etc/fstab
# Created by anaconda on Thu Jul  9 00:04:07 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/sdb1                                 /home/newdisk           ext4    defaults        0 0  
UUID=0f7d9402-9f6c-4b87-ab96-8e16dab67957 /                       ext4    defaults        1 1
UUID=2c00004a-e632-48d9-bc8e-d8c83acb2019 /boot                   ext4    defaults        1 2
UUID=50fe9b93-7fe0-4c14-acfc-44c35e3b034a swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0

[root@localhost Desktop]# mount -a 
[root@localhost Desktop]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0     11:0    1 1024M  0 rom  
sda      8:0    0   40G  0 disk 
├─sda1   8:1    0  300M  0 part /boot
├─sda2   8:2    0  3.9G  0 part [SWAP]
└─sda3   8:3    0 35.8G  0 part /
sdb      8:16   0    2G  0 disk 
└─sdb1   8:17   0    2G  0 part /home/newdisk  # 挂载成功, 重启之后挂载仍可用。

Guess you like

Origin blog.csdn.net/weixin_43705953/article/details/108224088