Attaching a disk to a VM

    When you select a disk to attach to a VM, you can specify whether that disk should be deleted when deleting the VM. You can choose this option when creating a VM, updating it, and attaching a new disk to it.

    To attach a disk to a VM:

    The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name or --folder-id flag.

    1. See the description of the CLI's attach disk command:

      $ yc compute instance attach-disk --help
      
    2. Get a list of VMs in the default folder:

      $ yc compute instance list
      +----------------------+-----------------+---------------+---------+----------------------+
      |          ID          |       NAME      |    ZONE ID    | STATUS  |     DESCRIPTION      |
      +----------------------+-----------------+---------------+---------+----------------------+
      | fhm0b28lgfp4tkoa3jl6 | first-instance  | ru-central1-a | RUNNING | my first vm via CLI  |
      | fhm9gk85nj7gcoji2f8s | second-instance | ru-central1-a | RUNNING | my second vm via CLI |
      +----------------------+-----------------+---------------+---------+----------------------+
      
    3. Select the ID or NAME of the VM you need (for example, first-instance).

    4. Stop the VM:

      $ yc compute instance stop first-instance
      
    5. Get a list of disks in the default folder:

      $ yc compute disk list
      +----------------------+-------------+-------------+---------------+--------+------------------------+
      |          ID          |     NAME    |    SIZE     |     ZONE      | STATUS |      DESCRIPTION       |
      +----------------------+-------------+-------------+---------------+--------+------------------------+
      | fhm4aq4hvq5g3nepvt9b | first-disk  | 10737418240 | ru-central1-a | READY  | my first disk via CLI  |
      | fhmo6rdqg5folrc3lsaf | second-disk | 8589934592  | ru-central1-a | READY  | my second disk via CLI |
      +----------------------+-------------+-------------+---------------+--------+------------------------+
      
    6. Select the ID or NAME of the necessary disk, for example first-disk. To view the list of disks attached to the VM, run the command:

      $ yc compute instance get --full first-instance
      
    7. Attach the disk to the VM:

      $ yc compute instance attach-disk first-instance \
          --disk-name first-disk \
          --mode rw
      

      To specify whether to delete the disk when deleting the VM, set the --auto-delete flag:

      $ yc compute instance attach-disk first-instance \
          --disk-name first-disk \
          --mode rw \
          --auto-delete
      
    8. Start the VM:

      $ yc compute instance start first-instance
      

    Use the attachDisk method for the Instance resource.