{"id":18784,"date":"2023-09-19T21:46:08","date_gmt":"2023-09-19T18:46:08","guid":{"rendered":"https:\/\/kifarunix.com\/?p=18784"},"modified":"2024-03-10T10:52:58","modified_gmt":"2024-03-10T07:52:58","slug":"shrink-linux-root-filesystem-by-migrating-to-new-disk","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/shrink-linux-root-filesystem-by-migrating-to-new-disk\/","title":{"rendered":"Shrink Linux Root Filesystem by Migrating to New Disk"},"content":{"rendered":"\n<p>In this tutorial, you will learn how to shrink Linux root filesystem by migrating to new disk. Shrinking the Linux root filesystem can be necessary for a variety of reasons, such as when you want to migrate the system to a smaller disk to save under-utilized disk space. However, shrinking the root filesystem can be a delicate operation, as it can damage the system and subsequently lead to loss of data if done incorrectly.<\/p>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>Table of Contents<\/h2><nav><ul><li><a href=\"#shrinking-linux-root-filesystem-by-migrating-to-new-disk\">Shrinking Linux Root Filesystem by Migrating to New Disk<\/a><ul><li><a href=\"#backup-your-system-data\">Backup your System Data<\/a><\/li><li><a href=\"#attach-new-disk-to-the-system\">Attach New Disk to the System<\/a><\/li><li><a href=\"#attach-live-bootable-medium-to-the-linux-system\">Attach Live Bootable Medium to the Linux system<\/a><\/li><li><a href=\"#boot-into-live-system-environment\">Boot into Live System Environment<\/a><\/li><li><a href=\"#create-relevant-partitions-and-filesystems-on-the-new-disk\">Create Relevant Partitions and Filesystems on the New Disk<\/a><ul><li><a href=\"#get-information-about-available-block-devices\">Get Information about Available Block Devices<\/a><\/li><li><a href=\"#check-disk-partition-scheme-and-firmware-interface-type\">Check Disk Partition Scheme and Firmware Interface Type<\/a><\/li><li><a href=\"#partition-new-disk\">Partition New Disk<\/a><\/li><li><a href=\"#create-logical-volume-on-the-root-filesystem\">Create Logical Volume on the Root Filesystem<\/a><\/li><\/ul><\/li><li><a href=\"#copy-data-from-old-disk-to-new-disk\">Copy Data from Old Disk to New Disk<\/a><ul><li><a href=\"#mount-old-and-new-disks\">Mount Old and New Disks<\/a><\/li><li><a href=\"#copy-data-from-old-disk-to-new-disk-1\">Copy Data from Old Disk to New Disk<\/a><\/li><\/ul><\/li><li><a href=\"#install-grub-bootloader\">Install GRUB Bootloader<\/a><\/li><li><a href=\"#update-the-filesystem-table-fstab\">Update the Filesystem Table (FSTAB)<\/a><\/li><li><a href=\"#exit-chroot-and-reboot-the-system\">Exit Chroot and Reboot the System<\/a><\/li><li><a href=\"#other-tutorials\">Other Tutorials<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"shrinking-linux-root-filesystem-by-migrating-to-new-disk\">Shrinking Linux Root Filesystem by Migrating to New Disk<\/h2>\n\n\n\n<p>As already stated, shrinking the root filesystem of a Linux can be a delicate and risky task as you might easily loose your data. One way to shrink the root filesystem is to migrate it to a new disk, that is relatively smaller. This is a relatively safe operation, as it does not involve modifying the existing filesystem. To migrate the root filesystem to a new disk, you will need to:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Backup your data<\/li>\n\n\n\n<li>Power off and attach a new disk of smaller size to the system<\/li>\n\n\n\n<li>Create Bootable USB or simply attach Live bootable ISO to your Linux system<\/li>\n\n\n\n<li>Boot into Live System Environment<\/li>\n\n\n\n<li>Create relevant partitions and filesystems on the new disk.<\/li>\n\n\n\n<li>Copy the contents of the root filesystem to the new disk.<\/li>\n\n\n\n<li>Update the bootloader\/Install grub to boot partition on the new disk.<\/li>\n<\/ol>\n\n\n\n<p>For the purposes of demo, we will be using an Ubuntu 22.04 Virtual Machine running on KVM.<\/p>\n\n\n\n<p>See the disk usage on my Ubuntu VM;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>df -hT<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>Filesystem                        Type   Size  Used Avail Use% Mounted on\ntmpfs                             tmpfs  195M  1.2M  194M   1% \/run\n<strong>\/dev\/mapper\/ubuntu--vg-ubuntu--lv ext4   105G  6.8G   94G   7% \/<\/strong>\ntmpfs                             tmpfs  972M     0  972M   0% \/dev\/shm\ntmpfs                             tmpfs  5.0M     0  5.0M   0% \/run\/lock\n\/dev\/vda2                         ext4   2.0G  130M  1.7G   8% \/boot\n\/dev\/vda1                         vfat   1.1G  6.1M  1.1G   1% \/boot\/efi\ntmpfs                             tmpfs  195M  4.0K  195M   1% \/run\/user\/1000\n<\/code><\/pre>\n\n\n\n<p>As you can see, I am only using ~7G out of 100G+ assigned to the system. This space is under utilized and we are looking at reducing it to 20G.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"backup-your-system-data\">Backup your System Data<\/h3>\n\n\n\n<p>Before you can even think of attempting to play with resizing your system disk, ensure your have your backups done. Resizing filesystem can lead to system brick if a mistake arises during the process.<\/p>\n\n\n\n<p>You can check various backup tutorials we have;<\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/?s=backup\" target=\"_blank\" rel=\"noreferrer noopener\">How to backup Linux data<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"attach-new-disk-to-the-system\">Attach New Disk to the System<\/h3>\n\n\n\n<p>Power off the Linux system and attach a new disk of smaller size. Smaller is used to mean your desired size.<\/p>\n\n\n\n<p>As already mentioned, am using an Ubuntu VM on KVM. See screenshot below.<\/p>\n\n\n\n<p>First disk with current OS and data;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1020\" height=\"507\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/09\/kvm-disk-1.png\" alt=\"\" class=\"wp-image-18831\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/09\/kvm-disk-1.png?v=1695238656 1020w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/09\/kvm-disk-1-768x382.png?v=1695238656 768w\" sizes=\"(max-width: 1020px) 100vw, 1020px\" \/><\/figure>\n\n\n\n<p>New smaller disk attached;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1018\" height=\"537\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/09\/kvm-disk-2.png\" alt=\"Shrink Linux Root Filesystem by Migrating to New Disk\" class=\"wp-image-18832\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/09\/kvm-disk-2.png?v=1695238671 1018w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/09\/kvm-disk-2-768x405.png?v=1695238671 768w\" sizes=\"(max-width: 1018px) 100vw, 1018px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"attach-live-bootable-medium-to-the-linux-system\">Attach Live Bootable Medium to the Linux system<\/h3>\n\n\n\n<p>Next, attach a live bootable medium to your Linux system. Am using <a href=\"https:\/\/ubuntu.com\/download\" target=\"_blank\" rel=\"noreferrer noopener\">Ubuntu<\/a> 20.04 live ISO file in this guide.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1012\" height=\"530\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/09\/bootable-medium-attached.png\" alt=\"\" class=\"wp-image-18833\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/09\/bootable-medium-attached.png?v=1695238712 1012w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/09\/bootable-medium-attached-768x402.png?v=1695238712 768w\" sizes=\"(max-width: 1012px) 100vw, 1012px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"boot-into-live-system-environment\">Boot into Live System Environment<\/h3>\n\n\n\n<p>Once you have attached a new disk and a Live bootable medium, you need to configure your Linux system to boot from the live bootable medium.<\/p>\n\n\n\n<p>Thus, under the VM hardware details, click on <strong>Boot Options<\/strong> and select only the live ISO medium to boot from.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1015\" height=\"834\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/09\/boot-from-medium.png\" alt=\"\" class=\"wp-image-18834\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/09\/boot-from-medium.png?v=1695238738 1015w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/09\/boot-from-medium-768x631.png?v=1695238738 768w\" sizes=\"(max-width: 1015px) 100vw, 1015px\" \/><\/figure>\n\n\n\n<p>Apply the changes.<\/p>\n\n\n\n<p>Start the machine.<\/p>\n\n\n\n<p>When the system boots completely, you are provided with two options, <strong>Try Ubuntu<\/strong> or <strong>Install Ubuntu<\/strong>.<\/p>\n\n\n\n<p>Click <strong>Try Ubuntu<\/strong>. This could be different and may vary from distro to distro. Ensure you trying from Live ISO.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1016\" height=\"840\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/09\/boot-live-system.png\" alt=\"\" class=\"wp-image-18835\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/09\/boot-live-system.png?v=1695238773 1016w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/09\/boot-live-system-768x635.png?v=1695238773 768w\" sizes=\"(max-width: 1016px) 100vw, 1016px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"create-relevant-partitions-and-filesystems-on-the-new-disk\">Create Relevant Partitions and Filesystems on the New Disk<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"get-information-about-available-block-devices\">Get Information about Available Block Devices<\/h4>\n\n\n\n<p>Once you boot into the live system, open the terminal run the command below to get some information about the attached block devices.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo su -<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>lsblk  | grep -v loop<\/code><\/pre>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT\nvda                       252:0    0   110G  0 disk \n\u251c\u2500vda1                    252:1    0   1.1G  0 part \n\u251c\u2500vda2                    252:2    0     2G  0 part \n\u2514\u2500vda3                    252:3    0   107G  0 part \n  \u2514\u2500ubuntu--vg-ubuntu--lv 253:0    0   107G  0 lvm  \nvdb                       252:16   0    20G  0 disk \nvdc                       252:32   0   4.1G  0 disk \n\u251c\u2500vdc1                    252:33   0   4.1G  0 part \/cdrom\n\u2514\u2500vdc2                    252:34   0     4M  0 part\n<\/code><\/pre>\n\n\n\n<p>As can be see in the output above, we have three disk\/devices attached.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>vda<\/strong>: this is our main disk with our data<\/li>\n\n\n\n<li><strong>vdb<\/strong>: this is the new small disk we want to use in our system.<\/li>\n\n\n\n<li><strong>vdc<\/strong>: this is the bootable medium we are booting live system from.<\/li>\n<\/ul>\n\n\n\n<p>Also, you can see that the system was using LVM partition for the root disk.<\/p>\n\n\n\n<p>Logical volume details;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>lvs<\/code><\/pre>\n\n\n\n<pre class=\"scroll-sz\"><code>  LV        VG        Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert\n  ubuntu-lv ubuntu-vg -wi-a----- <106.95g \n<\/code><\/pre>\n\n\n\n<p>Volume group;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vgs<\/code><\/pre>\n\n\n\n<pre class=\"scroll-sz\"><code>  VG        #PV #LV #SN Attr   VSize    VFree\n  ubuntu-vg   1   1   0 wz--n- <106.95g    0\n<\/code><\/pre>\n\n\n\n<p>Physical volume;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pvs<\/code><\/pre>\n\n\n\n<pre class=\"scroll-sz\"><code>  PV         VG        Fmt  Attr PSize    PFree\n  \/dev\/vda3  ubuntu-vg lvm2 a--  <106.95g    0\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"check-disk-partition-scheme-and-firmware-interface-type\">Check Disk Partition Scheme and Firmware Interface Type<\/h4>\n\n\n\n<p>Another important step is to find out the current partition scheme of the disk with your current OS and data as well as the firmware interface whether it is BIOS or UEFI based system.<\/p>\n\n\n\n<p>If you have already noticed, we have three partitions:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>vda1<\/strong>: This is the EFI\/EFI system partition which contains bootloaders. It is known as BIOS partition for the BIOS based systems. Usually, it has to be FAT32 formatted.<\/li>\n\n\n\n<li><strong>vda2<\/strong>: This now is the boot partition which contains the kernel and other files such as vmlinuz, initial ram disk e.t.c that are needed to start the operating system.<\/li>\n\n\n\n<li><strong>vda3<\/strong>: This is the main root partition.<\/li>\n<\/ul>\n\n\n\n<p>How to check if a Linux system is using BIOS or UEFI firmware interface has been extensively discussed in the guide below;<\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/quickly-check-if-linux-system-is-using-bios-or-uefi\/\">Quickly Check If Linux System is Using BIOS or UEFI<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"partition-new-disk\">Partition New Disk<\/h4>\n\n\n\n<p>Next, partition the new disk to match how the old disk is partitioned. Ensure the partition scheme and firmware interfaces are same.<\/p>\n\n\n\n<p>In my system, the firmware interface for the old disk is UEFI, with GPT partition table.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>parted \/dev\/vda p<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>Model: Virtio Block Device (virtblk)\nDisk \/dev\/vda: 118GB\nSector size (logical\/physical): 512B\/512B\n<strong>Partition Table: gpt <\/strong>\nDisk Flags: \n\nNumber  Start   End     Size    File system  Name  Flags\n 1      1049kB  1128MB  1127MB  fat32              <strong>boot, esp<\/strong>\n 2      1128MB  3276MB  2147MB  ext4\n 3      3276MB  118GB   115GB\n<\/code><\/pre>\n\n\n\n<p>The new disk has no partitions yet;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>parted \/dev\/vdb p<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>Error: \/dev\/vdb: unrecognised disk label\nModel: Virtio Block Device (virtblk)                                      \nDisk \/dev\/vdb: 21.5GB\nSector size (logical\/physical): 512B\/512B\nPartition Table: unknown\nDisk Flags:\n<\/code><\/pre>\n\n\n\n<p>So, let's partition it. You can use gdisk\/fdisk or parted command. Let's use <strong>parted<\/strong> command here.<\/p>\n\n\n\n<p>Initialize the disk to GPT partition;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>parted \/dev\/vdb mklabel gpt<\/code><\/pre>\n\n\n\n<p>Create EFI partition. We set it to 512MiB.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>parted \/dev\/vdb mkpart primary fat32 1MiB 513MiB<\/code><\/pre>\n\n\n\n<p>Initialize the ESP partition;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>parted \/dev\/vdb set 1 esp on<\/code><\/pre>\n\n\n\n<p>Create Boot partition of 1GB;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>parted \/dev\/vdb mkpart primary ext4 513MiB 1537MiB<\/code><\/pre>\n\n\n\n<p>Create the root partition;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>parted \/dev\/vdb mkpart primary ext4 1537MiB 100%<\/code><\/pre>\n\n\n\n<p>Create filesystems on the partitions;<\/p>\n\n\n\n<p>EFI parition should be set to FAT32<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkfs.fat -F32 \/dev\/vdb1<\/code><\/pre>\n\n\n\n<p>Boot device, set to EXT4;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkfs.ext4 \/dev\/vdb2<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"create-logical-volume-on-the-root-filesystem\">Create Logical Volume on the Root Filesystem<\/h4>\n\n\n\n<p>As seen above, the old disk had an LVM the root filesystem.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>lvs<\/code><\/pre>\n\n\n\n<pre class=\"scroll-sz\"><code>  LV        VG        Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert\n  ubuntu-lv ubuntu-vg -wi-a----- <106.95g \n<\/code><\/pre>\n\n\n\n<p>So, we have to create a logical volume on the new disk root filesystem;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pvcreate \/dev\/vdb3<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>vgcreate ubuntu-vg-01 \/dev\/vdb3<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>lvcreate -n ubuntu-vg-01 -l +100%FREE ubuntu-vg-01<\/code><\/pre>\n\n\n\n<p>Confirm;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>lvs<\/code><\/pre>\n\n\n\n<pre class=\"scroll-sz\"><code>  LV           VG           Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert\n  ubuntu-lv    ubuntu-vg    -wi-a----- <106.95g                                                    \n  <strong>ubuntu-vg-01 ubuntu-vg-01 -wi-a-----  <18.50g<\/strong>\n<\/code><\/pre>\n\n\n\n<p>Create Filesystem on the new root disk logical volume;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkfs.ext4 \/dev\/ubuntu-vg-01\/ubuntu-vg-01<\/code><\/pre>\n\n\n\n<p>Confirm the changes;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>parted \/dev\/vdb p<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>Model: Virtio Block Device (virtblk)\nDisk \/dev\/vdb: 21.5GB\nSector size (logical\/physical): 512B\/512B\nPartition Table: gpt\nDisk Flags: \n\nNumber  Start   End     Size    File system  Name   Flags\n 1      1049kB  538MB   537MB   fat32        fat32  boot, esp\n 2      538MB   1612MB  1074MB  ext4         ext4\n 3      1612MB  21.5GB  19.9GB               ext4\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"copy-data-from-old-disk-to-new-disk\">Copy Data from Old Disk to New Disk<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"mount-old-and-new-disks\">Mount Old and New Disks<\/h4>\n\n\n\n<p>To copy data from the old disk to the new disk, you have to first mount both devices and respective partitions. So, create the mount directories.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir -p \/mnt\/{vda,vdb}<\/code><\/pre>\n\n\n\n<p>Mount the old disk onto <strong><code>\/mnt\/vda<\/code><\/strong> and new disk on <strong><code>\/mnt\/vdb<\/code><\/strong>.<\/p>\n\n\n\n<p>Mounting old disk;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mount \/dev\/ubuntu-vg\/ubuntu-lv \/mnt\/vda\/<\/code><\/pre>\n\n\n\n<p>Mount the boot partition;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mount \/dev\/vda2 \/mnt\/vda\/boot\/<\/code><\/pre>\n\n\n\n<p>Mount EFI partition;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mount \/dev\/vda1 \/mnt\/vda\/boot\/efi\/<\/code><\/pre>\n\n\n\n<p>Mount the new disk;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mount \/dev\/ubuntu-vg-01\/ubuntu-vg-01 \/mnt\/vdb\/<\/code><\/pre>\n\n\n\n<p>Create boot directory on the new disk;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir \/mnt\/vdb\/boot<\/code><\/pre>\n\n\n\n<p>Mount the new disk boot and EFI partition;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mount \/dev\/vdb2 \/mnt\/vdb\/boot\/<\/code><\/pre>\n\n\n\n<p>Create EFI directory and mount EFI partition;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir \/mnt\/vdb\/boot\/efi<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>mount \/dev\/vdb1 \/mnt\/vdb\/boot\/efi<\/code><\/pre>\n\n\n\n<p>Confirm the mounting;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>df -hT | grep -E \"vda|vdb\"<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\/dev\/mapper\/ubuntu--vg-ubuntu--lv         ext4      105G  6.8G   94G   7% \/mnt\/vda\n\/dev\/vda2                                 ext4      2.0G  130M  1.7G   8% \/mnt\/vda\/boot\n\/dev\/vda1                                 vfat      1.1G  6.1M  1.1G   1% \/mnt\/vda\/boot\/efi\n\/dev\/mapper\/ubuntu--vg--01-ubuntu--vg--01 ext4       19G   32K   18G   1% \/mnt\/vdb\n\/dev\/vdb2                                 ext4      974M   28K  907M   1% \/mnt\/vdb\/boot\n\/dev\/vdb1                                 vfat      511M  4.0K  511M   1% \/mnt\/vdb\/boot\/efi\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"copy-data-from-old-disk-to-new-disk-1\">Copy Data from Old Disk to New Disk<\/h4>\n\n\n\n<p>Now that the partitions are created and mounted, copy the data from the old disk to new disk. To ensure that proper <strong>permissions<\/strong> and <strong>ownership<\/strong> are retained, use <strong><code>rsync<\/code><\/strong> command.<\/p>\n\n\n\n<p>You can exclude unnecessary directories such as \/tmp, \/proc, \/dev, \/sys, e.tc<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rsync -avhP --exclude={\"\/dev\/*\",\"\/proc\/*\",\"\/sys\/*\",\"\/tmp\/*\",\"\/run\/*\",\"\/mnt\/*\",\"\/media\/*\",\"\/lost+found\"} \/mnt\/vda\/ \/mnt\/vdb\/<\/code><\/pre>\n\n\n\n<p>The rsync options, <code>-avhP<\/code> used are explained below:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>-a<\/code>: Stands for \"archive\" and is used to preserve the file attributes and permissions during the synchronization. It's a shorthand for several other flags like <code>-rlptgoD<\/code>.<\/li>\n\n\n\n<li><code>-v<\/code>: Stands for \"verbose\" and makes <code>rsync<\/code> display detailed information about the files being copied, which can be helpful for tracking progress.<\/li>\n\n\n\n<li><code>-h<\/code>: Stands for \"human-readable\" and makes the output more easily understandable for humans by using units like \"K\" (kilobytes), \"M\" (megabytes), etc.<\/li>\n\n\n\n<li><code>-P<\/code>: Combines two options:\n<ul class=\"wp-block-list\">\n<li><code>--progress<\/code>: Displays progress information during the transfer, including the percentage of completion.<\/li>\n\n\n\n<li><code>--partial<\/code>: Allows resuming partially transferred files.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>Once the data copying is done, you can confirm the sizes;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>du -hd1 \/mnt\/<\/code><\/pre>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>7.1G\t\/mnt\/vdb\n6.9G\t\/mnt\/vda\n14G\t\/mnt\/<\/code><\/pre>\n\n\n\n<p>Also try to verify permissions and ownerships;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ls -alh \/mnt\/vda\/home\/<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>total 12K\ndrwxr-xr-x  3 root root 4.0K Sep 19 19:45 .\ndrwxr-xr-x 19 root root 4.0K Sep 19 19:52 ..\ndrwxr-x---  4 1000 1000 4.0K Sep 19 19:59 kifarunix<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>ls -alh \/mnt\/vdb\/home\/<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>total 12K\ndrwxr-xr-x  3 root root 4.0K Sep 19 19:45 .\ndrwxr-xr-x 18 root root 4.0K Sep 19 19:52 ..\ndrwxr-x---  4 1000 1000 4.0K Sep 19 19:59 kifarunix<\/code><\/pre>\n\n\n\n<p>All seems good!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"install-grub-bootloader\">Install GRUB Bootloader<\/h3>\n\n\n\n<p>Now, it is time to install the GRUB bootloader on to the new disk. Proceed as follows;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mount --bind \/dev \/mnt\/vdb\/dev<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>mount --bind \/dev\/pts \/mnt\/vdb\/dev\/pts<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>mount --bind \/proc \/mnt\/vdb\/proc<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>mount --bind \/sys \/mnt\/vdb\/sys<\/code><\/pre>\n\n\n\n<p>If your system was on UEFI, then ensure that you mount the <strong><code>\/sys<\/code><\/strong> with <strong><code>--rbind<\/code><\/strong> option. This enables <strong><code>grub-install<\/code><\/strong> command to manipulate the EFI variables.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mount --rbind \/sys \/mnt\/vdb\/sys<\/code><\/pre>\n\n\n\n<p>Chroot into your system disk and install bootloader:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>chroot \/mnt\/vdb\/<\/code><\/pre>\n\n\n\n<p>Run the command below to install GRUB on the specified device's boot sector or EFI System Partition (ESP) if it's an EFI system;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grub-install \/dev\/vdb<\/code><\/pre>\n\n\n\n<p>If using GRUB 2, then update the command above accordingly.<\/p>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Installing for x86_64-efi platform.\nInstallation finished. No error reported.<\/code><\/pre>\n\n\n\n<p>If you get the warnings below;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>grub-install: warning: EFI variables cannot be set on this system.\ngrub-install: warning: You will have to complete the GRUB setup manually.<\/strong><\/code><\/pre>\n\n\n\n<p>Ensure <strong>\/sys<\/strong> is mounted with <strong>--rbind<\/strong> option.<\/p>\n\n\n\n<p>Verify GRUB installation;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grub-install --recheck \/dev\/vdb<\/code><\/pre>\n\n\n\n<p>Next, run the command below to generate the GRUB configuration file, <strong><code>grub.cfg<\/code><\/strong>. This configuration file contains information about the installed operating systems and their boot options.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>update-grub<\/code><\/pre>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>Sourcing file `\/etc\/default\/grub'\nSourcing file `\/etc\/default\/grub.d\/init-select.cfg'\nGenerating grub configuration file ...\nFound linux image: \/boot\/vmlinuz-5.15.0-84-generic\nFound initrd image: \/boot\/initrd.img-5.15.0-84-generic\nWarning: os-prober will not be executed to detect other bootable partitions.\nSystems on them will not be added to the GRUB boot configuration.\nCheck GRUB_DISABLE_OS_PROBER documentation entry.\nAdding boot menu entry for UEFI Firmware Settings ...\ndone\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"update-the-filesystem-table-fstab\">Update the Filesystem Table (FSTAB)<\/h3>\n\n\n\n<p>FSTAB (<strong>\/etc\/fstab<\/strong>) defines how storage devices and partitions are mounted into the file system hierarchy at system boot.<\/p>\n\n\n\n<p>First, let's get the old system fstab configurations (note that we are still within chroot!);<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep -vE \"^$|^#\" etc\/fstab<\/code><\/pre>\n\n\n\n<pre class=\"scroll-sz\"><code>\/dev\/disk\/by-id\/dm-uuid-LVM-awWizgiQ7tshV7Kq5lenkb7e9vSxyqCZvwOqKFNl6b4A6q08Fn17iBEq1xwurcqh \/ ext4 defaults 0 1\n\/dev\/disk\/by-uuid\/fb4ed301-cac4-491d-a5fa-00f9b1aa88ea \/boot ext4 defaults 0 1\n\/dev\/disk\/by-uuid\/8597-8981 \/boot\/efi vfat defaults 0 1\n\/swap.img\tnone\tswap\tsw\t0\t0\n<\/code><\/pre>\n\n\n\n<p>You have to update the device UUIDS to match your new device UUIDS.<\/p>\n\n\n\n<p>You can get UUIDs using <strong>blkid<\/strong> command;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>blkid | grep vdb<\/code><\/pre>\n\n\n\n<pre class=\"scroll-sz\"><code>\/dev\/vdb2: UUID=\"7bec7baf-5297-4c56-8f76-21c4a88181ce\" BLOCK_SIZE=\"4096\" TYPE=\"ext4\" PARTLABEL=\"ext4\" PARTUUID=\"38e2e473-f5b1-4aa6-ac83-168255158552\"\n\/dev\/vdb3: UUID=\"z7VMXq-SEwc-xfb9-k1mq-8HWy-hMaw-LjCnjm\" TYPE=\"LVM2_member\" PARTLABEL=\"ext4\" PARTUUID=\"18b0aaed-3b6c-4eca-9d71-a5ed39707e4d\"\n\/dev\/vdb1: UUID=\"A27D-CDB9\" BLOCK_SIZE=\"512\" TYPE=\"vfat\" PARTLABEL=\"fat32\" PARTUUID=\"cc769272-7d22-4c21-b80b-61054c4ac0c7\"\n\n<\/code><\/pre>\n\n\n\n<p>The update fstab as follows (<strong><em>we excluded swap. We can work on that later on<\/em><\/strong>).<\/p>\n\n\n\n<pre class=\"scroll-sz\"><code>cat > etc\/fstab &lt;&lt; 'EOL'\nUUID=z7VMXq-SEwc-xfb9-k1mq-8HWy-hMaw-LjCnjm \/ ext4 defaults 0 1\nUUID=7bec7baf-5297-4c56-8f76-21c4a88181ce \/boot ext4 defaults 0 1\n\/dev\/disk\/by-uuid\/A27D-CDB9 \/boot\/efi vfat defaults 0 1\nEOL\n<\/code><\/pre>\n\n\n\n<p>At this point, you are now almost done.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"exit-chroot-and-reboot-the-system\">Exit Chroot and Reboot the System<\/h3>\n\n\n\n<p>Exit the chroot environment and umount the \/dev, \/sys, \/proc directories;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>exit<\/code><\/pre>\n\n\n\n<pre class=\"scroll-sz\"><code>umount \/mnt\/vdb\/sys\numount \/mnt\/vdb\/proc\numount \/mnt\/vdb\/dev\/pts\numount \/mnt\/vdb\/dev\n<\/code><\/pre>\n\n\n\n<p>Shutdown the system and configure it to boot from the new disk. Detach the old disk and the bootable medium.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1018\" height=\"582\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/09\/boot-from-new-disk.png\" alt=\"\" class=\"wp-image-18836\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/09\/boot-from-new-disk.png?v=1695238804 1018w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/09\/boot-from-new-disk-768x439.png?v=1695238804 768w\" sizes=\"(max-width: 1018px) 100vw, 1018px\" \/><\/figure>\n\n\n\n<p>Power on the system and hold your breath! Check if the system boots;<\/p>\n\n\n\n<p>And voila!!<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1021\" height=\"484\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/09\/system-boot-after-migration.png\" alt=\"\" class=\"wp-image-18837\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/09\/system-boot-after-migration.png?v=1695238818 1021w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/09\/system-boot-after-migration-768x364.png?v=1695238818 768w\" sizes=\"(max-width: 1021px) 100vw, 1021px\" \/><\/figure>\n\n\n\n<p>Confirm disk usage;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>df -hT<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>root@u22:~# df -hT\nFilesystem                                Type   Size  Used Avail Use% Mounted on\ntmpfs                                     tmpfs  195M  1.2M  194M   1% \/run\n\/dev\/mapper\/ubuntu--vg--01-ubuntu--vg--01 ext4    19G  7.1G   11G  42% \/\ntmpfs                                     tmpfs  972M     0  972M   0% \/dev\/shm\ntmpfs                                     tmpfs  5.0M     0  5.0M   0% \/run\/lock\n\/dev\/vda2                                 ext4   974M  130M  777M  15% \/boot\n\/dev\/vda1                                 vfat   511M  6.1M  505M   2% \/boot\/efi\ntmpfs                                     tmpfs  195M  4.0K  195M   1% \/run\/user\/1000\n<\/code><\/pre>\n\n\n\n<p>And that is it! You have successfully shrinked your Linux root filesystem (<strong>110G to 20G<\/strong>) without damaging the system itself.<\/p>\n\n\n\n<p>That marks the end of our tutorial on how to shrink Linux root filesystem by migrating to new disk.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"other-tutorials\">Other Tutorials<\/h3>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/shrink-kvm-virtual-machine-lvm-partitioned-disk\/\" target=\"_blank\" rel=\"noreferrer noopener\">Shrink KVM Virtual Machine LVM Partitioned Disk<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/decrease-shrink-kvm-virtual-machine-disk-size\/\" target=\"_blank\" rel=\"noreferrer noopener\">Easy Way to Decrease\/Shrink KVM Virtual Machine Disk Size<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to shrink Linux root filesystem by migrating to new disk. Shrinking the Linux root filesystem can be necessary<\/p>\n","protected":false},"author":10,"featured_media":18839,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[121,49],"tags":[7246,7247,7248],"class_list":["post-18784","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","category-command-cheatsheets","tag-migrate-linux-to-new-disk","tag-move-linux-root-filesystem-to-new-disk","tag-shrink-linux-by-migrating-to-small-disk","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/18784"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=18784"}],"version-history":[{"count":25,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/18784\/revisions"}],"predecessor-version":[{"id":20847,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/18784\/revisions\/20847"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/18839"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=18784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=18784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=18784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}