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. - Go to 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.encrypterDecrypterrole 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 installed yet, install and initialize it.
By default, the CLI uses the folder specified when creating the profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also set a different folder for any specific command using 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 listResult:
+----------------------+-----------------+---------------+---------+----------------------+ | 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
IDorNAMEof the VM, e.g.,first-instance. -
Get a list of disks in the default folder:
yc compute disk listResult:
+----------------------+--------------+-------------+-------------------+--------+----------------------+-----------------+-------------+ | 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
IDorNAMEof 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 rwTo automatically delete the disk when deleting the VM, set the
--auto-deleteflag.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-instanceThen, try to attach the disk once again.
-
If the VM was stopped, restart it:
yc compute instance start first-instance
With Terraform
Terraform is distributed under the Business Source License
For more information about the provider resources, see the relevant documentation on the Terraform
If you do not have Terraform yet, install it and configure the Yandex Cloud provider.
-
In the configuration file, add the new
secondary_disksection to theyandex_compute_instanceresource description:resource "yandex_compute_instance" "vm-1" { ... secondary_disk { disk_id = "<disk_ID>" } ... }Where
disk_idis the secondary disk ID.Note
The secondary disk and VM must be in the same availability zone.
For more information about
yandex_compute_diskproperties, see this Terraform article. -
Apply the new configuration:
-
In the terminal, go to the directory where you edited the configuration file.
-
Make sure the configuration file is correct using this command:
terraform validateIf the configuration is correct, you will get this message:
Success! The configuration is valid. -
Run this command:
terraform planYou will see a detailed list of resources. No changes will be made at this step. If the configuration contains any errors, Terraform will show them.
-
Apply the changes:
terraform apply -
Type
yesand press Enter to confirm the changes.
Terraform will update all the required resources. You can check the update using the management console
. -
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 over SSH.
-
Run the
blkidcommand and make sure there are no partitions with the same UUIDs:sudo blkidResult:
/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
blkidcommand output. In the example from the previous step, you need to generate a UUID for the/dev/vdb2partition:sudo e2fsck -f /dev/vdb2 sudo tune2fs -U $(uuidgen) /dev/vdb2This method works for partitions with
ext2,ext3, andext4file systems. The latter is used in Linux images provided by Yandex Cloud. The file system type is returned by theblkidcommand in theTYPEparameter.If you have a different file system, use the appropriate commands. For example, for
XFS, run the following:sudo xfs_admin -U generate /dev/vdb2To see if the UUID changed, run the
blkidcommand again:sudo blkidResult:
/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
mountutility. For example, mount thevdb2partition to the/mntdirectory:sudo mkdir /mnt/vdb2 sudo mount /dev/vdb2 /mnt/vdb2 -
Run the
dfcommand to check the state of the file system.
-
Connect to the VM via RDP.
-
Run Computer Management (the
compmgmt.mscsnap-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-idResult:
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 -> ../../vdaWhere:
- Network disks have links in
virtio-<disk_ID>format. For example,virtio-fhm1dn62tm5d******** -> ../../vdcmeans 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 -> ../../vdameans 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,fdisk, orpartedutility .Run commands as a superuser. To do this, use
sudoin 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.,vdborvdc.You will be taken to the
fdiskmenu. To get a list of available commands, typemand press Enter. -
Create a new partition: type
nand press Enter. -
Set the partition as primary: type
pand 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
pand press Enter.Result:
Device Boot Start End Sectors Size Id Type /dev/vdc1 2048 41943039 41940992 20G 83 LinuxWhere
vdc1is 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
wand press Enter.
-
-
Format the partition to the required file system. To do this, you can use the
mkfsutility . 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: doneWhere
Filesystem UUIDis 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
mountutility . To mount the/dev/<partition_label>partition to the/mnt/new_diskdirectory, 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
chmodutility . 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_diskdirectory when the VM starts:-
Open the
/etc/fstabfile, e.g., using thenanotext editor:sudo nano /etc/fstab -
Add the following line to the end of the file, specifying the ID of your partition in the
UUIDparameter:UUID=<partition_ID> /mnt/new_disk ext4 defaults 0 2Where
UUIDis 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:
dfResult:
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.mscsnap-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.