我正在使用terraform和Chef创建多个AWS ebs卷并将它们附加到EC2实例。
问题是我希望能够给每个ebs卷一个特定的Windows驱动器号。问题是当实例化EC2实例时,窗口只给它顺序的驱动器号(D,E,F等)
某些驱动器的大小相同,因此我不必根据驱动器的大小进行重命名。有谁知道用Terraform或Chef做这件事的方法。我的Google Foo找不到任何东西。
当然这必须要针对其他人吗?
我确实看到了使用EC2Config Windows GUI进行设置的参考,但是重点是要使过程自动化,因为最终我希望厨师安装SQL Server,并且某些驱动器号上应该包含某些数据。
这似乎可行-尽管我确实想知道是否没有更简单的方法。
function Convert-SCSITargetIdToDeviceName
{
param([int]$SCSITargetId)
If ($SCSITargetId -eq 0) {
return "/dev/sda1"
}
$deviceName = "xvd"
If ($SCSITargetId -gt 25) {
$deviceName += [char](0x60 + [int]($SCSITargetId / 26))
}
$deviceName += [char](0x61 + $SCSITargetId % 26)
return $deviceName
}
Get-WmiObject -Class Win32_DiskDrive | ForEach-Object {
$DiskDrive = $_
$Volumes = Get-WmiObject -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='$($DiskDrive.DeviceID)'} WHERE AssocClass=Win32_DiskDriveToDiskPartition" | ForEach-Object {
$DiskPartition = $_
Get-WmiObject -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='$($DiskPartition.DeviceID)'} WHERE AssocClass=Win32_LogicalDiskToPartition"
}
If ($DiskDrive.PNPDeviceID -like "*PROD_PVDISK*") {
$BlockDeviceName = Convert-SCSITargetIdToDeviceName($DiskDrive.SCSITargetId)
If ($BlockDeviceName -eq "xvdf") { $drive = gwmi win32_volume -Filter "DriveLetter = '$($Volumes.DeviceID)'"; Set-WmiInstance -input $drive -Arguments @{DriveLetter="D:"; Label="SQL Data"} };
If ($BlockDeviceName -eq "xvdg") { $drive = gwmi win32_volume -Filter "DriveLetter = '$($Volumes.DeviceID)'"; Set-WmiInstance -input $drive -Arguments @{DriveLetter="L:"; Label="SQL Logs"} };
If ($BlockDeviceName -eq "xvdh") { $drive = gwmi win32_volume -Filter "DriveLetter = '$($Volumes.DeviceID)'"; Set-WmiInstance -input $drive -Arguments @{DriveLetter="R:"; Label="Report Data"} };
If ($BlockDeviceName -eq "xvdi") { $drive = gwmi win32_volume -Filter "DriveLetter = '$($Volumes.DeviceID)'"; Set-WmiInstance -input $drive -Arguments @{DriveLetter="T:"; Label="Temp DB"} };
If ($BlockDeviceName -eq "xvdj") { $drive = gwmi win32_volume -Filter "DriveLetter = '$($Volumes.DeviceID)'"; Set-WmiInstance -input $drive -Arguments @{DriveLetter="M:"; Label="MSDTC"} };
If ($BlockDeviceName -eq "xvdk") { $drive = gwmi win32_volume -Filter "DriveLetter = '$($Volumes.DeviceID)'"; Set-WmiInstance -input $drive -Arguments @{DriveLetter="B:"; Label="Backups"} };
} ElseIf ($DiskDrive.PNPDeviceID -like "*PROD_AMAZON_EC2_NVME*") {
$BlockDeviceName = Get-EC2InstanceMetadata "meta-data/block-device-mapping/ephemeral$($DiskDrive.SCSIPort - 2)"
If ($BlockDeviceName -eq "xvdf") { $drive = gwmi win32_volume -Filter "DriveLetter = '$($Volumes.DeviceID)'"; Set-WmiInstance -input $drive -Arguments @{DriveLetter="D:"; Label="SQL Data"} };
If ($BlockDeviceName -eq "xvdg") { $drive = gwmi win32_volume -Filter "DriveLetter = '$($Volumes.DeviceID)'"; Set-WmiInstance -input $drive -Arguments @{DriveLetter="L:"; Label="SQL Logs"} };
If ($BlockDeviceName -eq "xvdh") { $drive = gwmi win32_volume -Filter "DriveLetter = '$($Volumes.DeviceID)'"; Set-WmiInstance -input $drive -Arguments @{DriveLetter="R:"; Label="Report Data"} };
If ($BlockDeviceName -eq "xvdi") { $drive = gwmi win32_volume -Filter "DriveLetter = '$($Volumes.DeviceID)'"; Set-WmiInstance -input $drive -Arguments @{DriveLetter="T:"; Label="Temp DB"} };
If ($BlockDeviceName -eq "xvdj") { $drive = gwmi win32_volume -Filter "DriveLetter = '$($Volumes.DeviceID)'"; Set-WmiInstance -input $drive -Arguments @{DriveLetter="M:"; Label="MSDTC"} };
If ($BlockDeviceName -eq "xvdk") { $drive = gwmi win32_volume -Filter "DriveLetter = '$($Volumes.DeviceID)'"; Set-WmiInstance -input $drive -Arguments @{DriveLetter="B:"; Label="Backups"} };
} Else {
write-host "Couldn't find disks";
}
}
如果考虑到此链接中的表,请执行以下操作:https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-windows-volumes.html
您可以看到,在EBS上,第一行是:
Bus Number 0, Target ID 0, LUN 0 /dev/sda1
Bus Number 0, Target ID 1, LUN 0 xvdb
[磁盘0(/ dev / sda1)始终由EC2设置为C:
所以您知道当您运行“ New-Partition -DiskNumber 1 -UseMaximumSize -IsActive -AssignDriveLetter”时,将得到D:。
因此,如果您使用Packer在Builders中使用以下卷来配置AMI映像(此示例中仅两个,但可以执行很多操作:]
"launch_block_device_mappings": [{
"device_name": "/dev/sda1",
"volume_size": 30,
"volume_type": "gp2",
"delete_on_termination": true
},
{
"device_name": "xvdb",
"volume_size": 30,
"volume_type": "gp2",
"delete_on_termination": true
}]
..您可以计划,知道xvd [b]实际上是要映射的内容后面两个字母。
然后使用Terraform启动此多卷AMI的EC2实例,并将其放在aws_instance资源的user_data部分中:
user_data = <<EOF
<powershell>
Initialize-Disk -Number 1 -PartitionStyle "MBR"
New-Partition -DiskNumber 1 -UseMaximumSize -IsActive -AssignDriveLetter
Format-Volume -DriveLetter d -Confirm:$FALSE
Set-Partition -DriveLetter D -NewDriveLetter S
</powershell>
EOF
Set-Partition -DriveLetter D -NewDriveLetter S
行是用来将已知的顺序驱动器重命名为您习惯使用的任何字母的工具。就我而言,他们想要D:作为S:-只需重复此行即可将E:重命名为X:或您需要的其他名称。
希望这会有所帮助。
UPDATE:还有另一种方法(Server 2016 up),当我发现Sysprep破坏了所有映射到AMI映像中的映射时发现的。
您必须在C:\ ProgramData \ Amazon \ EC2-Windows \ Launch \ Config中提供一个DriveLetterMappingConfig.json文件来进行映射。该文件的格式为:
{
"driveLetterMapping": [
{
"volumeName": "sample volume",
"driveLetter": "H"
}
]
}
...仅,默认情况下,我的驱动器没有volumeName;他们是空白。让我们回到1980年代的旧式“ LABEL”命令。将D:驱动器标记为volume2。因此文件看起来像:
{
"driveLetterMapping": [
{
"volumeName": "volume2",
"driveLetter": "S"
}
]
}
正在运行C:\ ProgramData \ Amazon \ EC2-Windows \ Launch \ Scripts \ InitializeDisks.ps1进行了测试(D:成为S:)
<< :在实例上以S:形式返回。 (我将文件与我们将要安装在盒子上的所有其他废话放到了S3存储桶中。)我将磁盘中的内容放入.ps1,并从预配器中调用它:
{“ type”:“ powershell”,“ script”:“ ./setup_two_drive_names_c_and_s.ps1”},
上面的.ps1是:
# Do volume config of the two drives
write-host "Setting up drives..."
Initialize-Disk -Number 1 -PartitionStyle "MBR"
New-Partition -DiskNumber 1 -UseMaximumSize -IsActive -AssignDriveLetter
Format-Volume -DriveLetter d -Confirm:$FALSE
label c: "volume1"
label d: "volume2"
Set-Partition -DriveLetter D -NewDriveLetter S
# Now insert DriveLetterMappingConfig.json file into C:\ProgramData\Amazon\EC2-Windows\Launch\Config to ensure instance starts with correct drive mappings
Write-Host "S3 Download: DriveLetterMappingConfig.json"
Read-S3Object -BucketName ********* -Key DriveLetterMappingConfig.json -File 'c:\temp\DriveLetterMappingConfig.json'
Write-Host "Copying DriveLetterMappingConfig.json to C:\ProgramData\Amazon\EC2-Windows\Launch\Config..."
Copy-Item "c:\temp\DriveLetterMappingConfig.json" -Destination "C:\ProgramData\Amazon\EC2-Windows\Launch\Config\DriveLetterMappingConfig.json" -Force
Write-Host "Set Initialze Disks to run on every boot..."
C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeDisks.ps1 -Schedule
是的,没有理由给c贴上标签:但是我卷起来了...带有“ -Schedule”参数的最后一行表示此问题在每次引导时发生。