2017年2月26日日曜日

ハードディスク換装 ubuntu

ハードディスクがなんかエラーを吐きはじめた


[ 1641.504719] ata1.00: exception Emask 0x0 SAct 0x7fff SErr 0x0 action 0x0
[ 1641.504722] ata1.00: irq_stat 0x40000008
[ 1641.504724] ata1.00: failed command: READ FPDMA QUEUED
[ 1641.504727] ata1.00: cmd 60/08:08:30:50:65/00:00:5c:00:00/40 tag 1 ncq 4096 in
                        res 41/40:08:30:50:65/00:00:5c:00:00/00 Emask 0x409 (media error)
[ 1641.504728] ata1.00: status: { DRDY ERR }
[ 1641.504729] ata1.00: error: { UNC }
[ 1641.538356] ata1.00: configured for UDMA/133
[ 1641.538375] sd 0:0:0:0: [sda] tag#1 FAILED Result: hostbyte=DID_OK driverbyte=DRIVER_SENSE
[ 1641.538378] sd 0:0:0:0: [sda] tag#1 Sense Key : Medium Error [current] [descriptor]
[ 1641.538380] sd 0:0:0:0: [sda] tag#1 Add. Sense: Unrecovered read error - auto reallocate failed
[ 1641.538382] sd 0:0:0:0: [sda] tag#1 CDB: Read(10) 28 00 5c 65 50 30 00 00 08 00
[ 1641.538384] blk_update_request: I/O error, dev sda, sector 1550143536
[ 1641.538396] ata1: EH complete
ta

再起動したらinitramfsで止まる。
とりあえずここを参考にfsckでbadblockを避けるようにする。(sda6がroot)

(initramfs)$ fsck -t -y -f -c /dev/sda6
(initramfs)$ reboot

とりあえず起動はするようになったが寿命だろう。
(seagate2TBだ。もっと前に買ったHGSTのはまだ大丈夫そう。新しい奴もHGSTのやつにしたかったが大容量のものは売ってなかった。仕方ないのでWD)
動いているうちに何とかしたい。再インストールしようかとも思ったが、いろいろカスタマイズしているので、もう一度やるのはめんどくさい。
新しいハードディスクをつなげてパーティション分けてコピーする。
/bootと/を分けてないので分けている人は注意。

パーティションを分ける


fdisk /dev/sdb

g create a new empty GPT partition table
DOSパーティションタイプは古いらしい。GPTとかいうやつにしてみる。
n add a new partition
swapとrootを作る
→間違いだった。UEFIでブートできるようにgrub用領域とかESP領域とかを作る必要があったようだ。(参照リンク
t change a partition type
swapはタイプを変更しておく
w write table to disk and exit
書き込む

フォーマット

mkswap /dev/sdb1
mkfs.ext4 /dev/sdb2

適当にマウントしてコピーする。


tar cSpf - . | tar xpf - -C /mnt/dst/

(linuxを2つ入れていたので壊れてない方から実行した。
ない場合はCD起動とかrsyncで--one-file-systemでコピーとかしたらいいはず)

rootのマウント先を変えておく


UUIDをチェック

$ ls -l /dev/disk/by-uuid/
合計 0
lrwxrwxrwx 1 root root 10  2月 26 02:35 6b0b30e8-略 -> ../../sda1
lrwxrwxrwx 1 root root 10  2月 26 03:53 8e7422d4-略 -> ../../sdb1
lrwxrwxrwx 1 root root 10  2月 26 02:35 97ecc96f-略 -> ../../sdb2
lrwxrwxrwx 1 root root 10  2月 26 02:35 d227743f-略 -> ../../sda6

コピー先のfstabを編集してチェックしたUUIDを置き換えたい先のUUIDに書き換える
# / was on /dev/sda6 during installation
UUID=d227743f-略 /               ext4    errors=remount-ro 0       1
# swap was on /dev/sda1 during installation
UUID=6b0b30e8-略 none            swap    sw              0       0
↓みたいに変える
# / was on /dev/sda6 during installation
UUID=97ecc96f-略 /               ext4    errors=remount-ro 0       1
# swap was on /dev/sda1 during installation
UUID=8e7422d4-略 none            swap    sw              0       0

次はgrubをなんとかしたい


新しい方で起動させ、そっちの方からgrubを入れる方針
update-grub2を実行すると自動的に他にインストールされているディストリを探してセットしてくれる

$ sudo update-grub2
Generating grub configuration file ...
Linux イメージを見つけました: /boot/vmlinuz-4.4.0-64-generic
Found initrd image: /boot/initrd.img-4.4.0-64-generic
Linux イメージを見つけました: /boot/vmlinuz-4.4.0-63-generic
Found initrd image: /boot/initrd.img-4.4.0-63-generic
Linux イメージを見つけました: /boot/vmlinuz-4.4.0-53-generic
Found initrd image: /boot/initrd.img-4.4.0-53-generic
Found memtest86+ image: /boot/memtest86+.elf
Found memtest86+ image: /boot/memtest86+.bin
Found Ubuntu 16.04.1 LTS (16.04) on /dev/sda5
Found Ubuntu 16.04.1 LTS (16.04) on /dev/sdb2  <== 新しいやつ

再起動して新しいやつから起動
起動したかと思ったが、root=UUID=のところが新しいUUIDに変わってなかった。
ただのコピーである/boot/grub/grub.cfgから拾ってきているからだろう。
eキーを押して手作業でUUIDのところを変更してctrl-xで起動する。

linux   /boot/vmlinuz-4.4.0-53-generic root=UUID=d227743f-略 ro  quiet splash $vt_handoff
起動したらそっちでgrubをインストールする。

$ sudo grub-install /dev/sdb
grub-install: warning: this GPT partition label contains no BIOS Boot Partition; embedding won't be possible.
grub-install: warning: Embedding is not possible. GRUB can only be installed in this setup by using blocklists. However, blocklists are UNRELIABLE and their use is discouraged..
grub-install: エラー: will not proceed with blocklists.

エラーがでてインストールできなかった。まずい。
以下回復作業を色々試行錯誤。

どうやらGPTにしたのでUEFI用の何かをやらないといけなかったようだ。
UEFIでブートできるようにESP領域とやらを作る必要があるらしい。
また、grub用の領域も必要らしい。

いったん旧OSで起動し、ここを参考にgdiskでswapを削除、grub用の領域とESP領域とを作成。

1: サイズ2M, タイプ EF02(BIOS boot partition)
2: サイズ+512M, タイプ EF00(EFI System)
6: サイズは残りを割り当てる, タイプ Linux swap

パーティションIDがずれるのでsを押してsort
sdb2とかの番号が変わるので、うっかり防止のため再起動
mkswapをやり直し、UUIDが変わるのでfdiskを再編集した。

$ sudo grub-install /dev/sdb 

で、BIOSの設定を変えて再起動し確認する。
一度パーティションIDをsortする前にgrub-installしてその後sortしたら
grubのrescueで止まってしまったが、もう一度grub-installしたらOKだった。
最後、ここを参考にして、壊れているパッケージをチェックした。

$ sudo debsums_init
$ sudo debsums -cs
見つかったパッケージを
$ sudo apt install --reinstall 見つかったパッケージ

一応これでOKのはず。