Tuesday, 25 June 2013

Converting lvm to a normal partition

Converting lvm to a normal partition I've recently set up a new gentoo-box and first decided to use lvm2 on my root. Well, I ran into some issues with the kernel and initrd which I could figure out and fix. But then I noticed that, because of the lvm, I won't be able to access the disk from Windows with the free ext3-drivers that are available. Linux will even boot faster because I'll have no need for the initrd anymore. That's when I decided to get rid of the lvm.And that's actually easier than you'd think: If you have a spare-partition or -harddisk around that is at least the size of the logical volume that you'd like to convert to a partition, you can easily do this with dd. Imagine that /dev/vg/volume is a logical volume that consists of only one partition, /dev/sday:

 sh# dd if=/dev/vg/volume of=/dev/sdbx bs=8M
 sh# dd if=/dev/sdbx of=/dev/sday bs=8M

 That's it. This will back up the logical, continuous data that's hosted on the lvm to a partition. After the first dd you'll be able to mount /dev/sdbx and see how the content of /dev/vg/volume has been copied. The mounted partition's usable size will be exactly the same as the volume's size, even if the partition itself is much bigger. That's because the filesystem on it will still be the same size it was before. You could (but it wouldn't make much sense because we want to move the data to the other partition anyways) fix this with resize2fs (if you use ext2 or ext3, that is). The second dd copies the data back to the partition that it was formerly stored on, but without the additional lvm-abstraction. The lvm will be overwritten by the 'flat' filesystem-data. If sdbx happens to be bigger than sday, an error will be printed that dd reached the end of the partition. This is nothing to worry about since the data left on sdbx is not interesting to us anyways. You can fix the filesystem-size to the actual partition size with resize2fs. Since the lvm itself needs some space, too, it will be slightly (a few bytes) larger now.

4 comments:

 Logu July 1, 2011 at 6:05 AM
 Excelent buddy. I was really looking for it. I am going to do this for a vmware vm
1. Create virtual disk (sdb) to hold the LVM enabled disk on the guest
2. Partiton the disk similar to the LVM layout.
3. Boot the LVM machine with a live CD.
4. dd if=/dev/vg/volume|ssh vmguest > /dev/sdbx
 Reply
 ---------------------------------------------------------------------------------------------
 IkemFebruary 3, 2013 at 6:11 AM > If you have a spare-partition _or_ -harddisk around
 Reply
 Replies Daniel AlbuschatFebruary 4, 2013 at 4:02 AM
Fixed - thanks!
 Reply
 ----------------------------------------------------------------------------------------------

Mohsin sarip June 25, 2013 at 7:44 AM
Tqvm Daniel. this command:
" dd if=/dev/vg/volume of=/dev/sdbx bs=8M "
 this will overide all data in /dev/sdbx (possibly your ext harddisk).

 what if we use this command:
 " mount /dev/sdbx /mnt "
 " dd if=/dev/vg/volume of=/mnt/datafile.dd bs=8M "
 my 2 cent

No comments:

Post a Comment