Tuesday, July 25, 2017

Configuring a new hard drive for LVM

This tutorial provides the steps to detect and configure a new Hard Drive for Logical Volume Manager (LVM) use.

These instructions are provided for a Linux system running Ubuntu 11.04. Some commands may differ between operating systems.

Prerequisites

Ubuntu may not include LVM support by default. You can verify this with:

$ lvm help

If this command fails you can install the necessary prerequisites with:

$ sudo apt-get install -y lvm2

Disk Detection

Your first step is to connect a new disk (or USB drive) to your server. You can easily tell what the operating system can see with:

$ cat /proc/partitions
major minor  #blocks  name

   8        0 1953514584 sda
   8        1    2929688 sda1
   8        2    2929688 sda2
   8        3    1953125 sda3
   8        4    4882813 sda4
   8        5  292968750 sda5
   8        6   15625000 sda6
   8       80 1465138584 sdf

In this case, a new 1.5TB HDD was added, and this is seen in the last line as /dev/sdf. You can also see this detail using fdisk

$ sudo fdisk -l

Disk /dev/sda: 2000.4 GB, 2000398934016 bytes
255 heads, 63 sectors/track, 243201 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1      243202  1953514583+  ee  GPT

Disk /dev/sdf: 1500.3 GB, 1500301910016 bytes
255 heads, 63 sectors/track, 182401 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/sdf doesn't contain a valid partition table

Configuration LVM

$ cat /proc/partitions
$ sudo fdisk /dev/sdf
n
p
1
+100G
L
8e
p
w
$ sudo pvdisplay
$ sudo pvcreate /dev/sdf1
$ sudo pvdisplay
$ sudo vgcreate db /dev/sdf1
$ sudo vgdisplay
$ sudo vgscan
$ sudo lvcreate --name p0 --size 20G db
$ sudo lvcreate --name p1 --size 20G db
$ sudo lvdisplay
$ sudo lvscan
$ sudo mkfs.ext3 /dev/db/p0
$ sudo mkfs.ext3 /dev/db/p1
$ sudo mkdir /mysql
$ cat /etc/fstab
$ sudo su -
$ mkdir /mysql
$ echo "/dev/db/p0  /mysql   ext3  rw,noatime  0 0" >> /etc/fstab
$ mount /mysql
$ df -h