システムの救出（レスキュー）

はじめに

第44章はじめに

遅かれ早かれ、システムは、適切なブートでも、ファイルシステムのマウントでも、デスクトップ表示の開始などでも、失敗するような重大な障害に遭遇する可能性があります。光ディスクまたはポータブルUSBドライブ形式のシステム レスキュー メディアを使用して、状態を修正できます。緊急モードまたはシングル ユーザー モードでブートすると、一連のLinuxツールを使用して、システムを通常の機能に修復できます。


学習目標
この章の終わりまでに、次のことができるようになります。

システム レスキュー メディアがどのような形式で提供され、それらをどのように利用可能にするか、または準備できるかを説明できます。
緊急モードに入る方法とそこでできることを知ることができます。
シングル ユーザー モードに入る方法、そこでできること、および緊急モードとの違いを理解できます。


システムの救出（レスキュー）

レスキュー メディアとトラブルシューティング
レスキュー  ディスクとリカバリ ディスク、およびその他のメディアについては、次のセッションで説明します。ここでは、トラブルシューティングについて説明します。これらのメディアには、問題のあるシステムを評価（および修正）するための貴重なツールが含まれています。

何を選択するのが正しいかはLinuxディストリビューションによって異なりますが、インストールする場合、またはライブCD/DVDやUSBドライブから起動する場合、Rescue Installed Systemなどの名前のオプションを選択します。

レスキュー ディスクには、多くの便利なプログラムが含まれています。

パーティションの作成、RAIDデバイスの管理、論理ボリュームの管理、ファイルシステムの作成のためのディスク ユーティリティ。fdisk、mdadm、pvcreate、vgcreate、lvcreate、mkfsなど。
ネットワークのデバッグとネットワーク接続のためのネットワーク ユーティリティ。ifconfig、route、traceroute、mtr、host、ftp、scp、ssh など。
その他のユーティリティ
ログ ファイル。


レスキュー／リカバリ イメージの使用
レスキューのイメージは、開始時にいくつかの質問をしてきます。その1つは、（可能な場合）ファイルシステムをマウントするかどうかです。

もしマウントする場合、どこかに、通常は/mnt/sysimageに、マウントします。そのディレクトリに移動してファイルにアクセスできます。もしくは次のコマンドでその環境に変更できます。

$ sudo chroot /mnt/sysimage

ネットワーク ベースのレスキューの場合、/mnt/sourceをマウントするよう求められる場合もあります。

ソフトウェア パッケージは、chrootした環境からインストールできます。また、chrootした環境の外部からインストールすることもできます。たとえば、rpmベースのシステムでは、-rootオプションを使用してルート ディレクトリの場所を指定します。

$ sudo rpm -ivh --force --root=/mnt/sysimage /mnt/source/Packages/vsftpd-2*.rpm


システムのレスキューとリカバリ
システムは、適切なブート、ファイルシステムのマウント、そしてデスクトップ表示の開始などでも、どのみち失敗するような重大な障害に遭遇する可能性があります。光ディスクまたはポータブルUSBドライブ形式のシステム レスキュー メディアを使用して、状態を修正できます。緊急モードまたはシングル ユーザー モードでブートすると、一連のLinuxツールを使用して、システムを通常の機能に修復できます。


緊急用ブート メディア
緊急用ブート メディアは、ファイルの欠落、構成の誤り、ファイルの破損、サービスの構成の誤りなどの問題が原因で、システムがブートしない場合に役立ちます。 

rootパスワードが、何らかの理由で失われたり変に改ざんされたりしてリセットする必要がある場合にも、レスキュー メディアが役立つことがあります。

ほとんどのLinuxディストリビューションでは、インストール メディア（CD、DVD、USB）またはライブ メディア、あるいはその両方が、レスキュー ディスクとしての目的を果たすことができるため、二重の意味で非常に便利です。専用のレスキュー ディスクも利用できます。

（任意の形式の）ライブ メディアは、ディスクからロードするのではなく、メモリ内で実行される完全で起動可能なオペレーティング システムを提供します。ユーザーは、実際にインストールしたり、コンピュータ上の既存のオペレーティング システムに変更を加えたりせずに、オペレーティング システムやLinuxディストリビューションを体験および評価できます。

