Using a new empty volume on a Linux VM

From CC Doc
Jump to: navigation, search
Other languages:
English • ‎français

On most Linux distributions the following steps can be used to partition, format, and mount the newly created volume. NOTE: If this is not a newly created volume the partition and format steps should be skipped as they will result in loss of data on that volume, and only the steps to mount the volume should be followed.

  1. Create a partition on the volume with
    [name@server ~]$ sudo fdisk /dev/vdb
    fdisk will prompt you to enter a command. Use this sequence of single-character commands to create a new partition on your volume.
  2. n => new partition
    p => primary, only one partition on disk
    1 => partition number 1
    <return> => first sector (use default)
    <return> => last sector (use default)
    w => write partition table to disk and exit
  3. Format the newly created partition with
    [name@server ~]$ sudo mkfs -t ext4 /dev/vdb1
  4. Create a place to mount the device with
    [name@server ~]$ sudo mkdir /media/data
  5. Finally, mount the volume with
    [name@server ~]$ sudo mount /dev/vdb1 /media/data

If the VM is rebooted for some reason the volume will need to be remounted. To cause the VM to mount the volume automatically at boot time, edit /etc/fstab and add a line like

 /dev/vdb1 /media/data ext4 defaults 0 2

For more details about the fstab file see this wikipedia article. If you are not rebooting, you can mount the device just added to /etc/fstab with

[name@server ~]$ sudo mount -a

Unmounting a volume or device

If you need to remove a volume or other device for some reason, for example to create image from it, or to attach it to a different VM, it is best to unmount it first. Unmounting a volume before detaching it helps prevent data corruption.

To unmount our previously mounted volume above, use the following command:

[name@server ~]$ sudo umount /media/data

This command will work if no files are being accessed by the operating system or any other program running on the VM. This can be both reading and writing to files. If this is the case, when you try to unmount a volume, you will get a message letting you know that the volume is still busy and it won't be unmounted.