为什么调整根EBS卷大小后EC2实例不能正确启动?

9
我正在使用 https://matt.berther.io/2015/02/03/how-to-resize-aws-ec2-ebs-volumes/http://atodorov.org/blog/2014/02/07/aws-tip-shrinking-ebs-root-volume-size/ 上的指南进行操作,以将其移动到一个更小的EBS卷。在两种情况下,当我将缩小的EBS卷(作为/dev/xdva或/dev/sda1)附加到EC2实例并启动它时,它会自动停止,并显示以下消息:
State transition reason
Client.InstanceInitiatedShutdown: Instance initiated shutdown

一些调试后,我发现新卷没有BIOS引导分区。因此,我使用gdisk创建了一个并将MBR从原始卷(可以工作并启动实例)复制到新卷。现在,该实例不会终止,但我无法通过ssh连接到新启动的实例。
可能的原因是什么?我如何在日志/AWS控制台等方面获取更多信息以了解发生了什么?

你的机器使用什么操作系统?你在 AWS EC2 控制台中有 Syslog 信息吗? - Mircea
Amazon Linux和AWS EC2控制台为空。 - web_ninja
4个回答

6
要将GPT分区的引导EBS卷缩小到标准镜像似乎使用的8GB以下,您可以执行以下操作:(稍微变化了来自https://matt.berther.io/2015/02/03/how-to-resize-aws-ec2-ebs-volumes/dd方法)。
源磁盘为/dev/xvdf,目标为/dev/xvdg
  1. Shrink source partition

    $ sudo e2fsck -f /dev/xvdf1
    $ sudo resize2fs -M /dev/xvdf1
    

    Will print something like

    resize2fs 1.42.12 (29-Aug-2014)  
    Resizing the filesystem on /dev/xvdf1 to 257491 (4k) blocks.  
    The filesystem on /dev/xvdf1 is now 257491 (4k) blocks long.
    

    I converted this to MB, i.e. 257491 * 4 / 1024 ~= 1006 MB

  2. copy above size + a bit more from device to device (!), not just partition to partition, because that includes both partition table & data in the boot partition

    $ sudo dd if=/dev/xvdf of=/dev/xvdg bs=1M count=1100
    
  3. now use gdisk to fix the GPT partition on the new disk

    $ sudo gdisk /dev/xvdg
    

    You'll be greeted with roughly

    GPT fdisk (gdisk) version 0.8.10
    
    Warning! Disk size is smaller than the main header indicates! Loading
    secondary header from the last sector of the disk! You should use 'v' to
    verify disk integrity, and perhaps options on the experts' menu to repair
    the disk.
    Caution: invalid backup GPT header, but valid main header; regenerating
    backup header from main header.#
    
    Warning! One or more CRCs don't match. You should repair the disk!
    
    Partition table scan:
      MBR: protective
      BSD: not present
      APM: not present
      GPT: damaged
    
    ****************************************************************************
    Caution: Found protective or hybrid MBR and corrupt GPT. Using GPT, but disk
    verification and recovery are STRONGLY recommended.
    ****************************************************************************
    
    Command (? for help):
    

    The following is the keyboard input within gdisk. To fix the problems, the data partition that is present in the copied partition table needs to be resized to fit on the new disk. This means it needs to be recreated smaller and it's properties need to be set to match the old partition definition. Didn't test it so it's maybe not required to relocate the backup table to the actual end of the disk but I did it anyways:

    • go to extra expert options: x
    • relocate backup data structures to the end of the disk: e
    • back to main menu: m

    Now to fixing the partition size

    • print and note some properties of partition 1 (and other non-boot partitions if they exist):
      i
      1
      Will show something like

      Partition GUID code: 0FC63DAF-8483-4772-8E79-3D69D8477DE4 (Linux filesystem)
      Partition unique GUID: DBA66894-D218-4D7E-A33E-A9EC9BF045DB
      First sector: 4096 (at 2.0 MiB)
      Last sector: 16777182 (at 8.0 GiB)
      Partition size: 16773087 sectors (8.0 GiB)
      Attribute flags: 0000000000000000
      Partition name: 'Linux'
      
    • now delete
      d
      1
      and recreate the partition
      n
      1
      Enter the required parameters. All defaults worked for me here (= press enter), when in doubt refer to partition information from above

      • First sector = 4096
      • Last sector = whatever is the actual end of the new disk - take the default here
      • type = 8300 (Linux)
    • The new partition's default name did not match the old one. So change it to the original One
      c
      1
      Linux (see Partition name from above)

    • Next thing to change is the partition's GUID
      x
      c
      1
      DBA66894-D218-4D7E-A33E-A9EC9BF045DB (see Partition unique GUID, not the partition guid code above that)
    • That should be it. Back to main menu & print state
      m
      i
      1
      Will now print

      Partition GUID code: 0FC63DAF-8483-4772-8E79-3D69D8477DE4 (Linux filesystem)
      Partition unique GUID: DBA66894-D218-4D7E-A33E-A9EC9BF045DB
      First sector: 4096 (at 2.0 MiB)
      Last sector: 8388574 (at 4.0 GiB)
      Partition size: 8384479 sectors (4.0 GiB)
      Attribute flags: 0000000000000000
      Partition name: 'Linux'
      

      The only change should be the Partition size.

    • write to disk and exit
      w
      y
  4. grow filesystem to match entire (smaller) disk. The fist step shrunk it down to the minimal size it can fit

    $ sudo resize2fs -p /dev/xvdg1
    
  5. We're done. Detach volume & snapshot it.

  6. Optional step. Choosing proper Kernel ID for the AMI.
如果您正在处理PVM图像并在实例日志中遇到以下挂载错误,则当您的实例未通过启动检查时,您可能需要执行此附加步骤。
此错误的解决方法是在从快照创建图像时为您的PVM图像选择适当的内核ID。可以在此处获取内核ID(AKI)的完整列表。
请为您的图像选择适当的AKI,它们受地区和架构的限制!

删除/重新创建不会影响实际数据,只会影响分区表吗? - Suncatcher
@Suncatcher 是的,分区表并不知道文件系统中的数据,它只需要知道文件系统在字节边界上的位置,然后您可以重新创建该数据。 - zapl
在你的解决方案中添加了一个可选步骤,这是我成功调整大小序列中缺失的。谢谢,伙计! - Suncatcher
谢谢您的回复,我很惊讶它没有被标记为答案。 - Fotis
正是我所需要的,非常感谢@zapl和Suncatcher!但愿有办法让我请你们喝杯咖啡或者其他什么。 - pragman

2
问题出在BIOS引导分区上。我通过先初始化一个较小的EBS卷解决了这个问题。然后,将该卷从实例中分离并附加到另一个实例上,该实例将用于将内容从较大的卷复制到较小的卷中。这样就创建了一个实际工作的BIOS引导分区。简单地创建一个新的引导分区并复制引导分区是不起作用的。
现在,按照任何两个链接中概述的步骤之一,可以帮助缩小根EBS的容量。

2

今天,使用UBUNTU在这里没有任何其他解决方案。然而,我找到了一个:

  1. 为谨慎起见:快照大容量(备份)
  2. 创建尽可能相同的实例,例如使大容量运行良好。 但是使用更小的容量(期望的大小)
  3. 分离他的新卷并将大卷附加(作为/dev/sda1),然后启动实例
  4. 较小的新卷作为/dev/sdf附加
  5. 登录新实例。将较小的卷挂载在/mnt上: sudo mount -t ext4 /dev/xvdf1 /mnt
  6. 删除/mnt上的所有内容。忽略警告/错误,因为无法删除/mnt :) 使用sudo rm -rf /mnt
  7. 将整个/复制到较小的卷中:sudo cp -ax / /mnt
  8. 从实例退出并在AWS控制台中停止它
  9. 分离两个卷。现在,重新附加较小的卷,重要的是作为/dev/sda1
  10. 启动实例。登录实例并确认较小的卷是否正常
  11. 删除大卷,删除大快照,创建较小卷的新快照。 结束

哥们!太牛了!这是唯一一套在我的Ubuntu 20.04.1 EC2实例上真正有效的步骤。其他指南会导致实例无法启动。 - Josh Yolles
你的回答真是太棒了,值得一枚奖章。这绝对是缩小根EBS卷最简单的方法,我可以证实它是可行的。 - Mukarram Khalid
只有这种方法适用于ubuntu-focal-20.04-amd64-server AMI,其他方法会产生无法启动的卷。 - andlvovsky

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接