0
votes

I used the template bellow to create a VM in Azure with terraform. 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.

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

terraform template

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
1
Have you checked if the disk is still there in the Azure portal? I assume that the disk was not configured to be automatically mounted. Can you check if the file /etc/fstab contains an entry for this disk? - NillsF
The disk is still the after I restart the VM. I added the script that I used to mount the disk, could you help me? - Tiago Feitor

1 Answers

0
votes

To ensure that the drive is remounted automatically after a reboot, it must be added to the /etc/fstab file. 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).

To find the new disk UUID via sudo -i blkid, then add the following line to the end of the /etc/fstab file:

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. 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.

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