[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[linux-lvm] Bringing an existing filesystem under LVM2 control



A question which came up again on irc last night was whether an existing
filesystem on a non-LVM partition can be turned into an LVM logical volume
without having to wipe the disk.

With LVM2, I think the answer is often 'Yes'.


N.B.  These instructions are untested.

Perhaps someone would be interested in trying them out and turning them 
into a script?


LVM2 needs to use for itself just one sector on the device (for a label).
By default this is the second sector - ext2 and ext3, for example, don't 
use this sector.  But some bootloaders might - check first.

You'll also need at least one other normal physical volume in your volume
group to hold the LVM2 metadata.  Not a problem if the reason for the
exercise is to expand the filesystem!  But it could be quite small.


Say the existing filesystem is on /dev/sda1.

1. Run:  pvcreate -M2 --metadatacopies 0 -Zn /dev/sda1

Do *not* miss the '-Zn' or you will wipe some of your data!
  -M2 is unnecessary if it's the default.

This command generates an LVM2 label and writes it into the second sector.


2.   If you're using an existing volume group, skip this step. 
Otherwise create the second PV in the normal way.  Say it's /dev/sdb (not
partitioned).

  Run:  pvcreate -M2 --metadatacopies 1 /dev/sdb
  -M2 is unnecessary if it's the default.
  --metadatacopies 1 is unnecessary if it's the default.
  (You can also use 2 but not 0.)


3. Determine the actual size of your existing filesystem, the actual size of
the disk or partition holding your filesystem and choose a suitable physical
extent size for the volume group.

The size of your new LV will be an exact multiple of this physical extent
size and needs to be at least as large as the filesystem.  

The size of your new LV needs to be no larger than the actual size of the
disk or partition that holds the filesystem.

  Disk/partition size >= New LV size >= filesystem size

In the example here, I'm going to use 4k, but normally you'll be able to use
a much larger size and you should do so.

You can use 'pvs -o+dev_size' to display the device size and use --units to
do divisions for you (see man page).

If you're using an existing volume group, use vgchange -s to reduce the
physical extent size if necessary and then run vgextend to add /dev/sda1 to
it.

Otherwise create a new volume group.
Let's say the volume group is called vg0.

  vgcreate -M2 -s 4k vg0 /dev/sdb /dev/sda1
  -M2 is unnecessary if it's the default.

Let's say you've calculated that the number of PEs in your new LV will be
2000.


4. Now the part we don't yet have a tool option for.  [This should be
handled with a new vgcreate/vgextend parameter that sets pe_start to 0 and
does the necessary calculations.] 

Edit /etc/lvm/backup/vg0 (generated by the vgcreate or the vgextend).

Find the physical volume section for /dev/sda1 and reduce pe_start to 0 and
increase pe_count to the number of physical extents you decided you needed
for your new LV in step 3, 2000 in this example.  (Typically you'll need to
add the original value of pe_start divided by pe_size expressed in 512 byte
sectors).

Apply your revised backup file with 'vgcfgrestore vg0'.


5. Create your new logical volume.
Important: don't forget -Zn or you'll wipe the start of your filesystem!

lvcreate -Zn -l2000 vg0 /dev/sda1


6. Finally run fsck on your new logical volume and check everything worked.


Alasdair
-- 
agk redhat com


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]