Basic use of hyper-v virtual machine

Preface

I switched from Vmware to Hyper-V some time ago, and the most intuitive feeling is that there are more blue screens (Mechanical Revolution cannot use the driver software to install the driver, it is best to use the official website). It's fine for other uses, but it's perfect when paired with remoteApp. Anything can be placed in a virtual machine, and it will have the same effect as on the host.

This operation can facilitate migration, and basically the environment is installed in the virtual machine. To reinstall the system in the future, you only need to enable hyper-v and then import the virtual machine.

Insert image description here
Pay attention to the icons on the taskbar. The software with multiple small circles in the lower right corner is software installed on the virtual machine, but it is used the same as installed on the host. The only disadvantage is that sometimes it is inconvenient to operate files. Although the clipboard is shared, some software does not support pasting files directly.

The icon below can also be removed. In the taskbar settings, remove the display corner icon and the effect will be exactly the same as that of the host machine.

You can create a virtual machine specifically to host rogue software.

Basic usage

Turn on hyper-v

Not much to say about turning on the hyper-v function, just turn it on in Control Panel -> Programs -> Programs and Features -> Turn Windows features on or off Hyper-V. I usually turn on 适用于Linux的Windows子系统, 虚拟机平台and Windows虚拟机监控程序平台.

After turning on the function and restarting the computer, you can find the hyper-v manager in the search
Insert image description here

install the system

This operation is also very simple, basically the same as Vmware. Prepare an iso image and fill in some parameters. There is an option to specify the generation: I usually choose second generation when installing Windows, and choose first generation when installing Linux. I’m not sure what the essential difference is between the two, or what the different feeling will be when using them.

If you choose the second generation, you need to press F2 when starting up for the first time to enter the installation interface.

To modify the default storage path, operate-》Hyper-v Settings-》Virtual Machine and select a path.

If you want to use a ghost file to install a virtual machine, it will be a little troublesome. Please see another article for details: Installing ghost backup files in hyper-v

If it is a ghost file backed up by another machine, there is a high probability that it will not be able to be used normally after the installation is completed. For example, it will be stuck. This is because the driver is incompatible.

Virtual machine switch

Insert image description here
When Windows 10 starts a virtual machine, there will be a Defaullt Switchdefault network. This network will automatically assign IP and DNS to the virtual machine. The strange thing is that the IP assigned each time is not fixed.

