I have been given a box that has /dev/mapper/root mounted as / and mapped to /dev/dm-0. It has /dev/mapper/home mounted as /home and mapped to /dev/dm-2. The dm-0 device is 5G and the dm-2 device is 2T. I've been asked to swap them: make / 2T and /home 5G. I have resized, deleted, and added logical volumes. Is there a way to swap logical volumes?

In case it matters... They want to use this as a database server. The database is in /var/lib/mysql, which is under /, which only has 5G and is already 80% full. /home is 1% full.

  • There's no easy way to swap volumes just like that, you'll have to repeat what you have already done: resize, delete, add, etc. Btw, if the 2TB /home is currently 1% full, that's still 20GB and too big for the 5GB /home you're aiming at. – ckujau Mar 11 '16 at 21:02
  • Thanks. I've made a proposal that they let me simply place the database in /home instead of /var/lib. No mucking around and it will just work. Hopefully they see the light. – kainaw Mar 11 '16 at 23:16
  • 1
    the "traditional" way of doing this is to move and symlink. e.g. mkdir -p /home/var/lib, do whatever your init requires to stop mysql, mv /var/lib/mysql /home/var/lib/, ln -s /home/var/lib/mysql/ /var/lib and then restart mysql. Alternatively, paranoia dictates that it is safer to cp or rsync /var/lib/mysql to /home/var/lib/, then rename /var/lib/mysql to /var/lib/mysql.old and then create the symlink. When you're sure it's working OK, you can delete /var/lib/mysql.old...this avoids the (tiny) risk that the mv operation might fail part-way through, leaving you a mess to clean up. – cas Mar 12 '16 at 0:33
  • in either case, whether copying/rsyncing or moving, mysql MUST be stopped while the transfer is in progress. – cas Mar 12 '16 at 0:35

There's no direct way to swap two devices, but this isn't what you need anyway. Keep root as / and home as /home, shrink home to the desired smaller size, and enlarge root using the space that was freed up.

Depending on the filesystems, you may or may not need to unmount them to resize them. Ext4 filesystems can be enlarged while mounted, but not shrunk; use the command resize2fs. Btrfs filesystems can be resized in either direction while mounted.

To shrink an LVM logical volume, use lvreduce. Make sure that you don't accidentally shrink it smaller than the filesystem — this is the one step here where an incorrect manipulation can cause data loss (and it's very likely to do so if you get the number wrong). Be careful, with LVM, lowercase suffixes (k, m, g, …) use powers of 1024, but uppercase suffixes (K, M, G, …) use powers of 1000. To enlarge an LVM logical volume, use lvextend.

For example, with ext4:

umount /home
resize2fs /dev/mapper/home 5G
lvreduce -L 5g /dev/mapper/home    # note lowercase g
lvextend 2t /dev/mapper/root
resize2fs /dev/mapper/root

Note that if /home is really 2TB and is 1% full, it won't fit in 5GB. Adjust sizes accordingly.

Alternatively, you may want to move the database to a filesystem of its own. It's often a good idea to use separate volumes for the operating system and for large datasets, because they can have different storage policies (regarding redundancy, performance, backups, etc.). If that server is only a database server, it would make sense for it to have two filesystems: / (including the /home directories which don't contain much more than the administrators' configuration files) and /var/lib/mysql.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.