[英]Automate GCP persistent disk initialization
Are there any scripts that automate persistent disks formatting and attaching to the Google Cloud VM instance, instead of doing formatting & mounting steps ?是否有任何脚本可以自动格式化永久磁盘并将其附加到 Google Cloud VM 实例,而不是执行格式化和安装步骤?
The persistent disk is created with Terraform, which also creates a VM and attaches the disk to it with the attached_disk
command.永久磁盘是使用 Terraform 创建的,它还会创建一个 VM 并使用attached_disk
命令将磁盘附加到它。
I am hoping to run a simple script on the VM instance start that would:我希望在 VM 实例启动时运行一个简单的脚本:
Have you considered using a startup script on the instance (I presume you can also add a startup-script with Terraform)?您是否考虑过在实例上使用启动脚本(我认为您也可以使用 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).您可以使用if
循环来发现磁盘是否已格式化,如果没有,您可以尝试运行您链接的文档中的格式化/挂载命令(我意识到您已经建议您不要遵循文档中的手动步骤,但这些都可以集成到启动脚本中以达到预期的效果)。
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
The marked answer did not work for me as the sudo blkid /dev/sdb
part always returned a value (hence, true) and the script would exit.标记的答案对我不起作用,因为sudo blkid /dev/sdb
部分总是返回一个值(因此为真)并且脚本将退出。
I updated the script to check for the entry in fstab
and added safety options to the script.我更新了脚本以检查fstab
的条目并向脚本添加了安全选项。
#!/bin/bash
set -uxo pipefail
MNT_DIR=/mnt/disks/persistent_storage
DISK_NAME=my-disk
# Check if entry exists in fstab
grep -q "$MNT_DIR" /etc/fstab
if [[ $? -eq 0 ]]; then # Entry exists
exit
else
set -e # The grep above returns non-zero for no matches & we don't want to exit then.
# Find persistent disk's drive value, prefixed by `google-`
DEVICE_NAME="/dev/$(basename $(readlink /dev/disk/by-id/google-${DISK_NAME}))"
sudo mkfs.ext4 -m 0 -F -E lazy_itable_init=0,lazy_journal_init=0,discard $DEVICE_NAME
sudo mkdir -p $MOUNT_DIR
sudo mount -o discard,defaults $DEVICE_NAME $MOUNT_DIR
# Add fstab entry
echo UUID=$(sudo blkid -s UUID -o value $DEVICE_NAME) $MNT_DIR ext4 discard,defaults,nofail 0 2 | sudo tee -a /etc/fstab
fi
Here's the gist if you want to download it - https://gist.github.com/raj-saxena/3dcaa5c0ba0be88ed91ef3fb50d3ce85如果你想下载它,这是要点 - https://gist.github.com/raj-saxena/3dcaa5c0ba0be88ed91ef3fb50d3ce85
Formatting, mounting and adding entry in /etc/fstab is necessary almost all the time.几乎所有时间都需要在 /etc/fstab 中格式化、挂载和添加条目。 Here is a solution I came up with and might help others.这是我想出的解决方案,可能会对其他人有所帮助。 This can also, for sure, be improved.当然,这也可以改进。 I added echo commands to explain what each block does.我添加了 echo 命令来解释每个块的作用。
About disk name you could add device_name on your terraform code when you attach your disks to the instance(s) like mentioned here: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_attached_disk关于磁盘名称,当您将磁盘附加到此处提到的实例时,您可以在 terraform 代码中添加设备名称: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_attached_disk
device_name - (Optional) Specifies a unique device name of your choice that is reflected into the /dev/disk/by-id/google- tree of a Linux operating system running within the instance. device_name -(可选)指定您选择的唯一设备名称,该名称反映在实例中运行的 Linux 操作系统的 /dev/disk/by-id/google-树中。 This name can be used to reference the device for mounting, resizing, and so on, from within the instance.*此名称可用于从实例中引用用于安装、调整大小等的设备。*
#!/bin/bash
DISKS_PATH=/dev/disk/by-id
DISKS=(disk1 disk2)
check_disks () {
for disk in "${DISKS[@]}"; do
MOUNT_DIR="/$disk"
echo "$MOUNT_DIR"
if sudo blkid $DISKS_PATH/google-${disk}; then
echo "$disk is already formatted, nothing to do"
echo "checking if $disk is present in fstab"
UUID=$(sudo blkid -s UUID -o value $DISKS_PATH/google-${disk})
grep -q "UUID=${UUID} $MOUNT_DIR" /etc/fstab
if [[ $? -eq 0 ]]; then
echo "$disk already present in fstab, continuing with checking mount"
echo "Now checking if $disk is already mounted"
grep -qs "$MOUNT_DIR" /proc/mounts
if [[ $? -eq 0 ]]; then
echo "$disk is already mounted, so doing nothing with mount"
else
echo "$disk is not mounted, so mounting it"
sudo mkdir -p $MOUNT_DIR
sudo mount -o discard,defaults $DISKS_PATH/google-${disk} $MOUNT_DIR
fi
elif [[ $? -ne 0 ]]; then
echo "$disk not present in fstab, so adding it"
echo UUID="$UUID" $MOUNT_DIR xfs discard,defaults,nofail 0 2 | sudo tee -a /etc/fstab
echo "Now checking if $disk is already mounted"
grep -qs "$MOUNT_DIR" /proc/mounts
if [[ $? -eq 0 ]]; then
echo "$disk is already mounted, so doing nothing with mount"
else
echo "$disk is not mounted, so mounting it"
sudo mkdir -p $MOUNT_DIR
sudo mount -o discard,defaults $DISKS_PATH/google-${disk} $MOUNT_DIR
fi
fi
else
echo "Formatting ${disk}"
sudo mkfs.ext4 $DISKS_PATH/google-${disk};
echo "Creating directory for ${disk} on $MOUNT_DIR"
sudo mkdir -p $MOUNT_DIR
echo "adding $disk in fstab"
UUID=$(sudo blkid -s UUID -o value $DISKS_PATH/google-${disk})
echo UUID="$UUID" $MOUNT_DIR xfs discard,defaults,nofail 0 2 | sudo tee -a /etc/fstab
echo "Mounting $disk"
sudo mount -o discard,defaults $DISKS_PATH/google-${disk} $MOUNT_DIR
fi
done
}
check_disks
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.