缩小 Amazon EBS 卷大小

缩小 Amazon EBS 卷大小

我见过这是针对不断增长的 EBS 卷的答案,但我想缩小一个。

默认的 Ubuntu Server 镜像为 15 GB,而我实际上最多只需要 2 GB(我使用不同的卷来存储数据)。有没有办法缩小卷的大小?

答案1

我和你有同样的疑问,所以我想出了如何解决。

首先,我是从美国东部地区的 Ubuntu 32 位 EBS 支持的 ami 执行此操作的,其他操作系统或图像可能工作方式不同。但是,我认为只要您使用的是 ext* 文件系统,就应该没问题。它可能适用于其他文件系统,但您必须自己弄清楚如何调整它们的大小。

步骤基本上是:

  1. 将两个卷附加到正在运行的实例,第一个卷基于您想要缩小的快照,第二个卷是具有您想要缩小到的新大小的空白卷。

  2. 检查第一卷的文件系统并修复任何错误。

  3. 缩小第一个卷上的文件系统,以便它的大小恰好满足保存数据所需的大小。

  4. 将文件系统从第一个卷复制到第二个卷。

  5. 将第二个卷上的文件系统扩展至其最大大小。

  6. 检查第二卷是否有错误,确保一切正常。

  7. 拍摄第二个卷的快照。

  8. 根据您刚刚拍摄的第二个卷的快照创建机器映像。

首先,您需要从要缩小的 AMI 获取一些信息。特别是,您需要内核 ID 和 ramdisk ID(如果有的话)(我缩小的映像没有 ramdisk)。所有这些信息都应该可以从 AMI 窗口中的 aws 管理控制台获得。

内核 ID 看起来像 kia-xxxxxxxx,快照 ID 看起来像 snap-xxxxxxxx,ramdisk ID 看起来像 RIA-xxxxxxxx。

接下来,启动一个 Linux 实例。我启动了一个 Ubuntu 实例。如果您愿意,可以使用 t1.micro 实例。执行这些后续步骤不需要太多电力。

机器运行后,附加您在第一步中记下的快照。 在我的例子中,我将其附加到 /dev/sdf

然后,创建一个新卷,大小​​为您想要的大小。就我而言,我创建了一个 5GB 的卷,因为这就是我想要缩小到的大小。不要从快照创建这个新卷。我们需要一个新的空白卷。接下来,将其附加到正在运行的实例,就我而言,我将其附加为 /dev/sdg

接下来,通过 ssh 进入机器,但不要挂载附加的卷。

此时,我有点过于谨慎,我选择检查大卷上的文件系统,以确保没有错误。如果您确信没有错误,则可以跳过此步骤:

$ sudo e2fsck -f /dev/sdf

接下来,我调整了大卷上的文件系统的大小,使其与磁盘上的数据一样大:

$ sudo resize2fs -M -p /dev/sdf

-M 将其缩小,-p 打印进度。

resize2fs 应该会告诉你缩小的文件系统有多大。在我的例子中,它给出了 4K 块的大小。

现在,我们将缩小的文件系统复制到新磁盘。我们将以 16MB 的块为单位复制数据,因此我们需要确定需要复制多少个 16MB 的块。这时缩小的文件系统大小就派上用场了。

就我而言,缩小后的文件系统刚好超过 1 GB,因为在拍摄快照之前,我在基本 Ubuntu 系统上安装了很多其他程序。我可能只需复制四舍五入到最接近的 16MB 的文件系统大小即可,但我想稳妥行事。

因此,128 乘以 16MB 块 = 2GB:

$ sudo dd if=/dev/sdf ibs=16M of=/dev/sdg obs=16M count=128

我以 16MB 为单位进行复制,因为使用 EBS,您需要为每次读取和写入付费,所以我想尽可能减少它们的数量。我不知道这样做是否能达到目的,但可能没什么坏处。

然后,我们需要调整刚刚复制到新卷的文件系统的大小,以便它使用卷上的所有可用空间。

$ sudo resize2fs -p /dev/sdg

最后,检查一下,确保一切正常:

$ sudo e2fsck -f /dev/sdg

这就是我们需要在这台机器上做的所有事情,尽管挂载新卷也无妨,只是作为测试。然而,这一步几乎肯定是可选的,因为 e2fsck 应该已经发现任何问题。

现在我们需要对新卷进行快照,并基于此创建 AMI。我们已经完成了机器的操作,因此您可以根据需要终止它。

如果您已安装小卷,请确保它已卸载,然后对其拍摄快照。同样,您可以在管理控制台中执行此操作。

最后一步需要命令行 ec2 工具。

编辑:

