Useful Bash Scripts for Disk Partitioning and LVM Management
This article provides three interactive Bash scripts that automate Linux disk partitioning, create standard ext4 or XFS filesystems, and set up LVM logical volumes—complete with fdisk commands, pvcreate/vgcreate/lvcreate steps, filesystem formatting, mounting, and /etc/fstab updates.
1. Create a primary partition and format it as ext4 or XFS
The first script guides the user through interactive prompts to select a target disk (e.g., /dev/sda), verify its existence, and specify partition parameters such as partition number, start sector, and size. It then uses fdisk with a here‑document to create the partition, runs partprobe, and displays the new partition table.
After the partition is created, the script asks the user to choose a filesystem type (ext4 or XFS). Depending on the choice, it formats the new partition with mkfs.ext4 or mkfs.xfs, creates a mount point under /mnt/ if needed, updates /etc/fstab (adding a new line or replacing an existing one with sed), mounts the filesystem, and finally runs df -Th to show the result.
#! /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 second script asks for a whole disk, verifies it, and then creates a physical volume with pvcreate. It creates a volume group named myvg, reports the free space, and prompts for the size of a new logical volume. After creating the LV with lvcreate, it again offers a choice between ext4 and XFS, formats the LV, creates a mount point, updates /etc/fstab, mounts, and displays usage.
#!/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 then turn the partition into an LVM logical volume
The third script combines the previous two approaches. It partitions a specified disk, marks the new partition with type 8e (Linux LVM), creates a physical volume on that partition, adds it to a volume group myvg (creating or extending the group as needed), and then creates a logical volume. The user again selects the filesystem type, formats, mounts, and updates /etc/fstab.
#! /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
fi
free=`vgs| awk '$1~/myvg/{print}'|awk '{print $7}'`
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 "你的输入有误!!"
;;
esacAll three scripts follow the same pattern: gather user input, validate devices, perform partitioning or LVM commands, format the resulting block device, ensure a mount point exists, update /etc/fstab for persistent mounting, and finally mount and display the filesystem status.
Linux Tech Enthusiast
Focused on sharing practical Linux technology content, covering Linux fundamentals, applications, tools, as well as databases, operating systems, network security, and other technical 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.
