Move /var to a new partition in VM
2021-Jan-19 • Tags: fedora
Well if there's not enough planning in building a resource it'll eat away our productive time when it fails or doesn't work as anticipated. In the current context the effort in migrating /var partition might be less but if not done properly may result in data loss!
Current setup before migrating /var to new partition:
1 |
2 PRETTY_NAME="Fedora 32 (Server Edition)"
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
As you can infer from above there's no separate mount for /var and currently is part of /, however I intend to move /var to a different partition for future use. Being a VM running on KVM host below are the steps that'll be followed in brief:
- Create a
qcow2image and attach to the VM - Create and mount
xfsfilesystem on newly added disk,rsyncexisting/varto newly mounted disc - Add details in
/etc/fstabto mount/varon reboot
1. Attach disk to VM §
On a sidenote, if performing partioning on an existing disk please follow this guide. Back to our scenario, create and attach a new disk to the guest machine and perform below on KVM machine
1 # Change below vars to your needs
2
3
4
5
6
7 # Get KVM default pool, if using some fancy directoy name please assign to 'pool_path' directly
8
9
10 # Create 'qcow2' image with required size
11
12
13 # Attach newly created disk to VM
14
Verify the disk is created and attached to the guest (Fedora) machine
1 # On GUEST Machine verify disk is recognized
2
3
4
5
6
7
8
9
2. Create a file system on the disk §
After adding disk to VM, you can follow this guide until creation of filesystem, cause in our scenrio we are creating an xfs filesystem. Perform mkfs.xfs /dev/vg1/lv1 on host VM and a new filesystem will be created on the disk.
If incase you want to use already existing disk and create xfs on it, wipe (WARNING: potential data loss) existing file system headers by running wipefs -a <disk> and create new filesystem on that.
1 # On Guest (VM) verify volume group is created
2
3
4
5
6
7
8
9
10
*If you wish not to go through all the hassle of creating LVM, you can simply run mkfs.xfs on new disk.
3. Sync /var to new disk
§
We will mount new disk and to avoid any data writes we'll be dropping to runlevel 1 and perform rsync data of /var to mounted disk
1
2
3
4 # Drop to single user mode
5
6
7 # Check the runlevel and take note of last runlevel
8
9
10
11 # rsync (or 'cp' also would work the same) 'var' contents to '/mnt/new-dir'
12
13
14 # Rename '/var'
15 &&
16
17 # Take note of UUID of LVM
18
19
20 # Add entry in '/etc/fstab'
21
22
23 # Unmount /mnt/new-dir and mount '/var' as mentioned in '/etc/fstab'
24
25
26
27 # IMP: Restore SELinux labels on newly mounted partition
28
29
30 # It's best to reboot the server to ascertain no errors
31 # Go to multi user mode (revert to last runlevel from line #9) and reboot the server
32 33
If everything checks out well, server should reboot without any issues with /var mounted as per /etc/fstab entries.
Verification and minimal troubleshoot §
After server restarts mount | grep var should be successfull, df -h should list /var being mounted on different filesystem and lsblk stating /var on lvm
Below is the info after migration:
1 |
2 )
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
If for some reason the server didn't come up as expected, login to VM via console connection (virt-manager) from KVM and perform below:
1 # Backup and remove newly added entry in /etc/fstab
2
3
4
5 # Unmount '/var' and rename old directory (from previous step) to '/var'
6 &&
7
8 # Reboot server and compare info against before migration, it should match
9
Many services use /var for log messages and storing data apart from configs, it's always a good idea particulary in server environments to have a healthy amount of free space available in /var directory.
All above steps are performed and verified on Fedora 32 Server Edition as part of making space for container images and related volume mounts.
Send an email for any comments. Kudos for making it to the end. Thanks!