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:

If you don't have the Yandex.Cloud command line interface yet, install it.

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 |     INSTANCE IDS     |       DESCRIPTION       |
    +----------------------+--------------+-------------+---------------+--------+----------------------+-------------------------+
    | a7lqgbt0bb9s40tifuhc | first-disk   | 20401094656 | ru-central1-a | READY  | a7lcvu28njbhnkcteb5n |                         |
    | a7lv5j5hm1p1hppprga7 | second-disk  | 21474836480 | ru-central1-a | READY  |                      |                         |
    +----------------------+--------------+-------------+---------------+--------+----------------------+-------------------------+
    
  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
    

    Note

    Empty disks do not have a file system. If you attach an empty disk, partition and mount it manually. Alternatively, instead of attaching an empty disk, you can create a snapshot of the boot disk and create a VM based on the snapshot.

    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.

Partition and mount an empty disk on Linux

To partition and mount an empty disk yourself:

  1. Run the lsblk command to check whether the disk is connected as a device and get its path in the system. An empty disk is usually labeled /dev/vdb.

  2. Partition your disk. To do this, create partitions using cfdisk, fdisk, or parted utilities. Format the disk for the appropriate file system. For example, you can use the mkfs utility.

  3. Run the mount command to mount the partition.

  4. Run the df command to check the state of the file system.