2015年4月9日木曜日

resize2fs と fdisk で ext4 のパーティションを縮小する


1 resize2fs と fdisk で ext4 のパーティションを縮小する




1.1 はじめに



パーティションの拡大縮小はいつもGParted を使っていた.

しかし,今回はディスプレイが繋がっていないマシンのパーティションを変更するために,CLI で操作できるresize2fs と fdisk で操作した.

結構悩んだが,わかればそんなにややこしい手順じゃないはず.






1.2 作業の流れ



パーティションを縮小すると言っても,実はパーティションのサイズだけ縮小してもダメらしい.

ファイルシステムのサイズも変えないといけないんだそうだ.

しかも,この2つのサイズが一致してないとまずいらしい.



resize2fs はサイズ指定なしで実行すると,ファイルシステムのサイズを可能な限り大きくする.

つまり,パーティションのサイズいっぱいにファイルシステムのサイズを広げてくれる.

これを利用すれば,面倒な計算なしに,ファイルシステムとパーティションサイズのサイズを合わせられる.

(逆にパーティションサイズに合わせて縮小,ということはやってくれないっぽい?)



というわけで,以下の手順を取る.



  1. resize2fs でファイルシステムを縮小(予定サイズよりもちょっと小さめに)


  2. fdisk でパーティションのサイズを縮小(予定サイズに縮小)


  3. resize2fs でファイルシステムのサイズをパーティションのサイズに合わせる(勝手に計算してくれる)







1.3 resize2fs でファイルシステム



今回は/dev/sdb1 を512GB から32GB に縮小する例を示す.



まずは,resize2fs でファイルシステムのサイズを予定サイズより少し小さめに変更.

512GB–>31GB の変更は結構時間がかかる(10分くらい?).



# e2fsck -f /dev/sdb1
e2fsck 1.42.5 (29-Jul-2012)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdb1: 184019/30777344 files (1.9% non-contiguous), 7022898/123088640 blocks
# resize2fs /dev/sdb1 31G
resize2fs 1.42.5 (29-Jul-2012)
Resizing the filesystem on /dev/sdb1 to 8388608 (4k) blocks.
The filesystem on /dev/sdb1 is now 8388608 blocks long.
#


次にfdisk でパーティションのサイズを変更する.

操作的にはサイズを変更というより,元のパーティションを削除して,同じところに小さいサイズのパーティションを作りなおすと,なる.



# fdisk /dev/sdb
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.

Command (m for help): p

Disk /dev/sdb: 512.1 GB, 512110190592 bytes
255 heads, 63 sectors/track, 62260 cylinders, total 1000215216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x000c3f6e

Device Boot Start End Blocks Id System
/dev/sdb1 * 2048 984711167 492354560 83 Linux
/dev/sdb2 984713214 1000214527 7750657 5 Extended
Partition 2 does not start on physical sector boundary.
/dev/sdb5 984713216 1000214527 7750656 82 Linux swap / Solaris

Command (m for help): d <-- パーティション削除
Partition number (1-5): 1

Command (m for help): n <-- 新しいパーティション作成
Partition type:
p primary (0 primary, 1 extended, 3 free)
l logical (numbered from 5)
Select (default p):
Using default response p
Partition number (1-4, default 1):
Using default value 1
First sector (2048-1000215215, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-984713213, default 984713213): +32G

Command (m for help): p

Disk /dev/sdb: 512.1 GB, 512110190592 bytes
255 heads, 63 sectors/track, 62260 cylinders, total 1000215216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x000c3f6e

Device Boot Start End Blocks Id System
/dev/sdb1 2048 69208063 34603008 83 Linux
/dev/sdb2 984713214 1000214527 7750657 5 Extended
Partition 2 does not start on physical sector boundary.
/dev/sdb5 984713216 1000214527 7750656 82 Linux swap / Solaris

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

Calling ioctl() to re-read partition table.
Syncing disks.
#


最後にファイルシステムのサイズをパーティションのサイズに合わせる.



# resize2fs /dev/sdb1
resize2fs 1.42.5 (29-Jul-2012)
Resizing the filesystem on /dev/sdb1 to 8650752 (4k) blocks.
The filesystem on /dev/sdb1 is now 8650752 blocks long.
#


これで完了(のはず)






1.4 おわりに



パーティションの操作にあまり慣れていなかったので,結構調べるのに時間かかった.






--
My Emacs Files At GitHub

Debian でQEMU をビルド


1 Debian でQEMU をビルド




1.1 はじめに



QEMU の環境を何度も作る機会があったので,書いておく.





1.2 参考ページ



Debian上にOSvビルド環境を構築してみた - 夜はいよいよ冴えたのだ。

記事自体はOSv の環境構築について書いているものであるが,その一部としてQEMU のビルドの方法が書かれている.

その部分をありがたく参考にさせていただいた.






1.3 環境




# cat /etc/debian_version
7.8
# uname -a
Linux kvm-host2 3.2.0-4-amd64 #1 SMP Debian 3.2.65-1+deb7u2 x86_64 GNU/Linux





1.4 手順



まずはqemu のソースコードを入手する.

ソースコードはgit でクローンするので,git のインストールも行う.



# apt-get install git
# git clone git://git.qemu.org/qemu.git


次にビルドに必要なパッケージをインストールする.




# apt-get install make
# apt-get install gcc
# apt-get install pkg-config
# apt-get install libglib2.0-dev
# apt-get install libcap-dev
# apt-get install libattr1-dev
# apt-get install libpixman-1-dev


次に,ビルドの準備をする.

具体的には,ビルドしたいQEMU のバージョンのソースコードへチェックアウト,configure の実行である.

configure の実行はオプションが多くなりがちなので, setup.sh というスクリプトファイルにして実行してみた.



cd ~/qemu
git checkout v2.2.1
vi setup.sh




./configure --prefix=/opt/qemu-2.2.1 \
--target-list=x86_64-softmmu,x86_64-linux-user \
--enable-kvm \
--enable-vhost-net \




# sh setup.sh 2>&1 | tee -a _setup.log
# make -j8 2>&1 | tee -a _make.log
# make install 2>&1 | tee -a _make-install.log


最後に動作確認



# /opt/qemu-2.2.1/bin/qemu-system-x86_64 --version
QEMU emulator version 2.2.1, Copyright (c) 2003-2008 Fabrice Bellard




1.5 おわりに



書いてみて気づいたが,特別難しいところはなさそう.

ビルドに必要なパッケージをいつも忘れるくらい.






--
My Emacs Files At GitHub