LVM: 使用中のパーティションを拡張することは可能ですか?

LVM: 使用中のパーティションを拡張することは可能ですか?

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?

答え1

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
      

      最後に、ファイルを編集して、2 番目の列に/etc/fstab含まれる行の後に次の行を追加します。/

      /dev/mapper/alex_os-db /var/lib/mysql ext4 errors=remount,ro 0 2
      
    • 新しいディスクを特定のサービスに結び付けたくない場合は、別の場所にマウントします。たとえば、2 番目の列に/etc/fstab含まれる行の後に次の行を追加します。/

      /dev/mapper/alex_os-db /media/data ext4 errors=remount,ro 0 2
      

      次にマウント ポイントを作成し、ファイルシステムをマウントします。

      mkdir /media/data
      mount /media/data
      

      そこに保存したいデータを移動して、シンボリックリンクサービスがファイルを期待する場所から移動します。ファイルを移動している間は、ファイルにアクセスしているサービスを停止してください。たとえば、の内容を/var/lib/mysql新しいディスクに移動するには、次のようにします。

      service mysql stop
      mv /var/lib/mysql /media/data/
      ln -s /media/data/mysql /var/lib/mysql
      service mysql start
      

システムを新しいディスクに転送する

  1. メディアを復旧するには再起動してください(例:システムレスキューCD) を使用して、データを新しいディスクにコピーします。一貫性のあるスナップショット² を取得することは不可能であるため、実行中のシステムからコピーすることはできません。データをコピーするには、次の 2 つの方法があります。

    • ファイルシステムを作成し、ファイルをコピーします。たとえば、ext4 ファイルシステムの場合:

      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
      
    • ファイルシステム イメージをコピーし、それを拡張していっぱいにします (例: ext4 ファイルシステムの場合)。

      cat /dev/sda1 >/dev/mapper/alex_os-root
      resize2fs /dev/mapper/alex_os-root
      
  2. 新しいディスクを起動可能にします。具体的な方法はディストリビューションによって異なります。通常はブートローダーをインストールしてinitramfsを再構築する必要があります。たとえばUbuntuでは、通常次のようなものが必要です。

    chroot /media/alex_os-root
    update-initramfs
    update-grub
    grub-install /dev/sdb
    

¹パーティション上のファイルシステムのサイズ変更は別の問題です。ほとんどの最新のファイルシステムはオンライン拡張をサポートしていますが、オンライン縮小はサポートしていません。²
技術的には5月システムがアイドル状態であれば、運が良ければ可能かもしれませんcp -aが、それは災難のもとです。これは追跡困難な遅延バグの原因となります。LVM の利点の 1 つは、一貫性のあるスナップショットを撮ることができることです。一部のファイルシステム (zfs、btrfs) には独自のスナップショット機能がありますが、ext4 にはそれがありません。

関連情報