LoginSignup
0
0

KVMのディスク内のLVをディスク外でマウントする

Last updated at Posted at 2023-12-21

はじめに

これはKMC Advent Calender 2022の23日目の記事です。実に1年越しの投稿です。

突然KVMのゲストOSが起動しなくなった!
バックアップも取っていない!

......となったときに手持ちの仮想ディスクをホストOSにマウントして中身だけ救出したいという記事です。
バックアップは取っておきましょう。

TL;DR

sudo modprobe nbd
sudo qemu-nbd --connect=/dev/nbd0 root.img
ls /dev/nbd0*
sudo vgscan
sudo lvdisplay → NOT available
sudo lvchange -ay /dev/ubuntu-vg/ubuntu-lv
sudo lvdisplay → available
sudo mount /dev/ubuntu-vg/ubuntu-lv /mnt/vm

環境

  • ゲストOS
    Ubuntu 22.04
    インストール時にLVMを設定しており、パーティション構成は以下の通り
vda                       252:0    0    30G  0 disk
|-vda1                    252:1    0     1M  0 part
|-vda2                    252:2    0     2G  0 part /boot
`-vda3                    252:3    0    28G  0 part
  `-ubuntu--vg-ubuntu--lv 253:0    0    14G  0 lvm  /

手法

基本的には【Linux】KVMの仮想ハードディスクイメージをマウントするに従いました。

sudo modprobe nbd
sudo qemu-nbd --connect=/dev/nbd0 root.img

として、
ls /dev/nbd0* を実行すると

ls /dev/nbd0*
/dev/nbd0  /dev/nbd0p1  /dev/nbd0p2  /dev/nbd0p3

となりました。
各ファイルがどのパーティションに当たるか確認します。

sudo file -s /dev/nbd0
/dev/nbd0: DOS/MBR boot sector, extended partition table (last)
sudo file -s /dev/nbd0p1
/dev/nbd0p1: data
sudo file -s /dev/nbd0p2
/dev/nbd0p2: Linux rev 1.0 ext4 filesystem data, UUID=9eb83deb-0c7a-456b-aa40-c83d01a4e2cf (extents) (64bit) (large files) (huge files)
sudo file -s /dev/nbd0p3
/dev/nbd0p3: LVM2 PV (Linux Logical Volume Manager), UUID: RQ0pW0-G6F5-424u-qPy0-PElY-ueeg-n6KGLv, size: 105223553024

番号はもとのファイルシステムに対応するようです。
/のデータが欲しいので今回マウントする対象は/dev/nbd0p3になります。LVM2 PVと記載があります。

ではこれをマウントしてみましょう。
コマンドはsudo mount /dev/ubuntu-vg/ubuntu-lv /mnt/vmです。

sudo mount /dev/ubuntu-vg/ubuntu-lv /mnt/vm
mount: /mnt/vm: special device /dev/ubuntu-vg/ubuntu-lv does not exist.
       dmesg(1) may have more information after failed mount system call.

does not exist.と出たのでLVの情報を見てみましょう。

sudo vgscan
  Found volume group "ubuntu-vg" using metadata type lvm2
sudo lvdisplay
  --- Logical volume ---
  LV Path                /dev/ubuntu-vg/ubuntu-lv
  LV Name                ubuntu-lv
  VG Name                ubuntu-vg
  LV UUID                fyPl34-vdGG-J4WA-ubDw-Xbq0-QfyD-DRpnxN
  LV Write Access        read/write
  LV Creation host, time ubuntu-server, 2023-12-06 13:57:52 +0900
  LV Status              NOT available
  LV Size                <98.00 GiB
  Current LE             25087
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto

以下の部分が原因です。

  LV Status              NOT available

そこで、LVをActivateします。

sudo lvchange -ay /dev/ubuntu-vg/ubuntu-lv

結果、availableになっているのを確認できます。

sudo lvdisplay
  --- Logical volume ---
  LV Path                /dev/ubuntu-vg/ubuntu-lv
  LV Name                ubuntu-lv
  VG Name                ubuntu-vg
  LV UUID                fyPl34-vdGG-J4WA-ubDw-Xbq0-QfyD-DRpnxN
  LV Write Access        read/write
  LV Creation host, time ubuntu-server, 2023-12-06 13:57:52 +0900
  LV Status              available
  # open                 0
  LV Size                <98.00 GiB
  Current LE             25087
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:0

これでmountできるようになります。

sudo mount /dev/ubuntu-vg/ubuntu-lv /mnt/vm

の結果、

df -TH
/dev/mapper/ubuntu--vg-ubuntu--lv ext4      103G   31G   68G  32% /mnt/vm

無事mountされました。

おわりに

バックアップは必ずしましょうね!

Advent Calenderもちゃんと期日通りに投稿しましょうね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0