This discussion is archived
0 Replies Latest reply: Sep 2, 2012 8:44 PM by Dude! RSS

HOWTO: Converting LVM root to BtrFS in Oracle Linux 6

Dude! Guru
Currently Being Moderated
h1. Converting LVM root to BtrFS in Oracle Linux 6

By Dude, 31-Aug-2012

h2. 1. Summary

This document describes how to convert a Linux LVM (Logical Volume Manager) root volume to BtrFS. It outlines how to remove LVM by performing a backup and restore from USB storage media, and how to create a new root and swap partition using the GNU parted utility.

h2. 2. Disclaimers and Copyright

This document is presented under the terms of using the Oracle Web sites:

You may use this document as reference for the purpose of presentation and education, provided it is not redistributed under a different name or author, and available for free and not sold for profit. Any other unforeseen use will require permission. This information is presented in the hope that it will be helpful. Use at your own risk.

Copyright 2012, Dude,

h2. 3. Content

1. Summary
2. Disclaimers and Copyright
3. Content
4. Prerequisites
5. Instructions
6. Final
7. History

h2. 4. Prerequisites

- Oracle Linux 6.
- Oracle UEK2 (2.6.39) kernel.
- Boot partition.
- Red Hat Fedora 16 Live Desktop CD.
- USB storage media.

WARNING: Do not convert a root partition to BtrFS that is required by the Grub Bootloader in order to startup the system. Red Hat Enterprise Linux 6 (RHEL) and Oracle Linux 6 use Grub version 0.97, which does not support BtrFS. Support for BtrFS was added in Grub 1.99.

The OS installation automatically installs a separate boot partition by default, which allows converting the root volume to BtrFS. Use the following command to verify your installation:
su - root
parted -s /dev/sda print
For example:

*1 1049kB 525MB 524MB primary ext4 boot*
2 525MB 21.5GB 20.9GB primary lvm

Verify the OS and kernel version:
lsb_release -d; uname -r
For example:

Description: Oracle Linux Server release 6.3

Install the BtrFS utilities prior to performing the maintenance.
yum install btrfs-progs
NOTE: The incorrect use of disk partition utilities and LVM tools can result in the complete loss of data. If you have important data, please verify you have a valid backup of all data, in addition to the backup you will create following these instructions.

h2. 5. Instructions

a) Boot the system with the latest Fedora Live CD

Press the Tab key at the system start-up screen to modify the startup parameters. Add the number *3* to start the system in VGA text mode.

For example:

...quiet rhgb rd.luks=0 *3*

Login as root to enter the command-line prompt:

[root@localhost ~]#

You may find it useful to disable the virtual console screen-saver.
setterm -blank 0
Tip: Use the Shift-PageUp and Shift-PageDown keys to scroll the screen.

b) Analyze physical and logical LVM volumes
lvscan; pvs 
For example:

ACTIVE '/dev/vg_vm003/lv_root' [15.57 GiB] inherit
ACTIVE '/dev/vg_vm003/lv_swap' [3.94 GiB] inherit

/dev/sda2 vg_vm003 lvm2 a-- 19.51g 0

Note the following:

LVM volume group: vg_vm003
Disk partition: */dev/sda2*
LVM volumes in vg_vm003: lv_root 15.57 GB
lv_swap 3.94 GB


The LVM volume group vg_vm003 contains 2 logical volumes: lv_root, lv_swap.
The physical device is */dev/sda2*. It will be necessary to create a new root and swap partition.

c) Initialize the USB media

Attach the USB storage media. Watch the screen to obtain the device name.

For example:

[ 7390.948626 ] sd 3:0:0:0 [ *sdb* ] ...

You can also check /var/log/messages or use the blkid utility to identify devices.

WARNING: All previous data will be lost!
parted -s /dev/sdb mklabel msdos mkpart primary 1 -- -1
mkfs -t ext4 -L backup /dev/sdb1
d) Mount the USB media

Use the device partition created in the previous step.
mkdir /mnt/usb
mount /dev/sdb1 /mnt/usb
e) Verify/ Repair and mount the LVM root volume

Use the path of the root volume determined in step 5b.

fsck -fy $a
mkdir /mnt/root
mount $a /mnt/root
f) Verify available disk space

Verify the USB drive has enough capacity to store a backup of the LVM root volume.
df -h /mnt/usb /mnt/root
For example:

/dev/sdb1 7.6G 146M *7.1G* 2% /mnt/usb
/dev/mapper/vg_vm003-lv_root 16G *2.3G* 13G 16% /mnt/root

g) Backup the LVM root partition

Type the following commands exactly as shown:
cd /mnt/root
find . -type s > /$$
tar -cf - . -X /$$ | (cd /mnt/usb; tar -xf -; sync)
The backup may take a while, depending on the size of the root volume and the hardware.
There must not be any warnings or errors!

h) Remove the LVM root and swap volumes

Use the LVM volume information according to step 5b.

WARNING: All previous data will be lost!

umount /mnt/root
lvchange -a n $a
lvremove $a

swapoff -a
lvchange -a n $b
lvremove $b
i) Remove the LVM physical volume

