Interactive Bash script for managing LVM (Logical Volume Manager) on Ubuntu Server 24.04 LTS.
lvm-manager.sh provides a guided menu to create new LVM storage, extend existing logical volumes, and inspect the current LVM layout — with confirmations, input validation, and logging suitable for production use.
- Interactive menu-driven workflow
- Automatic detection of empty, unused disks
- Create full LVM stack: partition → PV → VG → LV → filesystem → mount →
/etc/fstab - Extend existing LVs (optionally add a new disk to the VG)
- Automatic filesystem resize (
resize2fsfor ext4,xfs_growfsfor xfs) - Display LVM and disk information (
lsblk,pvs,vgs,lvs,df, mounts) - Colored terminal output and session logging
- Safety checks to protect the OS/root disk and existing data
| Item | Details |
|---|---|
| OS | Ubuntu Server 24.04 LTS |
| Shell | Bash only |
| Privileges | root (sudo) |
Install dependencies on Ubuntu 24.04:
sudo apt-get update
sudo apt-get install -y lvm2 parted e2fsprogs xfsprogs util-linux| Command | Package |
|---|---|
lsblk, blkid, findmnt |
util-linux |
parted, partprobe |
parted |
pvcreate, vgcreate, vgextend, lvcreate, lvextend |
lvm2 |
resize2fs |
e2fsprogs |
xfs_growfs |
xfsprogs |
mount, df |
util-linux / coreutils |
The script checks for missing commands at startup and prints the required package names.
git clone <repository-url>
cd lvm-setup-script
chmod +x lvm-manager.shsudo ./lvm-manager.sh========================================
LVM Manager
========================================
1. Create New LVM
2. Extend Existing LVM
3. Show LVM Information
4. Exit
- Detect and select an empty disk (auto-selected if only one is found)
- Confirm the operation
- Create GPT partition table and a single LVM partition
- Create Physical Volume (PV)
- Prompt for Volume Group (VG) name
- Prompt for Logical Volume (LV) name and size (
100G,50G,100%FREE, etc.) - Choose filesystem:
ext4orxfs - Prompt for mount point (directory created if missing)
- Mount the LV and append a UUID entry to
/etc/fstab - Verify the mount and print a success summary
- Select an existing Volume Group
- Select a Logical Volume inside that VG
- If an empty disk is available, optionally add it to the VG (
pvcreate+vgextend) - Enter extension size (
+50G,+100G,+100%FREE, etc.) - Run
lvextendand resize the filesystem automatically - Print final size summary
Displays a formatted report of:
lsblk— block devicespvs— physical volumesvgs— volume groups (including free space)lvs— logical volumesdf -h— disk usage- Active mounts and filesystem types
- Root/OS disk and available empty disks
A disk is offered for use only when all of the following are true:
TYPE=diskinlsblk- Not mounted (disk or any child)
- Has no partitions
- Has no filesystem signature (
blkid) - Is not a Physical Volume
- Is not used by any Volume Group
- Is not the root/OS disk
| Disks found | Behavior |
|---|---|
| 0 | Message: No available disk detected. |
| 1 | Automatically selected |
| 2+ | Interactive selection menu |
The script is designed to avoid accidental data loss:
- Never offers the root/OS disk as an empty disk
- Skips disks that are mounted, partitioned, or already part of LVM
- Asks for confirmation before destructive operations
- Double confirmation before:
mklabel(partition table creation)pvcreatevgcreatelvcreatemkfs(filesystem format)
- Backs up
/etc/fstabbefore appending new entries - Skips duplicate
fstabentries (same mount point or UUID) - Aborts immediately on command failure (
set -Eeuo pipefail)
Warning: Operations on the wrong disk can destroy data. Always verify disk selection in virtualized or cloud environments where device names (
/dev/sdb,/dev/nvme1n1, etc.) may differ between reboots.
All actions are logged to:
/var/log/lvm-manager.log
Each executed command is printed to the terminal and recorded in the log file.
Assume /dev/sdb is a blank 500 GB data disk:
sudo ./lvm-manager.sh
# Select: 1. Create New LVM
# Confirm disk: /dev/sdb
# VG name: data-vg
# LV name: lv-storage
# LV size: 100%FREE
# Filesystem: ext4
# Mount point: /dataExpected summary:
========================================
Operation Completed Successfully
========================================
VG : data-vg
LV : lv-storage
Filesystem : ext4
Mount : /data
Size : 500G
========================================
sudo ./lvm-manager.sh
# Select: 2. Extend Existing LVM
# Select VG: data-vg
# Select LV: lv-storage
# Use new disk? yes (if /dev/sdc is available)
# Extension size: +100%FREEThe script extends the LV and grows the filesystem to use the new space.
| Issue | Suggestion |
|---|---|
No available disk detected |
Attach a new disk or ensure the target disk has no partitions, filesystem, or LVM signatures |
This script must be run as root |
Run with sudo ./lvm-manager.sh |
| Missing command errors | Install packages listed in Requirements |
Partition not found after partprobe |
Wait a few seconds and retry; check dmesg for kernel errors |
xfs_growfs requires a mount point |
Ensure the LV is mounted before extending an XFS volume |
| Operation aborted mid-way | Check /var/log/lvm-manager.log and run lsblk, pvs, vgs, lvs to assess partial state |
The script is organized into reusable functions:
check_root print_header print_menu
detect_empty_disks choose_disk create_partition
create_pv create_vg create_lv
format_fs mount_lv update_fstab
extend_vg extend_lv resize_filesystem
show_info confirm_action log
cleanup
MIT License.
Use at your own risk on production systems — test in a non-production environment first.