Friday, April 25, 2014

LVM: how to make a snapshot of root logical volume and rollback it

RHEL6 introduces new features on LVM. One of them is the snapshots. 
For this test I maked a screenshot of root logical volume , I deleted /etc directory and restored it from the sreenshot. All worked !
[root@localhost ~]# df -h
 Filesystem Size Used Avail Use% Mounted on
 /dev/mapper/rootvg-rootvol
 23G 3.1G 18G 15% /
 tmpfs 498M 0 498M 0% /dev/shm
 /dev/sda1 485M 33M 427M 8% /boot
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
rootvg 1 2 0 wz–n- 24.51g 0
I saw no free space on rootvg so i added a new disk
[root@localhost ~]# sfdisk -l /dev/sdb
Disk /dev/sdb: 652 cylinders, 255 heads, 63 sectors/track
Units = cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0
Device Boot Start     End   #cyls    #blocks   Id  System
/dev/sdb1          0+    651     652-   5237158+  8e  Linux LVM
/dev/sdb2          0       -       0          0    0  Empty
/dev/sdb3          0       -       0          0    0  Empty
/dev/sdb4          0       -       0          0    0  Empty
[root@localhost ~]# pvcreate /dev/sdb1
Writing physical volume data to disk “/dev/sdb1″
Physical volume “/dev/sdb1″ successfully created

[root@localhost ~]# vgextend rootvg /dev/sdb1
Volume group “rootvg” successfully extended

[root@localhost ~]# vgs
VG     #PV #LV #SN Attr   VSize  VFree
rootvg   2   2   0 wz–n- 29.50g 4.99g

Nice. After i maked a screenshot with all free space on the vg.

[root@localhost ~]# lvcreate -l 100%FREE –snapshot –name rootsnap /dev/rootvg/rootvol
Logical volume “rootsnap” created
[root@localhost ~]# lvs
LV       VG     Attr     LSize  Pool Origin  Data%  Move Log Copy%  Convert
rootsnap rootvg swi-a-s-  4.99g      rootvol   0.00
rootvol  rootvg owi-aos- 22.46g
swapvol  rootvg -wi-ao–  2.05g
s attribute said to me that rootsnap is a snapshot !
[root@localhost ~]# lvdisplay /dev/rootvg/rootvol
— Logical volume —
LV Path                /dev/rootvg/rootvol
LV Name                rootvol
VG Name                rootvg
LV UUID                MgWa8V-yUnK-hqC0-Kv6X-strT-eccc-Kiwt8S
LV Write Access        read/write
LV Creation host, time localhost.localdomain, 2013-01-29 19:04:19 +0100
LV snapshot status     source of
rootsnap [active]

LV Status              available
# open                 1
LV Size                22.46 GiB
Current LE             5750
Segments               1
Allocation             inherit
Read ahead sectors     auto
- currently set to     256
Block device           253:1
First test I did:
[root@localhost ~]# dd if=/dev/zero of=test.img bs=1024k count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 80.6583 s, 13.0 MB/s
THE SPACE ON THE SNAPSHOT MUST BE CHECKED !!!
[root@localhost ~]# lvs
LV       VG     Attr     LSize  Pool Origin  Data%  Move Log Copy%  Convert
rootsnap rootvg swi-a-s-  4.99g      rootvol  18.21
rootvol  rootvg owi-aos- 22.46g
swapvol  rootvg -wi-ao–  2.05g
[root@localhost ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/rootvg-rootvol
23G  4.2G   17G  20% /
tmpfs                 498M     0  498M   0% /dev/shm
/dev/sda1             485M   33M  427M   8% /boot
After i erased /etc directory!
[root@localhost ~]# rm -rf /etc
[root@localhost ~]# cd /etc
-bash: cd: /etc: No such file or directory
[root@localhost ~]# df -h
df: cannot read table of mounted file systems: No such file or directory
I did a reboot and as expected the system was not able to boot up.
So , I used rescue mode from CD and I ran on the shell:
lvconvert –merge rootvg/rootsnap
At next boot the system came up without any problem/warning!
[root@localhost ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/rootvg-rootvol
23G  3.1G   18G  15% /
tmpfs                 498M     0  498M   0% /dev/shm
/dev/sda1             485M   33M  427M   8% /boot
[root@localhost ~]# cd /root
[root@localhost ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog
( The 1 gb file does not exist anymore )
[root@localhost ~]# lvs
LV      VG     Attr     LSize  Pool Origin Data%  Move Log Copy%  Convert
rootvol rootvg -wi-ao– 22.46g
swapvol rootvg -wi-ao–  2.05g
( the snapshot is not present )
With this i saw that the system was rollbacked with success.
So i will use this method on RHEL6 for rollingback after a patching!

Source: Redhat

No comments:

Post a Comment