HyperV migration kvm

De BlaxWiki
Aller à la navigationAller à la recherche
# Import xml de libvirt
# convert raw en VHD
# convert vhd en vhdx
# Ajout VM dans cluster


$ErrorActionPreference = "Stop"

# Demandes des informations
Write-Output "Choisir le chemin de la VM a importer :"
$InputVmSrcPath=ls Z: | Select FullName | Out-GridView -OutputMode Single -Title "Choisir le chemin de la VM a importer :"
Write-Output $InputVmSrcPath.Fullname

if (-not (Test-Path -Path "$($InputVmSrcPath.FullName)\*.xml")) {
   Write-Error -Message "Erreur : XML de la VM non trouvé"
   exit 
}

Write-Output "Choisir le chemin d'export :"
$InputVmDestPath=ls C:\ClusterStorage | Select FullName | Out-GridView -OutputMode Single -Title "Choisir où exporter la VM :"
Write-Output $InputVmDestPath.Fullname

### Lecture XML
$ConfigPath="$($InputVmSrcPath.FullName)\*.xml" | Resolve-Path
[xml]$XmlDocument = Get-Content -Path $ConfigPath
$VMName=$XmlDocument.domain.name
$VMCpu=$XmlDocument.domain.vcpu.'#text'
$VMMemory="$($XmlDocument.domain.memory.'#text')$($($XmlDocument.domain.memory.unit).Replace('iB','B'))"/1
if (($VMMemory/2MB) % 2 -ne 0) { ## The VM memory size need to be a multiple of 2MB
    $VMMemory=([Math]::Ceiling($VMMemory/2MB)*2MB)
}

$VMDisks=$XmlDocument.domain.devices.disk
$VMInterfaces=$XmlDocument.domain.devices.interface
$VMPath="$($InputVmDestPath.FullName)\$VMName"

### Check presence fichier disks
Foreach ($VMDisk in $VMDisks) {
    if ($VMDisk.device -eq 'disk') {
        if ($VMDisk.source.dev -match '/dev/(?<volume>[^/]+)/(?<name>.+)') {
            $name=$Matches.name
            if (Test-Path -PathType Leaf -Path "$($InputVmSrcPath.FullName)\$name.raw") {
                Write-Output "$name.raw trouvé"
            }
            else {
                Write-Error "Fichier $name.raw non trouvé dans $($InputVmSrcPath.FullName) !"
                Exit
            }
        }
    }
}

### Création dossiers
Write-Output "Création des dossiers dans le ClusterStorage"
New-Item -ItemType "Directory" -path $InputVmDestPath.FullName -Name $VMName -ErrorAction Ignore
New-Item  -ItemType "Directory" -path $VMPath -Name "Snapshots" -ErrorAction Ignore
New-Item  -ItemType "Directory" -path $VMPath -Name "Virtual Hard Disks" -ErrorAction Ignore
New-Item  -ItemType "Directory" -path $VMPath -Name "Virtual Machines" -ErrorAction Ignore
Write-Output "Fin de la création des dossiers"

### Creation VM
$VM=New-VM -Name $VMName -Generation 1 -MemoryStartupBytes $VMMemory -Path $InputVmDestPath.FullName -BootDevice IDE -Verbose
$VM | Set-VMProcessor -Count $VMCpu -verbose
$VM | Remove-VMNetworkAdapter
$VM | Get-VMDvdDrive | Remove-VMDvdDrive

### Ajout interfaces
Foreach ($VMInterface in $VMInterfaces) {
    if ($VMInterface.source.bridge -match '(?<sw>[a-zA-Z]+)(?<vlan>\d+)') {
        if ($VMInterface.mac.address) {
            Write-Output "Ajout interface $($VMInterface.mac.address) VLAN $($Matches.vlan)"
            $adp=$VM | Add-VMNetworkAdapter -StaticMacAddress $VMInterface.mac.address -SwitchName 'vswitch_csn' -Name "Interface VLAN $($Matches.vlan)" -Passthru -Verbose
        }
        else {
            Write-Output "Ajout interface VLAN $($Matches.vlan)"
            $adp=$VM | Add-VMNetworkAdapter -DynamicMacAddress $true -SwitchName 'vswitch_csn' -Name "Interface VLAN $($Matches.vlan)" -Passthru -Verbose
        }
        $adp | Set-VMNetworkAdapterVlan -Access -VlanId $Matches.vlan -Verbose
    }
    else {
        Write-Error "Erreur ajout interface $($VMInterface.mac.address)"
    }
    
}

### Ajout disks
Foreach ($VMDisk in $VMDisks) {
    if ($VMDisk.device -eq 'disk') {
        if ($VMDisk.source.dev -match '/dev/(?<volume>[^/]+)/(?<name>.+)') {
            $name=$Matches.name
            Write-Output "$name.raw : Conversion du raw en VHD avec qemu-convert"   
            & qemu-img.exe convert "$($InputVmSrcPath.FullName)\$name.raw" -O vpc -o subformat=fixed "$($InputVmSrcPath.FullName)\$name.vhd"
            
            Write-Output "$name.vhd : Suppression du sparse flag"
            & fsutil sparse setflag "$($InputVmSrcPath.FullName)\$name.vhd" 0
            
            Write-Output "$name.vhd : Conversion du VHD en VHDX + mise dans ClusterVolume"
            $VHDXPath="$VMPath\Virtual Hard Disks\$name.vhdx" 
            Convert-VHD -Path "$($InputVmSrcPath.FullName)\$name.vhd" -DestinationPath $vhdxPath -VHDType "Fixed"
            
            Write-Output "Ajout du disque $name dans $VHDXPath"
            $VM | Add-VMHardDiskDrive -Path $VHDXPath 
        }
        else {
            Write-Warning "Impossible de matcher automatiquement $($VMDisk.source.dev). Faire a la main"
        }
    }
    else {
        Write-Output "Ignore ajout type non-disk"
   }
}


### Ajout Cluster
Write-Output "Ajout de la VM au cluster"
$VM  | Add-ClusterVirtualMachineRole
Write-Output "VM ajoutée"

Write-Output "Fin des opérations du script"