

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 設定 Amazon EC2 AMI 的開機模式
<a name="set-ami-boot-mode"></a>

AMI 預設會繼承用於建立 AMI 的 EC2 執行個體的啟動模式。例如，若透過舊式 BIOS 上執行的 EC2 執行個體來建立 AMI，新 AMI 的啟動模式為 `legacy-bios`。若透過使用啟動模式 `uefi-preferred` 的 EC2 執行個體來建立 AMI，新 AMI 的啟動模式為 `uefi-preferred`。

當您註冊 AMI 時，可將 AMI 的啟動模式設定為 `uefi`、`legacy-bios` 或 `uefi-preferred`。

當 AMI 開機模式設為 `uefi-preferred` 時，執行個體會以下列方式開機：
+ 對於同時支援 UEFI 和 Legacy BIOS (例如 `m5.large`) 的執行個體類型，執行個體會使用 UEFI 開機。
+ 對於僅支援 Legacy BIOS (例如 `m4.large`) 的執行個體類型，執行個體會使用 Legacy BIOS 開機。

如果您將 AMI 開機模式設定為 `uefi-preferred`，則作業系統必須支援 UEFI 和 Legacy BIOS 開機功能。

若要將現有的 Legacy BIOS 型執行個體轉換為 UEFI，或將現有的 UEFI 型執行個體轉換為 Legacy BIOS，您必須先修改執行個體的磁碟區和作業系統，以支援選取的開機模式。然後，建立磁碟區的快照。最後，透過快照建立 AMI

