====== Converting a machine to an LVM root FS ====== ===== IMPORTANT =====
This topic explains how to modify the root file system and repartition the disk of a running system. These are dangerous activities and if not performed correctly, can result in the loss of data and/or an unbootable machine. You are recommended to test the following guide on a test machine (perhaps a virtual server you can create on your own network) before risking a system with valuable data on it, or which would be difficult to re-install if it becomes unbootable.
You should DEFINITELY have a backup of the system you plan to do this to, although if you don't keep good backups as standard procedure, you probably shouldn't be dealing with this level of system administration, until you've got some basics put in place.
# update-initramfs -u
# reboot
When it comes back up, the **df -h** command should show that the root file system is now much smaller than it used to be (and will therefore have a higher usage percentage).
Once the machine has rebooted, the next step is to reduce the size of the partition in which the (now smaller) root file system resides, so that there is unallocated space on the disk, which we can later use for LVM.
**fdisk /dev/sda** (adjusted for whatever your root file system device name is) will probably show a single partition **sda1** which is the same size as your root FS used to be, and might also have another partition for swap. If it's any more complicated than that, you are thoroughly recommended to create a matching setup on a virtual server and try out these instructions on that in order to be sure that you understand what you're doing and how to adapt these instructions to your starting setup.
In **fdisk** you need to:
- delete the partition holding the root FS
- create a new partition with the same number, and starting at the same sector, and at least 10% larger than the size you made the root FS itself in the previous step (which, if you recall, was at least 10% larger than **df -h** told you the root FS was actually using).
- Just to be very clear about that, take the size **df -h** told you the root FS was using to start with, add 10% and shring the root FS to that size (above) then add __another__ 10% and make that the size of the new partition you are creating now
- you will almost certainly be told that **Partition #1 contains a ext4 signature** and asked **Do you want to remove the signature?**
- You **do not** want to remove the signature.
- write the changes to disk
A small adjustment to the initramfs script we created earlier will now __increase__ the size of the root FS to occupy precisely the amount of space available in this new partition.
Edit the script you created in /etc/initramfs-tools/scripts/local-premount and remove the value of the **size** variable (leave it saying simply **size=""**). Then update the initramfs with the modified script and reboot the server again:# update-initramfs -u
# reboot
When the machine restarts, **df -h** should tell you that the root FS is now about 10% bigger than it was after the previous reboot, and this means that it is now using the entire space available in the (much smaller than previously) partition you created.
We have now finished with the script added to the initramfs, so remove the files you created and rebuild the initramfs:# rm /etc/initramfs-tools/scripts/local-premount/resize /etc/initramfs-tools/hooks/copyfile
# update-initramfs -u
The next step is to use the now-unallocated disk space to create an LVM partition and create some Logical Volumes in it.
==== Create a new partition for LVM ====
If LVM is not installed (which is likely, given that you're weren't previously using it), install the **lvm2** package:# aptitude install lvm2
Then create a new disk partition using **fdisk /dev/sda** (again, adjust the device name as appropriate for your system) and create a new partition of type primary using all the available space which now follows the smaller root partition you worked on earlier. Make the partition type **8e** (for LVM).
You can use the new partition immediately:# pvcreate /dev/sda2
# vgcreate System /dev/sda2
# lvcreate -L 20G -n root System
Adjust **/dev/sda2** to be the partition you just created for LVM to use, substitute whatever name you want to use for your Logical Volume Group in place of **System** and adjust **20G** to be an appropriate size for the new (LVM-based) root partition, __bearing in mind__ that /home, /var and any other partitions you choose to create will occupy __their own space__ and therefore the space taken up by these does __not__ need to be included in the size you make **root**.
Continue to use **lvcreate** to set up all the partitions you want to have on your new system. Common choices might be:
* /
* /home
* /var
* /var/log
You can also create a swap partition in LVM if you wish.
==== Create file systems in the new partitions ====
Once you have created all the partitions you want in LVM, format each of them. For standard EXT file systems, use# mkfs.ext4 -L root /dev/System/root
etc.
This will format an EXT4 file system with a label (whatever follows **-L**) which you can then use in **/etc/fstab** to ensure the right system get mounted on the correct mount points.
If you made an LVM swap partition, format that too with# mkswap -L swap /dev/System/swap
Once all the partitions are formatted, mount them so that the system itself can be copied across:# mount /dev/System/root /mnt
# mkdir /mnt/home
# mount /dev/System/home /mnt/home
# mkdir /mnt/var
# mount /dev/System/var /mnt/var
# mkdir /mnt/var/log
# mount /dev/System/varlog /mnt/var/log
# rsync -Pavx / /mnt
Note that the **-x** parameter to **rsync** tells it not to cross partition boundaries, so it will not, for example, try to copy the directory tree which is now under /mnt into /mnt/mnt etc. If **/boot** happened to be a separate partition on your old system, make sure that gets copied as well:# rsync -Pav /boot /mnt
==== Switch to the new file system and tell GRUB to boot into it ====
Now you have a copy of the running system's file systems in the new LVM partitions, and everything is mounted under /mnt.
To get GRUB to use this next time you reboot, **chroot* into the new file system and update /etc/fstab, then update GRUB:# mount --bind /dev /mnt/dev
# chroot /mnt
# mount -t proc /proc /proc
# mount -t sysfs /sys /sys
# vi /etc/fstab
You want **/etc/fstab** to contain the new mount points and the corresponding labels for the Logical Volumes:#
LABEL=root / ext4 errors=remount-ro 0 1
LABEL=home /home ext4 errors=remount-ro,nodev,nosuid 0 2
LABEL=var /var ext4 errors=remount-ro,nodev,nosuid 0 2
LABEL=varlog /var/log ext4 errors=remount-ro,nodev,nosuid,noexec 0 2
The mount options shown for the non-root partitions match the recommendations from [[https://github.com/ovh/debian-cis|the security hardening script]] I was using on this system.
Once that is done, tell GRUB to boot into the LVM system and not the old partition:# update-grub
# grub-install /dev/sda
# umount /sys
# umount /proc
# exit
# reboot
Once the machine reboots, check that the root file system is mounted from LVM:# df -h /
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/System-root 20G 1.5G 3.2G 8% /
===== Congratulations =====
You have now converted the machine from having a single disk partition with the entire file system inside it, into a machine with as many separate LVM partitions as you like, and you've confirmed that it boots correctly.
Everything has been done remotely with no requirement to boot from a rescue system or to log in to the console.
----
[[.:|Go up]]\\
Return to [[:|main index]].