- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
如何使用 Docker Desktop/Hyper-V/MobyLinuxVM 从 Windows 主机 LAN 透明地访问 Linux 容器?
我的希望 :
最佳答案
经过一周关于如何使用 MobyLinuxVM Hyper-V 从 Windows 主机专用 LAN 透明访问 Linux 容器的调查,以及大量阅读论坛后,这是我的方法:
Param(
[string] $VmName = "MobyLinuxVM",
[string] $IsoFile = ".\docker-for-win.iso",
# BLT
# [string] $SwitchName = "DockerNAT",
[string] $SwitchName = "Custom",
[string] $VhdPathOverride = $null,
[long] $VhdSize = 64*1000*1000*1000,
[string] $confIsoFile = $null,
[string] $DockerIsoFile = $null,
[Parameter(ParameterSetName='Create',Mandatory=$false)][switch] $Create,
[Parameter(ParameterSetName='Create',Mandatory=$false)][int] $CPUs = 2,
[Parameter(ParameterSetName='Create',Mandatory=$false)][long] $Memory = 2048,
[Parameter(ParameterSetName='Create',Mandatory=$false)][string] $SwitchSubnetAddress = "10.0.75.0",
[Parameter(ParameterSetName='Create',Mandatory=$false)][int] $SwitchSubnetMaskSize = 24,
[Parameter(ParameterSetName='Destroy',Mandatory=$false)][switch] $Destroy,
[Parameter(ParameterSetName='Destroy',Mandatory=$false)][switch] $KeepVolume,
[Parameter(ParameterSetName='Start',Mandatory=$false)][switch] $Start,
[Parameter(ParameterSetName='Stop',Mandatory=$false)][switch] $Stop
)
Write-Output "Script started at $(Get-Date -Format "HH:mm:ss.fff")"
# This makes sure the system modules can be imported
$env:PSModulePath = [Environment]::GetEnvironmentVariable('PSModulePath','Machine')
# Make sure we stop at Errors unless otherwise explicitly specified
$ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"
# Explicitly disable Module autoloading and explicitly import the
# Modules this script relies on. This is not strictly necessary but
# good practise as it prevents arbitrary errors
$PSModuleAutoloadingPreference = 'None'
Import-Module Microsoft.PowerShell.Utility
Import-Module Microsoft.PowerShell.Management
Import-Module Hyper-V
Import-Module NetAdapter
Import-Module NetTCPIP
Write-Output "Modules loaded at $(Get-Date -Format "HH:mm:ss.fff")"
function Get-Vhd-Root {
if($VhdPathOverride){
return $VhdPathOverride
}
# Default location for VHDs
$VhdRoot = "$((Hyper-V\Get-VMHost -ComputerName localhost).VirtualHardDiskPath)".TrimEnd("\")
# Where we put Moby
return "$VhdRoot\$VmName.vhdx"
}
function New-Switch {
$ipParts = $SwitchSubnetAddress.Split('.')
[int]$switchIp3 = $null
[int32]::TryParse($ipParts[3] , [ref]$switchIp3 ) | Out-Null
$Ip0 = $ipParts[0]
$Ip1 = $ipParts[1]
$Ip2 = $ipParts[2]
$Ip3 = $switchIp3 + 1
$switchAddress = "$Ip0.$Ip1.$Ip2.$Ip3"
# BLT
# $vmSwitch = Hyper-V\Get-VMSwitch $SwitchName -SwitchType Internal -ea SilentlyContinue
$vmSwitch = Hyper-V\Get-VMSwitch $SwitchName -ea SilentlyContinue
$vmNetAdapter = Hyper-V\Get-VMNetworkAdapter -ManagementOS -SwitchName $SwitchName -ea SilentlyContinue
if ($vmSwitch -and $vmNetAdapter) {
Write-Output "Using existing Switch: $SwitchName"
} else {
Write-Output "Creating Switch: $SwitchName..."
Hyper-V\Remove-VMSwitch $SwitchName -Force -ea SilentlyContinue
# BLT
# Hyper-V\New-VMSwitch $SwitchName -SwitchType Internal -ea SilentlyContinue | Out-Null
Hyper-V\New-VMSwitch $SwitchName -NetAdapterInterfaceDescription "XXXXXXXXXXX Microsoft KM-TEST" -ea SilentlyContinue | Out-Null
$vmNetAdapter = Hyper-V\Get-VMNetworkAdapter -ManagementOS -SwitchName $SwitchName
Write-Output "Switch created."
}
# Make sure there are no lingering net adapter
$netAdapters = Get-NetAdapter | ? { $_.Name.StartsWith("vEthernet ($SwitchName)") }
if (($netAdapters).Length -gt 1) {
Write-Output "Disable and rename invalid NetAdapters"
$now = (Get-Date -Format FileDateTimeUniversal)
$index = 1
$invalidNetAdapters = $netAdapters | ? { $_.DeviceID -ne $vmNetAdapter.DeviceId }
foreach ($netAdapter in $invalidNetAdapters) {
$netAdapter `
| Disable-NetAdapter -Confirm:$false -PassThru `
| Rename-NetAdapter -NewName "Broken Docker Adapter ($now) ($index)" `
| Out-Null
$index++
}
}
# Make sure the Switch has the right IP address
$networkAdapter = Get-NetAdapter | ? { $_.DeviceID -eq $vmNetAdapter.DeviceId }
if ($networkAdapter | Get-NetIPAddress -IPAddress $switchAddress -ea SilentlyContinue) {
$networkAdapter | Disable-NetAdapterBinding -ComponentID ms_server -ea SilentlyContinue
$networkAdapter | Enable-NetAdapterBinding -ComponentID ms_server -ea SilentlyContinue
Write-Output "Using existing Switch IP address"
return
}
# BLT
#$networkAdapter | Remove-NetIPAddress -Confirm:$false -ea SilentlyContinue
#$networkAdapter | Set-NetIPInterface -Dhcp Disabled -ea SilentlyContinue
#$networkAdapter | New-NetIPAddress -AddressFamily IPv4 -IPAddress $switchAddress -PrefixLength ($SwitchSubnetMaskSize) -ea Stop | Out-Null
$networkAdapter | Disable-NetAdapterBinding -ComponentID ms_server -ea SilentlyContinue
$networkAdapter | Enable-NetAdapterBinding -ComponentID ms_server -ea SilentlyContinue
Write-Output "Set IP address on switch"
}
function Remove-Switch {
Write-Output "Destroying Switch $SwitchName..."
# Let's remove the IP otherwise a nasty bug makes it impossible
# to recreate the vswitch
$vmNetAdapter = Hyper-V\Get-VMNetworkAdapter -ManagementOS -SwitchName $SwitchName -ea SilentlyContinue
if ($vmNetAdapter) {
$networkAdapter = Get-NetAdapter | ? { $_.DeviceID -eq $vmNetAdapter.DeviceId }
$networkAdapter | Remove-NetIPAddress -Confirm:$false -ea SilentlyContinue
}
Hyper-V\Remove-VMSwitch $SwitchName -Force -ea SilentlyContinue
}
function New-MobyLinuxVM {
if (!(Test-Path $IsoFile)) {
Fatal "ISO file at $IsoFile does not exist"
}
$CPUs = [Math]::min((Hyper-V\Get-VMHost -ComputerName localhost).LogicalProcessorCount, $CPUs)
$vm = Hyper-V\Get-VM $VmName -ea SilentlyContinue
if ($vm) {
if ($vm.Length -ne 1) {
Fatal "Multiple VMs exist with the name $VmName. Delete invalid ones or reset Docker to factory defaults."
}
} else {
Write-Output "Creating VM $VmName..."
$vm = Hyper-V\New-VM -Name $VmName -Generation 2 -NoVHD
$vm | Hyper-V\Set-VM -AutomaticStartAction Nothing -AutomaticStopAction ShutDown -CheckpointType Disabled
}
if ($vm.Generation -ne 2) {
Fatal "VM $VmName is a Generation $($vm.Generation) VM. It should be a Generation 2."
}
if ($vm.State -ne "Off") {
Write-Output "VM $VmName is $($vm.State). Cannot change its settings."
return
}
Write-Output "Setting CPUs to $CPUs and Memory to $Memory MB"
$Memory = ([Math]::min($Memory, ($vm | Hyper-V\Get-VMMemory).MaximumPerNumaNode))
$vm | Hyper-V\Set-VM -MemoryStartupBytes ($Memory*1024*1024) -ProcessorCount $CPUs -StaticMemory
Ensure-VHD-Path($vm)
$vmNetAdapter = $vm | Hyper-V\Get-VMNetworkAdapter
if (!$vmNetAdapter) {
Write-Output "Attach Net Adapter"
$vmNetAdapter = $vm | Hyper-V\Add-VMNetworkAdapter -Passthru
}
Write-Output "Connect Internal Switch $SwitchName"
# BLT
# $vmNetAdapter | Hyper-V\Connect-VMNetworkAdapter -VMSwitch $(Hyper-V\Get-VMSwitch -ComputerName localhost $SwitchName -SwitchType Internal)
$vmNetAdapter | Hyper-V\Connect-VMNetworkAdapter -VMSwitch $(Hyper-V\Get-VMSwitch -ComputerName localhost $SwitchName)
if ($vm.DVDDrives) {
Write-Output "Remove existing DVDs"
Hyper-V\Remove-VMDvdDrive $vm.DVDDrives -ea SilentlyContinue
}
Write-Output "Attach DVD $IsoFile"
$vm | Hyper-V\Add-VMDvdDrive -Path $IsoFile
$iso = $vm | Hyper-V\Get-VMFirmware | select -ExpandProperty BootOrder | ? { $_.FirmwarePath.EndsWith("Scsi(0,1)") }
$vm | Hyper-V\Set-VMFirmware -EnableSecureBoot Off -FirstBootDevice $iso
$vm | Hyper-V\Set-VMComPort -number 1 -Path "\\.\pipe\docker$VmName-com1"
# Enable only required VM integration services
$intSvc = @()
$intSvc += "Microsoft:$($vm.Id)\84EAAE65-2F2E-45F5-9BB5-0E857DC8EB47" # Heartbeat
$intSvc += "Microsoft:$($vm.Id)\9F8233AC-BE49-4C79-8EE3-E7E1985B2077" # Shutdown
$intSvc += "Microsoft:$($vm.Id)\2497F4DE-E9FA-4204-80E4-4B75C46419C0" # TimeSynch
$vm | Hyper-V\Get-VMIntegrationService | ForEach-Object {
if ($intSvc -contains $_.Id) {
Hyper-V\Enable-VMIntegrationService $_
Write-Output "Enabled $($_.Name)"
} else {
Hyper-V\Disable-VMIntegrationService $_
Write-Output "Disabled $($_.Name)"
}
}
$vm | Hyper-V\Disable-VMConsoleSupport
Write-Output "VM created."
}
function Remove-MobyLinuxVM {
Write-Output "Removing VM $VmName..."
Hyper-V\Remove-VM $VmName -Force -ea SilentlyContinue
if (!$KeepVolume) {
$VmVhdFile = Get-Vhd-Root
Write-Output "Delete VHD $VmVhdFile"
Remove-Item $VmVhdFile -ea SilentlyContinue
}
}
function Start-MobyLinuxVM {
Write-Output "Starting VM $VmName..."
$vm = Hyper-V\Get-VM $VmName -ea SilentlyContinue
if ($vm.DVDDrives) {
Write-Output "Remove existing DVDs"
Hyper-V\Remove-VMDvdDrive $vm.DVDDrives -ea SilentlyContinue
}
Write-Output "Attach DVD $IsoFile"
$vm | Hyper-V\Add-VMDvdDrive -ControllerNumber 0 -ControllerLocation 1 -Path $IsoFile
if ((Get-Item $confIsoFile).length -gt 0) {
Write-Output "Attach Config ISO $confIsoFile"
if (($vm | Get-VMScsiController).length -le 1) {
$vm | Add-VMScsiController
}
$vm | Hyper-V\Add-VMDvdDrive -ControllerNumber 1 -ControllerLocation 1 -Path $confIsoFile
}
if ((Get-Item $DockerIsoFile).length -gt 0) {
Write-Output "Attach Docker ISO $DockerIsoFile"
if (($vm | Get-VMScsiController).length -le 2) {
$vm | Add-VMScsiController
}
$vm | Hyper-V\Add-VMDvdDrive -ControllerNumber 2 -ControllerLocation 1 -Path $DockerIsoFile
}
Ensure-VHD-Path($vm)
$iso = $vm | Hyper-V\Get-VMFirmware | select -ExpandProperty BootOrder | ? { $_.FirmwarePath.EndsWith("Scsi(0,1)") }
$vm | Hyper-V\Set-VMFirmware -EnableSecureBoot Off -BootOrder $iso
Hyper-V\Start-VM -VMName $VmName
}
function Ensure-VHD-Path {
Param($vm)
$VmVhdFile = Get-Vhd-Root
$vhd = Get-VHD -Path $VmVhdFile -ea SilentlyContinue
if (!$vhd) {
Write-Output "Creating dynamic VHD: $VmVhdFile"
$vhd = New-VHD -ComputerName localhost -Path $VmVhdFile -Dynamic -SizeBytes $VhdSize -BlockSizeBytes 1MB
}
if ($vm.HardDrives.Path -ne $VmVhdFile) {
if ($vm.HardDrives) {
Write-Output "Remove existing VHDs"
Hyper-V\Remove-VMHardDiskDrive $vm.HardDrives -ea SilentlyContinue
}
Write-Output "Attach VHD $VmVhdFile"
$vm | Hyper-V\Add-VMHardDiskDrive -Path $VmVhdFile
}
}
function Stop-MobyLinuxVM {
$vms = Hyper-V\Get-VM $VmName -ea SilentlyContinue
if (!$vms) {
Write-Output "VM $VmName does not exist"
return
}
foreach ($vm in $vms) {
Stop-VM-Force($vm)
}
}
function Stop-VM-Force {
Param($vm)
if ($vm.State -eq 'Off') {
Write-Output "VM $VmName is stopped"
return
}
$code = {
Param($vmId) # Passing the $vm ref is not possible because it will be disposed already
$vm = Hyper-V\Get-VM -Id $vmId -ea SilentlyContinue
if (!$vm) {
Write-Output "VM with Id $vmId does not exist"
return
}
$shutdownService = $vm | Hyper-V\Get-VMIntegrationService -Name Shutdown -ea SilentlyContinue
if ($shutdownService -and $shutdownService.PrimaryOperationalStatus -eq 'Ok') {
Write-Output "Shutdown VM $VmName..."
$vm | Hyper-V\Stop-VM -Confirm:$false -Force -ea SilentlyContinue
if ($vm.State -eq 'Off') {
return
}
}
Write-Output "Turn Off VM $VmName..."
$vm | Hyper-V\Stop-VM -Confirm:$false -TurnOff -Force -ea SilentlyContinue
}
Write-Output "Stopping VM $VmName..."
$job = Start-Job -ScriptBlock $code -ArgumentList $vm.VMId.Guid
if (Wait-Job $job -Timeout 20) { Receive-Job $job }
Remove-Job -Force $job -ea SilentlyContinue
if ($vm.State -eq 'Off') {
Write-Output "VM $VmName is stopped"
return
}
# If the VM cannot be stopped properly after the timeout
# then we have to kill the process and wait till the state changes to "Off"
for ($count = 1; $count -le 10; $count++) {
$ProcessID = (Get-WmiObject -Namespace root\virtualization\v2 -Class Msvm_ComputerSystem -Filter "Name = '$($vm.Id.Guid)'").ProcessID
if (!$ProcessID) {
Write-Output "VM $VmName killed. Waiting for state to change"
for ($count = 1; $count -le 20; $count++) {
if ($vm.State -eq 'Off') {
Write-Output "Killed VM $VmName is off"
Remove-Switch
$oldKeepVolumeValue = $KeepVolume
$KeepVolume = $true
Remove-MobyLinuxVM
$KeepVolume = $oldKeepVolumeValue
return
}
Start-Sleep -Seconds 1
}
Fatal "Killed VM $VmName did not stop"
}
Write-Output "Kill VM $VmName process..."
Stop-Process $ProcessID -Force -Confirm:$false -ea SilentlyContinue
Start-Sleep -Seconds 1
}
Fatal "Couldn't stop VM $VmName"
}
function Fatal {
throw "$args"
Exit 1
}
# Main entry point
Try {
Switch ($PSBoundParameters.GetEnumerator().Where({$_.Value -eq $true}).Key) {
'Stop' { Stop-MobyLinuxVM }
'Destroy' { Stop-MobyLinuxVM; Remove-Switch; Remove-MobyLinuxVM }
'Create' { New-Switch; New-MobyLinuxVM }
'Create' { New-MobyLinuxVM }
'Start' { Start-MobyLinuxVM }
}
} Catch {
throw
Exit 1
}
关于linux - 如何使用 Docker Desktop/Hyper-V/MobyLinuxVM 从 Windows 主机 LAN 透明访问 Linux 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54275962/
我的网站有两个版本。桌面版和移动版。 当用户通过智能手机访问我的网站时,我将其指向“移动版本”->“m.mywebsite.com”。 为此,我使用了名为 Mobile Detect 的项目 到目前为
在java中,我尝试使用Desktop.getDesktop().open(File file)打开一个文件,但它抛出IOException,尽管Desktop.getDesktop().isSupp
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
JetBrains Compose for Desktop 中有没有办法更改标题栏背景颜色或仅将其更改为深色模式?我使用的是 MacOS,因此该栏可以是浅色或深色。也可以让标题栏本身不可见(但保留关闭
Tableau 架构显示 Desktop 连接到 Tableau Server(使用网关),然后连接到 Tableau Server 上的数据服务器。我的问题是 tableau 客户端必须与 tabl
我在 Flutter 桌面上工作了一段时间,并且实现过程中一切顺利。到目前为止,我只实现了 UI、网络和内存缓存。现在我开始面临一个真正的问题,我无法找到解决方案。是否有一些开箱即用的可能性将文件保存
有没有办法将 Github Desktop 中的文件更改列表显示为树而不是平面文件列表? 最佳答案 似乎该功能存在于桌面版 Git 的 v1 中,并在应用程序的 v2 中被删除。 2018 年有帖子要
有谁知道在移动浏览器中选择桌面 View 选项会如何影响 CSS 媒体查询和 Javascript? 我正在制作一个仅供移动用户使用的网站。当我在使用普通 Android 浏览器时选择“桌面 View
/usr/share/applications 中的.desktop 文件、xdg-desktop-menu 命令和update-desktop-database 命令之间有什么关系? 我正在尝试创建
当项目属于我的帐户(或我的组织之一)时,Github Desktop 让我可以轻松地从 Github 克隆项目。 我有什么办法可以使用 Github Desktop 克隆属于其他人的公共(public
我不想被 Selenium RC 启动的浏览器打扰。有什么方法可以强制它在另一个 X11 桌面上运行浏览器吗?在另一个桌面上运行 Selenuim 服务器并不能解决问题。 Vít Šesták 'v6
因此,我正在构建的 C 程序必须放置在我的 MAC 计算机 (OS X 10.9.4) 桌面上的 PA_mobile 文件中,以便它可以访问脚本和文本文件缓存与之相关。 现在,当我的程序启动时,它会验
为了在 Windows 平台上运行 Flutter,我使用 go-flutter-desktop 来实现。 我需要在单击按钮时打开 Excel 文件。 经过长时间的努力,我想分享这个片段 最佳答案 i
背景 我正在使用 Citrix Workspace(版本 20.2.0.25(2002))中的 Desktop Viewer 功能从我的家用计算机访问我的工作计算机。 请求 我希望能够更轻松地将我的(
我通过 NetBeans8.0 使用 Oracle JDK 1.8.0_05 在 Ubuntu 12.04 LTS 64 位(带有 Gnome Shell)上运行一些 Java 代码。 以下函数在 M
/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/Scre
嗨,我有一个 Linux 实例,我正在使用桌面类。代码: String path = request.getParameter("path"); try {
当我选择在桌面应用程序中显示检查器时,出现以下错误。 正如他们的 Wiki 所说,我遵循了这个: http://wiki.appcelerator.org/display/guides/Enablin
我在 OSX、Objective-C 上。 我有一个像 这样的路径/NSURL /Users/xxx/Desktop/image2.png 但我将它传递给第三方应用程序,该应用程序会像 excpect
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 4年前关闭。 Improve this
我是一名优秀的程序员,十分优秀!