Saturday, December 17, 2016

Expanding the Storage Space of an EBS Volume on Linux_AWS



Expanding the Storage Space of an EBS Volume on Linux
You can increase the storage space of an existing EBS volume without losing the data on the volume. To do this, you migrate your data to a larger volume and then extend the file system on the volume to recognize the newly-available space. After you verify that your new volume is working properly, you can delete the old volume.
Tasks
If you need to expand the storage space of a volume on a Windows instance, see Expanding the Storage Space of a Volume in the Amazon EC2 User Guide for Windows Instances.
If you create a larger volume, you will be charged for the additional storage. For more information, see the Amazon Elastic Block Store section on the Amazon EC2 Pricing page.
Note
If your storage needs demand a larger EBS volume than AWS provides, you may want to use RAID 0 to "stripe" a single logical volume across multiple physical volumes. For more information see RAID Configuration on Linux.
Migrating Your Data to a Larger Volume
You must stop your instance to expand the storage space. When you stop and start an instance, be aware of the following:
  • Any data on any instance store volumes is erased. Therefore, if you have any data on instance store volumes that you want to keep, back it up to persistent storage.
  • If your instance is running in a VPC and has a public IP address, we release the address and give it a new public IP address. The instance retains its private IP addresses and any Elastic IP addresses.
  • If your instance is running in EC2-Classic, we give it new public and private IP addresses, and disassociate any Elastic IP address that's associated with the instance. You must re-associate any Elastic IP address after you restart your instance.
  • If your instance is in an Auto Scaling group, the Auto Scaling service marks the stopped instance as unhealthy, and may terminate it and launch a replacement instance. To prevent this, you can temporarily suspend the Auto Scaling processes for the group. For more information, see Suspend and Resume Auto Scaling Processes in the Auto Scaling User Guide.
To migrate your data to a larger volume
  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
  2. In the navigation pane, choose Instances and then locate the instance with the volume that you want to expand.
  3. Make a note of the instance ID and Availability Zone. You will specify this information when you attach a new volume to the instance later in this procedure.
  4. Verify that the instance Shutdown Behavior is set to Stop and not Terminate.
    1. Choose the instance.
    2. From the context-menu (right-click) choose Instance Settings, and then chooseChange Shutdown Behavior.
    3. If the Shutdown behavior is set to Terminate, choose Stop, and then chooseApply.
If the Shutdown behavior is already set to Stop, then choose Cancel.
  1. Stop the instance. For more information about how to stop an instance, see Stopping and Starting Your Instances.
Warning
When you stop an instance, the data on any instance store volumes is erased. Therefore, if you have any data on instance store volumes that you want to keep, be sure to back it up to persistent storage.
  1. Create a snapshot of the volume to expand.
    1. In the navigation pane, choose Volumes, and then locate the volume you want to expand.
    2. From the context-menu (right-click) choose the volume that you want to expand, and then choose Create Snapshot.
    3. Enter information in the Name and Description fields, and then choose Yes, Create.
  2. Create a new volume from the snapshot.
    1. In the navigation pane, choose Snapshots.
    2. When the status of the snapshot that you just created is set to completed, choose the snapshot, and then from the context-menu (right-click) choose Create Volume.
    3. In the Create Volume dialog box, choose the desired volume type and enter the new volume size. You must also set the Availability Zone to match the instance Availability Zone. Choose Yes, Create.
Important
If you do not set the Availability Zone to match the instance then you will not be able to attach the new volume to the instance.
  1. Detach the old volume.
    1. In the navigation pane, choose Volumes, and then choose the old volume from the list. Make a note of the device name in the Attachment Information field. You will specify this information when you attach a new volume to the instance later in this procedure. The information appears in the following format:
i-xxxxxxxxxxxxxxxxx (instance_name):device_name
    1. From the context-menu (right-click) choose the old volume, and then chooseDetach Volume.
    2. In the Detach Volume dialog box, choose Yes, Detach. It may take several minutes for the volume to detach.
  1. Attach the newly expanded volume
    1. In the navigation pane, choose Volumes.
    2. From the context-menu (right-click) choose the new volume, and then chooseAttach Volume.
    3. Start typing the name or ID of the instance in the Instance field, and then choose the instance.
    4. Enter the same device name retrieved in Step 8.a, and then choose Yes, Attach. It is important to attach the new volume to the exact location you noted above (for example /dev/sda1).
  2. Restart the instance.
    1. In the navigation pane, choose Instances and then choose the instance you want to restart.
    2. From the context-menu (right-click) choose Instance State, and then chooseStart.
    3. In the Start Instances dialog box, choose Yes, Start. If the instance fails to start, and the volume being expanded is a root volume, verify that you attached the expanded volume using the same device name as the original volume, for example /dev/sda1.
