VMホストでDISK領域を拡張する方法 ※追記あり

· 6 min read
VMホストでDISK領域を拡張する方法 ※追記あり

※3/18追記
どうやら fdisk を使うやり方は少々古いようです。
parted コマンドで DISK 領域を増やす方法があったので、こちらもご紹介します。


parted のインストール

デフォルトだと parted コマンドがないのでインストールします。

# yum install parted

拡張前の状態を確認する

df だと 3.9GB しか認識されていませんが、sda 領域自体はトータル 10GB あります。

[root@db ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_templete-lv_root
                      3.9G  1.3G  2.4G  35% /
tmpfs                 242M     0  242M   0% /dev/shm
/dev/sda1             477M  101M  351M  23% /boot

[root@db ~]# lsblk
NAME                           MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                              8:0    0   10G  0 disk
├─sda1                           8:1    0  500M  0 part /boot
└─sda2                           8:2    0  4.5G  0 part
  ├─vg_templete-lv_root (dm-0) 253:0    0    4G  0 lvm  /
  └─vg_templete-lv_swap (dm-1) 253:1    0  512M  0 lvm  [SWAP]
sr0                             11:0    1 1024M  0 rom

fdisk でパーティションを確認。一部しか割り当たっておらず、残りはフリー。

[root@db ~]# fdisk -l /dev/sda

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x000a9c6d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64         653     4729856   8e  Linux LVM
Partition 2 does not end on cylinder boundary.

parted でパーティション切り直し

parted コマンドでパーティションを切り分けしていきます。

[root@db ~]# parted /dev/sda
GNU Parted 2.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)

初期状態だとセクタ単位がバラバラなので、
以下のコマンドで MB 単位に固定した方が見やすいです。

(parted) unit MB

(parted) print
Model: Msft Virtual Disk (scsi)
Disk /dev/sda: 10737MB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      1.05MB  525MB   524MB   primary  ext4         boot
 2      525MB   5369MB  4843MB  primary               lvm

2 のパーティションを削除します。
物騒な警告が出ますが、実データが即消えるわけではないので注意だけ。

(parted) rm 2
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy).  As a result, it may not reflect all of your changes until
after reboot.

(parted) print
Model: Msft Virtual Disk (scsi)
Disk /dev/sda: 10.7GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos

Number  Start   End    Size   Type     File system  Flags
 1      1049kB  525MB  524MB  primary  ext4         boot

全容量を使えるようにパーティションを再作成。増えているのがわかります。

(parted) mkpart primary 525MB 10.7GB

(parted) print
Model: Msft Virtual Disk (scsi)
Disk /dev/sda: 10.7GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  525MB   524MB   primary  ext4         boot
 2      525MB   10.7GB  10.2GB  primary

作成したパーティションに LVM フラグをセット。

(parted) set 2 lvm on

(parted) print
Model: Msft Virtual Disk (scsi)
Disk /dev/sda: 10.7GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  525MB   524MB   primary  ext4         boot
 2      525MB   10.7GB  10.2GB  primary               lvm

最後に quit で parted を抜けます。

あとは本文中の「新しいパーティションを既存ボリュームに追加する」手順を実行すれば
DISK 領域が拡張されます。reboot いらずでさらに楽ちんでした。


(以下・元記事)DISKの使用量が100%になったので増設した話

ふと見たら検証機の DISK 使用量が 100% になっていました。

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                      3.9G  3.7G  4.8M 100% /

不要ファイルを削除しても容量がなかなか変わらないので、
DISK 容量の増設をしてみた時の覚書です。


VM 管理画面上で DISK を増設

物理的な DISK の容量を拡張します。

※VMware の場合
VMware のメニューから
「仮想マシン → ハードディスク(IDE) → ハードディスク(IDE)設定」を選択し、
増やしたい分だけ容量を拡張。

※Hyper-V の場合
Hyper-V のメニューから
「ディスクの編集… → ディスクの場所(仮想マシンの DISK) → 拡張(E)」を選択し、
増やしたい分だけ増設。

今回は 5GB ほど容量を増やしました。
※増設にはサーバが停止している必要があります。


fdisk で DISK を増やす

増設したらサーバを起動します。
root でログインし、fdisk コマンドで増えているか確認。

[root@test ~]# fdisk -l /dev/sda

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x000bd63d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64         653     4729856   8e  Linux LVM
Partition 2 does not end on cylinder boundary.

元の DISK 容量は 3.9GB でしたが、総容量が 10.7GB に増えています。
しかし、実際に使える領域はまだ増えていません。

ここからパーティションを切り直します。Windows でいう「パーティション拡張/再作成」。
※念のためバックアップやスナップショット推奨。

[root@test ~]# fdisk /dev/sda

The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance may be impacted.

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help):

入ったら既存のパーティションを一旦消します。

Command (m for help): d
Partition number (1-4): 2

Command (m for help): p

Disk /dev/sda: 10.7 GB, 10737418240 bytes
...
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.

消えたので作り直します。

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (64-1305, default 64):
Using default value 64
Last cylinder, +cylinders or +size{K,M,G} (64-1305, default 1305):
Using default value 1305

Command (m for help): p
...
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        1305     9969388+  83  Linux

LVM 用にタイプ変更します。

Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 8e
Changed system type of partition 2 to 8e (Linux LVM)

Command (m for help): p
...
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        1305     9969388+  8e  Linux LVM

新しいパーティション構成を確認し、問題なければ保存して抜けます。

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

そしてマシンを再起動します。

# reboot

新しいパーティションを既存ボリュームに追加する

再起動直後はまだ DISK 領域は増えていないので、LVM を拡張します。

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                      3.9G  3.7G  4.6M 100% /

まず物理ボリュームをリサイズ。

[root@test ~]# pvresize /dev/sda2
  Physical volume "/dev/sda2" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized

次に論理ボリュームを拡張します。
lvextend で VG 上の未使用領域を LV に追加します(ここでは 100% を追加)。

[root@test ~]# lvextend -l +100%FREE /dev/mapper/VolGroup-lv_root
  Size of logical volume VolGroup/lv_root changed from 4.01 GiB (1026 extents) to 9.00 GiB (2305 extents).
  Logical volume lv_root successfully resized.

resize2fs でファイルシステムを拡張。

[root@test ~]# resize2fs /dev/mapper/VolGroup-lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/VolGroup-lv_root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/mapper/VolGroup-lv_root to 2360320 (4k) blocks.
The filesystem on /dev/mapper/VolGroup-lv_root is now 2360320 blocks long.

これで作業完了です。

[root@test ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                      8.8G  3.7G  4.7G  44% /

DISK の使用量が 100% から 44% まで減少しました。成功ですね。

※参考サイト
http://d.hatena.ne.jp/tatz_tsuchiya/20140202/1391328269
http://qiita.com/2or3/items/501f206a6091a4ce895f
http://qiita.com/g_maeda/items/3602dc2f7af3f711f9c4
http://yomon.hatenablog.com/entry/2016/04/22/233825