Attaching a disk to a VM
You can attach a disk to either a running or stopped virtual machine.
For a disk to be successfully attached to a running VM, the operating system must be ready to accept commands to attach disks. Before attaching a disk, make sure that the OS is loaded or stop the virtual machine, otherwise the attach disk operation fails. If an error occurs, stop the VM and repeat the operation.
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, or attaching a new disk to it.
Attaching a disk
To attach a disk to a VM:
- Select the folder the VM belongs to.
- Select Yandex Compute Cloud.
- Go to Disks.
- Under Disks, select an unmounted disk or create a new one.
- Next to the desired disk, click and select Attach.
- In the window that opens:
- In the VM field, select the virtual machine you want to mount your disk to.
- Enter a Device name.
- Select the value of the Delete automatically parameter.
- Click Attach.
If you don't have the Yandex.Cloud command line interface yet, install and initialize 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
parameter.
-
View a description of the CLI attach disk command:
$ yc compute instance attach-disk --help
-
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 | +----------------------+-----------------+---------------+---------+----------------------+
-
Select the VM
ID
orNAME
(for example,first-instance
). -
Get a list of disks in the default folder:
yc compute disk list
Result:
+----------------------+--------------+-------------+---------------+--------+----------------------+-------------------------+ | 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 | | | +----------------------+--------------+-------------+---------------+--------+----------------------+-------------------------+
-
Select the
ID
orNAME
of the required disk (for example,first-disk
). To view a list of disks attached to the VM, run the command:$ yc compute instance get --full first-instance
-
Attach the disk to the VM:
$ yc compute instance attach-disk first-instance \ --disk-name first-disk \ --mode rw
To automatically delete the disk when deleting the VM, set the
--auto-delete
flag.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.
If an error occurs, stop the virtual machine:
$ yc compute instance stop first-instance
Then reattach the disk.
-
If the virtual machine was stopped, restart it:
$ yc compute instance start first-instance
Use the attachDisk method for the Instance resource.
Mounting a disk created from a snapshot or image
To use the attached disk:
Mount the disk:
-
Connect to the VM via SSH.
-
Run the
blkid
command and make sure that there are no partitions with duplicate UUIDs:$ sudo blkid /dev/vda2: UUID="0d6dfef0-542d-47ba-b55b-18ab5f5f9210" TYPE="ext4" PARTUUID="752aa845-94ee-4850-9188-71c2f919ee7b" /dev/vdb2: UUID="0d6dfef0-542d-47ba-b55b-18ab5f5f9210" TYPE="ext4" PARTUUID="752aa845-94ee-4850-9188-71c2f919ee7b" ...
-
If there are, generate a new UUID for the duplicates that come last in the
blkid
command output. In the example from the previous step, you need to generate a UUID for the/dev/vdb2
partition:$ sudo e2fsck -f /dev/vdb2 $ sudo tune2fs -U $(uuidgen) /dev/vdb2
This method works for partitions with
ext2
,ext3
, andext4
file systems. The latter is used in the Linux images provided by Yandex.Cloud. The file system type is returned by theblkid
command in theTYPE
parameter.If you use another filesystem, run the suitable command. For example, to generate a UUID for
XFS
, run the following:$ sudo xfs_admin -U generate /dev/vdb2
To see if the UUID changed, run the
blkid
command again:$ sudo blkid /dev/vda2: UUID="0d6dfef0-542d-47ba-b55b-18ab5f5f9210" TYPE="ext4" PARTUUID="752aa845-94ee-4850-9188-71c2f919ee7b" /dev/vdb2: UUID="ea004485-07fb-4128-b20d-e408db1e8ae8" TYPE="ext4" PARTUUID="752aa845-94ee-4850-9188-71c2f919ee7b"
When creating a snapshot or image in Linux, the UUID and PARTUUID of disk partitions are saved. Linux uses this data when mounting partitions, including the root one, so the IDs must be unique. For example, if you clone a boot disk and attach it to the same VM, two partitions with the same UUIDs appear on the VM. As a result, after starting the VM, the root partition may not be the one you specified as the boot disk.
-
Mount disk partitions using the
mount
utility. For example, mount thevdb2
partition to the/mnt
directory:sudo mkdir /mnt sudo mount /dev/vdb2 /mnt
-
Run the
df
command to check the state of the file system.
-
Connect to the VM via RDP.
-
Assign a letter to the attached disk. For information about how to do this, see the Microsoft documentation.
Partitioning and mounting an empty disk
To partition and mount an empty disk yourself:
-
Check if the disk is attached as a device and get its path in the system:
lsblk
Command execution result:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT vda 252:0 0 13G 0 disk ├─vda1 252:1 0 1M 0 part └─vda2 252:2 0 13G 0 part / vdb 252:16 0 1G 0 disk
An empty disk is usually labeled /dev/vdb.
-
Partition your disk. To do this, create partitions using cfdisk, fdisk, or parted.
-
Format the disk for the appropriate file system. For example, you can use the mkfs utility.
-
Mount disk partitions using the
mount
utility. For example, mount thevdb2
partition to the/mnt
directory:sudo mkdir /mnt sudo mount /dev/vdb2 /mnt
-
Check the file system status:
df
Command execution result:
Filesystem 1K-blocks Used Available Use% Mounted on udev 989424 0 989424 0% /dev tmpfs 203524 816 202708 1% /run /dev/vda2 13354932 2754792 10015688 22% / tmpfs 1017608 0 1017608 0% /dev/shm tmpfs 5120 0 5120 0% /run/lock tmpfs 1017608 0 1017608 0% /sys/fs/cgroup tmpfs 203520 0 203520 0% /run/user/1000 /dev/vdb2 523260 3080 520180 1% /mnt
-
Run the Computer Management tool as an administrator.
-
Under Storage, select Disk Management.
-
Initialize the disk. To do this, right-click on the empty disk and select Initialize Disk. This opens the Initialize Disk dialog.
-
Select a partition style and click ОК.
-
Create partitions on the disk. To do this, right-click on the empty disk and select New Simple Volume.
-
Use the New Simple Volume Wizard to set the desired partition size, assign a drive letter, and specify the file system type.