Attaching a disk to a VM
You can attach an extra disk to either a running or stopped VM.
On a Linux VM deployed from Cloud Marketplace or custom images with the driver kit installed, the connected extra disk will be available as a new device without rebooting the VM.
For the disk to be successfully connected, 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, the connected extra disk will be available as a new device only after the VM reboots.
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
Note
You can only attach a local disk to a VM on a dedicated host while creating it. For more information, see this guide.
- 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 mount 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 a device name.
-
Enable the Delete along with the virtual machine option if needed.
-
- Click Attach.
If you do not 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
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 the
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 the
ID
orNAME
of the required disk, e.g.,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 such a snapshot.
If an error occurs, stop the VM:
yc compute instance stop first-instance
Then reattach the disk.
-
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 a disk to a VM instance.
-
Connect to the VM over SSH.
-
Run the
blkid
command and make sure that there are no partitions with duplicate 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 the 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
, execute: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 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.
-
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 steps 1 and 2 of this guide.
-
If the attached disk is Offline, right-click it and select Online.
-
Assign a letter to the attached disk if needed. For information about how to do this, see the 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 disk links have the
virtio-<disk_ID>
format. For example,virtio-fhm1dn62tm5d******** -> ../../vdc
means that an unpartitioned disk with thefhm1dn62tm5d********
ID is labeledvdc
. - Local disks on dedicated hosts have links, such as
virtio-nvme-disk-<disk_number>
(if you attached the disks to your 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 disk links have the
-
Partition your disk. To do this, create partitions
using thecfdisk
utility , thefdisk
utility , or theparted
utility .Run commands as a superuser. To do this, use
sudo
in each command, or run thesudo su -
command initially to switch to the superuser in the terminal. For example, let's create partitions usingfdisk
:-
Run the utility:
sudo fdisk /dev/<disk_label>
Where
<disk_label>
is the label of the disk to be partitioned, 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. -
Specify that the partition will be primary: type
p
and press Enter. -
You will be prompted to select a partition number. 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 has been created. To do 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 a disk label and a partition number. You will need the partition label for further actions with the partition. -
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 a partition toext4
, run the command, specifying the label of the previously created partition: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 a partition named/dev/<partition_label>
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 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 steps 1 and 2 of 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 opens the Initialize Disk dialog.
-
Select a partition style
and click OK. -
Create partitions on the disk. To do this, right-click the empty disk and select New Simple Volume.
-
Use the New Simple Volume Wizard to set the required 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.