Saturday, September 24, 2016

snapshot root file system before doing major update or modification to the os

lvm (logical volume management) have a feature that we take a snapshot of current system, so when we are going to do a big modification or update to os, and if some problems arise, we can revert to the point that we have taken the snapshot.

1- display which lvm logical volume we are using
$ df -h
Filesystem                 Size  Used Avail Use% Mounted on
udev                       469M     0  469M   0% /dev
tmpfs                       98M  6.7M   91M   7% /run
/dev/mapper/ulvm--vg-root  165G  2.5G  154G   2% /
tmpfs                      488M     0  488M   0% /dev/shm
tmpfs                      5.0M     0  5.0M   0% /run/lock
tmpfs                      488M     0  488M   0% /sys/fs/cgroup
/dev/sda1                  472M   57M  392M  13% /boot
tmpfs                       98M     0   98M   0% /run/user/1000



we are currently using /dev/mapper/ulvm--vg-root

2- display all the lvm logical volume, we are having.
$ sudo lvdisplay
  --- Logical volume ---
  LV Path                /dev/ulvm-vg/root
  LV Name                root
  VG Name                ulvm-vg
  LV UUID                OMBeBW-BY8b-D9He-u3vl-r1SI-e87j-eFK4Mf
  LV Write Access        read/write
  LV Creation host, time ulvm, 2016-09-23 19:16:11 -0400
  LV snapshot status     source of
                         rootsnap [active]
  LV Status              available
  # open                 1
  LV Size                166.82 GiB
  Current LE             42707
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:1
  
  --- Logical volume ---
  LV Path                /dev/ulvm-vg/swap_1
  LV Name                swap_1
  VG Name                ulvm-vg
  LV UUID                dxQPqJ-v7rY-8bap-pPdL-GXxZ-ZaKz-2MAOdd
  LV Write Access        read/write
  LV Creation host, time ulvm, 2016-09-23 19:16:11 -0400
  LV Status              available
  # open                 2
  LV Size                1020.00 MiB
  Current LE             255
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:4



3- take the snapshot of our current system, that's mean that we will take the snapshot of /dev/ulvm-vg/root and the snapshot volume will we call it rootsnap
$ sudo lvcreate --size 2G -s -n rootsnap /dev/ulvm-vg/root

4- use lvdisplay again we will see the new logical volume called rootsnap is created

$ sudo lvdisplay
  --- Logical volume ---
  LV Path                /dev/ulvm-vg/root
  LV Name                root
  VG Name                ulvm-vg
  LV UUID                OMBeBW-BY8b-D9He-u3vl-r1SI-e87j-eFK4Mf
  LV Write Access        read/write
  LV Creation host, time ulvm, 2016-09-23 19:16:11 -0400
  LV snapshot status     source of
                         rootsnap [active]
  LV Status              available
  # open                 1
  LV Size                166.82 GiB
  Current LE             42707
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:1
  
  --- Logical volume ---
  LV Path                /dev/ulvm-vg/swap_1
  LV Name                swap_1
  VG Name                ulvm-vg
  LV UUID                dxQPqJ-v7rY-8bap-pPdL-GXxZ-ZaKz-2MAOdd
  LV Write Access        read/write
  LV Creation host, time ulvm, 2016-09-23 19:16:11 -0400
  LV Status              available
  # open                 2
  LV Size                1020.00 MiB
  Current LE             255
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:4
  
  --- Logical volume ---
  LV Path                /dev/ulvm-vg/rootsnap
  LV Name                rootsnap
  VG Name                ulvm-vg
  LV UUID                7ot75W-mIXX-KOBt-v1BT-6uz4-iLl2-9U9Use
  LV Write Access        read/write
  LV Creation host, time ulvm, 2016-09-23 23:44:30 -0400
  LV snapshot status     active destination for root
  LV Status              available
  # open                 0
  LV Size                166.82 GiB
  Current LE             42707
  COW-table size         2.00 GiB
  COW-table LE           512
  Allocated to snapshot  7.60%
  Snapshot chunk size    4.00 KiB
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:3



5- now we can do the update with the snapshot, by reboot and choose the root point to our rootsnap logical volume

- on grub boot menu (shift to get the menu) choose edit (press e)
- then find the line start with linux and update root to rootsnap, below is the example
linux        /vmlinuz root=/dev/mapper/ulvm--vg-rootsnap ro quiet splash $vt_handoff
- then boot with th setting, by pressing ctrl+x or F10


6- to verify whether the root is map to the lvm logical volume snaproot we use df as step 1
$ df -h
Filesystem                     Size  Used Avail Use% Mounted on
udev                           469M     0  469M   0% /dev
tmpfs                           98M  6.7M   91M   7% /run
/dev/mapper/ulvm--vg-rootsnap  165G  2.5G  154G   2% /
tmpfs                          488M     0  488M   0% /dev/shm
tmpfs                          5.0M     0  5.0M   0% /run/lock
tmpfs                          488M     0  488M   0% /sys/fs/cgroup
/dev/sda1                      472M   57M  392M  13% /boot
tmpfs                           98M     0   98M   0% /run/user/1000



7- now you feel free to damage or mess up your system

8- after everything you have done to the system and it turns out, it is what you like you can merge the snapshot
- boot with the real root and issue the following command
$ sudo lvconvert --merge /dev/ulvm-vg/rootsnap
- or issue the above command in the snapshot root. it will do the merging when the next reboot

9- if you don't like the what you have done, just reboot it and you can remove the snapshot with the command below

$ sudo lvremove /dev/ulvm-vg/rootsnap
Do you really want to remove and DISCARD active logical volume ulvm-vg/rootsnap? [y/n]: y
  Logical volume "rootsnap" successfully remove

No comments:

Post a Comment