在 Hyper-v 上运行 Linux 的最佳实践Best Practices for running Linux on Hyper-V
01/08/2021
本文内容
适用于: Azure Stack HCI,版本 20H2;Windows Server 2019,Windows Server 2016,Hyper-v Server 2016,Windows Server 2012 R2,Hyper-v server 2012 R2,Windows Server 2012,Hyper-v Server 2012,Windows Server 2008 R2,Windows 10,Windows 8.1,Windows 8,Windows 7.1,Windows 7Applies To: Azure Stack HCI, version 20H2; Windows Server 2019, Windows Server 2016, Hyper-V Server 2016, Windows Server 2012 R2, Hyper-V Server 2012 R2, Windows Server 2012, Hyper-V Server 2012, Windows Server 2008 R2, Windows 10, Windows 8.1, Windows 8, Windows 7.1, Windows 7
本主题包含在 Hyper-v 上运行 Linux 虚拟机的建议列表。This topic contains a list of recommendations for running Linux virtual machine on Hyper-V.
优化动态 VHDX 文件上的 Linux 文件系统Tuning Linux File Systems on Dynamic VHDX Files
即使文件系统主要为空,某些 Linux 文件系统也可能会消耗大量的实际磁盘空间。Some Linux file systems may consume significant amounts of real disk space even when the file system is mostly empty. 若要减少动态 VHDX 文件的实际磁盘空间使用情况,请考虑以下建议:To reduce the amount of real disk space usage of dynamic VHDX files, consider the following recommendations:
创建 VHDX 时,请在 PowerShell 中使用默认 32MB) (1MB BlockSizeBytes,例如:When creating the VHDX, use 1MB BlockSizeBytes (from the default 32MB) in PowerShell, for example:
PS > New-VHD -Path C:\MyVHDs\test.vhdx -SizeBytes 127GB -Dynamic -BlockSizeBytes 1MB
Ext4 格式优先于 ext3,因为在与动态 VHDX 文件一起使用时,ext4 比 ext3 更节省空间。The ext4 format is preferred to ext3 because ext4 is more space efficient than ext3 when used with dynamic VHDX files.
创建 filesystem 时,请将组数指定为4096,例如:When creating the filesystem specify the number of groups to be 4096, for example:
# mkfs.ext4 -G 4096 /dev/sdX1
第2代虚拟机上的 Grub 菜单超时Grub Menu Timeout on Generation 2 Virtual Machines
由于从第2代虚拟机的模拟中删除了旧硬件,因此,将显示 grub 菜单倒计时计时器,以便显示该 grub 菜单,立即加载默认条目。Because of legacy hardware being removed from emulation in Generation 2 virtual machines, the grub menu countdown timer counts down too quickly for the grub menu to be displayed, immediately loading the default entry. 在将 grub 固定到使用支持 EFI 的计时器之前,请修改 /boot/grub/grub.conf、/etc/default/grub,或等效于使用 “timeout = 100000” 而不是默认值 “timeout = 5″。Until grub is fixed to use the EFI-supported timer, modify /boot/grub/grub.conf, /etc/default/grub, or equivalent to have “timeout=100000” instead of the default “timeout=5”.
第2代虚拟机上的 PxE 启动PxE Boot on Generation 2 Virtual Machines
由于第2代虚拟机中不存在 PIT 计时器,因此到 PxE TFTP 服务器的网络连接可能会提前终止,并阻止加载服务器从服务器读取 Grub 配置和加载内核。Because the PIT timer is not present in Generation 2 Virtual Machines, network connections to the PxE TFTP server can be prematurely terminated and prevent the bootloader from reading Grub configuration and loading a kernel from the server.
在 RHEL 1.x 以外的 Linux 分发版上,可以遵循类似的步骤配置 grub v 0.97,以便从 PxE 服务器加载 Linux 内核。On Linux distributions other than RHEL 6.x, similar steps can be followed to configure grub v0.97 to load Linux kernels from a PxE server.
此外,在 RHEL/CentOS 6.6 键盘和鼠标输入无法与预安装内核一起使用,这会阻止在菜单中指定安装选项。Additionally, on RHEL/CentOS 6.6 keyboard and mouse input will not work with the pre-install kernel which prevents specifying installation options in the menu. 必须将串行控制台配置为允许选择安装选项。A serial console must be configured to allow choosing installation options.
在 PxE 服务器上的 efidefault 文件中,添加以下内核参数 “console = ttyS1″In the efidefault file on the PxE server, add the following kernel parameter “console=ttyS1”
在 Hyper-v 中的 VM 上,使用以下 PowerShell cmdlet 设置 COM 端口:On the VM in Hyper-V, set up a COM port using this PowerShell cmdlet:
Set-VMComPort -VMName -Number 2 -Path \\.\pipe\dbg1
将 kickstart 文件指定到预安装内核还将避免在安装过程中需要键盘和鼠标输入。Specifying a kickstart file to the pre-install kernel would also avoid the need for keyboard and mouse input during installation.
在故障转移群集中使用静态 MAC 地址Use static MAC addresses with failover clustering
将使用故障转移群集部署的 Linux 虚拟机应配置为具有静态媒体访问控制, (每个虚拟网络适配器的 MAC) 地址。Linux virtual machines that will be deployed using failover clustering should be configured with a static media access control (MAC) address for each virtual network adapter. 在某些版本的 Linux 中,故障转移后可能会丢失网络配置,因为已将新的 MAC 地址分配给虚拟网络适配器。In some versions of Linux, the networking configuration may be lost after failover because a new MAC address is assigned to the virtual network adapter. 若要避免丢失网络配置,请确保每个虚拟网络适配器都有一个静态 MAC 地址。To avoid losing the network configuration, ensure that each virtual network adapter has a static MAC address. 可以通过在 Hyper-v 管理器或故障转移群集管理器中编辑虚拟机的设置来配置 MAC 地址。You can configure the MAC address by editing the settings of the virtual machine in Hyper-V Manager or Failover Cluster Manager.
使用 Hyper-v 特定的网络适配器,而不是旧的网络适配器Use Hyper-V-specific network adapters, not the legacy network adapter
配置并使用虚拟以太网适配器,该适配器是一种具有增强性能的 Hyper-v 特定网卡。Configure and use the virtual Ethernet adapter, which is a Hyper-V-specific network card with enhanced performance. 如果旧网络适配器和 Hyper-v 特定网络适配器均连接到虚拟机,则 ifconfig 的输出中的网络名称可能会显示随机值,例如 _tmp12000801310。If both legacy and Hyper-V-specific network adapters are attached to a virtual machine, the network names in the output of ifconfig -a might show random values such as _tmp12000801310. 若要避免此问题,请在 Linux 虚拟机中使用 Hyper-v 特定的网络适配器时,删除所有旧版网络适配器。To avoid this issue, remove all legacy network adapters when using Hyper-V-specific network adapters in a Linux virtual machine.
使用 i/o 计划程序 noop/none 提高磁盘 i/o 性能Use I/O scheduler noop/none for better disk I/O performance
Linux 内核提供两组磁盘 i/o 计划程序来重新排序请求。The Linux kernel offers two sets of disk I/O schedulers to reorder requests. 一个集用于较早的 “blk” 子系统,另一个集用于较新的 “blk” 子系统。One set is for the older ‘blk’ subsystem and one set is for the newer ‘blk-mq’ subsystem. 在这两种情况下,对于目前的固态磁盘,建议使用将计划决策传递到基础 Hyper-v 虚拟机监控程序的计划程序。In either case, with today’s solid state disks it is recommended to use a scheduler that passes the scheduling decisions to the underlying Hyper-V hypervisor. 对于使用 “blk” 子系统的 Linux 内核,这是 “noop” 计划程序。For Linux kernels using the ‘blk’ subsystem, this is the “noop” scheduler. 对于使用 “blk-mq” 子系统的 Linux 内核,这是 “无” 计划程序。For Linux kernels using the ‘blk-mq’ subsystem, this is the “none” scheduler.
对于特定的磁盘,可在此文件系统位置查看可用的计划程序:/sys/class/block/ /queue/scheduler,当前所选计划程序位于方括号中。For a particular disk, the available schedulers can be seen at this file system location: /sys/class/block//queue/scheduler, with the currently selected scheduler in square brackets. 您可以通过写入此文件系统位置来更改计划程序。You can change the scheduler by writing to this file system location. 必须将更改添加到初始化脚本,才能在重新启动后保持。The change must be added to an initialization script in order to persist across reboots. 有关详细信息,请参阅 Linux 发行版文档。Consult your Linux distro documentation for details.
NUMANUMA
低于 2.6.37 的 Linux 内核版本不支持具有更大 VM 大小的 Hyper-V 上的 NUMA。Linux kernel versions earlier than 2.6.37 don’t support NUMA on Hyper-V with larger VM sizes. 此问题主要影响使用上游 Red Hat 2.6.32 内核的旧分发版,在 Red Hat Enterprise Linux (RHEL) 6.6 (kernel-2.6.32-504) 中已解决。This issue primarily impacts older distributions using the upstream Red Hat 2.6.32 kernel, and was fixed in Red Hat Enterprise Linux (RHEL) 6.6 (kernel-2.6.32-504). 运行版本低于 2.6.37 的自定义内核的系统,或者版本低于 2.6.32-504 的基于 RHEL 的内核必须在 grub.conf 中的内核命令行上设置启动参数 numa=off。Systems running custom kernels older than 2.6.37, or RHEL-based kernels older than 2.6.32-504 must set the boot parameter numa=off on the kernel command line in grub.conf. For more information, see Red Hat KB 436883.
为 kdump 保留更多内存Reserve more memory for kdump
如果转储捕获内核在启动时出现死机,请为内核保留更多内存。In case the dump capture kernel ends up with a panic on boot, reserve more memory for the kernel. 例如,在 Ubuntu grub 配置文件中将参数 crashkernel = 384M-: 128M 更改为 crashkernel = 384M-: 256M 。For example, change the parameter crashkernel=384M-:128M to crashkernel=384M-:256M in the Ubuntu grub configuration file.
缩小 VHDX 或扩展 VHD 和 VHDX 文件可能导致 GPT 分区表错误Shrinking VHDX or expanding VHD and VHDX files can result in erroneous GPT partition tables
Hyper-v 允许压缩虚拟磁盘 (VHDX) 文件,而不考虑磁盘上可能存在的任何分区、卷或文件系统数据结构。Hyper-V allows shrinking virtual disk (VHDX) files without regard for any partition, volume, or file system data structures that may exist on the disk. 如果 VHDX 的结束时间收缩到在分区结束之前的 VHDX 位置,则数据可能会丢失,分区可能会损坏,或者在读取分区时返回的数据无效。If the VHDX is shrunk to where the end of the VHDX comes before the end of a partition, data can be lost, that partition can become corrupted, or invalid data can be returned when the partition is read.
调整 VHD 或 VHDX 大小后,管理员应使用诸如 fdisk 或 parted 的实用工具来更新分区、卷和文件系统结构,以反映磁盘大小的变化。After resizing a VHD or VHDX, administrators should use a utility like fdisk or parted to update the partition, volume, and file system structures to reflect the change in the size of the disk. 缩小或扩大包含 GUID 分区表 (GPT) 的 VHD 或 VHDX 的大小会导致在分区管理工具用于检查分区布局时出现警告,并会警告管理员修复第一个和第二个 GPT 标头。Shrinking or expanding the size of a VHD or VHDX that has a GUID Partition Table (GPT) will cause a warning when a partition management tool is used to check the partition layout, and the administrator will be warned to fix the first and secondary GPT headers. 此手动步骤可以安全地执行,而不会丢失数据。This manual step is safe to perform without data loss.
其他参考Additional References