Is it possible to on-line shrink a EXT4 volume with LVM?
As the message said, you can only grow an ext4 filesystem on-line. If you want to shrink it, you will need to unmount it first.
Sorry, on-line shrinking is not supported.
If the case is remote server without console, you can shrink filesystem using initramfs while rebooting. You need to add resize2fs to initramfs and run it before mounting root.
#!/bin/sh set -e PREREQS="" prereqs() < echo "$PREREQS"; >case $1 in prereqs) prereqs exit 0 ;; esac . /usr/share/initramfs-tools/hook-functions copy_exec /sbin/e2fsck copy_exec /sbin/resize2fs exit 0
#!/bin/sh set -e PREREQS="" prereqs() < echo "$PREREQS"; >case "$1" in prereqs) prereqs exit 0 ;; esac # simple device example /sbin/e2fsck -yf /dev/sda1 /sbin/resize2fs /dev/sda1 5G # see size info below /sbin/e2fsck -yf /dev/sda1 # complex device example # activate md-raid containing FS/PV #/sbin/mdadm -A /dev/md0 # activate VG containing FS #/sbin/lvm vgchange -ay vg0 # resize #/sbin/e2fsck -yf /dev/vg0/root # or /dev/md0 #/sbin/resize2fs /dev/vg0/root 5G #/sbin/e2fsck -yf /dev/vg0/root
Now run update-initramfs -u -k kernel-name to update initramfs for the kernel kernel-name which is set as default in your bootloader (grub). In case of boot failure — you can boot with another unmodified healthy kernel/initramfs. Or you can use all to update all kernels, but then you have no way back.
Check that you do not have any tools in your system that grow filesystem automatically (e.g. cloud-init — comment resizefs in /etc/cloud/cloud.cfg ).
Reboot. Check filesystem size. Remove initramfs scripts and run update-intiramfs -u -k kernel-name again.
Now you can shrink underlying device with parted resizepart / mdadm —grow —size= / lvresize -L . The size of the device should be bigger then the size of filesystem from the scripts. The simplest way to make no mistakes: if planned device size — 6G. set fs size in the script to 5G. after fs is resized — resize device to 6G, then grow fs to maximum online with resize2fs /dev/sda1 . Now your fs fits perfectly in your device.
For dracut (Fedora,CentOS) use the same logic (/usr/lib/dracut/modules.d).
This is very usefull for crypting or moving rootfs, with no free space available for the new partition.
How to shrink a Linux partition without losing data?
I used this method when I decided that I needed to re-partition my hard drive and create several new partitions to test different Linux distributions. However, I did not want to reinstall my current Arch Linux system. Instead, I shrank my Arch Linux partition and added new partitions using the free space I created. Below I share how I did that.
You can watch the video tutorial or continue reading below.
Make a GParted Live USB
The first step to shrink a partition in Linux is going to Gparted.org and download the GParted Live ISO.
Next, create a bootable USB using this ISO. You can do it from the terminal using the dd command:
sudo dd bs=4M if=gparted-live.iso of=/dev/sdX status=progress && sync
The if option is the path to GParted ISO, and the option of is the USB device where you will record the ISO. You can find out the name of your USB devise with sudo fdisk -l and the size of your USB.
Boot from GParted Live
Then, boot from this live USB drive. You can keep everything default.
Just press enter for every question.
In the end, you should get to the Live session of Gparted. This is the same Gparted program you may have on your system.
Shrink an existing partition
One advantage of Live USB Gparted is that your main system is not working and you can manipulate its partitions, while Gparted in a working system cannot resize a partition.
If you have several hard drives, make sure you selected the hard drive with the partition you want to resize in the top right part of the program window. In my case, I have one hard drive and one partition.
Below, you can see the yellow area is the part with the data. It is around 3 Gb in my case. The rest is free space. Let’s use this space to create another partition.
Select this partition and click on Resize option.
You can resize it using the numbers or by mowing the arrows. I will free up 10 Gb. Enter the number, press enter, and click on Resize.
After this, the main partition is shrunk.
Create an additional partition
Now, you can create another partition. Click on the unallocated space, then click New to create a new partition.
You can select its size, whether you want it to be primary or extended. An extended partition allows you to create more than 4 partitions. You can choose the file system you need. And you can also give a name to this partition. So, when you have set everything as you want, click Add.
In the below image, you can see the two partitions I have created.
Apply the GParted changes
Everything above created only the plan of the changes to do. You can find the detail of the changes you are about to apply at the bottom of Gparted.
So, if you notice that something is wrong, it is safe to cancel at this stage.
Also, let me warn you. Although Gparted is a reliable program, there is no guaranty that your data won’t be corrupted. So, before you resize your partition, back up all the important files.
If you have backed up all important data and you are sure about these settings, press apply.
Confirm and wait a few minutes until everything is complete.
Check that there were no errors.
Now, there are two partitions on this hard drive and your system files should be intact. Close the program, double-click on exit and reboot into your main system. It should work as before.
So, the process is complete.
Other options in GParted
As you can see, shrinking a partition in Linux is simple and reliable.
Moreover, you can use this Live GParted ISO not only to shrink a partition in Linux, but to do any other manipulations including deleting partitions, increasing partition’s size, and create a brand new partition table.
Have you used Gparted? Have you ever tried this method to create new partitions? Let me know in the comments.
Average Linux UserFollow I am the founder of the Average Linux User project, which is a hobby I work on at night. During the day I am a scientist who uses computers to analyze genetic data.