自从这个答案发布以来,AWS 控制台允许您简单地右键单击快照并选择从快照创建映像。您仍然需要选择适当的内核 ID。如果它没有出现在列表中,请确保您选择了适当的体系结构。

我们使用 ec2-register 应用程序根据您刚刚拍摄的快照注册一个 AMI,因此请记下您刚刚拍摄的快照中的 snap-xxxxxxxx 值。

然后您应该使用如下命令:

ec2-register -C cert.pem -K sk.pem -n The_Name_of_Your_New_Image
-d Your_Description_of_This_New_AMI --kernel aki-xxxxxxxx
-b "/dev/sda1=snap-xxxxxxxx" --root-device-name /dev/sda1

当然,您需要将内核 ID 替换为您在开始时记下的 ID,将快照 ID 替换为您在上一步中创建的 ID。您还需要将其指向上面的密钥(称为 sk.pem)和 x509 证书(称为 cert.pem)。当然,您可以随意选择名称和描述。

希望这可以帮助。

答案2

是的,我也想知道这一点。以下教程有点过头了,但我认为它包含了必要的工具: http://www.linuxconfig.org/Howto_CREATE_BUNDLE_UPLOAD_and_ACCESS_custom_Debian_AMI_using_ubuntu

无需像上面那样安装到新磁盘映像,而是可以启动大型 AMI,创建新的 EBS,将 EBS 附加到正在运行的实例,然后将正在运行的 AMI 复制到新的 EBS。最后,将新的 EBS 注册为 AMI。

请参阅此博客文章以了解更多背景信息,尤其是 freremark 的评论: http://alestic.com/2010/01/public-ebs-boot-amis-for-ubuntu-on-amazon-ec2

最后要说的是,euca2ools 似乎是 ec2-ami-tools 的绝佳替代品 - euca2ools 包含实际手册页!它们的名称与 ec2-* 命令相同,只是带有 euca- 前缀。 http://open.eucalyptus.com/wiki/Euca2oolsUsing

答案3

我想减少一般 EC2 实例使用的卷的大小。我按照与此处其他答案类似的步骤操作,但遇到了问题。因此,以下是缩小根卷所需的操作...

在 AWS 控制台中

 1. Stop the source EC2 instance
 2. Create a snapshot of the volume you want to shrink
 3. Use the snapshot to create a new 'source' volume
 4. Created a new volume with smaller size (made sure it was big enough for the data on source)
 5. Attached both volumes to any EC2 instance (mine were /dev/sdf = source & /dev/sdg = target)
 6. Start the EC2 instance

在 EC2 实例上

 7. sudo su -   (everything from here is run as root)
 8. mkdir /source /target
 9. mount -t ext4 /dev/sdf /source
 10. mkfs.ext4 /dev/sdg
 11. mount -t ext4 /dev/sdg /target
 12. rsync -aHAXxSP /source/ /target 
     ** notice that there is no trailing '/' after target if 
       you put one there your data will be copied to 
       /target/source and you will have to move it up a directory
 13. cat /boot/grub/grub.conf  (indicated that grub is using root=LABEL=/)
 14. cat /source/etc/fstab (indicated that fstab was also using LABEL=/)
 15. e2label /dev/sdg /
 16. umount /source
 17. umount /target

返回 AWS 控制台

 18. Stop the instance
 19. Detach ALL volumes from the instance
 20. Attach the 'target' volume to the instance using /dev/sda1 as the device
 21. Start the instance

这是我们遇到的问题,据我所知还没有被提及。 实例启动正常,太棒了!但是当我尝试 ssh 到实例时,我无法连接。在多次重复上述步骤后,我最终决定尝试使用新启动的 EC2 实例中的根卷。

在 AWS 控制台中

 1. Create a new EC2 instance with the right sized root volume
 2. Stop the new instance
 3. Detach the /dev/sda1 volume from the new instance
    ** used the 'source' volume from before & the new volume we just detached
 4. Attached both volumes to the original EC2 instance (/dev/sdf & /dev/sdg)
 5. Start the instance with the attached volumes

在 EC2 实例上

 1. sudo su - 
 2. mkdir /source /target (only need to do this if you don't already have these directories)
 3. mount -t ext4 /dev/sdf /source
 4. mount -t ext4 /dev/sdg /target (no need to create a file system because it is already there)
 5. rsync -aHAXxSP /source/ /target 
 6. umount /source
 7. umount /target

返回 AWS 控制台

 1. Stop the instance
 2. Detach the 'source' and 'target' volumes from instance
 3. Attach the 'target' volume to the instance from step 1 using /dev/sda1 as the device
 4. Start the instance
 5. ** we use an elastic IP so we just reassigned the IP to the new instance

希望这对某人有帮助

相关内容