viernes, 13 de enero de 2023

virtual disk and LVM

There are several ways to create a virtual disk on Linux, but one of the most common ways is to use the command-line tool dd.

dd is a command-line utility that can be used to create a file of a specified size and then format it as a virtual disk. Here is an example command that creates a virtual disk of 10GB:

dd if=/dev/zero of=/path/to/virtual_disk.img bs=1G count=10

This command creates a file called "virtual_disk.img" in the specified location, using data from /dev/zero, with a block size of 1GB and a total size of 10GB.

Once you have created the virtual disk, you can format it with a file system such as ext4, using the command mkfs.ext4 /path/to/virtual_disk.img

You can also use other tools such as fallocate and qemu-img to create virtual disk.

fallocate is an advanced command-line tool that can be used to create a virtual disk of a specified size, and it can also preallocate the space on the disk. Here's an example command:

fallocate -l 10G /path/to/virtual_disk.img

qemu-img is a command-line tool that is typically used to create and manage virtual disk images for use with QEMU, it can also be used to create a virtual disk. Here's an example command:

qemu-img create -f raw /path/to/virtual_disk.img 10G

Once you have created a virtual disk using dd, fallocate, or qemu-img, you can mount it as a regular disk on your Linux system. To mount the virtual disk persistently, you will need to add an entry to your system's fstab file. The fstab file is located at /etc/fstab and contains information about the file systems that are to be automatically mounted at boot time.

Here's an example of how you would add an entry to the fstab file to mount a virtual disk located at /path/to/virtual_disk.img:

/path/to/virtual_disk.img /mnt/virtual_disk ext4 defaults 0 0


In this example, the virtual disk is being mounted at /mnt/virtual_disk, using the ext4 file system with default options. The '0 0' at the end of the line indicates that the file system should not be dumped and that fsck should not check the file system.

Once you have added the entry to fstab, you can mount the virtual disk by running the command:


sudo mount -a


You can also mount the virtual disk without modifying the fstab file by running the command:

sudo mount /path/to/virtual_disk.img /mnt/virtual_disk -t ext4


It's important to keep in mind that the virtual disk will be unmounted after reboot and you would need to manually mount it every time or update fstab file.

Please note that, you should have the permissions to read and write to the mount point and also make sure that the file system is supported by your Linux distribution.



To create an LVM on a virtual disk, you will first need to create a virtual disk using one of the methods I mentioned in previous answer, and then format it with the physical volume (PV) format using the command pvcreate.

Here is an example command to create a PV on a virtual disk located at /path/to/virtual_disk.img:

pvcreate /path/to/virtual_disk.img

Once you have created a PV, you can create a Volume Group (VG) on top of it using the command vgcreate. A VG is a collection of one or more PVs that are used to create logical volumes.

Here is an example command to create a VG called "vg0" on top of the PV:

vgcreate vg0 /path/to/virtual_disk.img

You can now create logical volumes within the VG using the command lvcreate. Logical volumes can be used just like any other block device, and can be mounted and formatted with a file system.

Here is an example command to create a logical volume called "lv0" of size 10GB within the VG:

lvcreate -L 10G -n lv0 vg0

You can now format and mount the logical volume just like a regular disk.

Please note that LVM is a powerful tool and you should be familiar with it before using it in production. LVM is not recommended if you only have one virtual disk, but it makes sense when you have multiple virtual disks and you want to manage them with more flexibility.

No hay comentarios:

Publicar un comentario