LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Blogs > iamswift
User Name
Password

Notices

Rate this Entry

RHEL Create new logical volume from root volume

Posted 10-10-2012 at 04:51 AM by iamswift
Updated 10-11-2012 at 01:01 AM by iamswift

Introduction:
We know that modern systems can have multiple hard disks and each hard disk can be divided as partitions. The idea behind partition is that it gives flexibility in managing the disks. Let’s say we have multiple operating systems to be installed in the same machine, partition provides solution for this. Each partition can have its own operating system. Apart from this advantage, partitions also help in grouping the type of files that are stored.
The Linux Logical Volume Manager (LVM) gives more flexibility in managing disks. In LVM, logical partitions can be spanned across different physical hard drives. The physical volumes can be combined into one big entity named “logical volume group”. Under this group we can have several logical volumes. These individual logical volumes can be considered a partition.
There are tons and tons of LVM references available online.
Getting around LVM:
A Typical disk structure is shown below. The entire disk space is segregated into two partitions:
One for boot partition and another for remaining of disk made such that to be controlled by LVM.

Code:
# fdisk –l
Disk /dev/sda: 146.8 GB, 146810536448 bytes
255 heads, 63 sectors/track, 17848 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device    Boot      Start      End      Blocks        Id   System
/dev/sda1   *           1          13      104391         83   Linux
/dev/sda2              14       17848   143259637+  8e   Linux LVM
#
The “lvs” command can be used to list all the logical volumes available under LVM. We have two logical volumes: One for root (Lv_root) where OS files are available, and another logical volume for swap (Lv_swap). (The logical volume name can be any word / sequence of chars but would be reasonable to name it meaningful)

Code:
# lvs
  LV           VG                 Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  Lv_root    VolGroup01  -wi-ao 128.69G
  Lv_swap  VolGroup01  -wi-ao  17.62G
#
In order to make use any disk, we need to first mount it. On bootup, default file systems are mounted on specified mount point (based on /etc/fstab entires). We use “df” command to list the current mount info. For the above setup, below is the output of “df” command.

Code:
# df -h
Filesystem            			Size  	Used 	Avail 	Use% 	Mounted on
/dev/mapper/VolGroup01-LogVol00	129G  	7G   	122G  	5% 	/
/dev/sda1              			99M   	67M   	28M  	72% 	/boot
tmpfs                 				7.8G     0  	7.8G   	0% 	/dev/shm
#

The Lv_root logical volume is mounted on “/”. This is root partition where OS is installed. This filesystem cannot be un-mounted because the OS that is currently using this disk. All other mount entries can be un-mounted.
Note: The swap logical volume (Lv_swap) is not listed in “df” output.
Creating new logical volume by splitting root partition:
We have seen that root filesystem cannot be un-mounted as the OS is currently running and using the file system. So how do we unmount that? We need another temporary OS to which we can boot to. For temporary OS, the root partition of original OS is just another mount point. The original OS’ root partition is not used by temporary OS. Once booted to temporary OS, the original root partition can be unmounted and can be split to create another new logical volume.

But in the above sample setup, we just have single OS and there is no space to install temporary OS. How and where can we install temporary OS? This is where the swap logical volume tends its helping hand. The swap partition is required for applications that need heavy memory usage but for OS booting and operations, swap is not required. We can make use of the swap partition to install a temporary OS.
Fortunately we have 17.62GB available in swap. For installing a minimal version of Linux (as temporary OS) we just need 3GB or max 5GB.

First step is to identify which partition / logical volume is used for swap.

Code:
# swapon –s
Filename                                		Type            Size    	Used    Priority
/dev/mapper/VolGroup01-Lv_swap         partition       18481144        0       -1
#
Next, we have to switch off the swap. Use the partition or logical volume name from above command.

Code:
# swapoff  /dev/mapper/VolGroup01-Lv_swap
In case a partition (instead of logical volume) is used for swap, the same can be used as well. In the above swapoff command, instead of providing logical volume path, provide the partition used for swap. For e.g: swapoff /dev/sda2
The swap partition is of the swapfs type. We cannot install Linux in swap file system. So the swap partition needs to be formatted in ext2/ext3 format.

Code:
# mkfs –t ext3 /dev/mapper/VolGroup01-Lv_swap
Once the swap partition is formatted as ext3 format, we can mount this to a dir (mount point) and can carry out the temporary OS installation.

Code:
# mkdir –p /tmp/tmproot/
# mount /dev/mapper/VolGroup01-Lv_swap  /tmp/tmproot/
Installing temporary OS in swap logical volume (or partition) can be done in several ways. A yum local repository can be setup where the minimal version of RHEL OS rpms can be placed and installed into swap partition. Another method is to use the “rpm” command and install temporary OS in swap partition. The advantage of “yum” is that the package dependency is resolved internally and the order of installing the package is taken care by “yum”.

To set local yum repository:
For the baseurl attribute, provide the path where the RPMs are available.

Code:
# cd /etc/yum.repos.d/
# echo “[tempos]” > tempos.repo		;# Create temp os repo 
# echo “name=Temp OS” >> tempos.repo
# echo “baseurl=file:///path/to/rpm/” >> tempos.repo
# echo “enabled=0” >> tempos.repo
Once yum local repository is set, install temporary OS in the swap partition which is now mounted in /tmp/tmproot/ dir.

