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
-
#!/bin/bash # gdisk exercise # # Copyright (C) 2020 Heiko Sieger # https://heiko-sieger.info # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # Name of the image file: file="gpttest" # Specify size in MBytes: size=512 #user=$(logname) pv_list="" vg_name="" input="" GR='\033[1;32m' # Green CN='\033[1;36m' # Light Cyan NC='\033[0m' # No Color pause(){ msg="$1" cmd="$2" input="$3" echo -e "\n$msg" [[ -n "$input" ]] && read -p "$input: " input garbage [[ -n "$cmd" ]] && echo -e "\n${GR}$cmd${NC}\n" [[ -z "$input" ]] && read -p "Press [ENTER] to continue..." wait_for_enter echo -e "${CN}" ${cmd} echo -en "${NC}" } if [ $UID -ne 0 ]; then pause "Run script as 'root'! Abort." "exit 1" fi # Create file "$file" filled with zeros ($size Mbyte) pause "Creating $file filled with zeros ($size Mbyte)." "dd if=/dev/zero of=$file bs=1M count=$size" pause "Creating directory /mnt/exercise to mount partitions..." "mkdir /mnt/exercise" # Get the loop device that kpartx will map dev="$(kpartx -l $file | awk '{ print $NF }')" # Map $file as loop device pause "Mapping $file as loop device." "kpartx -av $file" echo -e "$dev\n" echo -e "In the next step we are going to invoke gdisk." echo -e "Use gdisk to create partitions, change partition type, etc." echo -e "Once you finished and checked everything, use the 'w' command" echo -e "to write the changes to the partition table." pause "" "gdisk $dev" dev2="$(kpartx -l $file | awk '{ print $1 }')" dev2=($dev2) pause "Finished gdisk. Now we update the partition mappings." "kpartx -u $file" pause "Show partitions inside $file using 'gdisk'." "gdisk -l $dev" type="$(gdisk -l $dev | egrep [[:space:]][0-9A-F]{4}[[:space:]]{2} | awk '{ print $6 }')" pause "Partition type(s):\n$type\n" "" type=($type) for ((i=0;i<${#dev2[*]};i++)); do device="/dev/mapper/${dev2[i]}" mnt="/mnt/exercise/${dev2[i]}" case ${type[i]} in 0700) cmd="mkfs.ntfs -v";; 2700) cmd="mkfs.ntfs -v";; 8200) cmd="mkswap";; 8300) cmd="mke2fs -v";; 8E00) pause "We need to create a PV (LVM) for partition type 8E00:" "pvcreate $device"; pv_list="$pv_list $device" if [ -z "$vg_name" ]; then rc=1; while [ $rc -ne 0 ] ; do pause "Now we create a VG (volume group)." "" "Enter the name of the VG"; vg_name=$input; [[ $vg_name =~ ^[a-zA-Z0-9]+$ ]] && rc=0 ; done ; pause "Creating VG..." "vgcreate $vg_name $device" "" ; sleep 2 ; else pause "Extending VG $vg_name..." "vgextend -A n -y -v $vg_name $device"; fi rc=1; sleep 1 ; while [ $rc -ne 0 ] ; do pause "Next we create a LV (logical volume)." "" "Enter the name of the LV"; [[ $input =~ ^[a-zA-Z0-9]+$ ]] && rc=0 ; [[ $input != $lv_name ]] && lv_name=$input || rc=1; done ; rc=1; sleep 1 ; while [ $rc -ne 0 ] ; do pause "Enter the size of the LV $lv_name." "" "Use K for Kilobytes, M for Megabytes, or G for Gigabytes (example 100M)"; lv_size=$input; pause "Creating LV..." "lvcreate -L $lv_size -n $lv_name $vg_name" ""; rc=$?; [[ $rc -ne 0 ]] && echo -e "Size is too big.\n"; done ; pause "Listing the LVs:" "lvs"; device="/dev/mapper/$vg_name-$lv_name"; cmd="mkfs.ext4 -v";; EF00) cmd="mkfs.fat -v -F 16";; *) cmd="mkfs.ext4 -v";; esac pause "Formating $device as ${type[i]}" "$cmd $device" pause "Creating mount point" "mkdir $mnt" pause "Mounting partition on $mnt" "mount $device $mnt" # pause "Changing ownership" "chown $user:$user $mnt" done pause "See new partitions as loop devices using lsblk:" "lsblk" pause "See if the partitions are mounted:" "df -Th" pause "Let's check /proc/mounts:" "grep loop /proc/mounts" homedir="$(pwd)" pause "Changing directory to /mnt/exercise..." "cd /mnt/exercise" # Find the terminal program in use - this part is ugly: terminal="$(ls /usr/bin/*terminal)" # works for Gnome, Mate, Xfce4 on Ubuntu, Centos and Manjaro # In case x-terminal-emulator exists, use it: [[ -x /usr/bin/x-terminal-emulator ]] && terminal="/usr/bin/x-terminal-emulator" # If nothing helps, assume it's the KDE konsole (wild guess, but works in SuSE): [[ -z "$terminal" ]] && terminal="konsole" pause "You can now exercise on file systems and LVM volumes. Type 'exit' when done." "$terminal -e bash" cd "$homedir" pause "Unmounting file system(s)..." "umount /mnt/exercise/*" pause "Removing mount points..." "rm -rf /mnt/exercise/*" if [ ! -z "$vg_name" ]; then pause "Removing all LVs we created..." "lvremove -A n -f -v $vg_name" pause "Removing the VG..." "vgremove -f -y -v $vg_name" pause "List the remaining VGs:" "vgs" pause "Removing PV$pv_list..." "pvremove -f -y -v$pv_list" pause "List the remaining PVs:" "pvs" fi pause "Deleting loop device mappings..." "kpartx -d $file" pause "Deleting $file..." "rm $file" pause "Deleting directory /mnt/exercise..." "rmdir /mnt/exercise" pause "All is back to as it was before. Done." "exit 0" #END
1
Categories
- All Categories
- 219 LFX Mentorship
- 219 LFX Mentorship: Linux Kernel
- 793 Linux Foundation IT Professional Programs
- 354 Cloud Engineer IT Professional Program
- 179 Advanced Cloud Engineer IT Professional Program
- 82 DevOps Engineer IT Professional Program
- 147 Cloud Native Developer IT Professional Program
- 137 Express Training Courses
- 137 Express Courses - Discussion Forum
- 6.2K Training Courses
- 47 LFC110 Class Forum - Discontinued
- 71 LFC131 Class Forum
- 42 LFD102 Class Forum
- 226 LFD103 Class Forum
- 19 LFD110 Class Forum
- 38 LFD121 Class Forum
- 18 LFD133 Class Forum
- 7 LFD134 Class Forum
- 18 LFD137 Class Forum
- 71 LFD201 Class Forum
- 5 LFD210 Class Forum
- 5 LFD210-CN Class Forum
- 2 LFD213 Class Forum - Discontinued
- 128 LFD232 Class Forum - Discontinued
- 2 LFD233 Class Forum
- 4 LFD237 Class Forum
- 24 LFD254 Class Forum
- 697 LFD259 Class Forum
- 111 LFD272 Class Forum
- 4 LFD272-JP クラス フォーラム
- 12 LFD273 Class Forum
- 149 LFS101 Class Forum
- 1 LFS111 Class Forum
- 3 LFS112 Class Forum
- 2 LFS116 Class Forum
- 4 LFS118 Class Forum
- LFS120 Class Forum
- 7 LFS142 Class Forum
- 5 LFS144 Class Forum
- 4 LFS145 Class Forum
- 3 LFS146 Class Forum
- 3 LFS147 Class Forum
- 1 LFS148 Class Forum
- 15 LFS151 Class Forum
- 2 LFS157 Class Forum
- 30 LFS158 Class Forum
- 7 LFS162 Class Forum
- 2 LFS166 Class Forum
- 4 LFS167 Class Forum
- 3 LFS170 Class Forum
- 2 LFS171 Class Forum
- 3 LFS178 Class Forum
- 3 LFS180 Class Forum
- 2 LFS182 Class Forum
- 5 LFS183 Class Forum
- 32 LFS200 Class Forum
- 737 LFS201 Class Forum - Discontinued
- 3 LFS201-JP クラス フォーラム
- 18 LFS203 Class Forum
- 134 LFS207 Class Forum
- 2 LFS207-DE-Klassenforum
- 1 LFS207-JP クラス フォーラム
- 302 LFS211 Class Forum
- 56 LFS216 Class Forum
- 52 LFS241 Class Forum
- 48 LFS242 Class Forum
- 38 LFS243 Class Forum
- 15 LFS244 Class Forum
- 2 LFS245 Class Forum
- LFS246 Class Forum
- 49 LFS250 Class Forum
- 2 LFS250-JP クラス フォーラム
- 1 LFS251 Class Forum
- 153 LFS253 Class Forum
- 1 LFS254 Class Forum
- 1 LFS255 Class Forum
- 9 LFS256 Class Forum
- 1 LFS257 Class Forum
- 1.3K LFS258 Class Forum
- 10 LFS258-JP クラス フォーラム
- 119 LFS260 Class Forum
- 159 LFS261 Class Forum
- 42 LFS262 Class Forum
- 82 LFS263 Class Forum - Discontinued
- 15 LFS264 Class Forum - Discontinued
- 11 LFS266 Class Forum - Discontinued
- 24 LFS267 Class Forum
- 23 LFS268 Class Forum
- 30 LFS269 Class Forum
- LFS270 Class Forum
- 202 LFS272 Class Forum
- 2 LFS272-JP クラス フォーラム
- 1 LFS274 Class Forum
- 4 LFS281 Class Forum
- 9 LFW111 Class Forum
- 259 LFW211 Class Forum
- 181 LFW212 Class Forum
- 13 SKF100 Class Forum
- 1 SKF200 Class Forum
- 1 SKF201 Class Forum
- 796 Hardware
- 199 Drivers
- 68 I/O Devices
- 37 Monitors
- 103 Multimedia
- 174 Networking
- 91 Printers & Scanners
- 85 Storage
- 758 Linux Distributions
- 82 Debian
- 67 Fedora
- 17 Linux Mint
- 13 Mageia
- 23 openSUSE
- 148 Red Hat Enterprise
- 31 Slackware
- 13 SUSE Enterprise
- 353 Ubuntu
- 468 Linux System Administration
- 39 Cloud Computing
- 71 Command Line/Scripting
- Github systems admin projects
- 93 Linux Security
- 78 Network Management
- 102 System Management
- 47 Web Management
- 63 Mobile Computing
- 18 Android
- 33 Development
- 1.2K New to Linux
- 1K Getting Started with Linux
- 371 Off Topic
- 114 Introductions
- 174 Small Talk
- 22 Study Material
- 805 Programming and Development
- 303 Kernel Development
- 484 Software Development
- 1.8K Software
- 263 Applications
- 183 Command Line
- 3 Compiling/Installing
- 987 Games
- 317 Installation
- 97 All In Program
- 97 All In Forum
Upcoming Training
-
August 20, 2018
Kubernetes Administration (LFS458)
-
August 20, 2018
Linux System Administration (LFS301)
-
August 27, 2018
Open Source Virtualization (LFS462)
-
August 27, 2018
Linux Kernel Debugging and Security (LFD440)