Welcome to the Linux Foundation Forum!

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:

  1. Inside gdisk, create a new partition using "n".
  2. Press Enter at the prompt to take the default (1).
  3. Again press Enter for the start of the first partition.
  4. Enter "+100M" for the end of the 1st partition.
  5. 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

  • heiko_s
    heiko_s Posts: 99
    #!/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
    

Categories

Upcoming Training