简体   繁体   English

terraform:azure storage_data_disk 重新启动后丢失

[英]terraform: azure storage_data_disk lost after reboot

I used the template bellow to create a VM in Azure with terraform.我使用下面的模板在 Azure 和 terraform 中创建了一个 VM。 The data disk was created and it was used in provision phase:数据磁盘已创建并在配置阶段使用:

/dev/sdb1       6.9G   32M  6.5G   1% /mnt
/dev/sdc1        25G  3.7G   20G  16% /datadrive
tmpfs           341M     0  341M   0% /run/user/1000

After reboot my VM, the data_disk disappeared, what I'm doing wrong in VM creation, I need to persist the data_disk.重新启动我的虚拟机后,data_disk 消失了,我在创建虚拟机时做错了什么,我需要保留 data_disk。

/dev/sdb1       6.9G   32M  6.5G   1% /mnt
tmpfs           341M     0  341M   0% /run/user/1000

terraform template terraform 模板

resource "azurerm_managed_disk" "data-disk" {
  name                 = "datadisk-${random_string.resource-id.result}"
  location             = data.azurerm_resource_group.azure-resource-rg.location
  resource_group_name  = data.azurerm_resource_group.azure-resource-rg.name
  storage_account_type = "Standard_LRS"
  create_option        = "Empty"
  disk_size_gb         = "128"
}


# Create virtual machine
resource "azurerm_virtual_machine" "azure-vm" {
    name                  = "${var.prefix}-${random_string.resource-id.result}"
    location              = (var.location)

    (...)

    storage_os_disk {
        name              = "${var.prefix}-${random_string.resource-id.result}-disk"
        caching           = "ReadWrite"
        create_option     = "FromImage"
        managed_disk_type = "Premium_LRS"
    }

    storage_data_disk {
        name            = azurerm_managed_disk.data-disk.name
        managed_disk_id = azurerm_managed_disk.data-disk.id
        create_option   = "Attach"
        lun             = 0
        disk_size_gb    = azurerm_managed_disk.data-disk.disk_size_gb
    }

    storage_image_reference {
        publisher = (var.os-publisher)
        offer     = (var.os-offer)
        sku       = (var.os-sku)
        version   = (var.os-version)
    }

    (...)

}

EDIT编辑

script executed to mount the data disk:挂载数据盘的脚本:

mountpoint /datadrive || echo -e "o\nn\np\n\n\n\n\nt\nfd\nw\n" | sudo fdisk "/dev/sdc"
sudo mkdir /datadrive
sudo mkfs -t ext4 /dev/sdc1
sudo mount /dev/sdc1 /datadrive
sudo -i blkid

To ensure that the drive is remounted automatically after a reboot, it must be added to the /etc/fstab file.为确保驱动器在重新启动后自动重新安装,必须将其添加到/etc/fstab文件中。 It is also highly recommended that the UUID (Universally Unique IDentifier) is used in /etc/fstab to refer to the drive rather than just the device name (such as, /dev/sdc1).还强烈建议在/etc/fstab中使用 UUID(通用唯一标识符)来指代驱动器,而不仅仅是设备名称(例如 /dev/sdc1)。

To find the new disk UUID via sudo -i blkid , then add the following line to the end of the /etc/fstab file:要通过sudo -i blkid查找新的磁盘 UUID,然后将以下行添加到/etc/fstab文件的末尾:

UUID=33333333-3b3b-3c3c-3d3d-3e3e3e3e3e3e   /datadrive   ext4   defaults,nofail   1   2

Note笔记

Improperly editing the /etc/fstab file could result in an unbootable system.不正确地编辑/etc/fstab文件可能会导致系统无法启动。 If unsure, refer to the distribution's documentation for information on how to properly edit this file.如果不确定,请参阅发行版的文档以获取有关如何正确编辑此文件的信息。 It is also recommended that a backup of the /etc/fstab file is created before editing.还建议在编辑之前创建/etc/fstab文件的备份。

Read https://docs.microsoft.com/en-us/azure/virtual-machines/linux/attach-disk-portal#mount-the-disk阅读https://docs.microsoft.com/en-us/azure/virtual-machines/linux/attach-disk-portal#mount-the-disk

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM