Software >> OS >> Unix >> Linux >> RHEL >> 7 >> How to migrate /var filesystem to another disk or partition


Important:Take a complete backup before starting this activity and test these steps in test environment first. Also, perform the below steps in a single-user mode.

In the following example, I am moving existing /var to /dev/sdb.

1. Check if the destination disk is presented to the server.


# lsblk
NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                 8:0    0   20G  0 disk
├─sda1              8:1    0    1G  0 part /boot
└─sda2              8:2    0   19G  0 part
  ├─rhel-root     253:0    0    8G  0 lvm  /
  ├─rhel-swap     253:1    0    1G  0 lvm  [SWAP]
  └─rhel-var      253:1    0    1G  0 lvm  /var
sdb                 8:16   0    5G  0 disk
sr0                11:0    1 1024M  0 rom  

2. Create LVM structure on the disk /dev/sdb


# pvcreate /dev/sdb

3. Create a volume group and a logical volume.


# vgcreate vg_var /dev/sdb
# lvcreate -n lv_var -l 100%FREE vg_var

4. Create file-system


# mkfs.xfs /dev/mapper/vg_var-lv_var

5. Mount the file-system and complete the following steps.


# mkdir /var1
# mount /dev/mapper/vg_var-lv_var /var1
# cd /var
# cp -dpRx * /var1/
# umount /var1
# cd ..
# umount /var
# mount /dev/mapper/vg_var-lv_var /var

6. Add an entry to /etc/fstab and restore the selinux context of /var

# echo "/dev/mapper/vg_var-lv_var  /var   xfs    defaults 0 0" >> /etc/fstab
# restorecon -vvFR /var

7. Make changes to the /etc/default/grub file as shown below.


Existing entry:
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet"

New entry:
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rd.lvm.lv=vg_var/lv_var rhgb quiet"


8. Changes to /etc/default/grub require rebuilding the grub.cfg file as follows:


On BIOS-based machines, issue the following command as root:

   # grub2-mkconfig -o /boot/grub2/grub.cfg


On UEFI-based machines, issue the following command as root:

   # grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg


9. Take backup of current initramfs file and rebuild initramfs image:


# cp /boot/initramfs-$(uname -r).img /boot/initramfs-$(uname -r).img.$(date +%m-%d-%H%M%S).bak
# dracut -v -f /boot/initramfs-$(uname -r).img $(uname -r)


10. Reboot the system


#
shutdown -r now