Topics

  • Disk partitioning (fdisk)
  • Physical volumes and volume groups
  • Logical volumes
  • LVM cleanup

exercise_01.sh

instructional safe: yes requires: /dev/sdb
#!/bin/bash
# @type: instructional
# @requires: /dev/sdb
# @safe: yes

# Exercise 1: Assuming you have a new, unpartitioned 20Gb disk named /dev/sdb. 
# Use fdisk /dev/sdb to enter the interactive mode. Create a new DOS (MBR) partition table for the disk. 
# Create one primary partition of 2 Gb and one extended partition. 
# Within the extended partition, create two logical partitions of 500 MiB each.
#
# Task: Create MBR partition table with primary and logical partitions using fdisk.
#
# WARNING: This script requires a disk device and will modify it. Use with caution!

echo "This script demonstrates the fdisk commands needed."
echo "Run: sudo fdisk /dev/sdb"
echo ""
echo "Commands to enter in fdisk:"
echo "n          - Create new partition"
echo "p          - Primary partition"
echo "1          - Partition number 1"
echo "[Enter]    - First sector (default)"
echo "+2G        - Size of 2GB"
echo ""
echo "n          - Create new partition"
echo "e          - Extended partition"
echo "2          - Partition number 2"
echo "[Enter]    - First sector (default)"
echo "[Enter]    - Last sector (use remaining space)"
echo ""
echo "n          - Create logical partition"
echo "[Enter]    - Accept logical partition"
echo "[Enter]    - First sector (default)"
echo "+500M      - Size of 500MB"
echo ""
echo "n          - Create logical partition"
echo "[Enter]    - Accept logical partition"
echo "[Enter]    - First sector (default)"
echo "+500M      - Size of 500MB"
echo ""
echo "p          - Print partition table"
echo "w          - Write changes and exit"

exercise_02.sh

instructional safe: yes requires: /dev/sdb
#!/bin/bash
# @type: instructional
# @requires: /dev/sdb
# @safe: yes

# Exercise 2: Convert partitions to LVM type if you used any other. From remaining space create another one.
#
# Task: Change partition types to Linux LVM (type 8e) and create additional partition.
#
# WARNING: This script requires a disk device. Use with caution!

echo "This script demonstrates the fdisk commands needed."
echo "Run: sudo fdisk /dev/sdb"
echo ""
echo "Commands to enter in fdisk:"
echo "t          - Change partition type"
echo "1          - Partition number 1"
echo "8e         - Linux LVM type"
echo ""
echo "t          - Change partition type"
echo "5          - Partition number 5"
echo "8e         - Linux LVM type"
echo ""
echo "t          - Change partition type"
echo "6          - Partition number 6"
echo "8e         - Linux LVM type"
echo ""
echo "n          - Create new partition"
echo "[Enter]    - Accept logical partition"
echo "[Enter]    - First sector (default)"
echo "[Enter]    - Last sector (use remaining space)"
echo ""
echo "t          - Change partition type"
echo "7          - Partition number 7"
echo "8e         - Linux LVM type"
echo ""
echo "p          - Print partition table"
echo "w          - Write changes and exit"

exercise_03.sh

executable safe: no requires: root, /dev/sdb
#!/bin/bash
# @type: executable
# @requires: root, /dev/sdb
# @safe: no
set -euo pipefail

# Exercise 3: Using the LVM partitions you created in a previous exercise, 
# create physical volumes and include into vg01 group.
#
# Task: Initialize LVM physical volumes and create a volume group.
#
# WARNING: This will format the specified partitions. Use with caution!

sudo pvcreate /dev/sdb{1,5,6,7}

sudo vgcreate vg01 /dev/sdb{1,5,6,7}

# Display the volume group information
sudo vgdisplay vg01

exercise_04.sh

executable safe: no requires: root
#!/bin/bash
# @type: executable
# @requires: root
# @safe: no
set -euo pipefail

# Exercise 4: From the vg01 volume group, create a new logical volume named lv01 with a size of 3 GiB.
# Create a second logical volume named lv02 that uses 40% of the remaining free space in the volume group.
#
# Task: Create two logical volumes with different sizing methods.

sudo lvcreate -n lv01 -L 3G vg01

sudo lvcreate -n lv02 -l 40%FREE vg01

# Display the logical volumes
sudo lvdisplay vg01

exercise_05.sh

executable safe: no requires: root, /dev/sdb
#!/bin/bash
# @type: executable
# @requires: root, /dev/sdb
# @safe: no
set -euo pipefail

# Exercise 5: Delete all LVs and VG.
#
# Task: Clean up LVM configuration by removing logical volumes, volume groups, and physical volumes.
#
# WARNING: This will destroy data. Use with caution!

# Remove logical volumes
sudo lvremove /dev/vg01/lv0{1,2}

# Remove volume group
sudo vgremove vg01

# Remove physical volumes
sudo pvremove /dev/sdb{1,5,6,7}

echo "All LVM structures have been removed."