<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Benedikt Langens on Medium]]></title>
        <description><![CDATA[Stories by Benedikt Langens on Medium]]></description>
        <link>https://medium.com/@benedikt.langens?source=rss-aed112aca1b1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Bjwf-8sQfJIVM9NvdpvQNA.jpeg</url>
            <title>Stories by Benedikt Langens on Medium</title>
            <link>https://medium.com/@benedikt.langens?source=rss-aed112aca1b1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 18:16:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@benedikt.langens/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to Shrink an EBS Root Volume (XFS) on Amazon Linux 2 / 2023]]></title>
            <link>https://medium.com/@benedikt.langens/how-to-shrink-an-ebs-root-volume-xfs-on-amazon-linux-2-2023-a7705c16e839?source=rss-aed112aca1b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/a7705c16e839</guid>
            <category><![CDATA[ec2]]></category>
            <category><![CDATA[xfs-filesystem]]></category>
            <category><![CDATA[amazon-linux]]></category>
            <category><![CDATA[ebs-volume]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Benedikt Langens]]></dc:creator>
            <pubDate>Sat, 24 Jun 2023 18:44:30 GMT</pubDate>
            <atom:updated>2023-06-24T18:59:21.801Z</atom:updated>
            <content:encoded><![CDATA[<p>The other day I had to shrink an EBS root volume of an <em>Amazon Linux 2</em> instance. While AWS supports <a href="https://repost.aws/knowledge-center/expand-root-ebs-linux">expanding EBS volumes</a> out of the box, shrinking them is not officially supported and a more involved process. To increase the challenge; the EBS volume was a root volume which means that it contains a boot partition (<em>Amazon Linux 2</em>) or even boot and EFI partitions (<em>Amazon Linux 2023</em>) as well.</p><p>There are a few guides on the internet that describe <a href="https://medium.com/@andtrott/how-to-downsize-a-root-ebs-volume-on-aws-ec2-amazon-linux-727c00148f61">how to shrink EBS volumes</a> — <a href="https://medium.com/@ztobscieng/shrink-an-amazon-aws-ebs-root-volume-2020-update-8db834265c3e">even EBS root volumes</a>. Here’s the catch though: all of the guides I found were for the ext4 file system. However, newer <em>Amazon Linux 2</em> and <em>Amazon Linux 2023</em> instances use the XFS file system by default.</p><p>While ext4 file systems come with some handy tools such as resize2fs that allow to easily shrink (or expand) a partition, XFS does not support shrinking partitions at all. XFS partitions can be expanded through xfs_growfs without issues, but <a href="https://wiki.archlinux.org/title/XFS#Resize">shrinking them</a> <a href="https://unix.stackexchange.com/a/348722">is not possible</a>.</p><p>After some research and digging through XFS documentation, I found a workaround. To shrink an XFS file system, it has to be backed up first with xfsdump and then restored on the new volume with xfsrestore. Since this is an EBS root volume, we’ll have to separately take care of the boot partition too though.</p><p>In the below guide I’ll provide a step-by-step tutorial on how to resize an EBS root volume with an XFS file system on <em>Amazon Linux 2</em>. The guide was tested on the latest <em>Amazon Linux 2023</em> as well and works the same way with slight differences that are mentioned in each step.</p><h3>Step-by-Step Guide</h3><p><strong>1 : </strong>Take a snapshot of the EBS root volume you want to shrink. You can always create a new volume from this snapshot, in case you mess up any of the steps below.<br>Optionally, you can temporarily stop the corresponding EC2 instance to avoid any more data written to its volume.</p><p><strong>2 :</strong> Create a <strong>new</strong> EBS volume from the snapshot you’ve taken in the previous step and leave the new volume unattached for now.</p><p><strong>3 :</strong> Create a new, temporary EC2 instance with <em>Amazon Linux 2 </em>or<em> 2023</em> that will be used for the shrinking operation. The temporary EC2 instance should have two EBS volumes attached:</p><ul><li>A root volume which is large enough to temporarily store an XFS backup of the EBS volume you want to shrink.<br>In this guide, the data on the EBS volume we want to shrink is 20 GiB in total. Therefore, we configure the size of the root volume of the temporary EC2 instance at 35 GiB (20 GiB data backup + 15 GiB for the OS, plus some headroom).</li><li>An extra EBS volume that will become your <strong>new</strong>, shrunken EBS root volume.<br>In this guide, we want to shrink our <strong>old</strong> EBS root volume from 100 GiB to 50 GiB. That means the extra EBS volume attached to this temporary EC2 instance will be 50 GiB.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/456/1*edq5l_F1Ak4_aKpieDLMSw.png" /><figcaption>Example storage configuration of temporary EC2 instance</figcaption></figure><p><strong>4 :</strong> Attach the EBS volume created from the snapshot in step 2 to the new, temporary EC2 instance. Attach it as device name /dev/sdc.</p><p><strong>5 :</strong> The temporary EC2 instance should have three EBS volumes attached now:</p><ul><li>Root volume /dev/xvda (35 GiB)</li><li>Volume /dev/sdb: <strong>New</strong> EBS root volume (50 GiB)</li><li>Volume /dev/sdc: <strong>Old</strong> EBS root volume (100 GiB)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/678/1*j3eNcda9HbipGp3wQSxSjQ.png" /><figcaption>Attached EBS volumes of temporary EC2 instance</figcaption></figure><p><strong>6 :</strong> Log in to your temporary EC2 instance via SSH and switch to root:<br>sudo -i.</p><p><strong>7 :</strong> Execute lsblk to see a list of all attached storage devices. You should see the following devices:</p><ul><li>nvme0n1: Root volume of the temporary EC2 instance (35 GiB)</li><li>nvme1n1: <strong>New</strong> EBS root volume (50 GiB)</li><li>nvme2n1: <strong>Old</strong> EBS root volume (100 GiB)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/501/1*qBjec-vAkXjlTqC53Fn71g.png" /><figcaption>Example output of command lsblk</figcaption></figure><p>The suffixes p1 and p128 identify the partition. In this guide, using <em>Amazon Linux 2</em>, p128 is the boot and p1 is the (first) data partition.</p><p>With <em>Amazon Linux 2023,</em> an additional EFI partition is shown as p128 instead, and the boot partition is p127. However, the (first) data partition is still p1.</p><p>You’ll also notice that the device names have been renamed from /dev/xvd* or /dev/sd* (as shown in the AWS console) to /dev/nvme*. This is normal as modern Linux kernels use this naming convention for NVMe storage devices.</p><p><strong>8 :</strong> Execute fdisk -l /dev/nvme2n1 to show disk and partition information for the <strong>old</strong> root volume. Make a note of the sector size and start sector number of the (first) data partition. Typically, the sector size is <strong>512</strong> bytes and the start sector number of the (first) data partition is <strong>4096</strong>.</p><p>With <em>Amazon Linux 2023,</em> the defaults are slightly different due to the additional EFI partition. The start sector number of the (first) data partition is typically <strong>24576</strong>. Make sure to consider this in the next steps.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/681/1*hEl2b_Hp69rP1hno8LeyWA.png" /><figcaption>Example output of command fdisk</figcaption></figure><p><strong>9 :</strong> Now, we have to copy the boot partition from the <strong>old</strong> root volume nvme2n1 to the <strong>new</strong> root volume nvme1n1. We’ll use dd to create a low-level copy of the boot partition using the information gathered in the previous step:</p><pre>dd if=/dev/nvme2n1 of=/dev/nvme1n1 bs=512 count=4096</pre><ul><li>if: input file/device; our <strong>old</strong> root volume.</li><li>of: output file/device; our <strong>new</strong> root volume.</li><li>bs: block size in bytes; this is equal to the sector size.</li><li>count: amount of blocks to copy; this is equal to the start sector of the (first) data partition.</li></ul><p>Beside the boot partition, the copy will also contain the partition table which we will adjust in the next steps. Don’t be alarmed if partitioning tools such as fdisk report that the <strong>new</strong> root volume is larger than its physical size, this will be corrected in step 11.</p><p>Using defaults for <em>Amazon Linux 2023</em>, count would be 24576 to copy both boot and EFI partitions.</p><p><strong>10 :</strong> Execute blkid /dev/nvme2n1p1 to display UUID information of the (first) data partition on the <strong>old</strong> root volume. Again, make a note of the information displayed here. We’ll need this later to adjust the UUID of the <strong>new</strong> root volume.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xRtjKtpb__Wb5CME9rNRAw.png" /><figcaption>Example output of command blkid</figcaption></figure><p><strong>11 :</strong> Next, we launch gdisk to update and re-partition the <strong>new</strong> root volume:</p><pre>gdisk /dev/nvme1n1</pre><p>First, we need to adjust the partition table (GPT). Since we copied over the old GPT, it reports a larger size than is physically available on the <strong>new</strong> root volume. This can be fixed in the expert’s menu of gdisk:</p><pre>Command: <strong>x</strong> <em>&lt;Enter&gt;</em><br>Command: <strong>e</strong> <em>&lt;Enter&gt;</em></pre><p>Go back to the main menu:</p><pre>Command: <strong>m</strong> <em>&lt;Enter&gt;</em></pre><p>Now, we want to delete the old data partition:</p><pre>Command: <strong>d</strong> <em>&lt;Enter&gt;</em><br>Partition number: <strong>1</strong> <em>&lt;Enter&gt;</em></pre><p>After that, we want to create a new data partition with a correct size:</p><pre>Command: <strong>n</strong> <em>&lt;Enter&gt;</em><br>Partition number: <strong>1</strong> <em>&lt;Enter&gt;</em><br>First sector: <strong>4096</strong> <em>&lt;Enter&gt;</em> <em>(Note: this is the start sector number of your (first) data partition from step 8.)</em><br>Last sector: <strong>0</strong> <em>&lt;Enter&gt;</em> <em>(Note: &quot;0&quot; stands for the last available sector on your physical disk which means the partition will &quot;fill up&quot; the rest of your disk.)</em><br>Hex code or GUID: <strong>8300</strong> <em>&lt;Enter&gt;</em></pre><p>Lastly, we want to write all of our changes to disk:</p><pre>Command: <strong>w</strong> <em>&lt;Enter&gt;</em><br>Do you want to proceed? <strong>Y</strong> <em>&lt;Enter&gt;</em></pre><p><strong>12 :</strong> It’s time to create an XFS file system on the (first) data partition of the <strong>new</strong> root volume:</p><pre>mkfs -t xfs /dev/nvme1n1p1</pre><p><strong>13 :</strong> Now that we prepared the <strong>new</strong> root volume, we want to make a backup of the (first) data partition of the <strong>old</strong> root volume.</p><p>Mount the data partition of the <strong>old</strong> root volume:</p><pre>mkdir /mnt/data-old-root<br>mount /dev/nvme2n1p1 /mnt/data-old-root</pre><p>Use xfsdump to create a backup:</p><pre>xfsdump -L data -f ~/data.xfsdump /mnt/data-old-root</pre><p>This may take a while depending on the size of your data. If you’re worried about the SSH session expiring, consider starting xfsdump with nohup in combination with &amp; to run as a background process.</p><p>If xfsdump asks you to enter a media label, you can enter any desired label.</p><p><strong>14 :</strong> Restore the XFS backup to the (first) data partition of the <strong>new</strong> root volume.</p><p>Mount the data partition of the <strong>new</strong> root volume:</p><pre>mkdir /mnt/data-new-root<br>mount /dev/nvme1n1p1 /mnt/data-new-root</pre><p>Use xfsrestore to restore the backup:</p><pre>xfsrestore -f ~/data.xfsdump /mnt/data-new-root</pre><p>Again, this may take a while depending on the size of your data. If you’re worried about the SSH session expiring, consider starting xfsrestore with nohup in combination with &amp; to run as a background process.</p><p><strong>15 :</strong> Almost done. Now, we update the UUID and LABEL of the (first) data partition on the <strong>new</strong> root volume to match the values of the <strong>old</strong> root volume.</p><p>Unmount the data partition of the <strong>new</strong> root volume first to be able to make the required updates:</p><pre>umount /mnt/data-new-root</pre><p>Update UUID and LABEL:</p><pre>xfs_admin -U <strong><em>&lt;UUID from step 10&gt;</em></strong> /dev/nvme1n1p1<br>xfs_admin -L / /dev/nvme1n1p1</pre><p><strong>16 :</strong> Finally, we can detach the <strong>new</strong> EBS root volume from the temporary EC2 instance and replace the <strong>old</strong> EBS root volume on the <em>original</em> EC2 instance.<br>The <em>original</em> EC2 instance needs to be stopped to be able to swap an EBS root volume. Make sure to use device name /dev/xvda when attaching the <strong>new</strong> root volume. After swapping, start the <em>original</em> EC2 instance to verify if the instance is able to boot and you’re able to log in.</p><p><strong>17 :</strong> Once you have verified everything is working as intended, you can safely terminate the temporary EC2 instance. You may want to keep the snapshot of the <strong>old</strong> EBS root volume from step 1 for a while, in case some issues appear at a later time.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a7705c16e839" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>