この記事は意外にやられてる方多いと思うので、すっとばして頂いても平気です。
今回の要件としては
-
- AmazonLinuxを使って欲しい
- ある事情から特定箇所をパーティション分けしないといけない
- コストは抑えたい(EBSの数は増やしたくないってことらしい)
ということでした。
そこで、一番初めに浮かんだのはLVM化。
実験で色々やってみたけどうまくいったものだけをこちらに記載しておきます。
まず、実施で使ったものを紹介
- AmazonLinuxAMI208.03.0(HVM
- t2.nano(1vCPU・0.5GiBMem)
- EBS
- /dev/xvda 8GiB(ルートボリューム)
- /dev/sdb 30GiB(今回LVM化するディスク)
まずは起動
マネジメントコンソールからでもawscliでもなんでもOKです。
SSHで接続を実施後、rootへ変更してからが本番です!
- ディスク状態の確認
設定した「追加」EBSの状態を確認
# df -kh Filesystem Size Used Avail Use% Mounted on devtmpfs 231M 64K 231M 1% /dev tmpfs 241M 0 241M 0% /dev/shm /dev/xvda1 7.8G 1.1G 6.7G 14% /
マウントされていない。
# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT xvda 202:0 0 8G 0 disk └─xvda1 202:1 0 8G 0 part / xvdb 202:16 0 30G 0 disk
どうやら、xvdbが追加したものらしい
- LVMの作成と追加
今回、LVMでパーティションを切りたかったのは
/var /var/log /home /swap
※Swapに関しては領域の作り方間違えてるかもですが、何度かやった時に動いたのがこれでした。
では作っていきましょう。
fdiskコマンドを使ってLVM領域をまずは作成します。
# fdisk /dev/xvdb 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 0x79db1f10. Command (m for help): m Command action a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition g create a new empty GPT partition table G create an IRIX (SGI) partition table l list known partition types m print this menu n add a new partition o create a new empty DOS partition table p print the partition table q quit without saving changes s create a new empty Sun disklabel t change a partition's system id u change display/entry units v verify the partition table w write table to disk and exit x extra functionality (experts only) 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-62914559, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-62914559, default 62914559): Using default value 62914559 Partition 1 of type Linux and of size 30 GiB is set
セクターはデフォルトのママなのでエンターでOK
そして、LVM化へ
Command (m for help): t Selected partition 1 Hex code (type L to list all codes): L 0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris 1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT- 2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT- 3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT- 4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx 5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data 6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / . 7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility 8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt 9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/ 10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b 11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor 12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor 14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary 16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS 17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE 18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto 1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep 1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT 1e Hidden W95 FAT1 80 Old Minix Hex code (type L to list all codes): 8e Changed type of partition 'Linux' to 'Linux LVM' Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
これで、LVMディスクが出来上がりました。
次に、pvやlvを作成していきます
- PV・VG・LVの作成
PVの作成
# pvcreate /dev/xvdb1 Physical volume "/dev/xvdb1" successfully created. # pvdisplay "/dev/sdb1" is a new physical volume of "30.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb1 VG Name PV Size 30.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID SjcJ1d-SKZ7-WGII-V2RR-1rGI-JEP0-t67bh6
VG(ボリュームグループ)の作成
ボリュームグループの名前はvg-allとします。
# vgcreate vg-all /dev/xvdb1 Volume group "vg-all" successfully created # vgdisplay --- Volume group --- VG Name vg-all System ID Format lvm2 Metadata Areas 1 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 1 Act PV 1 VG Size 30.00 GiB PE Size 4.00 MiB Total PE 7679 Alloc PE / Size 0 / 0 Free PE / Size 7679 / 30.00 GiB VG UUID gS3ekC-gM5Y-3FPq-2Ytc-hjmH-zUo4-NVSJE3
LVの作成
今回は以下で構成します。
Home->10GiB var->5GiB log->5GiB swap->4.9GiB <|| では作っていきましょう >|| # lvcreate -n lv-home --size 10GiB vg-all Logical volume "lv-home" created # lvcreate -n lv-var --size 5GiB vg-all Logical volume "lv-var" created. # lvcreate -n lv-log --size 5GiB vg-all Logical volume "lv-log" created. # lvcreate -n lv-swap --size 4.9GiB vg-all Rounding up size to full physical extent 4.90 GiB Logical volume "lv-swap" created.
各パーティションをフォーマット・ラベル作成
フォーマット
# mkfs -t ext4 /dev/vg-all/lv-home mke2fs 1.43.5 (04-Aug-2017) Creating filesystem with 2621440 4k blocks and 655360 inodes Filesystem UUID: 4e7d8c75-3a7f-416e-a0dc-0a92b433f9cb Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632 Allocating group tables: done Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done # mkfs -t ext4 /dev/vg-all/lv-var mke2fs 1.43.5 (04-Aug-2017) Creating filesystem with 1310720 4k blocks and 327680 inodes Filesystem UUID: 731aa7e0-829f-47aa-973b-feb9990b1a29 Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 Allocating group tables: done Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done # mkfs -t ext4 /dev/vg-all/lv-log mke2fs 1.43.5 (04-Aug-2017) Creating filesystem with 1310720 4k blocks and 327680 inodes Filesystem UUID: a45dd8f6-a9b3-4937-b9ed-7183abf23ce1 Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 Allocating group tables: done Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done # mkfs -t ext4 /dev/vg-all/lv-swap mke2fs 1.43.5 (04-Aug-2017) Creating filesystem with 1285120 4k blocks and 321280 inodes Filesystem UUID: 8d251aaf-e21d-478e-bae5-c2a021b734f1 Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 Allocating group tables: done Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done
ラベルは以下で設定
lv-home -> /home lv-var -> /var lv-log -> /var/log lv-swap -> /swap # e2label /dev/vg-all/lv-home /home # e2label /dev/vg-all/lv-var /var # e2label /dev/vg-all/lv-log /var/log # e2label /dev/vg-all/lv-swap /swap
- 既存データのコピー
LVMディスクに入れ替えを行うパーティションのデータをコピーする必要があるため
マウントポイント作成し、一度マウントを行う
マウントポイント作成
# mkdir /swap # mkdir /mnt/home # mkdir /mnt/var # mkdir /mnt/log
マウント実施
# mount /dev/vg-all/lv-home /mnt/home # mount /dev/vg-all/lv-var /mnt/var # mount /dev/vg-all/lv-log /mnt/log # mount /dev/vg-all/lv-swap /swap # df -kh Filesystem Size Used Avail Use% Mounted on devtmpfs 231M 84K 231M 1% /dev tmpfs 241M 0 241M 0% /dev/shm /dev/xvda1 7.8G 1.1G 6.7G 14% / /dev/mapper/vg--all-lv--home 9.8G 37M 9.3G 1% /mnt/home /dev/mapper/vg--all-lv--var 4.9G 20M 4.6G 1% /mnt/var /dev/mapper/vg--all-lv--log 4.9G 20M 4.6G 1% /mnt/log /dev/mapper/vg--all-lv--swap 4.8G 20M 4.5G 1% /swap
ゴミデータを削除
linux - lost+found ディレクトリは削除して問題ない? - スタック・オーバーフロー
このあたりを参照すると今回は新規ディスクなので削除していいらしいので削除
# cd /mnt/home/ # ls -la total 24 drwxr-xr-x 3 root root 4096 Nov 23 09:35 . drwxr-xr-x 5 root root 4096 Nov 23 09:41 .. drwx------ 2 root root 16384 Nov 23 09:35 lost+found # rm -rf lost+found # ls -la total 8 drwxr-xr-x 2 root root 4096 Nov 23 09:47 . drwxr-xr-x 5 root root 4096 Nov 23 09:41 .. # cd /mnt/var/ # ls -la total 24 drwxr-xr-x 3 root root 4096 Nov 23 09:35 . drwxr-xr-x 5 root root 4096 Nov 23 09:41 .. drwx------ 2 root root 16384 Nov 23 09:35 lost+found # rm -rf lost+found # ls -la total 8 drwxr-xr-x 2 root root 4096 Nov 23 09:47 . drwxr-xr-x 5 root root 4096 Nov 23 09:41 .. # cd /mnt/log/ # ls -la total 24 drwxr-xr-x 3 root root 4096 Nov 23 09:35 . drwxr-xr-x 5 root root 4096 Nov 23 09:41 .. drwx------ 2 root root 16384 Nov 23 09:35 lost+found # rm -rf lost+found # ls -la total 8 drwxr-xr-x 2 root root 4096 Nov 23 09:47 . drwxr-xr-x 5 root root 4096 Nov 23 09:41 .. # cd /swap/ # ls -la total 24 drwxr-xr-x 3 root root 4096 Nov 23 09:35 . drwxr-xr-x 5 root root 4096 Nov 23 09:41 .. drwx------ 2 root root 16384 Nov 23 09:35 lost+found # rm -rf lost+found # ls -la total 8 drwxr-xr-x 2 root root 4096 Nov 23 09:47 . drwxr-xr-x 5 root root 4096 Nov 23 09:41 .. # cd /root
Swap作成
swap用のパーティションはswap用として作成してるのでswapファイルを作成
今回swapの容量は4GiBとして設定する
# dd if=/dev/zero of=/swap/swapfile bs=1MB count="4096" 4096+0 records in 4096+0 records out 4096000000 bytes (4.1 GB) copied, 61.8299 s, 66.2 MB/s # chmod 600 /swap/swapfile # mkswap /swap/swapfile Setting up swapspace version 1, size = 3999996 KiB no label, UUID=85dba3d9-c56e-4cbe-9932-6b1e60ac193a # swapon /swap/swapfile # free -m total used free shared buffers cached Mem: 481 468 13 0 9 383 -/+ buffers/cache: 75 405
有効化が確認できたら一旦マウントは解除しておく
コマンドは以下
swapoff /swap/swapfile free -m
既存データのコピー
これでコピーのやり方を間違えると再起動時に起動できなくなることが確認できている
”/”だけだったり"/*”にしてなかったりするとうまく起動してこなかった。
# cp -rp /var/* /mnt/var/ # cp -rp /var/log/* /mnt/log/ # cp -rp /home/* /mnt/home/ # ls -la /home/ec2-user/ total 24 drwx------ 3 ec2-user ec2-user 4096 Nov 23 09:03 . drwxr-xr-x 3 root root 4096 Nov 23 09:03 .. -rw-r--r-- 1 ec2-user ec2-user 18 Aug 30 2017 .bash_logout -rw-r--r-- 1 ec2-user ec2-user 193 Aug 30 2017 .bash_profile -rw-r--r-- 1 ec2-user ec2-user 124 Aug 30 2017 .bashrc drwx------ 2 ec2-user ec2-user 4096 Nov 23 09:03 .ssh # ls -la /mnt/home/ec2-user/.ssh/ total 12 drwx------ 2 ec2-user ec2-user 4096 Nov 23 09:03 . drwx------ 3 ec2-user ec2-user 4096 Nov 23 09:03 .. -rw------- 1 ec2-user ec2-user 395 Nov 23 09:03 authorized_keys
ここで、必ずec2-userの鍵ファイルがちゃんとあり、権限設定も正しいこと(不安な人は中身も)確認しておく。
- アンマウントと一時マウントポイント削除
# umount /mnt/log # umount /mnt/var # umount /mnt/home # umount /swap # df -kh Filesystem Size Used Avail Use% Mounted on devtmpfs 231M 84K 231M 1% /dev tmpfs 241M 0 241M 0% /dev/shm /dev/xvda1 7.8G 1.1G 6.7G 14% / # rm -rf /mnt/log # rm -rf /mnt/var # rm -rf /mnt/home # ls -la /mnt/ total 8 drwxr-xr-x 2 root root 4096 Nov 23 10:00 . dr-xr-xr-x 26 root root 4096 Nov 23 09:41 ..
- fatabとrc.localへの追記
マウントポイントの変更とrc.localへ追記を行って
・起動時にLVMがマウントされること
・再起動後、swapが作られていること
を確認する。
fstabの変更
cp -rp /etc/fstab /root/fstab.org vi /etc/fstab
1行目をコピーして
2〜5行目に追記を行う
LABEL=/home /home ext4 defaults,noatime 1 1 LABEL=/var /var ext4 defaults,noatime 1 1 LABEL=/var/log /var/log ext4 defaults,noatime 1 1 LABEL=/swap /swap ext4 defaults,noatime 1 1
diffするとこんな感じ
# diff /etc/fstab /root/fstab.org 3,6d2 < LABEL=/home /home ext4 defaults,noatime 1 1 < LABEL=/var /var ext4 defaults,noatime 1 1 < LABEL=/var/log /var/log ext4 defaults,noatime 1 1 < LABEL=/swap /swap ext4 defaults,noatime 1 1
マウントの確認を行う
# mount -a # df -kh # df -kh Filesystem Size Used Avail Use% Mounted on devtmpfs 231M 84K 231M 1% /dev tmpfs 241M 0 241M 0% /dev/shm /dev/xvda1 7.8G 1.1G 6.7G 14% / /dev/mapper/vg--all-lv--home 9.8G 37M 9.3G 1% /home /dev/mapper/vg--all-lv--var 4.9G 72M 4.6G 2% /var /dev/mapper/vg--all-lv--log 4.9G 21M 4.6G 1% /var/log /dev/mapper/vg--all-lv--swap 4.8G 3.9G 685M 86% /swap
正しくマウントされている。
この状態で別ターミナルSSHアクセスができることも確認しておくとなお良い
$ ssh 52.69.244.157 -l ec2-user -i ****** Enter passphrase for key '******': Last login: Fri Nov 23 09:06:44 2018 from ****** __| __|_ ) _| ( / Amazon Linux AMI ___|\___|___| https://aws.amazon.com/amazon-linux-ami/2018.03-release-notes/
こんな感じ。
rc.localの最下行に以下を追記して保存する。
swapon /swap/swapfile
swap有効化の確認は
# sh /etc/rc.local # free -m total used free shared buffers cached Mem: 481 180 300 0 9 110 -/+ buffers/cache: 60 421 Swap: 3906 0 3906
- 再起動の確認と動作確認
念のため再起動とswapが有効になることを確認する。
# swapoff /swap/swapfile # free -m # umount -a umount: /: target is busy. (In some cases useful info about processes that use the device is found by lsof(8) or fuser(1)) umount: /dev: target is busy. (In some cases useful info about processes that use the device is found by lsof(8) or fuser(1)) # df -kh Filesystem Size Used Avail Use% Mounted on devtmpfs 231M 84K 231M 1% /dev /dev/xvda1 7.8G 1.1G 6.7G 14% / # reboot
再起動してきたら最終確認を行って通常セットアップを行っていく
# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT xvda 202:0 0 8G 0 disk └─xvda1 202:1 0 8G 0 part / xvdb 202:16 0 30G 0 disk └─xvdb1 202:17 0 30G 0 part ├─vg--all-lv--var 253:1 0 5G 0 lvm /var ├─vg--all-lv--log 253:2 0 5G 0 lvm /var/log ├─vg--all-lv--home 253:0 0 10G 0 lvm /home └─vg--all-lv--swap 253:3 0 4.9G 0 lvm /swap # df -kh Filesystem Size Used Avail Use% Mounted on devtmpfs 231M 84K 231M 1% /dev tmpfs 241M 0 241M 0% /dev/shm /dev/xvda1 7.8G 1.1G 6.7G 14% / /dev/mapper/vg--all-lv--home 9.8G 37M 9.3G 1% /home /dev/mapper/vg--all-lv--var 4.9G 72M 4.6G 2% /var /dev/mapper/vg--all-lv--log 4.9G 21M 4.6G 1% /var/log /dev/mapper/vg--all-lv--swap 4.8G 3.9G 685M 86% /swap # free -m total used free shared buffers cached Mem: 481 188 293 0 8 116 -/+ buffers/cache: 62 418 Swap: 3906 0 3906
お疲れ様でした!