So you need to build a network yourself and make the IP in the virtual machine fixed. There are three types of networks: external (bridged), internal (Nat), and private (don't know). Generally, it is enough to choose internal (Nat). There are some minor problems with bridging, such as only supporting wired network card bridging.

Create a new virtual machine network switch-"Internal-"Change the name and click Apply to create it.

Then open the network adapter, right-click the network you just created (for example, vEthernet (hyper-Nat)), properties, then select Internet Protocol version 4, and enter an intranet IP address of 192.168.x.1.
Insert image description here
If you only use the network you created in the virtual machine, you need to set the IP and DNS, because there is no DHCP service and IP and DNS will not be automatically assigned. For Windows virtual machines, the setup steps are the same as the picture above. You only need to change the IP to 192.168.x.2, and then set the domestic dns (114.114.114.114) below.

In order to avoid such trouble, I usually assign two network adapters to the virtual machine. One is the default for connecting to the Internet, and the other is the internal network I created for communication between the virtual machine and the host. Like Vmware, the Nat network is
Insert image description here
basically It can meet daily needs. The virtual machine can access things on the LAN at will. If an external computer needs to access the services of the virtual machine, it needs to do port forwarding on the host.

If you need bridging, please see: https://blog.51cto.com/u_15162069/2761935

port forwarding

Forward a port service of the virtual machine to a port of the host

Microsoft official tutorial: https://docs.microsoft.com/en-us/windows-server/networking/technologies/netsh/netsh-interface-portproxy

Generally, ipv4 is forwarded to ipv4, so take this as an example

netsh interface portproxy add v4tov4 listenport=主机端口 connectaddress=虚拟机IP connectport=虚拟机端口 listenaddress=主机IP protocol=tcp
For example, you need to forward port 3389 of the virtual machine to port 33333 of the host. Assume that the virtual machine IP is 192.168.158.100.

netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=33333 connectaddress=192.168.158.100 connectport=3389

In this way, other machines on the intranet can connect to the virtual machine through the host's IP: 33333.

netsh interface portproxy show v4tov4List all forwarding rules of v4tov4
netsh interface portproxy delete v4tov4 listenaddress=0.0.0.0 listenport=33333 Delete this forwarding rule

Note that this command is not specific to hyper-v. It can be used by any other service, including services in Vmware. However, port forwarding rules can be set in the Vmware Nat network.

hyper-v reserved port

Hyper-V will reserve some ports, but it is unclear what they are used for. If the port you are planning to assign is among the reserved ports, a port conflict will occur.

Therefore, you need to set the part of the port that he reserves in a section that is not commonly used. Please read this article for details.

View reserved ports
netsh int ipv4 show dynamicport tcp
View ports that have been used
netsh int ipv4 show excludedportrange protocol=tcp
Set reserved ports, starting at 49152 and totaling 16384
netsh int ipv4 set dynamic tcp start=49152 num=16384
netsh int ipv6 set dynamic tcp start=49152 num=16384

As long as you avoid using ports in the 49152-65535 range, there will be no conflicts.

Import and export virtual machines

Select export directly on the interface, and then specify a folder to export. In fact, it should just copy the virtual machine files. If you copy the virtual machine files to another machine, you can also import them directly.

Importing is also very convenient, just select the folder you just exported and take the next step. There is a choice of import type here. If it is a virtual machine copied from another machine, just select the first one to register locally. If it is a virtual machine you just exported locally and you want to import and copy one, select copy virtual machine.

There are three subdirectories under the virtual machine directory

  • Snapshots: Storage directory for snapshots (checkpoints)
  • Virtual Hard Disks: The storage directory of virtual machine disks
  • Virtual Machines: This should be the storage directory for virtual machine settings.

In fact, you only need Virtual Hard Disks to import this virtual machine on another machine. That is, snapshot information and virtual machine configuration information will be lost. There are two formats of disk files in the Virtual Hard Disks directory, .vhdx and .avhdx. The former is a virtual machine disk and the latter is a snapshot disk. To merge the two files first, please see the tutorial: Merging Hyper-V Snapshots

Probably first use Get-VHD to obtain the information of this disk (the hierarchical relationship between disks), and then use Merge-VHD to merge (the order must be consistent). This is generally not used, but I didn’t know there was an export and import function when I first started using it. I loaded the .vhdx disk directly and found that many of the installed software were gone. Then I merged other avhdx files and it became normal.

usb connection

Hyper-V can directly connect to the host's hard disk. First, right-click the required disk in the host's disk management to take it offline, and then add the offline disk in the virtual machine settings.

However, hyper-v cannot directly use other USB devices. For example, if I want to use USB in a virtual machine, I cannot use the adb command. Of course, this can be replaced by WiFiAdb. Hyper-V is a virtualization for servers. It is normal without this function.

After some searching on Baidu, some software can do it, such as usbip. I haven’t tested it yet. I’ll put down the links to these software first.

https://docs.microsoft.com/en-us/windows/wsl/connect-usb
https://github.com/dorssel/usbipd-win
https://github.com/cezanne/usbip-win
https://yadom.in/archives/usb-passthrough-hyper-v-and-wsl2.html

The first link has been tested, which is Microsoft's official documentation. The tests in WSL2 Ubuntu20 are normal, and the steps are very simple. Download the msi installation of GitHub in Windows, and install some packages with apt in WSL. You may be prompted to update wsl in the middle, just run it wsl --updateand wait for the update to complete. The test system is Windows10 21H2

Updated on 2022-10-17

Reduce virtual machine footprint

The disk file formats used by hyper-v are vhd and vhdx. If dynamic expansion is set, these two disk files will only increase the space, but will not reduce the space, even if you have deleted many files in the virtual machine.

Solution:

Right-click and select Settings, select the hard drive, click Edit on the right
Insert image description here
, then Next, and then select Compression. Just take the next step.
Insert image description here
Sometimes clicking the compression button will flash by and nothing will happen.

This requires first mounting the entire disk on the host and attaching the VHD in the disk manager. Note that it needs to be checked and then 只读
Insert image description here
right-click the local disk -> Properties in the file manager and select Optimize. Just select the virtual disk optimization you just attached.
Win10 cannot optimize the virtual disk. After optimization, it can be compressed. After
reading the article below, this step is not necessary and can be skipped. You can also try and re-execute
Insert image description here
the above compression operation to reduce the size of the vhdx. Then detach the mounted disk

Another way is to use the powershell command to operate, reference link: https://www.sikich.com/insight/how-to-compact-dynamic-vhd-and-vhdx-files-in-hyper-v-2012-and -newer/

Probably just execute three commands sequentially in powershell:

Mount the disk as read-only (the same as manually attaching the VHD in the Device Manager above)
Mount-VHD -Path "C:\ClusterStorage\Volume1\VM1\Virtual Hard Disks\VM1.vhdx" -ReadOnly
Optimize the disk
Optimize-VHD -Path "C:\ClusterStorage\Volume1\VM1\Virtual Hard Disks\VM1.vhdx" -Mode Full
Unmount the disk
Dismount-VHD "C:\ClusterStorage\Volume1\VM1\Virtual Hard Disks\VM1.vhdx"

There is a problem with the operation of Windows 10. No matter how you modify the C disk, you cannot reduce the space. This operation is only useful for the D disk of the virtual machine. Windows 11 was used for the above test and there was no problem.

Guess you like

Origin blog.csdn.net/Qwertyuiop2016/article/details/126440731