Mount Ubuntu 18.04 in VirtualBox to add a virtual hard disk

Mount a virtual hard disk in ubuntu and store it in the host hard disk. The purpose is to prevent data loss caused by the virtual machine system crash. After we create the virtual hard disk, we can use the virtual hard disk as the storage area for all files. If you want to use this virtual hard disk flexibly, you only need to mount it in a directory in ubuntu.

Operation in a virtualbox

1. Select the system you want to configure, click [Management], [Virtual Media Management], [Create], [VHD (Virtual Hard Disk)] in turn, and select the creation address and hard disk size according to your personal requirements.
insert image description here

2. In the rendering after creation, a new NewVirtualDisk.vhd will be created, where disk1 is also a virtual hard disk. Next, take disk1 as an example:
insert image description here

3. After the creation is complete, exit the current interface, select the system you want to configure, click [Settings], [Storage], [Controller SATA], right click the mouse and three blue options will appear, [Hard Disk], select the The created hard disk can be added.
After the mount is successful, in addition to ubuntu.vdi, there is also your newly created hard disk, I show disk1 here. The system is currently running, so it is grayed out.
Now that the operations in the virtual machine are complete, let's configure ubuntu.
insert image description here

Two configuration in ubuntu18.04

1. Find the disk (Disks) in the show application (9 dots) in the lower left corner of ubuntu, and the name of the virtual hard disk just created in the virtual machine will appear.
insert image description here

2. Click the setting button in the middle of the page, select Format Partition... (formatter), then click OK according to the prompts, and wait for the formatting to complete (I will not demonstrate it here).
insert image description here

3. After the formatting is complete, select the files package in the upper left corner, and select other location at the bottom on the pop-up interface, and the newly added virtual hard disk will be displayed.
insert image description here
4. Congratulations, if the results of the above steps are consistent, then your virtual hard disk is added successfully.

Three hard disk partitions

Personally, I feel that this step is optional. If you really need to specify the scope of the file, it is recommended to use the fourth step [Remote login to different users]
1. In the virtual machine, [Settings], [Storage], [Controller SATA], [ Hard Disk], [Create], [Add]

2. View the current partition table

sudo fdisk -l

3. Partition the newly added hard disk

sudo fdisk /dev/sdb
##注意:对/dev/sdb 写分区表时,需要注意扩展分区无法直接格式化,需要分为一或多个逻辑分区

You can refer to this article
to mainly browse step 8, run the virtual machine, open the terminal
① root login, switch to the dev directory (cd /dev), you will find that there is an sdb, which corresponds to the new disk
② Command: fdisk ./sdb, just For example, open the disk manager of windows and start to perform corresponding operations on the new disk.
③Command: p, check the current status of the new disk, you will find that it belongs to unallocated space
④Command: n, create a new partition
⑤There are two options: e (extended partition) and p (primary partition), select p to allocate Area code (select from 1 to 4)
⑥Select the starting position of the cylinder (the default is the first cylinder that can be allocated, press Enter directly)
⑦Select the partition size: +100M
⑧Now use the p command to check and you will find that there is one more The ./sdb1 partition is the newly created partition
⑨ Execute the above partition operations and exit: w. If you don't want to save the modification, just enter q to exit.
In addition, if you want to delete a partition, enter d, and then follow the corresponding prompts.

4. Formatting

sudo mkfs.ext4 /dev/sdb1    # 将/dev/sdb格式化为ext4文件系统

5. Manually mount

sudo mount /dev/sdb1 /home/new_disk_name

6. Automatically mount
In the /etc/fstab file, automatically mount /dev/sdb1 to a certain directory when booting

sudo vi /etc/fstab

添加:
/dev/sdb1   /home/new_disk_name         ext4   defaults   0 0

Check

sudo mount -a 
lsblk

Four remote login different users

If you want to remotely log in to different users in the ubuntu system and need to perform compilation operations, and each user will not affect each other, then you may need to create a directory in the system as a file in the virtual hard disk. Load point, link to the virtual hard disk, and then use Xshell or Puttykey to perform ssh remote connection access.
eg: The virtual hard disk is 700G, and different users or files need their own space. At this time, create a separate file directory (that is, the file directory method in Windows) for each user or file as its own partition, and then only need Create a directory in the system and mount each file as a link point (operating this link point is exactly the same as directly operating disk files, and all steps are performed synchronously).

1. Create a directory in the added virtual hard disk

sudo mkdir /media/root/disk1/new_usr_name

2. Create a directory under the system directory as the mount point in the previous step

sudo mkdir /home/new_usr_name/test

3. Mount the directory in the virtual hard disk to the directory just created in the system

sudo ln -s /media/root/disk1/new_usr_name /home/new_usr_name/test
##在目录/home/new_usr_name/test下建立一个符号链接文件,使它指向目录/media/root/disk1/new_usr_name

4. Check whether the creation is successful

ll /home/new_usr_name/develop
##成功:显示你的帐户名是海蓝色,且指向虚拟硬盘中的位置。  eg:new_usr_name -> /media/root/disk1/new_usr_name/

5. If you want to operate the files of different users in the ubuntu system under the windows interface, you need to configure samba for mapping. For related operations, please move to the following article.
ubuntu mapping files to windows, configure samba service

======================================

No matter how tired you are, you have to finish the road you choose.

Guess you like

Origin blog.csdn.net/qq_43604945/article/details/129212234