I have a disk in which I store the data of a DB. The disk is full now and I want to add another disk to the machine. I heard that by LVM you can add and extend partitions, so I would like to know if it is possible to extend the current full disk(without corrupting its current data) by adding a new disk?
LVM makes it easy to resize existing partitions and move them around¹. But they have to be LVM partitions in the first place. Since your current partition isn't on LVM, you'll have to do things manually. All the commands in my answer need to be executed as root.
Prepare the new disk
- Create a GPT or MBR partition for Linux's use. Use the whole disk unless you want to share the disk with another operating system. I'll call that partition
/dev/sdb1an LVM physical volume, i.e. declare that this part of the disk is to be used for LVM.
Create an LVM volume group containing this physical volume. All LVM volumes are part of a volume group. Pick a name for that volume group, e.g.
vgcreate alex_os /dev/sdb1
You now have some space for LVM use. Create an LVM logical volume that's big enough for the data you want to store on it. You can use all the space if you want (unless you want to reserve space for another filesystem or swap space), but since shrinking a filesystem is harder than expanding it, it's best if you only use what you think you'll need in the medium term plus a safety margin. Pick a name for the logical volume, e.g.
rootfor your root filesystem, or
dbif you're going to store your database on it.
lvcreate -L 40g -n root alex_os
What to do next depends on whether you want to move your OS partition to the new disk, or use the new disk as extra storage.
Use the new disk for extra storage
Create a filesystem on the new disk.
Register the new filesystem for use. Pick a mount point for it. This can either be an existing directory or a new directory.
If you want to move e.g.
/var/lib/mysqlto the new disk, make that the mount point. First mount the new filesystem to a temporary location, move the data (make sure the files are not in use while you do this!)
service mysql stop mount /dev/mapper/alex_os-db /mnt mv /var/lib/mysql/* /mnt mount --move /mnt /var/lib/mysql service mysql start
Finally edit the file
/etc/fstabto add the following line after the line containing
/in the second column:
/dev/mapper/alex_os-db /var/lib/mysql ext4 errors=remount,ro 0 2
If you don't want to tie the new disk to a specific service, mount it to a different location, e.g. add the following line to
/etc/fstabafter the line containing
/in the second column:
/dev/mapper/alex_os-db /media/data ext4 errors=remount,ro 0 2
Then create the mount point and mount the filesystem:
mkdir /media/data mount /media/data
You can now move whatever data you want to put there, and use symbolic links from the location where the service expects the files. Stop any services accessing the files while you're moving them! For example, to move the content of
/var/lib/mysqlto the new disk:
service mysql stop mv /var/lib/mysql /media/data/ ln -s /media/data/mysql /var/lib/mysql service mysql start
Transfer the system to the new disk
Reboot to rescue media (e.g. SystemRescueCD) to copy the data to the new disk. You can't do the copy from the running system because it's impossible to get a consistent snapshot². To copy the data, you have two possibilities:
Create a filesystem and copy the files, e.g. for an ext4 filesystem:
mkfs.ext4 /dev/mapper/alex_os-root mount /dev/mapper/alex_os-root /media/alex_os-root cp -a /media/sda1 /media/alex_os-root
Copy the filesystem image, then expand it to fill, e.g for an ext4 filesystem:
cat /dev/sda1 >/dev/mapper/alex_os-root resize2fs /dev/mapper/alex_os-root
Make the new disk bootable. Exactly how to do that depends on your distribution. You'll typically have to install the bootloader and rebuild the initramfs. For example, on Ubuntu, you'd typically need something like
chroot /media/alex_os-root update-initramfs update-grub grub-install /dev/sdb
¹ Resizing the filesystem on the partition is a separate matter; most modern filesystem support online expansion but not online shrinking.
² Technically it may be possible to get lucky with
cp -a if the system is otherwise idle, but that's a recipe for disaster; this can cause hard-to-track delayed bugs. One of the advantages of LVM is that you can use it to take a consistent snapshot. Some filesystems (zfs, btrfs) have a snapshot facility of their own, but not ext4.