準備するもの
① EC2 Instance(CentOS6)既存の動いているものでもOK
② 変換元のRoot deviceのSnapshot
③ ②から作成したEBS Volume (SSDのほうが作業が早い)
③ 空のEBS Volume (SSDのほうが作業が早い)
DEVICEの確認
1 2 3 4 5 6 |
# fdisk -l |grep dev Disk /dev/xvda: 10.7 GB, 10737418240 bytes Disk /dev/xvdb: 4289 MB, 4289200128 bytes Disk /dev/xvdf: 10.7 GB, 10737418240 bytes <-PV環境のRoot device Disk /dev/xvdg: 10.7 GB, 10737418240 bytes <-空のEBS Volume |
作業開始
1.PVのDiskを縮小し、コピー容量を減らす
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
作業前の状態確認 # mount /dev/xvdf /mnt/ # df -h /dev/xvdf Filesystem Size Used Avail Use% Mounted on /dev/xvdf 9.9G 2.3G 7.2G 24% /mnt # umount /mnt/ 縮小化 # e2fsck -f /dev/xvdf # resize2fs -M /dev/xvdf resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/xvdf to 598879 (4k) blocks. The filesystem on /dev/xvdf is now 598879 blocks long. 作業後の状態確認 # mount /dev/xvdf /mnt/ # df -h /dev/xvdf Filesystem Size Used Avail Use% Mounted on /dev/xvdf 2.3G 2.2G 0 100% /mnt # umount /mnt/ |
NOTE:
- Blockサイズをメモしておく(今回は598879 blocks)
2.新しいDiskへイメージ作成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# ls /dev/xvd* /dev/xvda /dev/xvda1 /dev/xvdb /dev/xvdf /dev/xvdg パーティション作成(ワンライナー) # parted /dev/xvdg --script 'mklabel msdos mkpart primary 1M -1s print quit' 確認 # ls /dev/xvd* /dev/xvda /dev/xvda1 /dev/xvdb /dev/xvdf /dev/xvdg /dev/xvdg1 # udevadm info --query=property --name=/dev/xvdg1 UDEV_LOG=3 DEVPATH=/devices/vbd-51808/block/xvdg/xvdg1 MAJOR=202 MINOR=97 DEVNAME=/dev/xvdg1 DEVTYPE=partition SUBSYSTEM=block ID_PATH=xen-vbd-51808 ID_PART_TABLE_TYPE=dos LVM_SBIN_PATH=/sbin DEVLINKS=/dev/block/202:97 /dev/disk/by-path/xen-vbd-51808-part1 |
※partedのやっていることは下記の通り。上記のパーティション作成を行えば、下記の作業不要
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# parted /dev/xvdg (parted) print Model: Xen Virtual Block Device (xvd) Disk /dev/xvdg: 10.7GB Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start End Size Type File system Flags (parted) mklabel msdos (parted) mkpart Partition type? primary/extended? primary File system type? [ext2]? Start? 1M End? -1s (parted) print Model: Xen Virtual Block Device (xvd) Disk /dev/xvdg: 10.7GB Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start End Size Type File system Flags 1 1049kB 10.7GB 10.7GB primary (parted) quit |
/dev/xvdg1が作成されている。なければ下記を実行。
1 2 3 |
# partprobe /dev/xvdg # udevadm settle |
3.Disk コピー
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# dd if=/dev/xvdf of=/dev/xvdg1 bs=4K count=598879 598879+0 records in 598879+0 records out 2453008384 bytes (2.5 GB) copied, 73.0507 s, 33.6 MB/s コピー状態確認 # mount /dev/xvdg1 /mnt/ # df -h /mnt/ Filesystem Size Used Avail Use% Mounted on /dev/xvdg1 2.3G 2.2G 0 100% /mnt 元のサイズに戻す # resize2fs /dev/xvdg1 拡張されたことを確認 # df -h /dev/xvdg1 Filesystem Size Used Avail Use% Mounted on /dev/xvdg1 9.9G 2.3G 7.2G 24% /mnt |
NOTE:
- ここで、上記のブロックサイズを利用する
- 2.5GBで、1分ちょっと
4.Chroot準備
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# ls -l /mnt/dev/ total 0 crw------- 1 root root 5, 1 Mar 30 2012 console crw-rw-rw- 1 root root 1, 3 Mar 30 2012 null crw-rw-rw- 1 root root 5, 0 Mar 30 2012 tty crw-rw-rw- 1 root root 1, 5 Mar 30 2012 zero deviceのコピー # cp -a /dev/xvdg /dev/xvdg1 /mnt/dev/ # ls -l /mnt/dev/ total 0 crw------- 1 root root 5, 1 Mar 30 2012 console crw-rw-rw- 1 root root 1, 3 Mar 30 2012 null crw-rw-rw- 1 root root 5, 0 Mar 30 2012 tty brw-rw---- 1 root disk 202, 96 Jul 11 02:10 xvdg brw-rw---- 1 root disk 202, 97 Jul 11 03:08 xvdg1 crw-rw-rw- 1 root root 1, 5 Mar 30 2012 zero Procファイルの作成 # mount -t proc none /mnt/proc |
5.HVMへの各種設定を行う
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# chroot /mnt ※以下、chrootで作業 (chroot)# yum list grub grub.x86_64 1:0.97-83.el6 @base ※入っていなければインストール (chroot)# ls -l /boot/grub/grub.conf ※リンクがなければSymbolicLinkを作成 (chroot)# ln -s /boot/grub/menu.lst /boot/grub/grub.conf (chroot)# ls -l /boot/grub/grub.conf lrwxrwxrwx 1 root root 19 7月 11 13:47 2014 /boot/grub/grub.conf -> /boot/grub/menu.lst (chroot)# ls -l /etc/grub.conf ※リンクがなければSymbolicLinkを作成 (chroot)# ln -s /boot/grub/grub.conf /etc/grub.conf (chroot)# ls -l /etc/grub.conf lrwxrwxrwx 1 root root 20 7月 11 13:47 2014 /etc/grub.conf -> /boot/grub/grub.conf (chroot)# ls /mnt/boot/grub/*stage* ※ファイルがあれば、既存は削除 (chroot)# rm -f /mnt/boot/grub/*stage* (chroot)# cp /mnt/usr/*/grub/*/*stage* /mnt/boot/grub/ (chroot)# ls -l /mnt/boot/grub/*stage* -rw-r--r-- 1 root root 13380 Jul 11 03:09 /mnt/boot/grub/e2fs_stage1_5 -rw-r--r-- 1 root root 12620 Jul 11 03:09 /mnt/boot/grub/fat_stage1_5 -rw-r--r-- 1 root root 11748 Jul 11 03:09 /mnt/boot/grub/ffs_stage1_5 -rw-r--r-- 1 root root 11756 Jul 11 03:09 /mnt/boot/grub/iso9660_stage1_5 -rw-r--r-- 1 root root 13268 Jul 11 03:09 /mnt/boot/grub/jfs_stage1_5 -rw-r--r-- 1 root root 11956 Jul 11 03:09 /mnt/boot/grub/minix_stage1_5 -rw-r--r-- 1 root root 14412 Jul 11 03:09 /mnt/boot/grub/reiserfs_stage1_5 -rw-r--r-- 1 root root 512 Jul 11 03:09 /mnt/boot/grub/stage1 -rw-r--r-- 1 root root 126100 Jul 11 03:09 /mnt/boot/grub/stage2 -rw-r--r-- 1 root root 126100 Jul 11 03:09 /mnt/boot/grub/stage2_eltorito -rw-r--r-- 1 root root 12024 Jul 11 03:09 /mnt/boot/grub/ufs2_stage1_5 -rw-r--r-- 1 root root 11364 Jul 11 03:09 /mnt/boot/grub/vstafs_stage1_5 -rw-r--r-- 1 root root 13964 Jul 11 03:09 /mnt/boot/grub/xfs_stage1_5 (chroot)# ls -l /mnt/boot/grub/device.map ※あれば、既存は削除 (chroot)# rm -f /mnt/boot/grub/device.map |
6.grub インストール
1 2 3 4 5 |
(chroot)# cat < > device (hd0) /dev/xvdo > root (hd0,0) > setup (hd0) > EOF |
※grubやっていることは下記の通り。上記のパーティション作成を行えば、下記の作業不要
1 2 3 4 5 6 7 8 9 10 11 12 |
(chroot)# grub --batch Probing devices to guess BIOS drives. This may take a long time. GNU GRUB version 0.97 (640K lower / 3072K upper memory) [ Minimal BASH-like line editing is supported. For the first word, TAB lists possible command completions. Anywhere else TAB lists the possible completions of a device/filename.] grub> device (hd0) /dev/xvdg grub> root (hd0,0) grub> setup (hd0) grub> quit |
7.ローレベルのDisk確認
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
ラベル確認 (chroot)# tune2fs -l /dev/xvdg1 |grep name Filesystem volume name: _/ Fsckの確認 (chroot)# tune2fs -l /dev/xvdg1 |egrep "Maximum|Check" Maximum mount count: 28 Check interval: 15552000 (6 months) 起動時にfsckを行わないように変更 (chroot)# tune2fs -i 0 -c 0 /dev/xvdg1 tune2fs 1.41.12 (17-May-2010) Setting maximal mount count to -1 Setting interval between checks to 0 seconds (chroot)# tune2fs -l /dev/xvdg1 |egrep "Maximum|Check" Maximum mount count: -1 Check interval: 0 () |
8.Grubの設定変更
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
(chroot)# cp -a /boot/grub/menu.lst{,.org} (chroot)# vi /boot/grub/menu.lst root (hd0) ↓ root (hd0,0) kernel /boot/vmlinuz-2.6.32-220.7.1.el6.x86_64 ro root=LABEL=_/ ↓ kernel /boot/vmlinuz-2.6.32-220.7.1.el6.x86_64 ro root=LABEL=_/ console=ttyS0 xen_pv_hvm=enable (chroot)# diff /boot/grub/menu.lst{,.org} 6,7c6,7 < root (hd0,0) < kernel /boot/vmlinuz-2.6.32-220.7.1.el6.x86_64 ro root=LABEL=_/ console=ttyS0 xen_pv_hvm=enable --- > root (hd0) > kernel /boot/vmlinuz-2.6.32-220.7.1.el6.x86_64 ro root=LABEL=_/ fstabの設定変更(ココらへんは既存の設定を踏襲するはず) # LABEL=_/ / ext4 defaults 1 1 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 |
9.作業完了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
(chroot)# exit # rm -f /mnt/dev/xvdg* # ls -l /mnt/dev/ total 0 crw------- 1 root root 5, 1 Mar 30 2012 console crw-rw-rw- 1 root root 1, 3 Mar 30 2012 null crw-rw-rw- 1 root root 5, 0 Mar 30 2012 tty crw-rw-rw- 1 root root 1, 5 Mar 30 2012 zero # umount /mnt/proc /mnt # df -h /dev/xvdg1 Filesystem Size Used Avail Use% Mounted on - 1.9G 156K 1.9G 1% /dev |
あとは、/dev/xvdgのSnapshotを取得し、SnapshotからAMIを作成する。
ココらへんは、ManagementConsoleから作業可能です。