ライブ リムーバブル メディアは、ハードディスク ドライブなどのセカンダリ ストレージがないコンピュータ、または破損したハードディスク ドライブやファイルシステムがあるコンピュータでも実行できるという優れた点があり、ユーザーがデータをレスキューできるようにします。


レスキュー メディアの使用
ライブ、インストール、レスキュー メディアのいずれを使用しても、レスキューとリカバリのために特別なオペレーティング システムを開始する手順は同じであり、紹介したように、1つのメディアが3つの目的すべてを果たします。

システムはリムーバブル メディアからブートすると、ブート メニューのオプションからレスキュー／リカバリ モードにアクセスできます。多くの場合、次のようにrescueを入力する必要があります。

boot: Linux rescue

各ディストリビューションには多少の違いがありますが、手順を確認するのは簡単ですので、ここですべての手順を説明することはしません。

次に、使用する言語などのいくつかの質問と、いくつかのディストリビューションに依存する選択があります。そして、正しいレスキュー イメージの場所を選択するように求められます。CD/DVD、ハード ドライブ、NFS、FTP、HTTPなど。

選択した場所には正しいインストール ツリーが含まれている必要があり、インストール ツリーはレスキュー ディスクと同じLinuxバージョンのものである必要があります。リムーバブル メディアを使用している場合、インストール ツリーはメディアの作成元と同じものでなければなりません。 ベンダーからダウンロードしたboot.isoイメージを使用している場合は、ネットワーク ベースのインストール ツリーも必要です。

ファイルシステムのマウントについても質問されます。それらが見つかると、/mnt/sysimageの下にマウントされます。その後、シェル プロンプトが表示され、システムに適切な修正を行うためのさまざまなユーティリティにアクセスできます。

chrootは、ルート（/）ファイルシステムへのアクセスを改善するために使用できます。


レスキュー用のUSBキー
多くのディストリビューションは、ダウンロード用のboot.isoイメージ ファイルを提供しています（名前は異なる場合があります）。次のようにddを使用して、これをUSBキー ドライブに置くことができます。

$ dd if=boot.iso of=/dev/sdX

上記は、システムがリムーバブル ドライブを/dev/sdXとして認識すると仮定しています。ただし、これにより、USBキー ドライブ上の既存のコンテンツが消去されることに注意してください！

システムにUSBメディアから起動する機能があり、BIOSがそのように構成されている場合、このUSBドライブから起動できます。その後は、レスキューCDやDVDと同じように機能します。ただし、インストール ツリーはUSBドライブに存在しないことに注意してください。したがって、この方法では、必要に応じてネットワーク ベースのインストール ツリーが必要になります。

livecd-toolsやliveusb-creatorなどの便利なユーティリティを使用すると、インストール イメージを取得する場所としてローカル ドライブまたはインターネットのいずれかを指定できます。起動可能なイメージを作成してリムーバブル ドライブに書き込むという面倒な作業をすべて実行することができます。これは非常に便利であり、実際、すべてのLinuxディストリビューションで機能します。


緊急モード
緊急モードでは、最小限の環境でブートします。ルート ファイルシステムは読み取り専用でマウントされ、initスクリプトは実行されず、ほとんど何もセット アップされません。

シングル ユーザー モード（次で説明します）よりも緊急モードが優れている主な点は、initが破損しているか機能していない場合でも、ファイルシステムをマウントして、再インストール中に失われたデータをリカバリできることです。

緊急モードに入るには、GRUBブート メニューからエントリを選択し、eを押して編集する必要があります。そして、システムにブートするように指示する前に、emergencyという単語をカーネル コマンド ラインに追加します。シェル プロンプトを表示する前に、rootのパスワードの入力を求められます。

💡
また、ファイルシステムの破損など、さまざまな理由でブートが失敗した場合にも、緊急モードに入ることができます。


シングル ユーザー モード
システムをブートした時に、ブートが完了してもログインできない場合は、シングル ユーザー モードを試してください。シングル ユーザー モードでは、

initが開始されます。
サービスは開始されていません。
ネットワークはアクティブ化されていません。
可能なすべてのファイルシステムはマウントされています。
rootアクセスはパスワードなしで許可されます。
システム メンテナンスのコマンド ライン シェルが起動します。

