- save the script below to a file on the root filesystem
- reboot into bash init=/bin/bash
- execute the script
The script will exit if any of the steps fail
#!/bin/bash
fail_notify() {
echo "step "$@" on line $BASH_LINENO failed <><><><><><><><><><><><><><><"
exit
}
grep 'vgsys-var_log[[:blank:]]' /etc/fstab
if [ $? == 0 ]
then echo "/var/log filesystem already exists"
else
start_udev || fail_notify "start_udev"
mount -o rw,remount / || fail_notify "remount of root"
vgchange -ay vgsys || fail_notify "activate vgsys"
lvchange -ay /dev/vgsys/var || fail_notify " activate lv var"
vgscan --mknodes -v || fail_notify "creating device nodes"
lvcreate vgsys -L 2G -n var_log || fail_notify "lvcreate"
mkfs.ext4 /dev/vgsys/var_log || fail_notify "mkfs"
mkdir /tmp/var_log # no test as the directory may already exist if rerunning script
mount /dev/vgsys/var_log /tmp/var_log || fail_notify "mount"
mount /var || fail_notify "mount var"
cd /var/log/ || fail_notify "cd /var/log "
echo "copying files from /var/log/ to new file system /tmp/var_log/"
find . -mount -print0 | cpio -0dump /tmp/var_log/ || fail_notofy "find"
# need to put this line before /var/log/audit mount point otherwise the the file systems will overlap
sed -i '/vgsys-var_log_audit/i \/dev\/mapper\/vgsys\-var\_log\ \/var\/log\ \ ext4 \ nodev\,nosuid\,noexec \ 1\ 2' /etc/fstab || fail notify " fstab update"
df # display a df to the user so they can check what has happened
echo "remounting filesystems as read only"
mount -o ro,remount /tmp/var_log || fail_notify "remount of var_log"
mount -o ro,remount /var || fail_notify "remount of var"
mount -o ro,remount / || fail_notify "remount of /"
echo "+--------------------------+"
echo "| power cycle vm to finish |"
echo "| or execute "exec init 6" |"
echo "+--------------------------+"
fi