Trying out LVM2 on Ubuntu

来源:互联网 发布:加入淘宝客要多少钱 编辑:程序博客网 时间:2024/05/21 17:44

Environment:

Ubuntu 8.04 server LTS in VMware

Partitioned with installation guide, whole with LVM

Extra empty virtual disk 8GB added as second SCSI device

 

Steps:

 

1. Partition the new disk:

fdisk /dev/sdb

create 4 partitions. don't format.

 

2. create Physical Volumns for new partitions

pvcreate /dev/sdb1

pvcreate /dev/sdb2

...

Find out sizes for each PVs, in the unit "Physical Extends" (PEs)

$ pvdisplay /dev/sdb1 | grep 'Total PE'

Total PE:         490

Remember the value, say $TPE .

 

3. Find out Volumn group new PVs to be added in

vgdisplay

say we are adding to VG "ubuntu".

If there is NO VG AVAILABLE IN SYSTEM, create one:

vgcreate ubuntu /dev/sdb1

 The second arg is a pvcreate-ed physical volumn.

4. Add new PV to VG

vgextend /dev/sdb1 ubuntu

to remove, use vgreduce

vgreduce ubuntu /dev/sdb4

5. Find Logic Volumn new space to be added into. LVs are the "actual partitions" to be mounted in the system.

lvdisplay

To create one, 

lvcreate $VGNAME -l$TPE -n new_lv_name

Say the Volume Group name is /dev/ubuntu/, as $VGNAME

new_lv_name will be the name of new volume, e.g. /dev/ubuntu/new_lv_name

-l$TPE could be specified instead as -L size, e.g. -L 40G

 

6. Extend the target LV

lvextend -l+$TPE $LVNAME

where $TPE is found in step 2 and $LVNAME is found in step 5.

 

7. Resize filesystem accordingly.

resize2fs /dev/ubuntu/root

resize2fs will automatically resize the filesystem to fit the total size of underlying device (here the LV /dev/ubuntu/root). For ext3 / reiserfs, online resize can be performed where ext2 requires offline (umount first) resize.

 

Now you can df to see the new size.

 

Enjoy :)

 

Reference:

http://tldp.org/HOWTO/LVM-HOWTO/commontask.html

 

Extras:

 

1. Use large regular file as part of LVM PVs

Via loopback device / losetup

losetup /dev/loop0 $LARGEFILE   ; associate $LARGEFILE with /dev/loop0

pvcreate /dev/loop0                     ; use /dev/loop0 a.k.a. $LARGEFILE as a PV

; other normal PV / VG / LV operations

2. Remove an PV from VG

pvmove $PV

Move all PE (data blocks) on $PV to other free PEs in the $VG. See vgdisplay for "Free PE" and pvdisplay for "Total PE" - there must be more "Free PE" in vg than "Total PE" in target PV. If not, add a new PV to VG (don't assign to LVs). pvmove is supposed to move all data blocks around is supposed to be slow.

vgreduce $VG $PV

removes target $PV from $VG.

pvremove $PV

optionally removes the PV.


3. Running system deployment tests with fresh Ubuntu on LVM2

Sample setup: Ubuntu 10.10 fresh on VG srv20, root fs /dev/srv20/root

Mount script:

#!/bin/bash[ -z "$LV" ] && LV=mspmasterexport LV./umount.shlvremove /dev/srv20/$LVlvcreate -s -L 40G -n $LV /dev/srv20/root || exit 1[ -d /mnt/$LV ] || mkdir /mnt/$LVmount /dev/srv20/$LV /mnt/$LVpushd /mnt/$LVecho "srv20-$LV" > ./etc/hostnamesed -i "s/srv20/srv20-$LV/g" ./etc/hostsrm ./base-systemtouch ./$LVmount --bind /dev/pts /mnt/$LV/dev/ptsmount --bind /proc /mnt/$LV/procmount --bind /sys /mnt/$LV/sys

Then one may chroot into /mnt/$LV and use the system. By modifying ~/.bashrc, change "\h" in $PS1 to `cat /etc/hostname` and one can identify which level of dream he is in.

Umount script:

#!/bin/bash[ -z "$LV" ] && LV=mspmasterumount /mnt/$LV/dev/ptsumount /mnt/$LV/procumount /mnt/$LV/sysumount /mnt/$LVrm -rf /mnt/$LV

 

Reference:

http://tldp.org/HOWTO/LVM-HOWTO/removeadisk.html

 

 

原创粉丝点击