Specify the disk partition determined in step 5b.
pvremove -ff /dev/sda2
Verify the physical volume has been removed.
No matching physical volumes found

j) Delete the disk partition

Delete the LVM disk partition according to the partition number shown in step 5b.
parted -s /dev/sda rm 2
k) Create new swap and root partition

Determine the start of the new swap partition using the size according to 5b.
parted -s /dev/sda unit MB print
1 1.05GB *525MB* 524MB primary ext4 boot

Note: In parted 1 GB = 1000 MB. Start and end values are disk locations.

lv_swap = *3.91 GB* x 1000 = 3910 MB
disk end = 3910 MB + 525 MB = *4435 MB*
parted -s /dev/sda mkpart primary linux-swap 525MB 4435MB
Create a new root partition using the remaining disk space:
parted -s /dev/sda mkpart primary 4435MB 100%
Verify the partitions:
parted -s /dev/sda print 
1 1049KB 525MB 524MB primary ext4 boot
2 525MB 4435MB *3910MB* primary
3 4435MB 21.5GB *17.0GB* primary

You can also verify the partition layout using the fdisk utility:
fdisk -l /dev/sda 
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 8663039 3891200 82 Linux-swap / Solaris
/dev/sda3 8663040 41943039 16640000 83 Linux

l) Initialize the swap and root partition

Use the swap partition created in the previous step.
mkswap /dev/sda2
Initialize the root partition using BtrFS:
mkfs.btrfs -L root /dev/sda3
The following link might be helpful if you plan to use BtrFS RAID:

m) Mount the root volume
mount /dev/sda3 /mnt/root
Verify the size of the root volume:
btrfs filesystem show /dev/sda3
Label: 'root' uuid: e126a0fe-b98a-4002-bb91-3e4b01aadc1e
Total device 1 FS bytes used 28.00KB
devid 1 size 15.87GB used 2.04GB path /dev/sda3

n) Restore the root volume
cd /mnt/usb
tar -cf - . | (cd /mnt/root; tar -xf -; sync)
There must be no errors or warnings!

In case of trouble, restart the system, mount the USB drive and root volume as previously outlined and restore the root volume again.

o) SELinux

SELinux requires a targeted policy re-label during the next system startup. A SELinux failure will abort the system startup showing the following error:
Respawning too fast. Stopped.
touch /mnt/root/.autorelabel
p) Correct the file system table

Edit */mnt/root/etc/fstab* and replace the existing root and swap entries. Be sure to use the correct partitions and do not modify the /boot partition.

In case you are unfamiliar with the Unix vi editor, use the sed utility:

cd /mnt/root/etc
cp fstab fstab.backup

sed -i "s:.*$a.*:$b swap swap defaults 0 0:g" fstab
sed -i "s:.*$x.*:$y / btrfs defaults 1 1:g" fstab
Verify the correct content:
cat fstab
For example:

/dev/sda3 / btrfs defaults 1 1
/dev/sda2 swap swap defaults 0 0

q) Mount the boot partition

Use the boot partition determined in previous steps.
mkdir /mnt/boot
mount /dev/sda1 /mnt/boot
r) Correct the Grub bootloader configuration

Edit */mnt/boot/grub/grub.conf* and correct the entries of previous LVM volumes.

Be sure to use the LVM group name according to 5b and use the correct partitions.

For example:

Remove: rd_LVM_LV=vg_vm003/lv_root
Remove: rd_LVM_LV=vg_vm003/lv_swap

Change: root=/dev/mapper/vg_vm003-lv_root
to: root=/dev/sda3

Or use the sed utility:


cd /mnt/boot/grub
cp grub.conf grub.conf_backup

sed -i "s:rd_LVM_LV=$a/$b::g" grub.conf
sed -i "s:rd_LVM_LV=$a/$c::g" grub.conf
sed -i "s:root=/dev/mapper/$a-$b:root=$root:g" grub.conf
s) Restart the system

Dismount all devices:
umount /mnt/usb
umount /mnt/root
umount /mnt/boot
Remove the USB drive and CD media.

Reset the system.

h2. 6. Final

After a system restart, login as root and verify the new system configuration.

Verify the disk partitions:
parted -s /dev/sda unit s print
For example:

1 2048s 1026047s 1024000s primary ext4 boot
2 1026048s 8663039s 7636992s primary linux-swap(v1)
3 8663040s 41943039s 33280000s primary

fdisk -clu /dev/sda
For example:

/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 8663039 3818496 82 Linux swap / Solaris
/dev/sda3 8663040 41943039 16640000 83 Linux

Use the fdisk -c parameter to switch off DOS-compatibility mode.

Verify swap space:
swapon -s
For example:

/dev/sda2 partition     3891196     0     -1

Verify BTRFS root filesystem:
btrfs filesystem show /dev/sda3
For example:

Label: 'root' uuid: e126a0fe-b98a-4002-bb91-3e4b01aadc1e
     Total devices 1 FS bytes used 2.04GB
     devid 1 size 15.80GB used 5.04GB path /dev/sda3

Btrfs Btrfs v0.19

h2. 7. History

31-Aug-2012: Created

Best of luck!


  • Correct Answers - 10 points
  • Helpful Answers - 5 points