如何使用 bash 动态查找有可用空间的磁盘来创建单独的分区

如何使用 bash 动态查找有可用空间的磁盘来创建单独的分区

我想首先声明,我是磁盘分区的新手,因此,如果在任何时候我想要的结果是在工作站上执行不可能的操作,请告诉我实现类似结果的正确方法。谢谢!

问题

我想编写一个 bash 脚本来管理多个 RHEL 工作站,所有工作站可能具有不同数量的磁盘以及不同的磁盘分区。

我想要完成的是4GiB在工作站上找到的合适磁盘上创建一个大小合适的新(单独)分区,并且如果脚本找不到任何适合创建单独分区的地方(由于磁盘存储空间不足,未分配)空格)然后脚本将打印"Unable to create separate partition on disk: Insufficient space"并退出。

另外,在执行此操作时,我是否需要担心会破坏/dev/mapper逻辑卷?

例子

此处所述的示例工作站分区并不是详尽的列表;这只是我在 RHEL 工作站网络中拥有的众多配置中的其中两种。

Example 1:

# fdisk -l
Disk /dev/sda: 64 GiB, 68719476736 bytes, 134217728 sectors
Disk model: 
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 

Device         Start       End   Sectors    Size Type
/dev/sda1       2048   1230847   1228800    600M EFI System
/dev/sda2    1230848   3327999   2097152      1G Linux filesystem
/dev/sda3    3328000 134215679 130887680   62.4G Linux LVM

在这个只有 1 个物理磁盘的工作站中,我希望脚本自动查找可以划分4GiB.例如,如果/dev/sda3可以分区4GiB空间,我希望最终的分区是这样的:

# fdisk -l
Disk /dev/sda: 64 GiB, 68719476736 bytes, 134217728 sectors
Disk model: 
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 

Device         Start         End   Sectors    Size Type
/dev/sda1       2048     1230847   1228800    600M EFI System
/dev/sda2    1230848     3327999   2097152      1G Linux filesystem
/dev/sda3    3328000   125827071 122499072   58.4G Linux LVM
/dev/sda4    125827072 134215679   8388608      4G Linux   # numbers here estimated based on calculation only

Example 2:

# fdisk -l
Disk /dev/sda: 64 GiB, 68719476736 bytes, 134217728 sectors
Disk model: 
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 

Device         Start       End   Sectors    Size Type
/dev/sda1       2048   1230847   1228800    600M EFI System
/dev/sda2    1230848   3327999   2097152      1G Linux filesystem
/dev/sda3    3328000 134215679 130887680   62.4G Linux LVM


Disk /dev/sdb: 32 GiB, 34359738368 bytes, 67108864 sectors
Disk model: 
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 

Device        Start      End  Sectors Size Type
/dev/sdb1      2048 20973567 20971520  10G Linux LVM
/dev/sdb2  20973568 67108830 46135263  22G Linux LVM

在这种情况下,假设/dev/sda已满,脚本将自动检测它并进行分区/dev/sdb

# fdisk -l
Disk /dev/sda: 64 GiB, 68719476736 bytes, 134217728 sectors
Disk model: 
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 

Device         Start       End   Sectors    Size Type
/dev/sda1       2048   1230847   1228800    600M EFI System
/dev/sda2    1230848   3327999   2097152      1G Linux filesystem
/dev/sda3    3328000 134215679 130887680   62.4G Linux LVM


Disk /dev/sdb: 32 GiB, 34359738368 bytes, 67108864 sectors
Disk model: 
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 

Device        Start      End  Sectors Size Type
/dev/sdb1      2048 20973567 20971520  10G Linux LVM
/dev/sdb2  20973568 58722303 37748736  18G Linux LVM
/dev/sdb3  58722304 67106815  8384512   4G Linux LVM

非常感谢对此的任何帮助,谢谢!

相关内容