All commands are intended with sudo.
First, attach the disk in any way. In my situation, I was using VMWare and attached a SCSI disk.
Then, you may restart Ubuntu. Otherwise, if you don't want (or cannot) restart and you have SCSI disks, use the following steps:
apt-get install scsitools
scsitools is a group of tools to manage SCSI drives.
cat /proc/partitions
Note down all the devices.
rescan-scsi-bus
rescan-scsi-bus checks for all the SCSI drives attached and distinguish the old ones from the new ones, then configures Ubuntu to enable working with the newly attached drives.
UPDATE: if your server is a VMWare virtual machine and you want to extend a disk instead of add a new one, you must run a command like this (probably you have to change the 2:0:0:0 part with your real disk ID) :
echo '1' > /sys/class/scsi_disk/2\:0\:0\:0/device/rescanAfter this, running fdisk -l you should be able to see your new disk size.
cat /proc/partitions
Note the new devices. It's gonna be something like /dev/sda or /dev/sdb or /dev/sdc or some other letter in alphabetic order (for a SCSI device).
fdisk /dev/sda
With this command you can partition your disk to be a LVM partition.
fdisk opens an internal menu. Note that it doesn't execute anything until you launch the command w (write).These following are the instruction I use:
n
p
<enter>
<enter>
t
8e
p
w
- n: this command tells fdisk to create a new partition
- p: primary - to create an LVM the partition needs to be primary (Wikipedia Primary Partition)
- <enter> : the cylinder starting number. Do not change unless you know what you're doing. I don't ;)
- <enter> : the cylinder ending number. Same as above.
- t : changhe partition type from the default. The default seems to be dependant from the OS type, but I didn't found certain references.
- 8e : this hexadecimal code indicates the LVM partition type
- p : shows the partition actual setup. Review what you have set, to avoid mistakes
- w : write the changes, so partition the disk.
If you have more than one disk, repeat the same procedure changing the disk number.
After this starting phase, let's start with the real LVM configuration.
pvcreate /dev/sda1
This command initialize the physical partition to be initialized by LVM
vgcreate vgpool /dev/sda1
This command creates a volume group with the name
vgpool formed by any number of volumes. If you have more than one physical volume, you can put multiple devices path after the volume group name. For example:
vgcreate vgpool /dev/sda1 /dev/sdb1 /dev/sdc1
Then if you want like me to create a single logical volume you have to find out the total size (in GB in these days) of the volume group.
vgdisplay
The output of this command is the configuration of all the volume groups, included size and UUID of the group. Take note of the total size in GB of the volume.
With this size, you can finally create the logical volume:
lvcreate -L 250G -n lvhalen vgpool
- L : indicates the size of the logical volume
- n : name of the logical volume
Now you have a logical partition of 250G named lvhalen, generally wthin the folder /dev/vgpool/lvhalen.
To use this volume, you need some additional steps. First, you need to format the volume to use a specific filesystem:
mkfs -t ext4 /dev/vgpool/lvhalen
t sets the filesystem type. In this case I used ext4.
Finally, mount the volume to be usable.
mkdir /home/vanhalenhome
mount -t ext4 /dev/vgpool/lvhalen /home/vanhalenhome
Note that you need to create the directory before mounting the filesystem.
If you want to mount the volume every time at startup, you can instead edit
/etc/fstab.
Take note of the UUID number of the volume:
partprobe
blkid /dev/sda1
The output of
blkid it's the wanted UUID. Then open /etc/fstab.
vi /etc/fstab
And insert the following line:
UUID=XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXXX /home/vanhalenhome ext4 errors=remount-ro 0 1
I admit that I don't understand everithing of the commands on this line, but it should mount at startup the volume with that UUID in the given directory (if both exists, obviously).
EXTEND VOLUME
If you want to extend a volume, you should follow all the above passages until
pvcreate.
In this way, you are gonna addThen, you need to do something different. You need to ADD the new disk (or, to be more precise, the new physical volume) to the volume group. This is the command:
vgextend vgpool /dev/sdd1
Now, your volume group has 4 physical volumes and it's greater in size. Then you can extend the logical volume (in this case I added a 50G hard drive):
lvextend -L+50G /dev/vgpool/lvhalen
Almost done! But there's a last step to take to be able to use the added space. You need to let the file system know that he can use more space. Note: the command to extend the filesystem is custom for every filesystem, DO NOT EXCHANGE the commands!
In this case I use the ext3/ext4 filesystem command:
resize2fs /dev/vgpool/lvhalen
Et voilĂ ! Now your friend Eddie Van Halen has 300Gb of home directory!
SHRINK VOLUME
First of all, BACKUP every file in the logical volume, for in this process you may lose files.
Then, you just have to use the above steps in the reverse order, use
- instead of + in lvextend (or simply use lvreduce) and use vgreduce.
REFERENCES:
-
LVM Management - a very good tutorial about managing LVM
-
Expand LVM in a live environment