WARNING: these operations is extremely risky, I give no warranty about it, it is recommended that you understand every single step and apply it at your own risk, if you don't you are probably gonna break your system and I am not liable for any damages arising from its use.
I have cloned all server disks on a local virtual machine on my workstation in order to try the commands without affecting my original server, if you are unsure you can do the same. After a extensive research and several experiments I finally found a solution.
Some systems uses systemd on initrd, after mounting the target root it switch the systemd environment the same way you would do with chroot, all services are shutdown and then restarted on the new environment.
We could take advantage of this feature to change the root again, doing so on a cloned environment on other disk will release the locks the system holds on the older environment, so you could safely umount these points - in fact it mostly will not even be mounted. But this cloned environment should guarantee the correct configuration to bring network up again and give ssh access. If it does not, consider yourself doomed.
It is important that you have a separated partition, once if you chroot to a directory inside other partition instead of a device itself the system can still hold the parent partition's device, for this use case we should consider it is intended it does not block any.
If you have unallocated free space, or you are able to shrink some partitions in order to have space for the cloned environment it is recommended. However if you does not have free storage space you can create a virtual memory mount point if you have enough memory. A network partition is not recommended once the connection can get lost during the procedure.
A virtual memory mount point can be done by
tmpfs, but it will cause you to have this memory locked on the older environment such a way you are unable to free it without a
kexec, a better approach is to use
zram, besides creating a memory device, it compress the data, so if you write
5G of data, it will in fact use much less than that of memory, but still, you will require enough memory.
For instance, if
7G is enough you can create it as following:
sudo modprobe zram num_devices=4
echo 7G | sudo tee /sys/block/zram0/disksize
sudo mkfs.ext4 -m0 /dev/zram0
After having provisioned the required partitions for the cloned environment here how it is going to work:
sudo mount /dev/zram0 /tmp/sys # or the target device in place of zram0
# mount other separate partitions if required
sudo tar -cpSf - \
--acls --xattrs --selinux \
--exclude '/dev/*' \
--exclude '/run/*' \
--exclude '/sys/*' \
--exclude '/proc/*' \
--exclude '/tmp/*' \
--exclude '/var/tmp/*' \
--exclude '/var/run/*' \
sudo tar -xvf - \
--acls --xattrs --selinux \
It is recommended to backup your
fstab before performing the switch:
sudo cp -a /tmp/sys/etc/fstab /tmp/sys/etc/fstab-
sudo truncate -s0 /tmp/sys/etc/fstab
If you intend to change the partition where some device is a
swap memory it is recommended to disable it first:
sudo swapoff -a
Then, to perform the chroot:
sudo mkdir /sysroot
sudo mount --rbind /tmp/sys /sysroot
sudo touch /etc/initrd-release
sudo systemctl --no-block isolate initrd-switch-root
# it will stop all other services (isolate) and call systemctl switch-root /sysroot
Note that it is not required to bind
dev as you normally do when performing a chroot, the systemd will do it for you. You may lost connection, if you wait some time and it still be unable to connect you have my condolences, you have been wasted.
Though if you were lucky and be able to connect now you can perform the partition table changes you desire.
It is important to adjust the devices path and uuid (
blkid) to match the new ones:
sudo mv /etc/fstab- /etc/fstab
sudo vi /etc/fstab
sudo vi /etc/default/grub
After you have done your changes, you can do the same strategy to switch back the real root device, once done that do not forget to install the bootloader for the new environment in order to make it up again in case of a shutdown:
sudo grub2-install /dev/sda # or the intended bootable disk
sudo grub2-mkconfig -o /etc/grub2.cfg
Once you have switched back to your physical devices, if you were using
zram you can release the memory by following:
echo 1 | sudo tee /sys/block/zram0/reset
sudo modprobe -r zram
Remember, do it at your own risk.