Attaching a disk to a VM
You can attach a secondary disk to either a running or stopped VM.
On a Linux VM deployed from Cloud Marketplace or custom images with the driver kit installed, a secondary disk you attach will be available as a new device without rebooting the VM.
To successfully attach a disk, the VM operating system must be fully booted or stopped; otherwise the operation will fail. If an error occurs, stop the VM and try again.
On a Windows VM, a secondary disk you attach will only show up as a new device after a reboot.
When selecting a disk to attach to your VM, you can specify whether to delete that disk when deleting the VM. You can enable this option when creating or updating a VM, or when attaching a new disk to it.
Attaching a disk
Note
You can only attach a local disk to a VM on a dedicated host while creating it. For more information, see these guides.
- In the management console
, select the folder the VM belongs to. - Select Compute Cloud.
- In the left-hand panel, select
Disks. - Select an unattached disk or create a new one.
- Next to the disk you want to attach, click
and select Attach. - In the window that opens:
-
In the VM field, select the virtual machine you want to attach your disk to.
-
To attach an encrypted disk, select a service account with the
kms.keys.encrypterDecrypter
role for the KMS key that was used to encrypt the disk. -
Enter the device name.
-
Enable Delete along with the virtual machine, if required.
-
- Click Attach.
If you do not have the Yandex Cloud CLI yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder through the --folder-name
or --folder-id
parameter.
-
See the description of the CLI command for attaching disks:
yc compute instance attach-disk --help
-
Get a list of VMs in the default folder:
yc compute instance list
Result:
+----------------------+-----------------+---------------+---------+----------------------+ | ID | NAME | ZONE ID | STATUS | DESCRIPTION | +----------------------+-----------------+---------------+---------+----------------------+ | fhm0b28lgfp4******** | first-instance | ru-central1-a | RUNNING | my first vm via CLI | | fhm9gk85nj7g******** | second-instance | ru-central1-a | RUNNING | my second vm via CLI | +----------------------+-----------------+---------------+---------+----------------------+
-
Select
ID
orNAME
of the VM, e.g.,first-instance
. -
Get a list of disks in the default folder:
yc compute disk list
Result:
+----------------------+--------------+-------------+-------------------+--------+----------------------+-----------------+-------------+ | ID | NAME | SIZE | ZONE | STATUS | INSTANCE IDS | PLACEMENT GROUP | DESCRIPTION | +----------------------+--------------+-------------+-------------------+--------+----------------------+-----------------+-------------+ | a7lqgbt0bb9s******** | first-disk | 20401094656 | ru-central1-a | READY | a7lcvu28njbh******** | | | | a7lv5j5hm1p1******** | second-disk | 21474836480 | ru-central1-a | READY | | | | +----------------------+--------------+-------------+-------------------+--------+----------------------+-----------------+-------------+
-
Select
ID
orNAME
of the disk, e.g.,first-disk
. To view a list of disks attached to the VM, run this 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 are attaching an empty disk, partition and mount it manually. Alternatively, rather than attaching an empty disk, you can create a snapshot of the boot disk and use it to create a VM.
If an error occurs, stop the VM:
yc compute instance stop first-instance
Then, try to attach the disk once again.
-
If the VM was stopped, restart it:
yc compute instance start first-instance
Use the attachDisk REST API method for the Instance resource or the InstanceService/AttachDisk gRPC API call.
Mounting a disk created from a snapshot or image
-
Attach the disk to the VM.
-
Connect to the VM via SSH.
-
Run the
blkid
command and make sure there are no partitions with the same UUIDs:sudo blkid
Result:
/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 Linux images provided by Yandex Cloud. The file system type is returned by theblkid
command in theTYPE
parameter.If you have a different file system, use the appropriate commands. For example, 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
Result:
/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 these IDs must be unique. For example, if you clone a boot disk and attach it to the same VM, the VM will have two partitions with identical UUIDs. As a result, the VM may boot from the root partition of the wrong disk, rather than 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/vdb2 sudo mount /dev/vdb2 /mnt/vdb2
-
Run the
df
command to check the state of the file system.
-
Connect to the VM via RDP.
-
Run Computer Management (the
compmgmt.msc
snap-in) as an administrator. -
Under Storage, select Disk Management.
Note
When you attach a disk to a running VM, it might not appear in the list. In that case, restart the OS and repeat the steps in this guide.
-
If the attached disk is Offline, right-click it and select Online.
-
Assign a drive letter to the attached disk, if required. To learn how to do this, see the relevant Microsoft documentation
. -
Open Explorer to check that the attached disk is properly mounted and available.
Partitioning and mounting an empty disk
-
Attach an empty disk to the VM.
-
Check whether the disk is attached as a device and get its path in the system:
ls -la /dev/disk/by-id
Result:
total 0 drwxr-xr-x 2 root root 140 Jan 16 12:09 . drwxr-xr-x 6 root root 120 Jan 13 13:51 .. lrwxrwxrwx 1 root root 9 Jan 16 12:09 virtio-fhm1dn62tm5d******** -> ../../vdc lrwxrwxrwx 1 root root 9 Jan 13 13:51 virtio-fhm4ev6dodt9******** -> ../../vdb lrwxrwxrwx 1 root root 10 Jan 13 13:51 virtio-fhm4ev6dodt9********-part1 -> ../../vdb1 lrwxrwxrwx 1 root root 10 Jan 13 13:51 virtio-fhm4ev6dodt9********-part2 -> ../../vdb2 lrwxrwxrwx 1 root root 9 Jan 13 13:51 virtio-nvme-disk-0 -> ../../vda
Where:
- Network disks have links in
virtio-<disk_ID>
format. For example,virtio-fhm1dn62tm5d******** -> ../../vdc
means that an unpartitioned disk with thefhm1dn62tm5d********
ID is labeledvdc
. - On dedicated hosts, local disks have links in
virtio-nvme-disk-<disk_number>
format (if you attached the disks to the VM when creating it). Disk numbering starts from zero. For example,virtio-nvme-disk-0 -> ../../vda
means that the first local disk (numbered zero) is labeledvda
.
- Network disks have links in
-
Partition your disk. To do this, create partitions
on the disk using thecfdisk
utility , thefdisk
utility , or theparted
utility .Run commands as a superuser. To do this, use
sudo
in each command, or before you start, run thesudo su -
command to switch your terminal to superuser mode. For example, let's create partitions usingfdisk
:-
Run the utility:
sudo fdisk /dev/<disk_label>
Where
<disk_label>
is the label of the disk you want to partition, e.g.,vdb
orvdc
.You will be taken to the
fdisk
menu. To get a list of available commands, typem
and press Enter. -
Create a new partition: type
n
and press Enter. -
Set the partition as primary: type
p
and press Enter. -
You will be prompted to select a number for the partition. Press Enter to create partition
1
. -
Leave the default values for the numbers of the first and last sectors of the partition: press Enter twice.
-
Make sure the partition was successfully created. For this, request a list of disk partitions: type
p
and press Enter.Result:
Device Boot Start End Sectors Size Id Type /dev/vdc1 2048 41943039 41940992 20G 83 Linux
Where
vdc1
is the partition label consisting of the disk label and the partition number. You will need the partition label for further partition operations. -
To save your changes, type
w
and press Enter.
-
-
Format the partition to the required file system. To do this, you can use the
mkfs
utility . For example, to format the partition toext4
, run the following command, specifying the label of the partition you created earlier:sudo mkfs.ext4 /dev/<partition_label>
Result:
Creating filesystem with 261888 4k blocks and 65536 inodes Filesystem UUID: 584a800c-e1fc-4f66-9228-a444f2d7440d Superblock backups stored on blocks: 32768, 98304, 163840, 229376 Allocating group tables: done Writing inode tables: done Creating journal (4096 blocks): done Writing superblocks and filesystem accounting information: done
Where
Filesystem UUID
is the unique partition ID. You will need the partition ID when setting up automatic mounting of this partition to the system. You can also get the partition ID using thesudo blkid /dev/<partition_label>
command. -
Mount the disk partition using the
mount
utility . To mount the/dev/<partition_label>
partition to the/mnt/new_disk
directory, run the following command:sudo mkdir /mnt/new_disk && sudo mount /dev/<partition_label> /mnt/new_disk
-
Configure the partition write and read permissions using the
chmod
utility . For example, to allow all users to write to the partition, run the following command:sudo chmod a+w /mnt/new_disk
-
Configure automatic mounting of the partition to the
mnt/new_disk
directory when the VM starts:-
Open the
/etc/fstab
file, e.g., using thenano
text editor:sudo nano /etc/fstab
-
Add the following line to the end of the file, specifying the ID of your partition in the
UUID
parameter:UUID=<partition_ID> /mnt/new_disk ext4 defaults 0 2
Where
UUID
is the unique partition ID obtained earlier during formatting, e.g.,584a800c-e1fc-4f66-9228-a444f2d7440d
. -
Save the changes and close the file.
-
-
Check the state of your file systems:
df
Result:
Filesystem 1K-blocks Used Available Use% Mounted on udev 989424 0 989424 0% /dev tmpfs 203524 816 202708 1% /run /dev/vdb2 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/vdc1 523260 3080 520180 1% /mnt/vdc1
-
Connect to the VM via RDP.
-
Run Computer Management (the
compmgmt.msc
snap-in) as an administrator. -
Under Storage, select Disk Management.
Note
When you attach a disk to a running VM, it might not appear in the list. In that case, restart the OS and repeat the steps in this guide.
-
If the attached disk is Offline, right-click it and select Online.
-
Initialize the disk. To do this, right-click it and select Initialize Disk. This will open the Initialize Disk dialog box.
-
Select the partition style
and click OK. -
Create partitions on the disk. To do this, right-click an empty disk and select New Simple Volume.
-
Use the New Simple Volume Wizard to set the partition size, assign a drive letter
, and specify the file system type. -
Open Explorer to check that the attached disk is properly mounted and available.