After the instance has started, you can check the file system size to see if your instance recognizes the larger volume space. On Linux, use the df -h command to check the file system size.
[ec2-user ~]$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/xvda1            7.9G  943M  6.9G  12% /
tmpfs                 1.9G     0  1.9G   0% /dev/shm
If the size does not reflect your newly-expanded volume, you must extend the file system of your device so that your instance can use the new space. For more information, seeExtending a Linux File System.
Extending a Linux File System
In Linux, you use a file system-specific command to resize the file system to the larger size of the new volume. This command works even if the volume you wish to extend is the root volume. For ext2ext3, and ext4 file systems, this command is resize2fs. For XFS file systems, this command is xfs_growfs. For other file systems, refer to the specific documentation for those file systems for instructions on extending them.
If you are unsure of which file system you are using, you can use the file -s command to list the file system data for a device. The following example shows a Linux ext4 file system and an SGI XFS file system.
[ec2-user ~]$ sudo file -s /dev/xvd*
/dev/xvda1: Linux rev 1.0 ext4 filesystem data ...
/dev/xvdf:  SGI XFS filesystem data ...
Note
If the volume you are extending has been partitioned, you need to increase the size of the partition before you can resize the file system. For more information, see Expanding a Linux Partition.
To check if your volume partition needs resizing
  • Use the lsblk command to list the block devices attached to your instance. The example below shows three volumes: /dev/xvda/dev/xvdb, and /dev/xvdf.
·         [ec2-user ~]$ lsblk
·         NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
·         xvda    202:0    0  30G  0 disk
·         └─xvda1 202:1    0  30G  0 part /
·         xvdb    202:16   0  30G  0 disk /mnt
·         xvdf    202:80   0  35G  0 disk
└─xvdf1 202:81   0   8G  0 part
The root volume, /dev/xvda1, is a partition on /dev/xvda. Notice that they are both 30 GiB in size. In this case, the partition occupies all of the room on the device, so it does not need resizing.
The volume /dev/xvdb is not partitioned at all, so it does not need resizing.
However, /dev/xvdf1 is an 8 GiB partition on a 35 GiB device and there are no other partitions on the volume. In this case, the partition must be resized in order to use the remaining space on the volume. For more information, see Expanding a Linux Partition. After you resize the partition, you can follow the next procedure to extend the file system to occupy all of the space on the partition.
To extend a Linux file system
  1. Log in to your Linux instance using an SSH client. For more information about connecting to a Linux instance, see Connecting to Your Linux Instance Using SSH.
  2. Use the df -h command to report the existing file system disk space usage. In this example, the /dev/xvda1 device has already been expanded to 70 GiB, but the ext4file system only sees the original 8 GiB size, and the /dev/xvdf device has been expanded to 100 GiB, but the XFS file system only sees the original 1 GiB size.
[ec2-user ~]$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/xvda1            7.9G  943M  6.9G  12% /
tmpfs                 1.9G     0  1.9G   0% /dev/shm
/dev/xvdf            1014M   33M  982M   4% /mnt
  1. Use the file system-specific command to resize the file system to the new size of the volume. For a Linux ext2ext3, or ext4 file system, use the following command, substituting the device name that you want to extend.
[ec2-user ~]$ sudo resize2fs /dev/xvda1
resize2fs 1.42.3 (14-May-2012)
Filesystem at /dev/xvda1 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 5
Performing an on-line resize of /dev/xvda1 to 18350080 (4k) blocks.
The filesystem on /dev/xvda1 is now 18350080 blocks long.
For an XFS file system, first install the XFS userspace tools:
[ec2-user ~]$ sudo yum install xfsprogs
Then use the following command, substituting the mount point of the file system (XFS file systems must be mounted to resize them).
[ec2-user ~]$ sudo xfs_growfs -d /mnt
meta-data=/dev/xvdf              isize=256    agcount=4, agsize=65536 blks
         =                       sectsz=512   attr=2
data     =                       bsize=4096   blocks=262144, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 262144 to 26214400
Note
If you receive an xfsctl failed: Cannot allocate memory error, you may need to update the Linux kernel on your instance. For more information, refer to your specific operating system documentation.
If you receive an The filesystem is already nnnnnnn blocks long. Nothing to do! error, see Expanding a Linux Partition.
  1. Use the df -h command to report the existing file system disk space usage, which should now show the full 70 GiB on the ext4 file system and 100 GiB on the XFS file system.
# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/xvda1             69G  951M   68G   2% /
tmpfs                 1.9G     0  1.9G   0% /dev/shm
/dev/xvdf             100G   45M  100G   1% /mnt
Deleting the Old Volume
After the new volume has been attached and extended in the instance, you can delete the old volume if it is no longer needed.
To delete the old volume
  1. In the Amazon EC2 console, choose Volumes in the navigation pane and then choose the volume you want to delete.
  2. From the context-menu (right-click) choose Delete Volume.
  3. In the Delete Volume dialog box, choose Yes, Delete.

1 comment:

  1. Good one... I am a regular reader of your blogs and thanks for your excellent articles. I have written a detailed post on EBS storage pricing which will be useful for the readers of this blog post.

    ReplyDelete