このモードでは、システムはランレベル1（SysVinit言語の場合）で起動します。シングル ユーザー モードは自動的にファイルシステムをマウントしようとするため、ルート ファイルシステムを正常にマウントできない場合、またはinit構成が破損している場合は使用できません。

シングル ユーザー モードで起動するには、1つの例外を除いて、緊急モードで説明したのと同じ方法を使用します。キーワードをemergencyからsingleに置き換えます。


演習

課題 44.1: レスキュー／リカバリ用のメディアの準備

🚩
以下のPDFドキュメントに埋め込まれた外部URLにアクセスする場合は、常に右クリックして新しいタブまたはウィンドウで開いてください。直接クリックしてURLを開こうとすると、コース ウィンドウ／タブが閉じます。

【【これ以降は橋本さんの訳を参照】】

Very Important

In the following exercises we are going to deliberately damage the system and then recover through the use of rescue media.  Thus, it is obviously prudent to make sure you can indeed boot off the rescue media before you try anything more ambitious.

So first make sure you have rescue media, either a dedicated rescue/recovery image, or an install or Live image oneither an optical disk or usb drive.

Boot off it and make sure you know how to force the system to boot off the rescue media (you are likely to have to fiddle with the BIOS settings), and when the system boots, choose rescue mode.

Please Note

If you are using a virtual machine, the procedure is logically the same with two differences:

•  Getting to the BIOS might be difficult depending on the hypervisor you use.  Some of them require very rapid keystrokes, so read the documentation and make sure you know how to do it.

•  You can use a physical optical disk or drive, making sure the virtual machine settings have it mounted, and if it is USB you may have some other hurdles to make sure the virtual machine can claim the physical device.  It is usually easier to simply connect a.iso image file directly to the virtual machine.

If you are working with a virtual machine, obviously things are less dangerous, and if you are afraid of corrupting the system in an unfixable way, simply make a backup copy of the virtual machine image before you do these exercises, you can always replace the image with it later. 

Very Important

Do not do the following exercises unless you are sure you can boot your system off rescue/recovery media!


課題 44.2: 破壊されたGRUB設定を復旧する

🚩
以下のPDFドキュメントに埋め込まれた外部URLにアクセスする場合は、常に右クリックして新しいタブまたはウィンドウで開いてください。直接クリックしてURLを開こうとすると、コース ウィンドウ／タブが閉じます。

Exercise 44.2: Recovering from a Corrupted GRUB Configuration

1.  Edit your GRUB configuration file (/boot/grub/grub.cfg,/boot/grub2/grub.cfgor/boot/grub/grub.conf), and modify the kernel line by removing the first character of the value in the field named UUID. Take note of which character you removed, you will replace it in rescue mode.  (If your root filesystem is identified by either label or hard disk device node, make an analogous simple change.) Keep a backup copy of the original.

On a BLSCFG System

You can corrupt the command line by editing /etc/grub2/grubenv instead.

2.  Reboot the machine. The system will fail to boot, saying something like No root device was found. You will also see that a panic occurred.

3.  Insert into your machine the installation or Live DVD or CD or USB drive (or network boot media) if you have access to a functioning installation server). Reboot again. When the boot menu appears, choose to enter rescue mode.

4.  As an alternative, you can try selecting a rescue image from the GRUB menu; most distributions offer this. You’ll get the same experience as using rescue media, but it will not always work.  For example, if the root filesystem is damaged it will be impossible to do anything.

5.  In rescue mode, agree when asked to search for filesystems. If prompted, open a shell, and explore the rescue system by running utilities such as mount and ps.

6.  Repair your broken system by fixing your GRUB configuration file, either by editing it or restoring from a backup copy.

7.  Typeexitto return to the installer, remove the boot media, and follow the instructions on how to reboot.  Reboot your machine. It should come up normally.


課題 44.3: パスワード失敗からの復旧

🚩
以下のPDFドキュメントに埋め込まれた外部URLにアクセスする場合は、常に右クリックして新しいタブまたはウィンドウで開いてください。直接クリックしてURLを開こうとすると、コース ウィンドウ／タブが閉じます。

Exercise 44.3: Recovering from Password Failure

1.  As root (not withsudo), change the root password. We will pretend we don’t know what the new password is.

2.  Log out and try to login again as root using the old password. Obviously you will fail.

3.  Boot using the rescue media,  and selectRescuewhen given the option.  Let it mount filesystems and then go to a command line shell.

4.  Go into yourchroot-ed environment (so you have normal access to your systems):
$ chroot /mnt/sysimage
and reset the root password back to its original value.

5.  Exit, remove the rescue media, and reboot, you should be able to login normally now.


課題 44.4: パーティション テーブルの破壊から復旧する

🚩
以下のPDFドキュメントに埋め込まれた外部URLにアクセスする場合は、常に右クリックして新しいタブまたはウィンドウで開いてください。直接クリックしてURLを開こうとすると、コース ウィンドウ／タブが閉じます。


Exercise 44.4: Recovering from Partition Table Corruption

Very Important

This exercise is dangerous and could leave to an unusable system.  Make sure you really understand things before doing it

Please Note

The following instructions for an MBR system. if you have GPT you need to use sgdisk with the --backup-file and --load-backup options as discussed in the partitioning chapter

1.  Login as root and save yourMBR:

$ dd if=/dev/sda of=/root/mbrsave bs=446 count=1
1+0 records in
1+0 records out
446 bytes (446 B) copied, 0.00976759 s, 45.7 kB/s

Be careful: make sure you issue the exact command above and that the file saved has the right length:

$ sudo ls -l /root/mbrsave
-rw-r--r-- 1 root root 446 Nov 12 07:54 mbrsave

2.  Now we are going to obliterate the MBR with:

$ dd if=/dev/zero of=/dev/sda bs=446 count=1
1+0 records in
1+0 records out
446 bytes (446 B) copied, 0.000124091 s, 3.6 MB/s

3.  Reboot the system; it should fail.

4.  Reboot into the rescue environment and restore the MBR:

$ dd if=/mnt/sysimage/root/mbrsave of=/dev/sda bs=446 count=1

5.  Exit from the rescue environment and reboot. The system should boot properly now.


課題 44.5: インストール イメージを利用した復旧

🚩
以下のPDFドキュメントに埋め込まれた外部URLにアクセスする場合は、常に右クリックして新しいタブまたはウィンドウで開いてください。直接クリックしてURLを開こうとすると、コース ウィンドウ／タブが閉じます。

Exercise 44.5: Recovering Using the Install Image

Please Note

This exercise has been specifically written for Red Hat-based systems.  You should be able to easily construct the appropriate substitutions for other distribution families.

1.  Remove the zsh package (if it is installed!):

$ yum remove zsh
または
$ rpm -e zsh

Note we have chosen a package that generally has no dependencies to simplify matters. If you choose something that does, you will have to watch your step in the below so that anything else you remove you reinstall as needed as well.

2.  Boot into the rescue environment.

3.  Re-install (or install) zsh from within the rescue environment. First, mount the install media at /mnt/source:

$ mount /dev/cdrom /mnt/source

Then reinstall the package:

$ rpm -ivh --force --root /mnt/sysimage /mnt/source/Packages/zsh*.rpm

The --force option tells rpm to use the source directory in determining dependency information etc.  Note that if the install image is much older than your system which has had many updates the whole procedure might collapse!

4.  Exit and reboot.

5.  Check thatzshhas been reinstalled:

$ rpm -q zsh
zsh-5.0.2-7.el7.x86_64

6.$ zsh
....
[coop@q7]/tmp/LFS201%


知識チェック

クイズ メッセージ：

「第44章 - システムの救出（レスキュー）」を完遂しました。おめでとうございます。このクイズに答えて、これまでに学んだ概念の理解度をチェックしてください。

クイズ開始

問題 44.1
ライブ イメージを使用してシステムをレスキューすることはできません。True or False?

A. True
B. False

問題 44.2
chrootの目的は、システムをリカバリするために、Linuxでmountやupdate-grubなどのコマンドを実行することです。True or False?

A. True
B. False

問題 44.3
ブートローダーが誤って削除されたシステムの場合、ネットワークのみを使用してレスキュー モードでブートすることはできません。True or False?

A. True
B. False

