使用Virutalbox实现从本地硬盘启动系统

使用双系统有很多苦恼,双系统套虚拟机更加烦人,但是因为一些平台专有软件,双系统和双系统套虚拟机又不得不用起来。

先说一下单纯双系统和双系统套虚拟机的缺点。

单纯双系统的缺点:

1.使用Windows时想着Linux下shell脚本的方便,utf8编码的好处,编译工具链的好用。。。

2.使用Linux时想着QQ一类的软件还是得用,尤其是嵌入式专业的,Linux下可没有Keil和Proteus。

3.使用特定软件要重启切换系统(最麻烦)。

双系统套虚拟机的缺点:

1.虚拟机额外占存储空间且Windows上配置Linux虚拟机,Linux上配置Windows虚拟机,相当于要安装配置4个系统,Windows下的软件装两套,Linux下的软件装两套。

2.虚拟机性能差,CPU虚拟化延时,硬盘IO读写延迟。。。

3.系统间数据难以互通,各种不方便,尤其是Windows下对EXT4等格式分区的读写非常不方便(官方没解决方案,第三方解决方案要钱且体验不好)

解决方案:物理磁盘分区安装双系统并使用VirtualBox配置启动,我称之为OS_Link方案。

我的经历和解决方案:

需求:

我的电脑是7700HQ的游戏本,GTX1050 2GB独显,16G内存,平时主要写写C/C++代码(单片机和QT小程序),偶尔也交叉编译一些软件放在Linux板子上跑(需要配置TFTP和NFS服务等,当然要用到Linux),偶尔也玩玩3A(原神,大误。。。这个VirtualBox跑不了,性能很差且需要折腾虚拟机反检测)

操作:

我的硬盘是1T的Nvme+1T机械,为了性能考虑,肯定是在Nvme上安装系统,具体分区如下

安装的系统是Windows10和Manjaro KDE(到手即用不折腾) 。

系统安装完毕之后各自配置系统,没什么好说的。

各自安装VirtualBox虚拟机,下一步和pacman -S即可,没什么好说的。

最关键的一步,建立对物理磁盘分区的映射文件。

