微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

更改分区大小的脚本:

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-@R_936_4045@ions 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

该脚本来自: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查看,空间已经变大了。

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