使用 Whiptail 创建磁盘分区、格式化和挂载,以及 UUID 开机挂载
#!/bin/bash
定义变量
DISKS=$(lsblk -d -o NAME,SIZE | awk '$2!~/M$/ && $2!=0 {print $1}') PARTITIONS=$(lsblk -d -o NAME,SIZE | awk '$2~/M$/ && $2!=0 {print $1}') MOUNT_POINTS=$(df -h | awk '{print $6}' | sed '1d') UUIDS=$(blkid -o value -s UUID $PARTITIONS)
定义函数
select_disk() {
DISK=$(whiptail --menu 'Select a disk to partition and format:' 0 0 0
$(for disk in $DISKS; do echo '$disk $(( $(lsblk -d -o SIZE -b /dev/$disk) / 1024 / 1024 / 1024 ))GB'; done)
3>&1 1>&2 2>&3)
if [ $? -ne 0 ]; then exit 1; fi
}
create_partitions() {
whiptail --yesno 'Do you want to create a single partition on $DISK?' 0 0
--yes-button 'Yes' --no-button 'No'
if [ $? -eq 0 ]; then
PARTITION='$DISK'1
parted /dev/$DISK mklabel gpt mkpart primary ext4 0% 100%
else
whiptail --msgbox 'You will need to manually create the partitions.' 0 0
exit 1
fi
}
format_partitions() {
FILESYSTEM=$(whiptail --menu 'Select a filesystem for the partition:' 0 0 0
'ext2' ''
'ext3' ''
'ext4' ''
'fat32' ''
'ntfs' ''
3>&1 1>&2 2>&3)
if [ $? -ne 0 ]; then exit 1; fi
mkfs.$FILESYSTEM /dev/$PARTITION
}
mount_partitions() {
MOUNT_POINT=$(whiptail --inputbox 'Enter a mount point for the partition:' 0 0 '/'
3>&1 1>&2 2>&3)
if [ $? -ne 0 ]; then exit 1; fi
if [ -d $MOUNT_POINT ]; then
whiptail --yesno 'The directory $MOUNT_POINT already exists. Do you want to use it as the mount point?' 0 0
--yes-button 'Yes' --no-button 'No'
if [ $? -ne 0 ]; then exit 1; fi
else
mkdir -p $MOUNT_POINT
fi
echo '/dev/$PARTITION $MOUNT_POINT $FILESYSTEM defaults 0 0' >> /etc/fstab
mount -a
}
uuid_mount() {
UUID=$(whiptail --menu 'Select a partition to mount by UUID:' 0 0 0
$(for uuid in $UUIDS; do echo '$uuid $(lsblk -d -o NAME,SIZE | grep $(blkid -o value -s UUID=$uuid) | awk '{print $1}')'; done)
3>&1 1>&2 2>&3)
if [ $? -ne 0 ]; then exit 1; fi
if grep -q '$UUID' /etc/fstab; then
whiptail --msgbox 'The partition is already set to mount by UUID.' 0 0
exit 1
else
echo 'UUID=$UUID $(whiptail --inputbox 'Enter a mount point for the partition:' 0 0 '/'
3>&1 1>&2 2>&3) $FILESYSTEM defaults 0 0' >> /etc/fstab
mount -a
fi
}
主程序
select_disk create_partitions format_partitions mount_partitions uuid_mount
exit 0
原文地址: https://www.cveoy.top/t/topic/nTfW 著作权归作者所有。请勿转载和采集!