Automate Linux Disk Partitioning and LVM Setup with Bash Scripts
This guide walks you through three Bash scripts that automate creating primary ext4/xfs partitions, building a full‑disk LVM logical volume, and converting a partition into a logical volume, complete with user prompts, filesystem formatting, and fstab updates.
This article provides Bash scripts for three common storage management tasks on Linux: creating a primary partition with ext4 or xfs, creating a logical volume using the entire disk, and converting a partition into an LVM logical volume.
1. Create a primary partition
Only primary partitions and standard Linux file systems (ext4/xfs) are supported. The script prompts for the target disk, partition type, number, start sector, and size, then uses fdisk to create the partition, runs partprobe, and updates /etc/fstab based on the chosen file system.
#!/bin/bash
# Function: 对硬盘进行分区,得到一个标准的linux文件系统(ext4/xfs)的主分区
cat /proc/partitions > old
read -p "请输入你要分区的硬盘(写绝对路径,如:/dev/sda):" A
if [ -e $A ]; then
echo "true"
else
echo "该设备不存在!!"
exit
fi
read -p "请输入你要创建的磁盘分区类型(这里只能是主分区,默认按回车即可):" B
read -p "请输入分区数字,范围1-4,默认从1开始,默认按回车即可:" C
read -p "请输入扇区起始表号,默认按回车即可:" D
read -p "请输入你要分区的分区大小(格式:如 +5G ):" E
fdisk $A << EOF
n
p
$C
$D
$E
w
EOF
echo "一个标准的linux文件系统的分区已经建立好!!"
partprobe $A
echo "-------------------------------"
cat /proc/partitions
cat /proc/partitions > new
F=`diff new old | grep "<" | awk '{print $5}'`
echo "-------------------------------"
echo $F
echo "你想对新分区设定什么类型的文件系统?有以下选项:"
echo "A:ext4文件系统"
echo "B:xfs文件系统"
read -p "请输入你的选择:" G
case $G in
a|A)
mkfs.ext4 /dev/$F
echo "该分区将被挂载在 /mnt/$F 下"
m=`ls /mnt/|grep $F | wc -l`
if [ $m -eq 0 ]; then
mkdir /mnt/$F
fi
n=`cat /etc/fstab | grep /dev/$F| wc -l`
if [ $n -eq 0 ]; then
echo "/dev/$F /mnt/$F ext4 defaults 0 0" >> /etc/fstab
else
sed -i '/^\/dev\/$F/c\/dev/$F /mnt/$F ext4 defaults 0 0' /etc/fstab
fi
mount -a
df -Th
;;
b|B)
mkfs.xfs -f /dev/$F
echo "该分区将被挂载在 /mnt/$F 下"
m=`ls /mnt/|grep $F | wc -l`
if [ $m -eq 0 ]; then
mkdir /mnt/$F
fi
n=`cat /etc/fstab | grep /dev/$F| wc -l`
if [ $n -eq 0 ]; then
echo "/dev/$F /mnt/$F xfs defaults 0 0" >> /etc/fstab
else
sed -i '/^\/dev\/$F/c\/dev/$F /mnt/$F xfs defaults 0 0' /etc/fstab
fi
mount -a
df -Th
;;
*)
echo "你的输入有误!!"
;;
esac2. Use an entire disk to create an LVM logical volume
The script verifies the disk, creates a physical volume with pvcreate, a volume group named myvg, and a logical volume of user‑specified size. It then formats the LV as ext4 or xfs, creates a mount point, updates /etc/fstab, and mounts the volume.
#!/bin/bash
# Function: 使用一整块硬盘创建LVM逻辑卷
read -p "请输入你要做成逻辑卷的硬盘(写绝对路径,如:/dev/sda):" path
if [ -e $path ]; then
echo "true"
else
echo "该设备不存在!!"
exit
fi
pvcreate $path
echo "该硬盘已做成物理卷!"
vgcreate myvg $path
echo "该物理卷已加入卷组 myvg 中"
vgs
free=`vgs| awk '$1~/myvg/{print}'|awk '{print $6}'`
echo "该物理卷剩余的空间大小为:$free "
read -p "请输入你要创建逻辑卷的大小(如:1G):" repy2
lvcreate -L $repy2 -n mylv myvg
echo "已成功创建逻辑卷mylv"
echo "------------------------"
lvs
echo "------------------------"
echo "你想对新分区设定什么类型的文件系统?有以下选项:"
echo "A:ext4文件系统"
echo "B:xfs文件系统"
read -p "请输入你的选择:" repy3
case $repy3 in
a|A)
mkfs.ext4 /dev/myvg/mylv
echo "该分区将被挂载在 /mnt/mylv 下"
m=`ls /mnt/|grep mylv | wc -l`
if [ $m -eq 0 ]; then
mkdir /mnt/mylv
fi
echo "/dev/myvg/mylv /mnt/mylv ext4 defaults 0 0" >> /etc/fstab
mount -a
df -Th
;;
b|B)
mkfs.xfs -f /dev/myvg/mylv
echo "该分区将被挂载在 /mnt/mylv 下"
m=`ls /mnt/|grep mylv | wc -l`
if [ $m -eq 0 ]; then
mkdir /mnt/mylv
fi
echo "/dev/myvg/mylv /mnt/mylv xfs defaults 0 0" >> /etc/fstab
mount -a
df -Th
;;
*)
echo "你的输入有误!!"
;;
esac3. Partition a disk and turn the partition into a logical volume
This script creates a primary partition, changes its type to LVM (code 8e), runs partprobe, creates a physical volume on the new partition, adds it to the myvg volume group, creates a logical volume, formats it, updates /etc/fstab, and mounts it.
#!/bin/bash
# Author: 谢公子
# Date: 2018-10-13
# Function: 新建一个分区,并做成逻辑卷
cat /proc/partitions > old
read -p "请输入你要分区的硬盘(写绝对路径,如:/dev/sda):" A
if [ -e $A ]; then
echo "true"
else
echo "该设备不存在!!"
exit
fi
read -p "请输入你要创建的磁盘分区类型(这里只能是主分区,默认按回车即可):" B
read -p "请输入分区数字,范围1-4,默认从1开始,默认按回车即可:" C
read -p "请输入扇区起始表号,默认按回车即可:" D
read -p "请输入你要分区的分区大小(格式:如 +5G ):" E
read -p "请输入你要划分为逻辑卷的分区盘符(默认回车即可):" Z
fdisk $A << EOF
n
p
$C
$D
$E
t
$Z
8e
p
w
EOF
echo "一个标准LVM的分区已经建立好!!"
partprobe $A
echo "-------------------------------"
cat /proc/partitions
cat /proc/partitions > new
F=`diff new old | grep "<" | awk '{print $5}'`
echo "-------------------------------"
echo $F
pvcreate /dev/$F
echo "该硬盘已做成物理卷!"
n=`vgs | grep myvg | wc -l`
if [ $n -eq 0 ]; then
vgcreate myvg /dev/$F
echo "该物理卷已加入卷组myvg中"
else
vgextend myvg /dev/$F
echo "该物理卷已加入卷组myvg中"
vgs
free=`vgs| awk '$1~/myvg/{print}'|awk '{print $7}'`
echo "该卷组剩余的空间大小为:$free "
lvs
exit
fi
vgs
free=`vgs| awk '$1~/myvg/{print}'|awk '{print $6}'`
echo "该卷组剩余的空间大小为:$free "
read -p "请输入你要创建逻辑卷的大小(如:1G):" repy2
lvcreate -L $repy2 -n mylv myvg
echo "已成功创建逻辑卷mylv"
echo "------------------------"
lvs
echo "------------------------"
echo "你想对新分区设定什么类型的文件系统?有以下选项:"
echo "A:ext4文件系统"
echo "B:xfs文件系统"
read -p "请输入你的选择:" G
case $G in
a|A)
mkfs.ext4 /dev/myvg/mylv
echo "该分区将被挂载在 /mnt/$F 下"
m=`ls /mnt/|grep $F | wc -l`
if [ $m -eq 0 ]; then
mkdir /mnt/$F
fi
echo "/dev/myvg/mylv /mnt/$F ext4 defaults 0 0" >> /etc/fstab
mount -a
df -Th
;;
b|B)
mkfs.xfs -f /dev/myvg/mylv
echo "该分区将被挂载在 /mnt/$F 下"
m=`ls /mnt/|grep $F | wc -l`
if [ $m -eq 0 ]; then
mkdir /mnt/$F
fi
echo "/dev/myvg/mylv /mnt/$F xfs defaults 0 0" >> /etc/fstab
mount -a
df -Th
;;
*)
echo "你的输入有误!!"
;;
esacSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
