Showing posts with label lvm. Show all posts
Showing posts with label lvm. Show all posts

2012-12-30

Migrate /home in Ubuntu

Sadly Ubuntu does not have LVM enabled in the default install, as this whole issue would be solved with a simple pvmigrate and lvresize...

My /home (which is on a separate /dev/sda5 partition) is filling up.
The availability of Steam on Linux gave it the final blow: it's time to bring in another partition.
As I don't want to commit a whole partition to /home, I've mounted /dev/sdc2 as /space and I'm going to create a symlink to an home subdirectory.

First, boot Ubuntu in rescue mode and select root shell.

Mount all filesystems

#mount -a

note that mounted filesystem won't be visible with a df, but you should cat /proc/mounts to see them
Set proper permission to the new partition

#chown root:root /space
#chmod 755 /space

Then create the new home and copy the old one

#mkdir /space/home
#cp -a /home/* /space/home/

Mount / read/write as we'll need to modify the fstab

#mount / -o rw,remount
#vi /etc/fstab

Comment the old /home
Remove the old home mount point and create the new symlink

#umount /home
#rmdir /home
#ls -s /space/home /home

Reboot

2011-12-04

Shrinking the root filesystem

The default install of RedHat 5 fills the entire disk with the root filesystem: this may backfire later during machine lifetime.
Here's a way to resize the root filesystem.
Note that while the size increase can be done online, as of RedHat 5, size reduction must be performed in rescue mode.

Here's a tipical installation:

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                      7.7G  3.1G  4.3G  42% /
/dev/sda1              99M   19M   75M  21% /boot
tmpfs                 502M     0  502M   0% /dev/shm

# vgdisplay -v
    Finding all volume groups
    Finding volume group "VolGroup00"
  --- Volume group ---
  VG Name               VolGroup00
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               9.88 GB
  PE Size               32.00 MB
  Total PE              316
  Alloc PE / Size       316 / 9.88 GB
  Free  PE / Size       0 / 0
  VG UUID               CJqD4r-kRQM-Cj9e-XwOr-6dn8-TkPH-MfTTyZ

  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol00
  VG Name                VolGroup00
  LV UUID                d4iToX-8m0c-jf9s-sMMN-VDpO-brE8-4kWhf2
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                7.91 GB
  Current LE             253
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol01
  VG Name                VolGroup00
  LV UUID                kEzo7d-AHuv-xiPD-3Qbo-v8yY-6w1D-DFMIwN
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                1.97 GB
  Current LE             63
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

  --- Physical volumes ---
  PV Name               /dev/sda2
  PV UUID               7bPvQd-H8yE-v00N-asPT-WQ92-6oCS-bBh6nj
  PV Status             allocatable
  Total PE / Free PE    316 / 0

The physical volume is full, with 0 free extents.

We are going to reduce / size to 6GB.
Boot in rescue mode

boot: linux rescue

Skip existing installation detection.
Activate existing volume groups:

# lvm vgscan
# lvm vgchange -ay /dev/VolGroup00

Run a filesystem check before resizing:

# e2fsck -f /dev/VolGroup00/LogVol00

Set the filesystem size a litte less than required, so the resizing of the underlying logical volume won't cut short the filesystem itself.

# resize2fs /dev/VolGroup00/LogVol00 5G

Resize the logical volume to the target size.

# lvm lvresize -L 6G /dev/VolGroup00/LogVol00

Now resize again the filesystem without giving any size, so it will fill up the logical volume.

# resize2fs /dev/VolGroup00/LogVol00

Reboot back into the system.
You can see the new / size:

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                      5.9G  3.1G  2.7G  54% /
/dev/sda1              99M   19M   75M  21% /boot
tmpfs                 502M     0  502M   0% /dev/shm

# vgdisplay -v
    Finding all volume groups
    Finding volume group "VolGroup00"
  --- Volume group ---
  VG Name               VolGroup00
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               9.88 GB
  PE Size               32.00 MB
  Total PE              316
  Alloc PE / Size       255 / 7.97 GB
  Free  PE / Size       61 / 1.91 GB
  VG UUID               CJqD4r-kRQM-Cj9e-XwOr-6dn8-TkPH-MfTTyZ

  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol00
  VG Name                VolGroup00
  LV UUID                d4iToX-8m0c-jf9s-sMMN-VDpO-brE8-4kWhf2
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                6.00 GB
  Current LE             192
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol01
  VG Name                VolGroup00
  LV UUID                kEzo7d-AHuv-xiPD-3Qbo-v8yY-6w1D-DFMIwN
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                1.97 GB
  Current LE             63
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

  --- Physical volumes ---
  PV Name               /dev/sda2
  PV UUID               7bPvQd-H8yE-v00N-asPT-WQ92-6oCS-bBh6nj
  PV Status             allocatable
  Total PE / Free PE    316 / 61

And the physical volume now has some spare space left.

2011-11-27

Boot disk transplant

Bare hands, MacGyver-style, boot disk transplant.

I've used a network attached linux machine as a temporary storage, but a local USB disk will do as fine.
The new disk should be as least as big as the old one.

First take note of the original disk configuration.
You will need the partition table:

# fdisk -l

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 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        1305    10377990   8e  Linux LVM

The volume group configuration (note the PE and the LE sizes)

# vgdisplay -v
    Finding all volume groups
  /dev/hda: open failed: No medium found
    Finding volume group "VolGroup00"
  --- Volume group ---
  VG Name               VolGroup00
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               9.88 GB
  PE Size               32.00 MB
  Total PE              316
  Alloc PE / Size       316 / 9.88 GB
  Free  PE / Size       0 / 0
  VG UUID               CJqD4r-kRQM-Cj9e-XwOr-6dn8-TkPH-MfTTyZ

  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol00
  VG Name                VolGroup00
  LV UUID                d4iToX-8m0c-jf9s-sMMN-VDpO-brE8-4kWhf2
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                7.91 GB
  Current LE             253
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol01
  VG Name                VolGroup00
  LV UUID                kEzo7d-AHuv-xiPD-3Qbo-v8yY-6w1D-DFMIwN
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                1.97 GB
  Current LE             63
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

  --- Physical volumes ---
  PV Name               /dev/sda2
  PV UUID               7bPvQd-H8yE-v00N-asPT-WQ92-6oCS-bBh6nj
  PV Status             allocatable
  Total PE / Free PE    316 / 0

And the fstab:

# cat /mnt/sysimage/etc/fstab
/dev/VolGroup00/LogVol00 /                       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/VolGroup00/LogVol01 swap                    swap    defaults        0 0

you will have to rebuild the logical volumes that belong to the transplanting disk

Boot the machine in rescue mode to do a full backup

boot: linux rescue

Start the network interface and do not mount any filesystem.
Backup the filesystems on /dev/sda and send them to the second machine via network.

#gzip -1 </dev/VolGroup00/LogVol00 | ssh user@secondmachine "cat >root.gz"
#gzip -1 </dev/sda1 | ssh user@secondmachine "cat >boot.gz"

Turn off the first machine, remove the old hd and put the new one.
Reboot again into linux rescue, with networking and without searching for existing installations.

Use fdisk to rebuild the same layout, with identical partitions, cylinder boundaries and partition Id (8e for LVM)
Now rebuild the LVM

#lvm pvcreate /dev/sda2

Note the PE size:

#lvm vgcreate -s 32M VolGroup00 /dev/sda2

and the LV size

#lvm lvcreate -l 253 -n LogVol00 VolGroup00
#lvm lvcreate -l 63 -n LogVol01 VolGroup00

Rebuild the swap partition

#mkswap /dev/VolGroup00/LogVol01

and write back the previous backups:

#ssh user@secondmachine "cat boot.gz" | gzip -d > /dev/sda1
#ssh user@secondmachine "cat root.gz" | gzip -d > /dev/VolGroup00/LogVol00

Reboot once again in rescue mode

#chroot /mnt/sysimage 
#grub-install /dev/sda

And finally reboot with your new disk.
Note that if the new disk is bigger than the old one, you will have some unpartitioned space left.
You can partition it with fdisk, pvcreate, and add it to LVM.

The broken LVM boot mirror

I was experimenting with LVM mirroring of the root partition and I've discovered that it won't fail gracefully.
After successfully setting up a mirror of LogVol00 to a second hd, I've rebooted the machine without the second hard disk.
I was expecting a normal boot from the first hd, which was the one with the grub and the boot partition.
I've got a kernel panic instead.
To recover the system I had to boot in rescue mode

boot: linux rescue

The rescue mode won't find any linux partition even if asked to search for them.
So back to the shell:

lvm pvscan

The system complained about an unknown device within VolGroup00

lvm vgscan
lvm lvscan

All logical volumes were marked as inactive

So, force the volume group activation

lvm vgchange -ay --partial VolGroup00

Now we can remove the missing mirror copy from the volume group

lvm vgreduce --removemissing --force VolGroup00

and the mirror logical volume will be converted back to linear.
Reboot.

2011-11-26

2011-11-25

How to rename the root volume group

This was tested on a RedHat 5.4
Boot the machine with installation CD into rescue mode

boot: linux rescue

at the request of mounting the existing installation, choose skip
Activate the volume group with

#lvm pvscan
#lvm vgscan

Now you can rename the volume group

#lvm vgrename VolGroup00 VolGroup99

Now you must change all the references to VolGroup00 on the filesytem to the new volume group name.
Activate the new volume group

#lvm vgchange -ay VolGroup99

Mount the root and the boot filesystems

#mkdir -p /mnt/sysimage
#mount /dev/VolGroup99/LogVol00 /mnt/sysimage

Mount the boot filesystems (note that the device name may vary according to your actual boot device)

#mount /dev/sda1 /mnt/sysimage/boot

Edit the fstab to look like this

#vi /mnt/sysimage/etc/fstab



/dev/VolGroup99/LogVol00 /                      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/VolGroup99/LogVol01 swap                   swap    defaults        0 0

and the grub configuration

#vi /mnt/sysimage/boot/grub/grub.conf




# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/VolGroup99/LogVol00
#          initrd /initrd-version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux Server (2.6.18-164.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-164.el5 ro root=/dev/VolGroup99/LogVol00 rhgb quiet
        initrd /initrd-2.6.18-164.el5.img

Now reboot once again in rescue mode.
This time choose to mount the filesystem onto /mnt/sysimage (not in read only)
Finally, rebuild the initrd image, as it yet contains the old fstab, matching your kernel version

#chroot /mnt/sysimage
#mkinitrd -v -f /boot/initrd-2.6.18-164.el5.img 2.6.18-164.el5

Reboot into your newly renamed system.

Note: if you don't reboot into rescue mode before rebuilding the initial ram disk, the mkinitrd will fail as it won't find the new /dev/VolGroup99 pseudo files

2011-10-08

Migrating logical volumes between volume groups

Some time ago I added a second hard disk to my machine and I joined it to the root volume group. I've also created a logical volume with a /data filesystem on it.
Now I need to move that data disk to a new machine, but as long as it's part of the root volume group I can't easily move it without making the whole machine unbootable.
These are the steps on a Red Hat 5.4 machine:
First, make sure that the disk you want to migrate does not contains logical volumes that are spanned on other disks also: I checked it with the system-config-lvm GUI, that shows how logical volumes are mapped to physical volumes, and I saw that my logical volume lv_data was entirely on /dev/sdb.

This is the initial fstab with the /data mounted from VolGroup00

/dev/VolGroup00/LogVol00 /                       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/VolGroup00/LogVol01 swap                    swap    defaults        0 0
/dev/VolGroup00/lv_data         /data           ext3    defaults        1 2

first, unmount the /data filesystem as this whole operation can't be done online.

[root@testlvm /]#umount /data

deactivate the logical volume

[root@testlvm /]#lvchange -an /dev/VolGroup00/lv_data

now split the root volume group creating a new VG_data

[root@testlvm /]# vgsplit VolGroup00 VG_data /dev/sdb
  New volume group "VG_data" successfully split from "VolGroup00"

you can see that the logical volume lv_data now belongs to the newly created volume group.
if you get an error, read at the bottom of this post.

[root@testlvm /]# vgdisplay -v
    Finding all volume groups
    Finding volume group "VG_data"
  --- Volume group ---
  VG Name               VG_data
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               1.97 GB
  PE Size               32.00 MB
  Total PE              63
  Alloc PE / Size       63 / 1.97 GB
  Free  PE / Size       0 / 0
  VG UUID               67mnJx-WyyP-7XQz-D1na-CNhr-rjUJ-79V8Fu


  --- Logical volume ---
  LV Name                /dev/VG_data/lv_data
  VG Name                VG_data
  LV UUID                lcwb7r-n0YZ-lslK-HoqE-VL6V-IMNl-rIY6N0
  LV Write Access        read/write
  LV Status              NOT available
  LV Size                1.97 GB
  Current LE             63
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto


  --- Physical volumes ---
  PV Name               /dev/sdb
  PV UUID               uwiyVE-Hkvt-dG3c-x1Ak-KBng-T91O-5Xuz2l
  PV Status             allocatable
  Total PE / Free PE    63 / 0


    Finding volume group "VolGroup00"
  --- Volume group ---
  VG Name               VolGroup00
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  6
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               9.88 GB
  PE Size               32.00 MB
  Total PE              316
  Alloc PE / Size       316 / 9.88 GB
  Free  PE / Size       0 / 0
  VG UUID               CJqD4r-kRQM-Cj9e-XwOr-6dn8-TkPH-MfTTyZ


  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol00
  VG Name                VolGroup00
  LV UUID                d4iToX-8m0c-jf9s-sMMN-VDpO-brE8-4kWhf2
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                7.91 GB
  Current LE             253
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0


  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol01
  VG Name                VolGroup00
  LV UUID                kEzo7d-AHuv-xiPD-3Qbo-v8yY-6w1D-DFMIwN
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                1.97 GB
  Current LE             63
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1


  --- Physical volumes ---
  PV Name               /dev/sda2
  PV UUID               7bPvQd-H8yE-v00N-asPT-WQ92-6oCS-bBh6nj
  PV Status             allocatable
  Total PE / Free PE    316 / 0

bring back online the logical volume

[root@testlvm /]#lvchange -ay /dev/VG_data/lv_data

note that you can't use TAB to complete the path of the logical volume, because the dev node won't be created until you activate it, so just type it as it is.

edit /etc/fstab with new new mount point


/dev/VolGroup00/LogVol00 /                       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/VolGroup00/LogVol01 swap                    swap    defaults        0 0
/dev/VG_data/lv_data            /data           ext3    defaults        1 2

and finally mount the /data filesystem from his new home

[root@testlvm /]#mount /data

now you can safely umount it, export the volume group and reimport it on another machine.

Note that as of Red Hat 5.4 you may get a an error on the vgsplit command if the target volume group already exists and has active logical volumes.

  Logical volumes in "VolGroup00" must be inactive


The error message is somewhat misleading: you'll get it even if the logical volumes you are migrating are already inactive.

Workaround: just make it in 2 steps: first split into a new "temp" volume group, then vgmerge the "temp" volume group into the target volume group.