收藏文章 楼主

windows 云盘在线扩容不成功: windows实例更新RedHat VirtIO驱动

版块:windows   类型:置顶   作者:小绿叶技术博客   查看:2090   回复:0   获赞:0   时间:2021-09-18 18:02:51


win+R  输入 cmd 进入命令界面:

powershell                                       # 命令进入脚本模式

[System.Diagnostics.FileVersionInfo]::GetVersionInfo("C:\Windows\System32\drivers\viostor.sys")

                                                        # 执行命令查看目前版本:尾号编码是版本,小于58011 需要升级

Set-ExecutionPolicy Unrestricted     # 授予shell 执行权限

    

 https://windows-driver-cn-beijing.oss-cn-beijing.aliyuncs.com/virtio/InstallVirtIo.ps1?spm=a2c4g.11186623.0.0.49da1527qE1DV9&file=InstallVirtIo.ps1

                                                        # 浏览器下载启动脚本

                                                        # 右键使用 powershell  打开

                                                        # 重启系统


控制台操作重启是,重启过程中会有阿里云api接口进行增大磁盘空间,服务器内操作重启无效


阿里官方教程:   https://help.aliyun.com/document_detail/113322.html

扩容只能对末尾分区进行扩容,如果创建了快照,建议使用分区工具进行扩容:https://www.diskgenius.cn/


Get-Disk | Select-Object uniqueid




#------------------------------ 脚本内容  ----------------------------------#

# Check the OS version

$OSversion = [Environment]::OSVersion.Version

if ($OSversion.Major -le 6 -and $OSversion.Minor -lt 1) {

    Write-Host "This scrip is not supported on Windows 2008 or lower"

    exit

} elseif ($OSversion.Major -eq 6 -and $OSversion.Minor -eq 1) {

    $os_ver = "win7"

} elseif ($OSversion.Major -eq 6 -and $OSversion.Minor -eq 2) {

    $os_ver = "win8"

} elseif ($OSversion.Major -eq 6 -and $OSversion.Minor -eq 3) {

    $os_ver = "win8"

} elseif ($OSversion.Major -ge 10) {

    $os_ver = "win10"

}


$cur_dir = $PSScriptRoot

$log_dir = [io.path]::combine($cur_dir, "log")

$CertPath = [io.path]::Combine($cur_dir, "aliyun.cer")


if (-not (Test-Path $log_dir)){

    New-Item -ItemType Directory -Path $log_dir -Force

}


$log_path = [io.path]::combine($log_dir, "test$(Get-date -format "yyyy-MM-dd").log")


function Write-Log ($content, $log_level = 0) {

    $cur_time = Get-Date -Format "yyy-MM-dd hh:mm:ss"

    Switch($log_level) {

        {($_ -eq 1)} { $errFlag = "warning"; $color = [ConsoleColor]::Yellow }

        {($_ -eq 2)}{ $errFlag = "error"; $color = [ConsoleColor]::Red }

        default { $errFlag = "info"; $color = [ConsoleColor]::Green }

    }


    $format_content = "[$cur_time] [${errFlag}] ${content}"

    Add-Content -Value $format_content -Path $log_path

}


function Check-Result($content, $ret_flag = $true) {

    if ($ret_flag) {

        Write-Log "${content} success."

    }else {

        throw "$content failed."

    }

}


function Check-LastExitCode($content, $exit_code = 0) {

     Switch($exit_code) {

        {($_ -eq 0)} { Write-Log "${content} success." }

        default {throw "$content failed." }

    }

}


function Download_file($url, $file_path) {

    if (Test-Path $file_path) {

        Write-Log "$file_path alreasy exist"

        return $true

    }

    try {

        Invoke-WebRequest -Uri $url -OutFile $file_path -UseBasicParsing -TimeoutSec 30

        return $?

    } catch {

        Write-Log "Download_file "+ $url +" failed"

    }

    return $false 

}


function Unzip-File($ZipFile, $TargetFolder) {

<#

    if (Test-Path $file_path) {

        Write-Log "$file_path already exist"

        return

    }

    Expand-Archive -Path $zip_path -DestinationPath $file_path

    Check-Result "unzip $zip_path" $?

#>

    if (!(Test-Path $ZipFile)) {

        throw "not exist $ZipFile"

    }

    if(!(Test-Path $TargetFolder)) { 

        New-Item -ItemType Directory $TargetFolder -Force

    }

    if ($os_ver -ne "win10") {

        $shellApp = New-Object -ComObject Shell.Application

        $files = $shellApp.NameSpace($ZipFile).Items()

        # 删除已存在的解压后的文件 

        $files|where{Remove-Item ("$TargetFolder/{0}*" -f  $_.name ) -Force -Recurse -ErrorAction:SilentlyContinue}

        $shellApp.NameSpace($TargetFolder).CopyHere($files)

    } else {

        Expand-Archive -Path $ZipFile -DestinationPath $TargetFolder -Force 

        Check-Result "Unzip $ZipFile" $?

    }

}