根据官方教程进行配置。网址(https://www.virtualbox.org/manual/UserManual.html#rawdisk),具体文字放在文章最后了,警告不用看,系统坏了修就是了,况且没那么容易坏。

在Linux下建立磁盘分区映射(映射必须使用vmdk格式)

# 首先设置设备读写权限
sudo chmod 666 /dev/nvme0*
sudo chmod 666 /dev/sda*
# 建立映射文件
VBoxManage internalcommands createrawvmdk -filename link_nvme0n1.vmdk -rawdisk /dev/nvme0n1 -relative
VBoxManage internalcommands createrawvmdk -filename link_nvme0n1p1234.vmdk -rawdisk /dev/nvme0n1 -partitions 1,2,3,4 -relative
VBoxManage internalcommands createrawvmdk -filename link_nvme0n1p34.vmdk -rawdisk /dev/nvme0n1 -partitions 3,4 -relative
VBoxManage internalcommands createrawvmdk -filename link_sda.vmdk -rawdisk /dev/sda -relative

然后你会获得这些

找个地方存放起来。

 然后在VirtualBox里面配置(除了硬盘配置以外,其他和正常虚拟机一样,为了方便使用,建议网络配置为桥接网卡,不这样也可以)

 详情介绍一下为什么使用link_nvme0n1p34.vmdk(映射nvme0n1但只允许读写3、4分区)而不是link_nvme0n1.vmdk(映射nvme0n1)?

这是为了防止对其他分区(Linux分区)的误操作(比如使用分区管理工具删除分区之类的)。

讲一下为什么使用Nvme分区却配置的SATA控制器?

因为Nvme控制器启动Windows10报错(我没有深究原因,能用就行,折腾Arch确实可以让人深切认同简单就是美的真理)。

为什么单独创建了一个Windows10_boot.vmdk(1G,动态分配)的盘?用来装ESP分区(300M以上就行),使用WinPE镜像启动虚拟机,新建ESP分区(可以使用DiskGenius之类的软件),然后使用UEFI修复工具给物理盘上的Windows10装引导(这样就能用虚拟的引导文件启动物理盘中的Windows10了,可以防止对真实的引导分区造成破坏)。

上一个效果

 注意事项,每次使用这种虚拟机之前需要修改设备访问权限,也就是sudo chmod 666 /dev/nvme0*之类的。可以编写脚本自动执行也可以改mdev配置向。没有别的了。

Windows下同理,不需要也没法改设备访问权限,直接使用管理员模式执行命令和打开虚拟机即可,其他参照上述步骤。

似乎Vmware下性能更好,但最近刚折腾了一遍,不想搞了,虽然甲骨文emmm,但开源的VirtualBox毕竟还是要支持的。

补充一下Windows下的操作和效果图。

#必须在具有管理员权限的CMD中执行

VBoxManage internalcommands createrawvmdk -filename link_nvme0n1.vmdk -rawdisk \\.\PhysicalDrive1 -relative
VBoxManage internalcommands createrawvmdk -filename link_nvme0n1p156.vmdk -rawdisk "\\.\PhysicalDrive1" -partitions 1,5,6
VBoxManage internalcommands createrawvmdk -filename link_sda.vmdk -rawdisk \\.\PhysicalDrive0 -relative

为了安全起见可以使用link_nvme0n1p156.vmdk而非link_nvme0n1.vmdk,但因为我的linux默认挂在了Windows10和Software两个分区,所以用的全盘。

为了方便起见使用了物理磁盘的EFI分区,考虑到安全性可以自建/boot/efi分区,但是需要改fstab或是改虚拟efi分区的uuid。

效果图:

 

感谢网友H Zeng的启发,本文是在此基础上的实践和改良。

在 Windows 中通过 VirtualBox 启动物理硬盘上的 Linux 操作系统 | 水景一页

9.7. Advanced Storage Configuration

9.7.1. Using a Raw Host Hard Disk From a Guest

As an alternative to using virtual disk images as described in Chapter 5, Virtual Storage, Oracle VM VirtualBox can also present either entire physical hard disks or selected partitions as virtual disks to virtual machines.

With Oracle VM VirtualBox, this type of access is called raw hard disk access. It enables a guest operating system to access its virtual hard disk without going through the host OS file system. The actual performance difference for image files compared to raw disk varies greatly depending on the overhead of the host file system, whether dynamically growing images are used, and on host OS caching strategies. The caching indirectly also affects other aspects such as failure behavior. For example, whether the virtual disk contains all data written before a host OS crash. Consult your host OS documentation for details on this.

Warning

Raw hard disk access is for expert users only. Incorrect use or use of an outdated configuration can lead to total loss of data on the physical disk. Most importantly, do not attempt to boot the partition with the currently running host operating system in a guest. This will lead to severe data corruption.

Raw hard disk access, both for entire disks and individual partitions, is implemented as part of the VMDK image format support. As a result, you will need to create a special VMDK image file which defines where the data will be stored. After creating such a special VMDK image, you can use it like a regular virtual disk image. For example, you can use the VirtualBox Manager, see Section 5.3, “The Virtual Media Manager”, or VBoxManage to assign the image to a virtual machine.

9.7.1.1. Access to Entire Physical Hard Disk

While this variant is the simplest to set up, you must be aware that this will give a guest operating system direct and full access to an entire physical disk. If your host operating system is also booted from this disk, please take special care to not access the partition from the guest at all. On the positive side, the physical disk can be repartitioned in arbitrary ways without having to recreate the image file that gives access to the raw disk.

On a Linux host, to create an image that represents an entire physical hard disk which will not contain any actual data, as this will all be stored on the physical disk, use the following command:

$ VBoxManage internalcommands createrawvmdk -filename \
path-to-file.vmdk -rawdisk /dev/sda

This creates the /path-to-file.vmdk file image that must be an absolute path. All data is read and written from /dev/sda.

On a Windows host, instead of the above device specification, for example use \\.\PhysicalDrive0. On a Mac OS X host, instead of the above device specification use for example /dev/disk1. Note that on Mac OS X you can only get access to an entire disk if no volume is mounted from it.

Creating the image requires read/write access for the given device. Read/write access is also later needed when using the image from a virtual machine. On some host platforms, such as Windows, raw disk access may be restricted and not permitted by the host OS in some situations.

Just like with regular disk images, this does not automatically attach the newly created image to a virtual machine. This can be done as follows:

$ VBoxManage storageattach WindowsXP --storagectl "IDE Controller" \
 --port 0 --device 0 --type hdd --medium path-to-file.vmdk

When this is done the selected virtual machine will boot from the specified physical disk.

9.7.1.2. Access to Individual Physical Hard Disk Partitions

This raw partition support is quite similar to the full hard disk access described above. However, in this case, any partitioning information will be stored inside the VMDK image. This means that you can install a different boot loader in the virtual hard disk without affecting the host's partitioning information. While the guest will be able to see all partitions that exist on the physical disk, access will be filtered in that reading from partitions for which no access is allowed the partitions will only yield zeroes, and all writes to them are ignored.

To create a special image for raw partition support, which will contain a small amount of data, on a Linux host, use the command:

$ VBoxManage internalcommands createrawvmdk -filename \
path-to-file.vmdk -rawdisk /dev/sda -partitions 1,5

The command is identical to the one for full hard disk access, except for the additional -partitions parameter. This example would create the image /path-to-file.vmdk, which must be absolute, and partitions 1 and 5 of /dev/sda would be made accessible to the guest.

Oracle VM VirtualBox uses the same partition numbering as your Linux host. As a result, the numbers given in the above example would refer to the first primary partition and the first logical drive in the extended partition, respectively.

On a Windows host, instead of the above device specification, use for example \\.\PhysicalDrive0. On a Mac OS X host, instead of the above device specification use /dev/disk1, for example. Note that on OS X you can only use partitions which are not mounted. Eject the respective volume first. Partition numbers are the same on Linux, Windows, and Mac OS X hosts.

The numbers for the list of partitions can be taken from the output of the following command:

$ VBoxManage internalcommands listpartitions -rawdisk /dev/sda

The output lists the partition types and sizes to give the user enough information to identify the partitions necessary for the guest.

Images which give access to individual partitions are specific to a particular host disk setup. You cannot transfer these images to another host. Also, whenever the host partitioning changes, the image must be recreated.

Creating the image requires read/write access for the given device. Read/write access is also later needed when using the image from a virtual machine. If this is not feasible, there is a special variant for raw partition access, currently only available on Linux hosts, that avoids having to give the current user access to the entire disk. To set up such an image, use:

$ VBoxManage internalcommands createrawvmdk -filename \
path-to-file.vmdk -rawdisk /dev/sda -partitions 1,5 -relative

When used from a virtual machine, the image will then refer not to the entire disk, but only to the individual partitions. In this example, /dev/sda1 and /dev/sda5. As a consequence, read/write access is only required for the affected partitions, not for the entire disk. During creation however, read-only access to the entire disk is required to obtain the partitioning information.

In some configurations it may be necessary to change the MBR code of the created image. For example, to replace the Linux boot loader that is used on the host by another boot loader. This enables for example the guest to boot directly to Windows, while the host boots Linux from the "same" disk. For this purpose the -mbr option is provided. It specifies a file name from which to take the MBR code. The partition table is not modified at all, so a MBR file from a system with totally different partitioning can be used. An example of this is:

$ VBoxManage internalcommands createrawvmdk -filename
path-to-file.vmdk -rawdisk /dev/sda -partitions 1,5 -mbr winxp.mbr

The modified MBR will be stored inside the image, not on the host disk.

The created image can be attached to a storage controller in a VM configuration as usual.

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值