嵌套虚拟化
物理机: windows 10 专业版,
k8s 节点: virtualbox 6.1.22 虚拟机,使用嵌套虚拟化,ubuntu 18.04.5, k8s 1.18.20。
Dockerfile
进入 windows server 2019下载目录(此目录应当只有一个iso)构建镜像
cd /media/sf_backup/dev/os/windows-server-1909
cat <<EOF | sudo tee Dockerfile
FROM scratch
ADD --chown=107:107 en_windows_server_2019_x64_dvd_4cb967d8.iso /disk/
EOF
docker build -t windows-server-2019:1909 .
w2019-1909-c.yaml
apiVersion: kubevirt.io/v1alpha3
kind: VirtualMachineInstance
metadata:
name: w2019-1909-c
labels:
kubevirt.io/domain: w2019-1909-c
spec:
domain:
firmware:
# this sets the bootloader type
bootloader:
bios: {}
cpu:
sockets: 1
cores: 2
threads: 2
features:
acpi: {}
apic: {}
hyperv:
relaxed: {}
vapic: {}
vpindex: {}
spinlocks:
spinlocks: 8191
resources:
requests:
memory: 4096M
cpu: 4
devices:
blockMultiQueue: true
#networkInterfaceMultiqueue: true
inputs:
- type: tablet
bus: usb
#bus: virtio
name: tablet1
disks:
- name: rootdisk
bootOrder: 1
disk:
bus: virtio
- name: containerdisk
bootOrder: 2
cdrom:
bus: sata
- name: virtiocontainerdisk
bootOrder: 3
cdrom:
bus: sata
interfaces:
- name: default
model: e1000
#bridge: {}
masquerade: {}
networks:
- name: default
pod: {}
volumes:
- name: containerdisk
containerDisk:
image: windows-server-2019:1909
- name: rootdisk
hostDisk:
capacity: 30Gi
path: /data/vm/instance/w2019-1909.img
type: DiskOrCreate
- name: virtiocontainerdisk
containerDisk:
image: kubevirt/virtio-container-disk:v0.37.0
kubectl create -f w2019-1909-c.yaml 创建虚拟机。
启动成功,运行 virtctl vnc w2019-1909-c 进行安装。
安装时,需要scan加载驱动,可选 Red Hat VirtIO SCSI controller。
w2019-1909-h.yaml
按照 w2019-1909-c.yaml 安装好windows2019后,可以使用 hostDisk 直接启动。
apiVersion: kubevirt.io/v1alpha3
kind: VirtualMachineInstance
metadata:
name: w2019-1909-h
labels:
kubevirt.io/domain: w2019-1909-h
spec:
domain:
firmware:
# this sets the bootloader type
bootloader:
bios: {}
cpu:
sockets: 1
cores: 2
threads: 2
features:
acpi: {}
apic: {}
hyperv:
relaxed: {}
vapic: {}
vpindex: {}
spinlocks:
spinlocks: 8191
resources:
requests:
memory: 4096M
cpu: 4
devices:
blockMultiQueue: true
#networkInterfaceMultiqueue: true
inputs:
- type: tablet
bus: usb
#bus: virtio
name: tablet1
disks:
- name: rootdisk
bootOrder: 1
disk:
bus: virtio
interfaces:
- name: default
model: e1000
#bridge: {}
masquerade: {}
networks:
- name: default
pod: {}
volumes:
- name: rootdisk
hostDisk:
capacity: 30Gi
path: /data/vm/instance/w2019-1909.img
type: DiskOrCreate
powershell 执行命令 Install-WindowsFeature nfs-client,安装 nfs client。
在cmd执行 mount \ip\data s: ,可挂载nfs。
w2019-ui-c.yaml
加入自动登录,执行开机脚本。在安装后,不再需要安装盘,把安装盘放到最后。
apiVersion: v1
kind: ConfigMap
metadata:
name: win2019-configmap
data:
autounattend.xml: |-
<?xml version="1.0" encoding="utf-8"?>
<unattend xmlns="urn:schemas-microsoft-com:unattend">
<settings pass="windowsPE">
<component xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Microsoft-Windows-International-Core-WinPE" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
<SetupUILanguage>
<UILanguage>en-US</UILanguage>
</SetupUILanguage>
<InputLocale>0409:00000409</InputLocale>
<SystemLocale>en-US</SystemLocale>
<UILanguage>en-US</UILanguage>
<UILanguageFallback>en-US</UILanguageFallback>
<UserLocale>en-US</UserLocale>
</component>
<component xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Microsoft-Windows-PnpCustomizationsWinPE" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
<DriverPaths>
<PathAndCredentials wcm:keyValue="4b29ba63" wcm:action="add">
<Path>d:\amd64\2k8</Path>
</PathAndCredentials>
<PathAndCredentials wcm:keyValue="25fe51ea" wcm:action="add">
<Path>d:\viostor\2k8\amd64</Path>
</PathAndCredentials>
</DriverPaths>
</component>
<component xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Microsoft-Windows-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
<DiskConfiguration>
<Disk wcm:action="add">
<CreatePartitions>
<CreatePartition wcm:action="add">
<Order>1</Order>
<Type>Primary</Type>
<Size>100</Size>
</CreatePartition>
<CreatePartition wcm:action="add">
<Extend>true</Extend>
<Order>2</Order>
<Type>Primary</Type>
</CreatePartition>
</CreatePartitions>
<ModifyPartitions>
<ModifyPartition wcm:action="add">
<Format>NTFS</Format>
<Label>System Reserved</Label>
<Order>1</Order>
<PartitionID>1</PartitionID>
<TypeID>0x27</TypeID>
</ModifyPartition>
<ModifyPartition wcm:action="add">
<Format>NTFS</Format>
<Label>OS</Label>
<Letter>C</Letter>
<Order>2</Order>
<PartitionID>2</PartitionID>
</ModifyPartition>
</ModifyPartitions>
<DiskID>0</DiskID>
<WillWipeDisk>true</WillWipeDisk>
</Disk>
</DiskConfiguration>
<ImageInstall>
<OSImage>
<InstallFrom>
<MetaData wcm:action="add">
<Key>/Image/Description</Key>
<Value>Windows server 2019</Value>
</MetaData>
</InstallFrom>
<InstallTo>
<DiskID>0</DiskID>
<PartitionID>2</PartitionID>
</InstallTo>
</OSImage>
</ImageInstall>
<UserData>
<AcceptEula>true</AcceptEula>
<FullName/>
<Organization/>
<ProductKey>
<Key/>
</ProductKey>
</UserData>
</component>
</settings>
<settings pass="offlineServicing">
<component xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Microsoft-Windows-LUA-Settings" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
<EnableLUA>false</EnableLUA>
</component>
</settings>
<settings pass="specialize">
<component xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Microsoft-Windows-International-Core" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
<InputLocale>0409:00000409</InputLocale>
<SystemLocale>en-US</SystemLocale>
<UILanguage>en-US</UILanguage>
<UILanguageFallback>en-US</UILanguageFallback>
<UserLocale>en-US</UserLocale>
</component>
<component xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Microsoft-Windows-Security-SPP-UX" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
<SkipAutoActivation>true</SkipAutoActivation>
</component>
<component xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Microsoft-Windows-SQMApi" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
<CEIPEnabled>0</CEIPEnabled>
</component>
</settings>
<settings pass="oobeSystem">
<component xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
<OOBE>
<HideEULAPage>true</HideEULAPage>
<HideOEMRegistrationScreen>true</HideOEMRegistrationScreen>
<HideOnlineAccountScreens>true</HideOnlineAccountScreens>
<HideWirelessSetupInOOBE>true</HideWirelessSetupInOOBE>
<NetworkLocation>Work</NetworkLocation>
<SkipUserOOBE>true</SkipUserOOBE>
<SkipMachineOOBE>true</SkipMachineOOBE>
<ProtectYourPC>3</ProtectYourPC>
</OOBE>
<AutoLogon>
<Password>
<Value>123456</Value>
<PlainText>true</PlainText>
</Password>
<Enabled>true</Enabled>
<Username>Administrator</Username>
</AutoLogon>
<UserAccounts>
<AdministratorPassword>
<Value>123456</Value>
<PlainText>true</PlainText>
</AdministratorPassword>
</UserAccounts>
<RegisteredOrganization/>
<RegisteredOwner/>
<TimeZone>China Standard Time</TimeZone>
<FirstLogonCommands>
<SynchronousCommand wcm:action="add">
<CommandLine>powershell -ExecutionPolicy Bypass -NoExit -NoProfile e:\post-install.ps1</CommandLine>
<RequiresUserInput>false</RequiresUserInput>
<Order>1</Order>
<Description>Post Installation Script</Description>
</SynchronousCommand>
</FirstLogonCommands>
</component>
</settings>
</unattend>
post-install.ps1: |-
# Remove AutoLogin
# https://docs.microsoft.com/en-us/windows-hardware/customize/desktop/unattend/microsoft-windows-shell-setup-autologon-logoncount#logoncount-known-issue
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v AutoAdminLogon /t REG_SZ /d 0 /f
# install Qemu Tools (Drivers)
#Start-Process msiexec -Wait -ArgumentList '/i d:\virtio-win-gt-x64.msi /qn /passive /norestart'
# install Guest Agent
#Start-Process msiexec -Wait -ArgumentList '/i d:\guest-agent\qemu-ga-x86_64.msi /qn /passive /norestart'
# Rename cached unattend.xml to avoid it is picked up by sysprep
mv C:\Windows\Panther\unattend.xml C:\Windows\Panther\unattend.install.xml
# Eject CD, to avoid that the autounattend.xml on the CD is picked up by sysprep
(new-object -COM Shell.Application).NameSpace(17).ParseName('e:').InvokeVerb('Eject')
# Run Sysprep and Shutdown
C:\Windows\System32\Sysprep\sysprep.exe /generalize /oobe /shutdown /mode:vm
#C:\Windows\System32\Sysprep\sysprep.exe /generalize /oobe /shutdown
---
apiVersion: kubevirt.io/v1alpha3
kind: VirtualMachineInstance
metadata:
name: w2019-ui-c
labels:
kubevirt.io/domain: w2019-ui-c
spec:
domain:
firmware:
# this sets the bootloader type
bootloader:
bios: {}
cpu:
sockets: 1
cores: 2
threads: 2
features:
acpi: {}
apic: {}
hyperv:
relaxed: {}
vapic: {}
vpindex: {}
spinlocks:
spinlocks: 8191
resources:
requests:
memory: 4096M
cpu: 4
devices:
blockMultiQueue: true
#networkInterfaceMultiqueue: true
inputs:
- type: tablet
bus: usb
#bus: virtio
name: tablet1
disks:
- name: rootdisk
bootOrder: 1
disk:
bus: virtio
- name: virtiocontainerdisk
bootOrder: 3
cdrom:
bus: sata
- name: sysprep
bootOrder: 4
cdrom:
bus: sata
- name: containerdisk
bootOrder: 2
cdrom:
bus: sata
interfaces:
- name: default
model: e1000
#bridge: {}
masquerade: {}
networks:
- name: default
pod: {}
volumes:
- name: rootdisk
hostDisk:
capacity: 30Gi
path: /data/vm/instance/w2019-c.img
type: DiskOrCreate
- name: virtiocontainerdisk
containerDisk:
image: kubevirt/virtio-container-disk:v0.37.0
- name: sysprep
sysprep:
configMap:
name: win2019-configmap
- name: containerdisk
containerDisk:
image: windows-server-2019:ui
w2019-ui-h.yaml
按照 w2019-ui-ok.yaml 安装好后,可以直接去掉安装盘。安装盘放最前,去掉安装盘后,还有麻烦要处理,无法运行开机脚本。
apiVersion: v1
kind: ConfigMap
metadata:
name: win2019-ok-configmap
data:
autounattend.xml: |-
<?xml version="1.0" encoding="utf-8"?>
<!-- responsible for installing windows, ignored on sysprepped images -->
unattend.xml: |-
<?xml version="1.0" encoding="utf-8"?>
<unattend xmlns="urn:schemas-microsoft-com:unattend">
<settings pass="oobeSystem">
<component xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
<OOBE>
<HideEULAPage>true</HideEULAPage>
<HideOEMRegistrationScreen>true</HideOEMRegistrationScreen>
<HideOnlineAccountScreens>true</HideOnlineAccountScreens>
<HideWirelessSetupInOOBE>true</HideWirelessSetupInOOBE>
<NetworkLocation>Work</NetworkLocation>
<SkipUserOOBE>true</SkipUserOOBE>
<SkipMachineOOBE>true</SkipMachineOOBE>
<ProtectYourPC>3</ProtectYourPC>
</OOBE>
<AutoLogon>
<Password>
<Value>XFuzz@666.cn</Value>
<PlainText>true</PlainText>
</Password>
<Enabled>true</Enabled>
<Username>Administrator</Username>
</AutoLogon>
<UserAccounts>
<AdministratorPassword>
<Value>XFuzz@666.cn</Value>
<PlainText>true</PlainText>
</AdministratorPassword>
</UserAccounts>
<RegisteredOrganization>Kuebvirt</RegisteredOrganization>
<RegisteredOwner>Kubevirt</RegisteredOwner>
<TimeZone>Eastern Standard Time</TimeZone>
<FirstLogonCommands>
<SynchronousCommand wcm:action="add">
<CommandLine>powershell -ExecutionPolicy Bypass -NoExit -WindowStyle Hidden -NoProfile e:\setup.ps1</CommandLine>
<RequiresUserInput>false</RequiresUserInput>
<Order>1</Order>
<Description>Customize Script</Description>
</SynchronousCommand>
</FirstLogonCommands>
<LogonCommands>
<AsynchronousCommand wcm:action="add">
<CommandLine>powershell -ExecutionPolicy Bypass -NoExit -WindowStyle Hidden -NoProfile e:\setup.ps1</CommandLine>
<RequiresUserInput>false</RequiresUserInput>
<Order>1</Order>
<Description>Customize Script</Description>
</AsynchronousCommand>
</LogonCommands>
</component>
</settings>
</unattend>
setup.ps1: |-
date >> C:\Users\Administrator\Desktop\tmp.txt
#after install nfs client, uncomment next 1 line
#$r = (cmd /c mount \\192.88.1.172\data s:)
---
apiVersion: kubevirt.io/v1alpha3
kind: VirtualMachineInstance
metadata:
name: w2019-h
labels:
kubevirt.io/domain: w2019-h
spec:
domain:
clock:
timezone: "Asia/Shanghai"
utc: {}
timer: {}
cpu:
sockets: 4
cores: 1
threads: 1
features:
acpi: {}
apic: {}
hyperv:
relaxed: {}
vapic: {}
vpindex: {}
spinlocks:
spinlocks: 8191
resources:
requests:
memory: 4096M
cpu: 4
devices:
blockMultiQueue: true
inputs:
- type: tablet
bus: usb
#bus: virtio
name: tablet1
disks:
- name: rootdisk
bootOrder: 1
disk:
bus: virtio
- name: virtiocontainerdisk
bootOrder: 2
cdrom:
bus: sata
- name: sysprep
bootOrder: 3
cdrom:
bus: sata
interfaces:
- name: default
model: e1000
masquerade: {}
networks:
- name: default
pod: {}
volumes:
- name: rootdisk
hostDisk:
capacity: 30Gi
path: /data/vm/instance/w2019-c.img
type: DiskOrCreate
- name: virtiocontainerdisk
containerDisk:
image: kubevirt/virtio-container-disk:v0.37.0
- name: sysprep
sysprep:
configMap:
name: win2019-ok-configmap