Code:
# yum ––installroot=/tmp/tmproot ––enablerepo=tempos  groupinstall  Base Core –y ––nogpgcheck
Once the installation is done, it is necessary that few important files are copied to temporary OS installation so that when the temporary OS is booted, the parameters of original OS are available to temp OS as well.

Code:
# cp –p /etc/fstab  /tmp/tmproot/etc/
# cp –p /etc/passwd  /tmp/tmproot/etc/
# cp –p /etc/shadow  /tmp/tmproot/etc/
# cp –p /etc/group  /tmp/tmproot/etc/
Edit the fstab to mount correct filesystems.
The copied fstab should look something as below:
Code:
# cat /tmp/tmproot/etc/fstab

/dev/VolGroup01/Lv_root 	/               ext3    defaults        1 1
LABEL=/boot             	/boot           ext3    defaults        1 2
tmpfs                   	/dev/shm        tmpfs   defaults        0 0
devpts                  	/dev/pts        devpts  gid=5,mode=620  0 0
sysfs                   	/sys            sysfs   defaults        0 0
proc                    	/proc           proc    defaults        0 0
/dev/VolGroup01/Lv_swap 	swap            swap    defaults        0 0
The fstab entries are to be edited so that /dev/VolGroup01/Lv_swap logical volume is to be mounted root (/) partition. The swap partition is not required or available. Edit the /tmp/tmproot/etc/fstab as below (changes in red).

Code:
/dev/VolGroup01/Lv_swap 	/               ext3    defaults        1 1			 Edited
LABEL=/boot             	/boot           ext3    defaults        1 2
tmpfs                   	/dev/shm        tmpfs   defaults        0 0
devpts                  	/dev/pts        devpts  gid=5,mode=620  0 0
sysfs                   	/sys            sysfs   defaults        0 0
proc                    	/proc           proc    defaults        0 0
#/dev/VolGroup01/Lv_swap 	swap            swap    defaults        0 0			 Edited
Edit the /boot/grub/grub.conf to boot from Lv_swap logical volume.
Typical grub.conf:

Code:
# cat /boot/grub/grub.conf

default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux Server (2.6.18-246.el5)
        root (hd0,0)
         kernel /vmlinuz-2.6.18-246.el5 ro root=/dev/VolGroup01/Lv_root  rhgb quiet
        initrd /initrd-2.6.18-246.el5.img
Modified grub.conf to boot temporary OS (changes in red)

Code:
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux Server (2.6.18-246.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-246.el5 ro root=/dev/VolGroup01/Lv_swap rhgb quiet             Edited 
        initrd /initrd-2.6.18-246.el5.img
Reboot the system to boot from temporary OS.

Code:
# reboot
Once booted on to temporary OS, the original root logical volume is now available for any modification.

First check the original file system using fsck.

Code:
# e2fsck –f  /dev/VolGroup01/Lv_root

Shrink the root logical volume to desired size. From the “lvs” command run earlier, the size of the root partition is 128GB. Make be we can shrink it to half the size and make it as 64GB for original root partition and another 64GB for the new partition to be created.
The resize2fs command will shrink the logical volume capacity to the size mentioned.

Code:
# resize2fs   /dev/VolGroup01/Lv_root  64G

The logical volume ledger too is to be updated on the reduced size of Lv_root logical volume. This is done with “lvreduce” command.


Code:
# lvreduce –f –L 64G /dev/VolGroup01/Lv_root

Now the original root logical volume is reduced in size, a new logical volume can be created with the available space freed up. The below command creates new logical volume with name “Lv_root2” under “VolGroup01” volume group. The size of new logical volume is set 64GB.

Code:
# lvcreate –L 64G –n VolGroup01 Lv_root2

All the disk operations on original root partition is complete. The grub.conf is now edited back to previous form so that the original OS is booted.
Code:
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux Server (2.6.18-246.el5)
        root (hd0,0)
         kernel /vmlinuz-2.6.18-246.el5 ro root=/dev/VolGroup01/Lv_root  rhgb quiet
        initrd /initrd-2.6.18-246.el5.img

Reboot the system to boot from original OS.

Code:
# reboot
After booting to original OS, now we have an extra logical volume carved out of the root logical volume. We have installed temporary OS in the swap partition; now this is to be made swap again.

Format the swap partition to swap filesystem type.

Code:
# mkswap  dev/VolGroup01/Lv_swap

Enable the swap.

Code:
# swapon  dev/VolGroup01/Lv_swap

Check out the current logical volumes available using lvs command.

Code:
# lvs
  LV           VG                 Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  Lv_root    VolGroup01  -wi-ao  64G
  Lv_root2  VolGroup01  -wi-ao  64G					 New logical volume
  Lv_swap  VolGroup01  -wi-ao  17.62G
#
- Lakshmivaragan
HCL Technologies Ltd,
Chennai, India
lakshmivaraganm at hcl dot com
Posted in Technical
Views 884 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 11:31 PM.

Main Menu
Advertisement

My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration