バックアップしたルートディレクトリから起動できるようにするための準備

ルートディレクトリ(/)のバックアップ - k3k1::logでルート以下をバックアップしました。バックアップ元が壊れた場合、代わりにバックアップ先のディスクをPCに接続して起動したくなると思います。あらかじめ準備しておくことで、バックアップ先のディスクに交換するだけで起動できるようになります。手順は以下の通りです。

  1. /proc, /sys, /tmp の作成
  2. ファイルシステムのラベルと、fstab の変更
  3. grub のインストール

swap パーティーションについては、なしでいきます。

以下ではルートディレクトリは /dev/hdc1 にバックアップされているとし、バックアップ先のパーティーションは /mnt/hdc1 にマウントされていると仮定します。

/proc, /sys, /tmp の作成

/mnt/hdc1 以下に proc, sys, tmp ディレクトリを作成しておきます。/tmp は chmod 1777 しておきます。

ファイルシステムのラベルと、fstab の変更

バックアップ先のディスクラベルを変更します。バックアップ元のラベルが / であれば、バックアップ先のラベルは例えば /1 のようにしておきます。

# e2label /dev/hdc1 /1
# e2label /dev/hdc1
/1

バックアップ先の fstab を編集します。ポイントは、

  • ルートディレクトリのファイルシステムのラベルを上記で設定した名前 (ここでは /1) に変更。
  • swap をマウントしないように変更。
  • ほかにも使用しないパーティーションをマウントしないように変更。

変更後の fstab は以下のようになります。

$ cat fstab
LABEL=/1                 /                       ext3    defaults        1 1
#LABEL=/export           /export                 ext3    defaults        1 2
#LABEL=/boot             /boot                   ext3    defaults        1 2
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
#LABEL=SWAP-hda3         swap                    swap    defaults        0 0

バックアップ前は /boot を分けていましたが、/boot を含めてバックアップしたので /boot パーティーションのマウントが必要なくなりました。

grub のインストール

device.map の修正

/mnt/hdc1/boot/gurb/device.map を書き換えます。現在 /dev/hdc として接続されていますので、device.map にダミーの /dev/hdc を追加します。また、バックアップ先から起動させる際は /dev/hda として認識される*1ので hda の行は残します。

変更後の device.map は以下のようになりました。

$ cat /mnt/hdc1/boot/grub/device.map
# this device map was generated by anaconda
(hd0)     /dev/hda
(hd1)     /dev/hdc
grub.conf の変更

/mnt/hdc1/boot/grub/grub.conf を変更します。
変更点は、

  • splashimage, kernel, initrd のパスに /boot を追加します。
  • kernel の LABEL を / から例えば /1 へ変更します。

変更後の grub.conf は以下のようになりました。

splashimage=(hd0,0)/boot/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.18-92.1.22.el5.centos.plus)
        root (hd0,0)
        kernel /boot/vmlinuz-2.6.18-92.1.22.el5.centos.plus ro root=LABEL=/1 quiet rhgb
        initrd /boot/initrd-2.6.18-92.1.22.el5.centos.plus.img
grub-install の実行
# grub-install --root-directory=/mnt/hdc1/ /dev/hdc

ブート確認

BIOS から起動順序を変更し、起動することを確認します。

追記 (2010/2/15) switchroot: mount failure と表示され kernel panic になる場合

mkinitrd で initrd を作りなおす必要があるそうです。
以下、http://sshida.com/~shida/diary/20071216-13-diary.htmlより引用です。

1. Boot using install DVD or rescue CD in rescue mode. When it asks if you should find Fedora installations click on continue so it mounts the installation to /mnt/sysimage.
2. At the command line chroot to /mnt/sysimage.
3. cd /boot, then determine kernel version using ls vmlinuz*
4. mv initrd-2.6.x.x-x.fc8.img initrd-2.6.x.x-x.fc8.old
5. mkinitrd initrd-2.6.x.x-x.fc8.img 2.6.x.x-x.fc8
6. exit, exit and it should reboot.

*1:IDE のプライマリマスタとして接続する場合。手元の環境では、IDEセカンダリマスタとして接続していてもBIOSの起動順で最優先に設定すると /dev/hda と認識するようです。