扩容Azure免费虚拟机的硬盘大小

微软免费使用一年的Azure虚拟机,默认提供了一个64G的磁盘,但是系统却只给分配了32个G,尝试了几次扩大分区,最终都导致系统崩溃了,只能重新开虚拟机,无奈,只好网上找来现成的脚本,自动调整分区大小,只需要输入想调整为多少G即可,终于成功把系统分区扩大了。

更改分区大小的脚本:https://github.com/EugenMayer/parted-auto-resize

#!/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
View Code

 

 

该脚本来自:https://serverfault.com/questions/870594/resize-partition-to-maximum-using-parted-in-non-interactive-mode

沙箱模式,用于预览可能发生的变化:./resize.sh /dev/sda 1

如果确认没问题,可以使用:./resize.sh /dev/sda 1 apply

其中1是指第一个分区

扩展空间成功后,只是分区变大了,文件系统还是原来的大小,就是通过df命令看,还是32G,这时候,需要使用resize2fs指令:resize2fs /dev/sda1

执行完后,再用df -h查看,空间已经变大了。

 

posted @ 2022-05-18 09:49  Easy C#  阅读(229)  评论(0编辑  收藏  举报