缩小Amazon EBS卷大小

我已经看到了增长EBS卷的这个答案 ,但我想缩小一个。

默认的Ubuntu服务器映像是15 GB,而我真的只需要2 GB(我使用不同的数据量)。 有没有办法缩小音量的大小?

    我和你有同样的问题,所以我研究了如何去做。

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

    步骤基本上是:

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

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

    3. 收缩第一卷上的文件系统,使其只能保存数据。

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

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

    6. 通过检查第二卷的错误确保一切看起来不错。

    7. 拍第二卷的快照。

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

    你首先需要从你想缩小的ami中获得一些信息。 特别是,你需要内核ID和虚拟磁盘ID(如果有的话)(缩小的图像没有虚拟磁盘)。 所有这些信息都应该在AMI窗口中的awspipe理控制台上提供。

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

    接下来,启动一个linux实例。 我启动了一个Ubuntu实例。 如果你喜欢,你可以使用t1.micro实例。 做这些后续步骤并不需要太多的权力。

    机器运行后,附上您从第一步中记下的快照。 在我的情况下,我把它附加到/ dev / sdf

    然后,创build一个具有所需大小的新卷。 在我的情况下,我创build了一个5GB的容量,因为这是我想缩小的大小。 不要从快照创build这个新卷。 我们需要一个新的空白卷。 接下来,将其附加到正在运行的实例,在我的情况下,我将其作为/ dev / sdg附加

    接下来,ssh进入机器,但不要安装附加的卷。

    在这一点上,我犯了偏执狂的一面,我select在大卷上检查文件系统,只是为了确保没有错误。 如果你确信没有,你可以跳过这一步:

    $ sudo e2fsck -f /dev/sdf 

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

     $ sudo resize2fs -M -p /dev/sdf 

    -M缩小它,-p打印进度。

    resize2fs应该告诉你shrunkin文件系统有多大。 就我而言,它给了我4K的大小。

    我们现在将shrunkin文件系统复制到新的磁盘上。 我们将以16MB块复制数据,所以我们需要弄清楚我们需要复制多less个16MB的块。 这是handey缩小的文件系统大小的地方。

    就我而言,缩小的文件系统刚刚超过1 GB,因为在拍摄快照之前,我已经在基本Ubuntu系统上安装了许多其他程序。 我可能只是将文件系统的大小复制到最接近的16MB,但我想保证安全。

    所以,128倍16MB块= 2GB:

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

    我复制了16MB的块,因为在EBS中,你为每个读写付费,所以我想尽可能地减less它们的数量。 我不知道这样做是否这样做,但它可能没有受到伤害。

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

     $ sudo resize2fs -p /dev/sdg 

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

     $ sudo e2fsck -f /dev/sdg 

    这就是我们需要在这台机器上做的事情,尽pipe安装新音量不会有什么坏处,只是作为一个testing。 但是,这一步几乎肯定是可选的,因为e2fsck应该有任何问题。

    我们现在需要快照新卷,并创build一个基于它的AMI。 我们已经完成了机器,所以你可以终止它,如果你喜欢。

    如果您装入了小卷,请确保小卷已经卸载,然后拍摄快照。 同样,您可以在pipe理控制台中执行此操作。

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

    编辑:

    自从发布此答案后,AWS控制台允许您简单地右键单击快照,然后select从快照创build映像。 您仍然需要select适当的内核ID。 如果它没有出现在列表中,请确保您select了适当的架构。

    我们使用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和快照IDreplace您在上一步中创build的内核ID。 您还需要将它指向上面的秘密密钥(称为sk.pem),以及您的x509证书(称为cert.pem)。 你当然可以select你想要的名称和描述。

    希望这可以帮助。

    是的,我也想知道。 以下教程是矫枉过正,但我​​认为它包含必要的工具: http : //www.linuxconfig.org/Howto_CREATE_BUNDLE_UPLOAD_and_ACCESS_custom_Debian_AMI_using_ubuntu

    而不是像上面那样安装到新磁盘映像上,应该可以启动大型AMI,创build新的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包括实际的manpages! 它们与ec2- *命令的名称完全相同,只是使用了euca-前缀。 http://open.eucalyptus.com/wiki/Euca2oolsUsing

    我想减less一般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 

    希望这有助于某人