1

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?

  • Probably. What is the underlying file system you are using? – Rui F Ribeiro Apr 15 '17 at 22:20
  • ext4. I have no idea of how to do it, can you give me a clue? thanks – Alex Apr 15 '17 at 22:21
  • Only if you’re already using LVM with your old disk. – user2233709 Apr 15 '17 at 22:21
  • 2
    @Alex cat /proc/mounts. If it shows that you are mounting /dev/sda1, then you’re not using LVM. If it shows that you are mounting `/dev/mapper/foo-bar', then you may be already using LVM. – user2233709 Apr 15 '17 at 22:26
  • 1
    You're adding a new disk. There should be little reason why you can't set that new disk up for LVM, copy your data across in its entirety, and then bring the old partition into the existing LVM configuration. Just make sure you're taking backups. If either of your two disks dies the entire LVM configuration may be wiped out. (Similar to RAID 0.) – roaima Apr 15 '17 at 23:27
3

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

  1. 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/sdb1.
  2. Make /dev/sdb1 an LVM physical volume, i.e. declare that this part of the disk is to be used for LVM.

    pvcreate /dev/sdb1
    
  3. 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. alex_os.

    vgcreate alex_os /dev/sdb1
    
  4. 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. root for your root filesystem, or db if 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

  1. Create a filesystem on the new disk.

    mkfs.ext4 /dev/mapper/alex_os-root
    
  2. 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/mysql to 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/fstab to 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/fstab after 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/mysql to 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

  1. 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
      
  2. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.