LFS201 - Chapter 23. Logical Volume Management (LVM) - script to exercise

I've already posted a script for disk partitioning and LVM under Chapter 17, but here is a much improved version. It's also on Google drive for download: gpt.sh
The bash script will show each command at a time. It creates a 512M image file named gpttest that is then mounted as a loop device using kpartx. It then runs gdisk where you can partition the "gpttest" virtual drive.
For LVM, try the following exercises:
- Inside gdisk, create a new partition using "n".
- Press Enter at the prompt to take the default (1).
- Again press Enter for the start of the first partition.
- Enter "+100M" for the end of the 1st partition.
- Enter "8e00" to create a LVM partition.
Repeat steps 1-5 three times to create 3 LVM partitions each 100M size. If you made a mistake, you can fix it, for example by deleting the partition using "d" and creating a new one ("n"). When ready, press "w" to write to disk and create the partitions.
The script will display some information. At one point, you'll be asked to enter the name of the VG. Just type "vg" or any name you choose (use a-z, A-Z, 0-9). After that you will be prompted for the name of a LV. Just enter for example "lv1". Next you need to enter the size. Try "40M" for each LV you will create.
Repeat the same for the second and 3rd LV, each giving it a size of "40M". Remember to use a different LV name.
The script automatically formats the LVM volumes to ext4 and mounts them under /mnt/exercise/loop0p1 etc.
It will then open a new terminal window (cross my fingers that it works on your machines) with root privileges where you can play with the newly created logical volumes to your hearts content.
Try the following:
lsblk - to see the devices
pvs - to scan the physical volumes you created
vgs - for the volume group(s)
lvs - the logical volumes (LV)
lvresize -L +100M -r /dev/VGname/LVname
lsblk to see how the LV is distributed over 2 or more virtual partitions (loop0p? devices)
umount a LV and remove it with lvremove.
Migrate the content on the 3rd PV (something like /dev/mapper/loop0p3) to the other PVs (make sure there is enough room):
pvmove /dev/mapper/loop0p3 (check to see that yours are mapped as loop0p..., it could be loop1p... etc.)
Why do you need to use /dev/mapper/loop... for pv... commands?
What's the difference between /dev/VGname/LVname and /dev/mapper/loop...?
If you really want to knock yourself out, reformat one of the LV to NTFS. Then try to extend (lvresize ... -r) the LV. Does it work?
When you are fed up with it, type exit. The script will then close the second terminal window and reverse the steps to return the system to the way it was before (unless you made changes outside the loop device).
Improvements are welcome.
Caveat: The script ONLY works on a Linux machine or VM with a graphical desktop!
Suggestion: Can anyone modify the script so it would pause and exit to the same terminal window and continue to run after some command? This way it could run also on a remote machine (cloud).
I''ll post it below for reference.
Comments