function Fix_Driver() {

    $fix_dir = $(pwd)

    Write-Log "fix dir is $fix_dir"

    try {

        & cmd /c pnputil -i -a *.inf

        Check-LastExitCode "fix virtio" $LASTEXITCODE

    } catch {

        Write-Log "system already installed the driver"

    }

}


function GetRegionId() {

    $resionId = ""

    try{

        $region=Invoke-WebRequest -Uri  "http://100.100.100.200/latest/meta-data/region-id" -UseBasicParsing -ErrorAction SilentlyContinue

        $resionId = $region.Content

    }

    catch{

    }

    return  $resionId

}



function GetDownloadUrlList ($file_name, $is_virtio){

    $url_list = New-Object -TypeName System.Collections.ArrayList

    $regionId = GetRegionId 

    $driver_type = "nvme"

    if ($is_virtio) {

        $driver_type = "virtio"

    }

    if ($regionId -ne "") {

        $null = $url_list.Add("http://windows-driver-"+$regionId+".oss-"+$regionId+"-internal.aliyuncs.com/"+$driver_type+"/"+$file_name)

    }

    $null = $url_list.Add("http://windows-driver-cn-beijing.oss-cn-beijing.aliyuncs.com/"+$driver_type+"/"+$file_name)

    $null = $url_list.Add("http://windows-driver-us-west-1.oss-us-west-1.aliyuncs.com/"+$driver_type+"/"+$file_name)

    return $url_list

}


function TryDownloadDriver($file_name, $is_virtio) {

    $driver_url_list = GetDownloadUrlList $file_name $is_virtio

    foreach ($url in $driver_url_list) {

        $local_file = [io.path]::Combine($cur_dir,$file_name)

        $ret = Download_file $url $local_file

        if ($ret) {

            return $true

        }

    }

    return $false

}


function Update-Cert() {

    $old_cert = & certutil.exe -store TrustedPublisher

    if ($old_cert.Count -eq 2) {

       Write-Log "aliyun certificate does not exist in the system"

       $old_id = $null

    } else {

        $old_id = $old_cert[2].split(' ')[-1]    

    }

    $new_id = ((& certutil.exe $CertPath)[2]).split(' ')[-1]

    if ($old_id -ne $new_id) {

        Write-Log "Now update system aliyun cert"

        & certutil.exe -addstore TrustedPublisher $CertPath

        if ($LASTEXITCODE -ne 0) { throw "update system aliyun cert error, please check" }

    }

}


function InstallDriver ($file_name, $is_virtio){

    $ret = TryDownloadDriver $file_name  $is_virtio

    Check-Result "TryDownloadDriver "+$zip_path $ret


    $ret = TryDownloadDriver 'aliyun.cer'  $is_virtio

    Check-Result "TryDownloadCert " $ret


    Update-Cert


    $virtio_file = [io.path]::Combine($cur_dir,$file_name)

    $driver_com_dir =  [io.path]::Combine($cur_dir,$file_name.Substring(0,$file_name.Length-4))

    Unzip-File $virtio_file $driver_com_dir

    $arch = "amd64"

    if (([Array](Get-WmiObject -Query "select AddressWidth from Win32_Processor"))[0].AddressWidth -ne 64) {

        $arch = "x86"

    }

    $driver_bin_dir = [io.path]::Combine($driver_com_dir, $os_ver, $arch)

    Push-Location

    Set-Location $driver_bin_dir

    Fix_Driver

    Pop-Location

}


InstallDriver "210408.1454.1459_bin.zip" $true

#--------------------------------------------------------------------------#








提供企业建站服务,免费网防系统,提交信息登录 http://yundun.ddoss.cn 邮箱: proposal@ddoss.cn 
回复列表
默认   热门   正序   倒序

回复:windows 云盘在线扩容不成功: windows实例更新RedHat VirtIO驱动

头像

用户名:

粉丝数:

签名:

资料 关注 好友 消息