3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Linux】VirtualBoxで仮想HDDを追加しLVMを操作する

Last updated at Posted at 2023-05-09

はじめに

今回は、実際にLVMをいじっていこうと思います。

今回の手順

1. VirtualBoxで新しい仮想ハードディスクを追加
2. 追加したディスクにパーティションを2つ作成
3. そのパーティションを使用しpv, vg, lvを作成
4. lvにファイルシステムを作成

まず、現在のハードディスクの状態をlsblkで確認していきます。
image.png

ここに、ハードディスク/dev/sdbを追加していきたいと思います

1. VirtualBoxで新しい仮想HDDを追加

追加するVMのストレージ設定画面を開きます。

  • 「コントローラー: SATA」を選択するとの右のほうにHDD追加ボタン
    image.png
    がでるので、それをクリック
    image.png

  • 「作成」をクリック
    image.png

  • VDIを選択(私の場合は、既存のディスクがVDIだったので同じVDIを選択しました。)
    image.png

  • 可変サイズを選択。固定でもOK
    (固定だといきなり指定サイズのファイルが作成される)
    image.png

  • 仮想ディスクイメージのファイル名と、サイズを設定
    image.png

  • 追加したディスクを選択
    image.png
    image.png
    選択するとこのように無事に追加されました。

その後改めてVirtual Boxを起動しUbuntu上でlsblkコマンドを入力すると
image.png

追加したハードディスク(sdb)が出力されました。

次にfdiskコマンドを使用しパーティションを作成します。

2. fdiskでパーティション操作

パーティションの操作コマンドは、partedやfdiskなどがありますが、今回は対話型で安全性があるfdiskを使用して行きます

[client1@client-VirtualBox ~]$ sudo fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help):

パーティション作成(/dev/sdb1)

nでパーティションの作成。ハードディスクは4つまでのパーティションに分けることができます。

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-83886079, default 2048): 2048
Last sector, +sectors or +size{K,M,G} (2048-83886079, default 83886079): +1G   
Partition 1 of type Linux and of size 1 GiB is set

Command (m for help): p

Disk /dev/sdb: 25 GiB,  26843545600 bytes, 52428800 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x094be466

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2099199     2097152   83  Linux

Command (m for help): 

パーティション作成(/dev/sdb2)

もう一度、先ほどと同じようにパーティションを作成していきます

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 2
First sector (2099200-83886079, default 2099200): 2099200
Last sector, +sectors or +size{K,M,G} (2099200-83886079, default 83886079): +2G
Partition 2 of type Extended and of size 2 GiB is set

Command (m for help): p

Disk /dev/sdb: 25 GiB,  26843545600 bytes, 52428800 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x094be466

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2099199     1048576   83  Linux
/dev/sdb2         2099200     6293503     4194304   83  Linux

作成が完了したら、wで保存します。保存せずに終了するとパーティションは変更されません。

*補足:作成を誤ってもqで取りやめすることができます。この部分がfdiskが便利な部分ですよね。

Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

3. LVM操作

次から本題であるLVMを操作していきたいと思います。

物理ボリューム作成

LVM ボリュームを作成します。

$ sudo pvcreate /dev/sdb1 /dev/sdb2 
  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdb2" successfully created

pvdisplay コマンドで確認します。

$ sudo pvdisplay
  "/dev/sdb1" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               1.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               4FverC-M1HX-fbiF-elDv-t3Hf-8g8G-rMWT04
   
  "/dev/sdb2" is a new physical volume of "2.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb2
  VG Name               
  PV Size               2.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               x3EZr5-dvdP-51MK-LKQ7-Dhcz-CWx1-DuwrQT

ボリュームグループ作成

vgcreate コマンドを使用してmyvg ボリュームグループを作成します。

$ sudo vgcreate myvg /dev/sdb1 /dev/sdb2 
  Volume group "myvg" successfully created

vgdisplay, コマンドで、ボリュームグループを確認します。

