{"id":2883,"date":"2015-01-19T10:11:33","date_gmt":"2015-01-19T10:11:33","guid":{"rendered":"http:\/\/blog.cloudthat.com\/?p=2883"},"modified":"2024-06-25T11:14:00","modified_gmt":"2024-06-25T11:14:00","slug":"shrinking-an-amazon-ebs-volume","status":"publish","type":"blog","link":"https:\/\/www.cloudthat.com\/resources\/blog\/shrinking-an-amazon-ebs-volume","title":{"rendered":"Shrinking An Amazon EBS Volume"},"content":{"rendered":"<p>Amazon\u2019s Elastic Block Store Volumes are easy to use and expand but notoriously hard to shrink once their size has grown. Here are the steps for Shrinking any mounted EBS volume on EC2 Instances.<\/p>\n<p>For various reasons you may need to expand or shrink the size of your EBS volume. Because you are only charged for the space currently allocated to your EBS volumes, it is also cost efficient to allocate only your approximate short term need and expand the volume as the need arises (this should be considered during the planning phase of your projects and\/or during formulation of growth strategies for your service).<\/p>\n<p>The below steps have been tested on an Amazon Linux instance to resize an EBS Volume using the AWS console<\/p>\n<ol>\n<li>Create a new EBS Voume of your desired size from the console and attach it to your instance<\/li>\n<li>Now login to your instance using ssh and format your new attached volume using the following command (Assuming that \u00a0new volume is attached at \/dev\/sdi)<\/li>\n<\/ol>\n<pre class=\"lang:sh decode:true\">ssh -i &lt;Private-key&gt; ec2-user@ip-address<\/pre>\n<pre class=\"lang:default decode:true\">sudo mkfs -t ext4 \/dev\/sdi<\/pre>\n<p>3.Then make a directory at \/mnt\/ebs1 and mount the new volume using following commands<\/p>\n<pre class=\"lang:default decode:true\">sudo mkdir \/mnt\/ebs1<\/pre>\n<pre class=\"lang:default decode:true\">sudo mount \/dev\/sdi \/mnt\/ebs1<\/pre>\n<p>4. Now copy your data recursively from your \u00a0source volume to your newly created volume using the following \u00a0 \u00a0 \u00a0 \u00a0 command(Assuming your old volume is mounted at \/mnt\/ebs)<\/p>\n<pre class=\"lang:default decode:true\">sudo cp -r --preserve=all \/mnt\/ebs\/. \/mnt\/ebs1 --verbose<\/pre>\n<p>Copying data recursively preserves the file attributes such as mode,ownership,timestamps and security contexts, if possible additional attributes such as links.<\/p>\n<p>5. \u00a0After copying the data detach your old volume and mount your new volume at the same mount point using the following command<\/p>\n<pre class=\"lang:default decode:true\">sudo umount -l \/mnt\/ebs<\/pre>\n<pre class=\"lang:default decode:true\">sudo mount \/dev\/sdi \/mnt\/ebs<\/pre>\n<h3>Automating the process by\u00a0Shell Script<\/h3>\n<p>In\u00a0order to automate all this process here is the Script which will \u00a0automate the process<\/p>\n<p>Download the zip from this page\u00a0<a title=\"Github\" href=\"https:\/\/github.com\/cloudthat\/ebs-shrink\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/cloudthat\/ebs-shrink<\/a><\/p>\n<p>Extract the zip and navigate to the extracted folder<\/p>\n<p><b>Step 1: Place the script on the instance with permissions to execute<\/b><\/p>\n<pre class=\"lang:default decode:true\">scp -i &lt;identity_file&gt; &lt;path-to&gt;\/ebsreducescript &lt;user&gt;@&lt;host_name&gt;:\/home\/ec2-user\/.<\/pre>\n<p>Now SSH into the instance using the following command and Update file permissions<\/p>\n<pre class=\"lang:default decode:true\">ssh -i &lt;Private-key&gt; ec2-user@ip-address<\/pre>\n<pre class=\"lang:default decode:true\">sudo chmod 500 ebsreducescript<\/pre>\n<p><strong>Step 2: Configure AWS \u00a0Cli<\/strong><\/p>\n<p>Also before executing the script you need to configure awscli use the following commands to install<\/p>\n<pre class=\"lang:default decode:true\">sudo yum install python-pip<\/pre>\n<pre class=\"lang:default decode:true\">sudo pip install awscli<\/pre>\n<p>Run aws configure at the command line to set up your credentials and settings.<\/p>\n<pre class=\"font:ubuntu-mono lang:default decode:true\">$ aws configure\r\nAWS Access Key ID [None]: ANKITIOSFODNN7EXAMPLE\r\nAWS Secret Access Key [None]: ANKITXUtnFEMI\/K7MDENG\/bPxRfiCYEXAMPLEKEY\r\nDefault region name [None]: us-west-1\r\nDefault output format [None]: json<\/pre>\n<p>This Script Will Create a New Volume of User Defined,Mount it to \/mnt\/ebs1 Copy all the data from the attached EBS Volume recursively that need to be Migrated preserving all the user permissions,then Unmount it and Mount the Newly Created EBS Volume at the Same Mount Point<\/p>\n<p>Note:The migrated volume is not deleted it is just unmounted So if any error occurs we can reattach it. This Script can only use for Attached(Mounted) EBS Volumes not Root Volume.<\/p>\n","protected":false},"author":219,"featured_media":0,"parent":0,"comment_status":"open","ping_status":"open","template":"","blog_category":[3606,3607,3665],"user_email":"prarthitm@cloudthat.com","published_by":"324","primary-authors":"","secondary-authors":"","acf":[],"_links":{"self":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/2883"}],"collection":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog"}],"about":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/types\/blog"}],"author":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/users\/219"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/comments?post=2883"}],"version-history":[{"count":1,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/2883\/revisions"}],"predecessor-version":[{"id":42362,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/2883\/revisions\/42362"}],"wp:attachment":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/media?parent=2883"}],"wp:term":[{"taxonomy":"blog_category","embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog_category?post=2883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}