本文介绍了自动执行 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 格式化
  • 检查磁盘是否挂载,如果没有挂载
  • 别无他法

推荐答案

你有没有考虑过使用 startup实例上的脚本(我想您也可以使用 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 永久性磁盘初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 16:37