**考量事項**
+ 設定 AMI 開機模式參數並不會自動針對指定的開機模式設定作業系統。您必須先對執行個體的磁碟區和作業系統進行適當的修改，以支援使用選取的啟動模式來啟動。否則，產生的 AMI 將不可使用。例如，如果您要將 Legacy BIOS Windows 執行個體轉換為 UEFI，可以使用 Microsoft 的 [MBR2GPT](https://learn.microsoft.com/en-us/windows/deployment/mbr-to-gpt) 工具，將系統磁碟從 MBR 轉換為 GPT。所需的修改視作業系統而異。如需詳細資訊，請參閱作業系統的手冊。
+ 您不能使用 [https://docs.aws.amazon.com/cli/latest/reference/ec2/register-image.html](https://docs.aws.amazon.com/cli/latest/reference/ec2/register-image.html) 命令或 [https://docs.aws.amazon.com/powershell/latest/reference/items/Register-EC2Image.html](https://docs.aws.amazon.com/powershell/latest/reference/items/Register-EC2Image.html) cmdlet 來建立同時支援 [NitroTPM](nitrotpm.md) 和 UEFI Preferred 的 AMI。
+ 某些功能 (例如 UEFI 安全開機) 僅適用於透過 UEFI 開機的執行個體。當您將 `uefi-preferred` AMI 開機模式參數與不支援 UEFI 的執行個體類型搭配使用時，執行個體會以 Legacy BIOS 的形式啟動，而且會停用與 UEFI 相關的功能。如果您依賴 UEFI 相關功能的可用性，則請將 AMI 開機模式參數設定為 `uefi`。

------
#### [ AWS CLI ]

**如需設定 AMI 的啟動模式**

1. 對執行個體的磁碟區和作業系統進行適當的修改，以支援透過選取的開機模式開機。所需的修改視作業系統而異。如需詳細資訊，請參閱作業系統的手冊。
**警告**  
如果您不執行此步驟，則 AMI 將無法使用。

1. 若要尋找執行個體的磁碟區 ID，請使用 [https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html](https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html) 命令。您將在下一個步驟中建立此磁碟區的快照。

   ```
   aws ec2 describe-instances \
       --instance-ids i-1234567890abcdef0 \
       --query Reservations[].Instances[].BlockDeviceMappings
   ```

   以下為範例輸出。

   ```
   [
       [
           {
               "DeviceName": "/dev/xvda",
               "Ebs": {
                   "AttachTime": "2024-07-11T01:05:51+00:00",
                   "DeleteOnTermination": true,
                   "Status": "attached",
                   "VolumeId": "vol-1234567890abcdef0"
               }
           }
       ]
   ]
   ```

1. 若要建立磁碟區的快照，請使用 [https://docs.aws.amazon.com/cli/latest/reference/ec2/create-snapshot.html](https://docs.aws.amazon.com/cli/latest/reference/ec2/create-snapshot.html) 命令。使用上一個步驟的磁碟區 ID。

   ```
   aws ec2 create-snapshot \
       --volume-id vol-01234567890abcdef \
       --description "my snapshot"
   ```

   以下為範例輸出。

   ```
   {
       "Description": "my snapshot",
       "Encrypted": false,
       "OwnerId": "123456789012",
       "Progress": "",
       "SnapshotId": "snap-0abcdef1234567890",
       "StartTime": "",
       "State": "pending",
       "VolumeId": "vol-01234567890abcdef",
       "VolumeSize": 30,
       "Tags": []
   }
   ```

1. 等到快照的狀態為 `completed` 後，再進行下一個步驟。如需獲取快照狀態，請使用上一個步驟的快照 ID 並搭配 [https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-snapshots.html](https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-snapshots.html) 命令。

   ```
   aws ec2 describe-snapshots \
       --snapshot-ids snap-0abcdef1234567890 \
       --query Snapshots[].State \
       --output text
   ```

   以下為範例輸出。

   ```
   completed
   ```

1. 若要建立新 AMI，請使用 [https://docs.aws.amazon.com/cli/latest/reference/ec2/register-image.html](https://docs.aws.amazon.com/cli/latest/reference/ec2/register-image.html) 命令。使用輸出 **CreateSnapshot** 中的值 `SnapshotId`。
   + 如需將啟動模式設定為 UEFI，請新增包含值 `uefi` 的 `--boot-mode` 參數。

     ```
     aws ec2 register-image \
        --description "my image" \
        --name "my-image" \
        --block-device-mappings "DeviceName=/dev/sda1,Ebs={SnapshotId=snap-0abcdef1234567890,DeleteOnTermination=true}" \
        --root-device-name /dev/sda1 \
        --virtualization-type hvm \
        --ena-support \
        --boot-mode uefi
     ```
   + 如需將啟動模式設定為 `uefi-preferred`，請將 `--boot-mode` 的值設定為 `uefi-preferred`

     ```
     aws ec2 register-image \
        --description "my description" \
        --name "my-image" \
        --block-device-mappings "DeviceName=/dev/sda1,Ebs={SnapshotId=snap-0abcdef1234567890,DeleteOnTermination=true}" \
        --root-device-name /dev/sda1 \
        --virtualization-type hvm \
        --ena-support \
        --boot-mode uefi-preferred
     ```

1. (選用) 如需驗證新建立的 AMI 是否具有您指定的啟動模式，請使用 [https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-images.html](https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-images.html) 命令。

   ```
   aws ec2 describe-images \
       --image-id ami-1234567890abcdef0 \
       --query Images[].BootMode \
       --output text
   ```

   以下為範例輸出。

   ```
   uefi
   ```

------
#### [ PowerShell ]

**如需設定 AMI 的啟動模式**

1. 對執行個體的磁碟區和作業系統進行適當的修改，以支援透過選取的開機模式開機。所需的修改視作業系統而異。如需詳細資訊，請參閱作業系統的手冊。
**警告**  
如果您不執行此步驟，則 AMI 將無法使用。

1. 如需尋找執行個體的磁碟區 ID，請使用 [https://docs.aws.amazon.com/powershell/latest/reference/items/Get-EC2Instance.html](https://docs.aws.amazon.com/powershell/latest/reference/items/Get-EC2Instance.html) cmdlet。

   ```
   (Get-EC2Instance `
       -InstanceId i-1234567890abcdef0).Instances.BlockDeviceMappings.Ebs
   ```

   以下為範例輸出。

   ```
   AssociatedResource  : 
   AttachTime          : 7/11/2024 1:05:51 AM
   DeleteOnTermination : True
   Operator            : 
   Status              : attached
   VolumeId            : vol-01234567890abcdef
   ```

1. 如需建立磁碟區的快照，請使用 [https://docs.aws.amazon.com/powershell/latest/reference/items/New-EC2Snapshot.html](https://docs.aws.amazon.com/powershell/latest/reference/items/New-EC2Snapshot.html) cmdlet。使用上一個步驟的磁碟區 ID。

   ```
   New-EC2Snapshot `
       -VolumeId vol-01234567890abcdef `
       -Description "my snapshot"
   ```

   以下為範例輸出。

   ```
   AvailabilityZone          : 
   Description               : my snapshot
   Encrypted                 : False
   FullSnapshotSizeInBytes   : 0
   KmsKeyId                  : 
   OwnerId                   : 123456789012
   RestoreExpiryTime         : 
   SnapshotId                : snap-0abcdef1234567890
   SseType                   : 
   StartTime                 : 4/25/2025 6:08:59 PM
   State                     : pending
   StateMessage              : 
   VolumeId                  : vol-01234567890abcdef
   VolumeSize                : 30
   ```

1. 等到快照的狀態為 `completed` 後，再進行下一個步驟。如需獲取快照狀態，請使用上一個步驟的快照 ID 並搭配 [https://docs.aws.amazon.com/powershell/latest/reference/items/Get-EC2Snapshot.html](https://docs.aws.amazon.com/powershell/latest/reference/items/Get-EC2Snapshot.html) cmdlet。

   ```
   (Get-EC2Snapshot `
       -SnapshotId snap-0abcdef1234567890).State.Value
   ```

   以下為範例輸出。

   ```
   completed
   ```

1. 如需使用 [https://docs.aws.amazon.com/powershell/latest/reference/items/Register-EC2Image.html](https://docs.aws.amazon.com/powershell/latest/reference/items/Register-EC2Image.html) cmdlet 來建立新的 AMI。使用輸出 **New-EC2Snapshot** 中的值 `SnapshotId`。
   + 如需將啟動模式設定為 UEFI，請新增包含值 `uefi` 的 `-BootMode` 參數。

     ```
     $block = @{SnapshotId=snap-0abcdef1234567890}
     Register-EC2Image ` 
        -Description "my image" `
        -Name "my-image" `
        -BlockDeviceMapping @{DeviceName="/dev/xvda";Ebs=$block} `
        -RootDeviceName /dev/xvda `
        -EnaSupport $true `
        -BootMode uefi
     ```
   + 如需將啟動模式設定為 `uefi-preferred`，請將 `-BootMode` 的值設定為 `uefi-preferred`

     ```
     $block = @{SnapshotId=snap-0abcdef1234567890}
     Register-EC2Image ` 
        -Description "my image" `
        -Name "my-image" `
        -BlockDeviceMapping @{DeviceName="/dev/xvda";Ebs=$block} `
        -RootDeviceName /dev/xvda `
        -EnaSupport $true `
        -BootMode uefi-preferred
     ```

1. (選用) 如需驗證新建立的 AMI 是否具有您指定的啟動模式，請使用 [https://docs.aws.amazon.com/powershell/latest/reference/items/Get-EC2Image.html](https://docs.aws.amazon.com/powershell/latest/reference/items/Get-EC2Image.html) cmdlet。

   ```
   (Get-EC2Image `
       -ImageId ami-1234567890abcdef0).BootMode.Value
   ```

   以下為範例輸出。

   ```
   uefi
   ```

------