自动执行GCP永久磁盘初始化 [英] Automate GCP persistent disk initialization

查看:73
本文介绍了自动执行GCP永久磁盘初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何脚本可以自动格式化持久磁盘并附加到Google Cloud VM实例,而不是执行

Are there any scripts that automate persistent disks formatting and attaching to the Google Cloud VM instance, instead of doing formatting & mounting steps?

永久磁盘是使用Terraform创建的,它还会创建VM,并使用 attached_disk 命令将磁盘附加到该磁盘.

The persistent disk is created with Terraform, which also creates a VM and attaches the disk to it with the attached_disk command.

我希望在VM实例启动时运行一个简单的脚本,该脚本将:

I am hoping to run a simple script on the VM instance start that would:

  • 检查附加磁盘是否已格式化,并在需要时使用ext4格式化
  • 检查磁盘是否已安装,如果未安装,请安装
  • 不执行其他操作

推荐答案

您是否考虑过使用启动实例上的脚本(我想您也可以使用Terraform添加启动脚本)?您可以使用 if 循环来发现磁盘是否已格式化,否则,可以尝试在链接的文档中运行格式化/挂载命令(我意识到您已经建议您不要请按照文档中的手动步骤进行操作,但是可以将这些步骤集成到启动脚本中以实现所需的结果.

Have you considered using a startup script on the instance (I presume you can also add a startup-script with Terraform)? You could use an if loop to discover if the disk is formatted, then if not, you could try running the formatting/mounting commands in the documentation you linked (I realise you have suggested you do not want to follow the manual steps in the documentation, but these can be integrated into the startup script to achieve the desired result).

运行以下输出,如果磁盘未格式化,则运行空字符串:

Running the following outputs and empty string if the disk is not formatted:

 sudo blkid /dev/sdb

因此,您可以在启动脚本中使用它来发现磁盘是否已格式化,如果不是,则执行格式化/挂载.例如,您可以使用类似的方法(注意***如果磁盘已格式化但未安装,则可能很危险,如果您的用例可能涉及可能已经格式化的现有磁盘,则不应使用):

You could therefore use this in a startup script to discover if the disk is formatted, then perform formatting/mounting if that is not the case. For example, you could use something like this (Note*** If the disk is formatted but not mounted this could be dangerous and should not be used if your use case could involve existing disks which may have already been formatted):

#!/bin/bash


if sudo blkid /dev/sdb;then 
        exit
else 
        sudo mkfs.ext4 -m 0 -F -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/sdb; \
        sudo mkdir -p /mnt/disks/newdisk
        sudo mount -o discard,defaults /dev/sdb /mnt/disks/newdisk
fi

这篇关于自动执行GCP永久磁盘初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