よくわからないエンジニア

よく分からないエンジニア(無音鈴鹿)の日々の記録

よくわからないエンジニア

【CentOS7】/homeを論理ボリュームとしてmountする

凄い的を射てないタイトルですね。色んな事の理解度が低いので、バシッとしたタイトルをつける事すら出来ません。一応やりたい事は、「初期設定時に/homeを分けずに/で作ってしまったが、後から/homeを別にしたくなったので、追加したディスクを/homeとしてmountする」です。期待している内容と違う場合は時間の無駄になってしまいますので、ブラウザの"戻る"ボタンを押して下さい。

目次

環境としては、CentOS7,ファイルシステムxfs,LVMです。

ディスククォータを設定したかった

事の発端はディスククォータの設定をしようとして、/etc/fstabに以下変更を加えた。

#vi /etc/fstab
/dev/mapper/centos-root /                       xfs     defaults,noatime,uquota,gquota        0 0

この後再起動して、/のquotaの設定状況を確認してみた。

# mount |grep quota
/dev/mapper/centos-root on / type xfs (rw,noatime,attr2,inode64,noquota)
# xfs_quota -x /
xfs_quota> state

反応がまるで無い。理由は分からないが/ディレクトリに対してクォータ設定が有効にならない。繰り返し言うが理由は分からない。教えて欲しい。

/から/homeを独立させる

とりあえずネットの情報を探していると、/homeを/から分けて使用しており、この構成であればquota設定が簡単に出来ているような気がします。なので当方の環境でも/homeを独立させます。尚、今回は物理ボリューム自体も独立させます。

現状の構成確認

まずは現状の構成を確認します。

# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x000b73a9

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1050623      524288   83  Linux
/dev/sda2         1050624    41943039    20446208   8e  Linux LVM

Disk /dev/mapper/centos-root: 19.9 GB, 19860029440 bytes, 38789120 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 /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 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
# pvdisplay -C
  PV         VG     Fmt  Attr PSize  PFree
  /dev/sda2  centos lvm2 a--  19.50g    0
# vgdisplay -C
  VG     #PV #LV #SN Attr   VSize  VFree
  centos   1   2   0 wz--n- 19.50g    0
# lvdisplay -C
  LV   VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- 18.50g
  swap centos -wi-ao----  1.00g

物理ボリューム:sda(20GBくらい)
ボリュームグループ:centos(20GBくらい)
論理ボリューム:/dev/mapper/centos-root(20GBくらい)

上記構成ですが、物理的なディスクを追加して、以下のような構成に変更します。
物理ボリューム:sda(20GBくらい) , sdb(5GB)
ボリュームグループ:centos(25GBくらい)
論理ボリューム:/dev/mapper/centos-root(20GBくらい) , /dev/mapper/centos-home

凄いツッコミどころ満載の構成ですが、気にせず続けていきます。

ディスクの追加~ボリュームグループの作成まで

仮想マシンに物理ディスク5GB追加します。

# dmesg |grep sdb
[  755.909183] sd 2:0:1:0: [sdb] 10485760 512-byte logical blocks: (5.36 GB/5.00 GiB)
[  755.909199] sd 2:0:1:0: [sdb] Write Protect is off
[  755.909201] sd 2:0:1:0: [sdb] Mode Sense: 61 00 00 00
[  755.909225] sd 2:0:1:0: [sdb] Cache data unavailable
[  755.909227] sd 2:0:1:0: [sdb] Assuming drive cache: write through
[  755.910638] sd 2:0:1:0: [sdb] Attached SCSI disk
#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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x61bfb1da.

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-10485759, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759):
Using default value 10485759
Partition 1 of type Linux and of size 5 GiB is set

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 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: 0x61bfb1da

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    10485759     5241856   8e  Linux LVM

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

Calling ioctl() to re-read partition table.
Syncing disks.
# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.
# pvdisplay -C
  PV         VG     Fmt  Attr PSize  PFree
  /dev/sda2  centos lvm2 a--  19.50g    0
  /dev/sdb1         lvm2 ---   5.00g 5.00g

物理ボリュームとして設定した後は、ボリュームグループに組み込みます。

# vgextend centos /dev/sdb1
  Volume group "centos" successfully extended
# vgdisplay -C
  VG     #PV #LV #SN Attr   VSize  VFree
  centos   2   2   0 wz--n- 24.49g 5.00g

ボリュームグループに追加した5GBの空き容量が出来ました。次は、論理ボリュームとしてhomeを作ります。

# lvcreate -n /dev/centos/home -l 100%FREE centos
  Logical volume "home" created.
# lvdisplay -C
  LV   VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home centos -wi-a-----  5.00g
  root centos -wi-ao---- 18.50g
  swap centos -wi-ao----  1.00g

最後にxfsファイルシステムとして使用出来るように設定します。

#mkfs.xfs /dev/mapper/centos-home

/homeとしてマウントする

対象サーバは当方しか使用していないのでそのままやっても問題ないですが、本来はシングルユーザーモードに切り替えてください。まずは適当な所に作成したcentos-homeをマウントして、/homeの中身をコピーします。コピーが終わったらumountし、/homeはディレクトリごと削除します。

#mount /dev/mapper/centos-home /mnt/
#cp -av /home/* /mnt/
# umount /mnt/
#rm -rf /home

では/homeにマウントしていきます。quotaが使えるように/etc/fstabに設定します。

#vi /etc/fstab
/dev/mapper/centos-home /home                       xfs     defaults,noatime,uquota,gquota        0 0
# mount -a
# mount |grep home
/dev/mapper/centos-home on /home type xfs (rw,noatime,attr2,inode64,usrquota,grpquota)

これで/homeに対してquota設定が使えるようになりました。めでたし。