$ sudo vgdisplay
  --- Volume group ---
  VG Name               lvg-share
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               2.99 GiB
  PE Size               4.00 MiB
  Total PE              766
  Alloc PE / Size       0 / 0   
  Free  PE / Size       766 / 2.99 GiB
  VG UUID               rFo1Ux-ajEi-MfP1-PqHm-mnGc-JmkI-9adz7h

論理ボリューム作成

今回は、以下のような見積もりで論理ボリューム構成を構築します。

論理ボリューム名 容量
public 1.5GB
web 0.5GB
file 0.5GB

lvcreate コマンドを使用して、論理ボリュームを作成していきます。

$ sudo lvcreate --name public --size 1.5GB myvg
  Logical volume "public" created
$ sudo lvcreate --name web --size 0.5GB myvg
  Logical volume "web" created
$ sudo lvcreate --name file --size 0.5GB myvg
  Logical volume "file" created

lvdisplay で確認してみます。

$ sudo lvdisplay
  --- Logical volume ---
  LV Path                /dev/myvg/public
  LV Name                public
  VG Name                myvg
  LV UUID                fIX90H-kCtn-WQPt-NZke-CkdR-qcSE-hKGsBK
  LV Write Access        read/write
  LV Creation host, time client1-VirtualBox, 2023-05-09 12:25:48 +0900
  LV Status              available
  # open                 0
  LV Size                1.5 GiB
  Current LE             384
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/myvg/web
  LV Name                web
  VG Name                myvg
  LV UUID                ibhzNE-BqkE-yFoM-suWh-G2Eb-5QzT-VHGMwD
  LV Write Access        read/write
  LV Creation host, time client1-VirtualBox, 2023-05-09 12:26:04 +0900
  LV Status              available
  # open                 0
  LV Size                512.00 GiB
  Current LE             128
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/myvg/file
  LV Name                file
  VG Name                myvg
  LV UUID                ymVytc-4wZQ-iCSc-loqY-1T8w-lqQx-k07qAH
  LV Write Access        read/write
  LV Creation host, time client1-VirtualBox, 2023-05-09 13:21:09 +0900
  LV Status              available
  # open                 0
  LV Size                512.00 GiB
  Current LE             128
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2

論理ボリューム削除

ここでは、/dev/myvg/fileを削除してみましょう。

$ sudo lvremove /dev/myvg/file

論理ボリューム拡張

ここでは、/dev/myvg/webを1GBにしたいので+0.5GB拡張してみましょう。

$ sudo lvextend -L+0.5GB /dev/myvg/web

4. ファイルシステム作成

ここまでの手順で作成された論理ボリュームにファイルシステムを作成します。
まず、/dev/myvg/public 論理ボリュームにext4 ファイルシステムを作成します。

$ sudo mkfs -t ext4 /dev/myvg/public
mke2fs 1.46.5 (30-Dec-2021)
Creating filesystem with 393216 4k blocks and 98304 inodes 
Filesystem UUID: 6a39bd8d-d545-4bcf-8270-3e2a3df5df5f
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912

Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

次に、/dev/myvg/web 論理ボリュームにxfs ファイルシステムを作成します。

$ sudo mkfs.xfs /dev/myvg/web
meta-data#/dev/myvg/web   isize=256    agcount=4, agsize=32769 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, aparese=1, rmapbt=0
         =                       reflink=1    bigtime=0 inobtcount=0
data     =                       bsize=4096   blocks=131072, imaxpct=25
         =                       sunit=0      swidth0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=1368, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

おわりに

自分自身、学生の身で勉強中なので実際にどのような感じで実務で使用するかわからないです。内容が微妙であったり間違ったやり方をしている場合もあります。ぜひ、参考にあるような他の記事を参照してみてください。次は、Apache2サーバをインストールし、今回作成したLVM上でwebを見れるようにしていきたいです。

参考

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?