Movin g /var/log/ Article Number: 473 Rating: Unrated Last Updated: Tue, Mar 29, 2016 at 5:56 PM O ve r vie w This KB article will walk you through the steps of moving the /var/log directory to a new disk/volume in your server. This is sometimes necessary to free up disk space on a volume that cannot have extra disk space easily added. This guide was performed on a VM running Nagios XI 5.2.5, however it can be applied to any of the Nagios products as the steps are not specific to XI. This guide has separate steps for CentOS 6.x and 7.x as there are subtle differences in the operating system versions. Here is a quick outline of what will be performed: View existing disks Configure OS for single user mode Add new disk to VM Prepare the new disk Move existing /var/log to new location Configure new disk to mount on boot Configure OS in multi user mode and reboot Final check Wa rning This guide requires multiple reboots of your s erver. C e nt O S 6.x First establish an SSH session to your server as the user root. Vie w Exis t ing Dis ks This will show the existing disks: /dev/sda /dev/sda1 /dev/sda2 Here you can see there is currently only s d a (sda1 and sda2 are partitions on s d a ). You also need to make sure the lvm2 programs are installed. yum -y install lvm2 Wait while the program is downloaded and installed. C o nf igure OS Fo r Single Us e r Mo de
First we are going to configure the operating system in single user mode. This allows us to boot the OS with nothing running, this will prevent any issues from occurring while making these changes. Wa rning Fro m this po int o n, yo ur s erver will no t perfo rm any mo nito ring until we finis h and put the s erver bac k in multi us er mo de. sed -i 's/^id:3:/id:1:/g' /etc/inittab There will be no output from this command. Add Ne w Dis k To VM Now we are going to shutdown the VM. Wa rning O nc e yo u s hutdo wn the VM, yo u will no lo nger have netwo rk c o nnec tivity to it (bec aus e we are in s ingle us er mo de). O nc e yo u have po wered o n yo ur VM, all c o mmands will need to be do ne o n the c o ns o le o f the VM. shutdown -h now Once the server has shutdown, using your Hypervisor, add the new disk to the VM. In this example I will be adding a 100GB disk. Power on the VM and wait for it to boot up, once booted it will look something like: Pre pare Ne w Dis k This will now show the new /dev/sdb disk: Now you will use fdisk to create a new partition: fdisk /dev/sdb The screenshot below shows the following commands you need to type: Command: n (primary partition): p
(partition number): 1 For the first and last cylinder accept the defaults by pressing Enter Command: t (hex code): 8e Command: w Now that the disk has been partitioned, the next step is to create the volume. This will now show the new /dev/sdb1 partition: The following steps will create a vo lume g ro up (vg_log) and a lo g ic a l vo lume (lv_log). vgcreate vg_log /dev/sdb1 lvcreate -l 100%VG -n lv_log vg_log The following steps will define the volume with an e xt4 filesystem. mkfs -t ext4 /dev/vg_log/lv_log
Finally we'll temporarily mount the new volume so we can use it and check it has been mounted by listing the directory. mkdir /mnt/tmp_vol mount -t ext4 /dev/vg_log/lv_log /mnt/tmp_vol/ ls -la /mnt/tmp_vol/ Mo ve Exis t ing /var/lo g To Ne w Lo c at io n Now we'll move the existing files in /var/log/ to the new volume. mv /var/log/* /mnt/tmp_vol/ Depending on the amount of files will depend how long this takes. If everything goes well, there will be no errors output on the screen. Once finished, check to see if there are any files remaining: ls -l /var/log Just to be sure, check the new volume for the files: ls -l /mnt/tmp_vol
This should output a bunch of files and folders. C o nf igure Ne w Dis k To Mo unt On Bo o t First we will un-mount the volume: umount /mnt/tmp_vol Next we add an entry to the /etc/fstab file to mount this volume to /var/log on bootup: echo /dev/vg_log/lv_log /var/log ext4 defaults 0 0 >> /etc/fstab Now reload the fstab file to mount the volume: mount -a Check the files and folders to see it mounted ok: ls -l /var/log/ If you see the same files and folders as you did after moving the files everything is OK. C o nf igure OS Fo r Mult i Us e r Mo de And Re boot Finally we are going to configure the operating system in multi user mode. sed -i 's/^id:1:/id:3:/g' /etc/inittab There will be no output from this command. Now reboot: reboot Final C he c k Once the server has rebooted, establish an SSH session to your server as the user root.
df -h Here you'll see the disk has been mounted and is now /var/log with ample free space: This completes moving /var/log to a new disk drive on CentOS 6.x. C e nt O S 7.x First establish an SSH session to your server as the user root. Vie w Exis t ing Dis ks This will show the existing disks: /dev/sda /dev/sda1 /dev/sda2 Here you can see there is currently only s d a (sda1 and sda2 are partitions on s d a ). You also need to make sure the lvm2 programs are installed. yum -y install lvm2 Wait while the program is downloaded and installed. C o nf igure OS Fo r Re s c ue Mo de First we are going to configure the operating system in rescue mode. This allows us to boot the OS with nothing running, this will prevent any issues from occurring while making these changes. Wa rning Fro m this po int o n, yo ur s erver will no t perfo rm any mo nito ring until we finis h and put the s erver bac k in multi us er mo de.
systemctl set-default rescue.target The following output will be shown: Removed symlink /etc/systemd/system/default.target. Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/rescue.target. Add Ne w Dis k To VM Now we are going to shutdown the VM. Wa rning O nc e yo u s hutdo wn the VM, yo u will no lo nger have netwo rk c o nnec tivity to it (bec aus e we are in s ingle us er mo de). O nc e yo u have po wered o n yo ur VM, all c o mmands will need to be do ne o n the c o ns o le o f the VM. shutdown -h now Once the server has shutdown, using your Hypervisor, add the new disk to the VM. In this example I will be adding a 100GB disk. Power on the VM and wait for it to boot up, once booted it will prompt you for the root password: Pre pare Ne w Dis k This will now show the new /dev/sdb disk: Now you will use fdisk to create a new partition: fdisk /dev/sdb The screenshot below shows the following commands you need to type: Command: n (primary partition): p (partition number): 1 For the first and last cylinder accept the defaults by pressing Enter Command: t (hex code): 8e Command: w
Now that the disk has been partitioned, the next step is to create the volume. This will now show the new /dev/sdb1 partition: The following steps will create a vo lume g ro up (vg_log) and a lo g ic a l vo lume (lv_log). vgcreate vg_log /dev/sdb1 lvcreate -l 100%VG -n lv_log vg_log The following steps will define the volume with an e xt4 filesystem. mkfs -t ext4 /dev/vg_log/lv_log
Finally we'll temporarily mount the new volume so we can use it and check it has been mounted by listing the directory. mkdir /mnt/tmp_vol mount -t ext4 /dev/vg_log/lv_log /mnt/tmp_vol/ ls -la /mnt/tmp_vol/ Mo ve Exis t ing /var/lo g To Ne w Lo c at io n Now we'll move the existing files in /var/log/ to the new volume. mv /var/log/* /mnt/tmp_vol/ Depending on the amount of files will depend how long this takes. If everything goes well, there will be no errors output on the screen. Once finished, check to see if there are any files remaining: ls -l /var/log
Just to be sure, check the new volume for the files: ls -l /mnt/tmp_vol This should output a bunch of files and folders. C o nf igure Ne w Dis k To Mo unt On Bo o t First we will un-mount the volume: umount /mnt/tmp_vol Next we add an entry to the /etc/fstab file to mount this volume to /var/log on bootup: echo /dev/vg_log/lv_log /var/log ext4 defaults 0 0 >> /etc/fstab Now reload the fstab file to mount the volume: mount -a Check the files and folders to see it mounted ok: ls -l /var/log/ If you see the same files and folders as you did after moving the files everything is OK. C o nf igure OS Fo r Mult i Us e r Mo de And Re boot Finally we are going to configure the operating system in multi user mode. systemctl set-default multi-user.target Now reboot: reboot
Final C he c k Once the server has rebooted, establish an SSH session to your server as the user root. df -h Here you'll see the disk has been mounted and is now /var/log with ample free space: This completes moving /var/log to a new disk drive on CentOS 7.x. Fina l Tho ught s This guide can easily be adapted to move any folder location to another disk. For any support related questions please visit the Nagios Support Forums at: http://support.nagios.com/forum/ Posted by: tle a - Tue, Feb 23, 2016 at 11:04 PM. This article has been viewed 5475 times. Online URL: https://support.nagios.com/kb/article/moving- var- log- 473.html