Eug*_*yer 10 linux partition lvm hypervisor
由于在这种情况下使用 fdisk 相当复杂,因为非交互式使用可能是不可能的,或者至少非常复杂(使用 printf),所以我想用于将parted resizepart分区大小调整为最大大小。
这可以用于虚拟机管理程序/云中的实际磁盘大小调整等场景,然后您需要将逻辑卷/pv 调整为新大小(LVM 情况),或者您想要将普通分区的分区大小调整为最大。
假设我想将磁盘 /dev/sda1 上的分区 /dev/sda1 调整到其最大可能大小 - 我将如何做到这一点而不被问到任何问题。
尽管parted /dev/sda resizepart 1存在,但它需要我计算并输入最大磁盘大小 - 这就是我在这里工作的实际线索
Tim*_*yev 13
似乎不同版本的parted行为有很大不同,因此这适用于parted版本 3.*,至少随 CentOS7 和 CentOS8 一起提供。
# echo 1 > /sys/block/sda/device/rescan
Run Code Online (Sandbox Code Playgroud)
# parted -s -a opt /dev/sda "print free"
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
32.3kB 1049kB 1016kB Free Space
1 1049kB 1075MB 1074MB primary xfs boot
2 1075MB 32.2GB 31.1GB primary lvm
32.2GB 53.7GB 21.5GB Free Space
Run Code Online (Sandbox Code Playgroud)
# parted -s -a opt /dev/sda "resizepart 2 100%"
# echo $?
0
Run Code Online (Sandbox Code Playgroud)
除了退出代码之外,此操作没有任何成功指示。
# parted -s -a opt /dev/sda "print free"
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
32.3kB 1049kB 1016kB Free Space
1 1049kB 1075MB 1074MB primary xfs boot
2 1075MB 53.7GB 52.6GB primary lvm
Run Code Online (Sandbox Code Playgroud)
作为奖励,您可以将所有这些放入一个命令行中(不要错过步骤 0):
# parted -s -a opt /dev/sda "print free" "resizepart 2 100%" "print free"
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
32.3kB 1049kB 1016kB Free Space
1 1049kB 1075MB 1074MB primary xfs boot
2 1075MB 32.2GB 31.1GB primary lvm
32.2GB 53.7GB 21.5GB Free Space
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
32.3kB 1049kB 1016kB Free Space
1 1049kB 1075MB 1074MB primary xfs boot
2 1075MB 53.7GB 52.6GB primary lvm
Run Code Online (Sandbox Code Playgroud)
提示:此脚本与parted v3+ OOTB兼容,如果您有parted 2,则需要更改parted resizepart为parted resize
让我们将其放入脚本中,acualy 命令是一个在线命令,我们只需添加更多内容以确保设置前 2 个参数:
#!/bin/bash
set -e
if [[ $# -eq 0 ]] ; then
echo 'please tell me the device to resize as the first parameter, like /dev/sda'
exit 1
fi
if [[ $# -eq 1 ]] ; then
echo 'please tell me the partition number to resize as the second parameter, like 1 in case you mean /dev/sda1 or 4, if you mean /dev/sda2'
exit 1
fi
DEVICE=$1
PARTNR=$2
APPLY=$3
fdisk -l $DEVICE$PARTNR >> /dev/null 2>&1 || (echo "could not find device $DEVICE$PARTNR - please check the name" && exit 1)
CURRENTSIZEB=`fdisk -l $DEVICE$PARTNR | grep "Disk $DEVICE$PARTNR" | cut -d' ' -f5`
CURRENTSIZE=`expr $CURRENTSIZEB / 1024 / 1024`
# So get the disk-informations of our device in question printf %s\\n 'unit MB print list' | parted | grep "Disk /dev/sda we use printf %s\\n 'unit MB print list' to ensure the units are displayed as MB, since otherwise it will vary by disk size ( MB, G, T ) and there is no better way to do this with parted 3 or 4 yet
# then use the 3rd column of the output (disk size) cut -d' ' -f3 (divided by space)
# and finally cut off the unit 'MB' with blanc using tr -d MB
MAXSIZEMB=`printf %s\\n 'unit MB print list' | parted | grep "Disk ${DEVICE}" | cut -d' ' -f3 | tr -d MB`
echo "[ok] would/will resize to from ${CURRENTSIZE}MB to ${MAXSIZEMB}MB "
if [[ "$APPLY" == "apply" ]] ; then
echo "[ok] applying resize operation.."
parted ${DEVICE} resizepart ${PARTNR} ${MAXSIZEMB}
echo "[done]"
else
echo "[WARNING]!: Sandbox mode, i did not size!. Use 'apply' as the 3d parameter to apply the changes"
fi
Run Code Online (Sandbox Code Playgroud)
将上面的脚本另存为resize.sh并使其可执行
# resize the fourth partition to the maximum size, so /dev/sda4
# this is no the sandbox mode, so no changes are done
./resize.sh /dev/sda 4
# apply those changes
./resize.sh /dev/sda 4 apply
Run Code Online (Sandbox Code Playgroud)
例如,如果您在使用 LVM 时在 /dev/sdb1 上有一个带有 lv“data”的 vg vgdata,则整个情况将如下所示
./resize.sh /dev/sdb 1 apply
pvresize /dev/sdb1
lvextend -r /dev/mapper/vgdata-data -l 100%FREE
Run Code Online (Sandbox Code Playgroud)
就是这样,调整了逻辑卷的大小,包括调整了文件系统的大小(-r) - 全部完成,检查它df -h:)
我们用来查找磁盘大小的是
resizepart ${PARTNR} `parted -l | grep ${DEVICE} | cut -d' ' -f3 | tr -d MB
Run Code Online (Sandbox Code Playgroud)
printf %s\\n 'unit MB print list' | parted | grep "Disk /dev/sdaa) 因此,获取我们所使用的相关设备的磁盘信息,printf %s\\n 'unit MB print list'以确保单位显示为 MB,否则它会因磁盘大小( MB、G、T )而异,并且没有更好的方法可以使用 parted 来做到这一点还剩3、4个
b) 然后使用输出的第三列(磁盘大小)cut -d' ' -f3(除以空间)
c) 最后用 blanc 切断单位“MB”tr -d MB
我在https://github.com/EugenMayer/parted-auto-resize上发布了脚本,因此如果有什么要改进功能的地方,请在那里使用拉取请求(任何不在这个问题范围内的内容)
| 归档时间: |
|
| 查看次数: |
56959 次 |
| 最近记录: |