家で半年くらい動いていたRaspberry PiのMicro SDが死んだ。 原因は誤って起動中にSDカードを抜いてしまったことだろう。確実に。
壊れ方がおもしろくて、カードの内容はちゃんと読める。 起動はするのだが、書き込めない。ファイルの更新とかが一切反映されない。
不幸中の幸い、ある時点までのデータは読めるのでこれを元にバックアップを取り、 新しいMicro SDにコピーすることにした。
参考
Raspberry Piのデータを抜き出して新しいMicro SDにコピーするのだが、 新しいMicro SDの容量が元のMicro SDより少しでも小さいとコピーできない。
コピーするためにMicro SDのデータに作られているパーティションのサイズを変更しなければならない。
パーティションを変更する方法として、VirtualBox VM上のDebianでGPartedを動かすことにした。
VirtualBoxでRaspberry PiのSDを読めるようにしなければならないため、 途中SDから抜き出したデータをVirtualBoxのディスクデータに変換しなければならない。
また、復元先のMicro SDに正常に書き込めるように、復元先と同じサイズかそれ以下のディスクイメージを作る必要がある。 もちろん、こちらもVirtualBox用ディスクデータから変換する。
結果、ざっくりとした手順はこうなる。
ddコマンドで抜き出すVBoxManageを使うddで抜き出した/etc/apt/sources.listを変更するddコマンドを用いるまずはRaspberry Pi のMicro SDがどこに割り当てられているか確認する。
確認方法は、Macではdiskutil listが利用できる。
diskutil list
---
/dev/disk2 (external, physical):
#: TYPE NAME SIZE IDENTIFIER
0: FDisk_partition_scheme *7.9 GB disk2
1: DOS_FAT_32 boot 78.6 MB disk2s1
2: Linux 7.9 GB disk2s3
今回の場合/dev/disk2がRaspberry PiのMicro SDだった。
ディスク番号が判明したところで自動マウントされているbootをアンマウントする。
diskutil unmount /Volumes/boot
Volume boot on disk2s1 unmounted
アンマウントできたので、データを吸い出す。
イメージ読み出し
以下は/dev/disk2がSDドライブの場合の例。以下のコマンドでSDカードの中身をRPi.imgというイメージファイルに読み出します。
$ sudo dd if=/dev/disk2 of=~/RPi.img
でも、Linux君達とは少し違うところもあるようで、
Ritsu:Desktop tetsu$ dd if=/dev/zero of=test bs=1M count=10 dd: bs: illegal numeric valueと怒られてしまいました。(;´Д`)
10MBのデータファイルを作成する場合は、
% dd if=/dev/zero of=test bs=1024000 count=10 10+0 records in 10+0 records out 10240000 bytes transferred in 0.105885 secs (96708667 bytes/sec)
ということなので、こうする。
dd if=/dev/rdisk2 of=~/backup.img bs=1024000
/dev/disk2 ではなく /dev/rdisk2 にしている。ディスク番号の前に r をつけると Raw状態で読み書きができるようになる。 r なしの場合、OS側が安全に読み書きできるように 余分な処理を入れるため遅い。
今回は、16GBのSDカード(実際は16GBのマイクロSDカード)から8GBのSDカードへまるごとコピーしてみました。 もちろんコピー元のSDカードでコピー先のSDカードの容量を超えて使用しているとコピーはできません。 今回は、コピー元のパーティションサイズを縮小してコピー先へコピーします。
コピーするにはGpartedというフリーのパーティション編集ツールをつかいます。 Gpartedを使い慣れている方なら以下の説明は不要かもしれません。
Raspberry PiにつながるUSBのCD/DVDなどがあればRaspberry Pi上でもできると思いますが、もってないのでパソコン上で行いました。 パソコン上で行うには、SDカードのリード/ライトできるUSBアダプタなど2つ(ソース、ディスティネーション)必要です。
検索してみると他にもRaspberry Piのパーティションをgpartedを用いてサイズ変更している人はそこそこいる。
この人はGPartedのLive CDで直接Micro SDからMicro SDへコピーをしているが、
コンピュータにCDドライブがないので直接コピーができない。
USBメモリも予備がないのでUSBブートも諦め、今回はVirtualBox VMでコピーする。
まずはRaspberry PiのイメージをVirtualBoxが読み込める形式にしなければならない。
RAWイメージから仮想ハードディスクイメージを作成する
VBoxManage convertfromraw sdb.img sdb.vdi --format VDI # VDIフォーマット VBoxManage convertfromraw sdb.img sdb.vmdk --format VMDK # VMDKフォーマット
初めて知ったがイメージからvdiへ変換するコマンドがVirtualBoxをインストールする際に自動的に入るようだ。 便利なのでこれを使う。
VBoxManage convertfromraw backup.img RPi.vdi --format VDI
そして移行先もVirtualBox側で読み込めないとマズいことに気づいた。 なので移行先をコンピュータに接続。イメージを作り、vdi形式に変換する。
dd if=/dev/rdisk2 of=~/recover.img bs=1024000
---
VBoxManage convertfromraw recover.img recover.vdi --format VDI
VirtualBox上でGparted LiveCDを動かす手もあるはずだが、 対応しているか不明=起動するのかわからない手段ではなく確実な方法で。
VirtualBox上でDebianを動かし、
Debianにgpartedをインストールして実行するようにした。慣れてるOSなので。
Ubuntuの場合、仮想マシン向けのハードディスクイメージを配布している ので、Debianも仮想HDDイメージがないか探したらOSBoxesが作成、配布しているものを見つけた。
別記事にまとめた。
ログインには
を使う。
まずは管理者rootになる。
su root
この状態でapt-get installすると
Media change: please insert the disc labeled ...
というエラーが出る。
Open and modify the file /etc/apt/sources.list
vi /etc/apt/sources.listYou will see an output like this:
# deb cdrom:[Debian GNU/Linux 7.0.0 _Wheezy_ - Official amd64 CD Binary-1 20130504-14:44]/ wheezy main deb http://ftp.us.debian.org/debian/ wheezy main deb-src http://ftp.us.debian.org/debian/ wheezy main deb http://security.debian.org/ wheezy/updates main deb-src http://security.debian.org/ wheezy/updates main # wheezy-updates, previously known as 'volatile' deb http://ftp.us.debian.org/debian/ wheezy-updates main deb-src http://ftp.us.debian.org/debian/ wheezy-updates main ~This file contains all your package sources. You might find a deb cdrom:[Debian GNU/Linux 7.0.0 Wheezy - Official amd64 CD Binary-1 20130504-14:44]/ wheezy main line indicating a local CDROM as a package source. Comment it out by placing a # symbol at the beginning of the line and save the file.
You should leave it like this:
# # deb cdrom:[Debian GNU/Linux 7.0.0 _Wheezy_ - Official amd64 CD Binary-1 20130504-14:44]/ wheezy main deb http://ftp.us.debian.org/debian/ wheezy main deb-src http://ftp.us.debian.org/debian/ wheezy main deb http://security.debian.org/ wheezy/updates main deb-src http://security.debian.org/ wheezy/updates main # wheezy-updates, previously known as 'volatile' deb http://ftp.us.debian.org/debian/ wheezy-updates main deb-src http://ftp.us.debian.org/debian/ wheezy-updates main ~
sources.listをみると、他にもなんか少ない。
Example sources.list
Below is an example of a
sources.listfor Debian 8/Jessie.deb http://httpredir.debian.org/debian jessie main deb-src http://httpredir.debian.org/debian jessie main deb http://httpredir.debian.org/debian jessie-updates main deb-src http://httpredir.debian.org/debian jessie-updates main deb http://security.debian.org/ jessie/updates main deb-src http://security.debian.org/ jessie/updates mainIf you also want the contrib and non-free components, add contrib non-free after main:
deb http://httpredir.debian.org/debian jessie main contrib non-free deb-src http://httpredir.debian.org/debian jessie main contrib non-free deb http://httpredir.debian.org/debian jessie-updates main contrib non-free deb-src http://httpredir.debian.org/debian jessie-updates main contrib non-free deb http://security.debian.org/ jessie/updates main contrib non-free deb-src http://security.debian.org/ jessie/updates main contrib non-free
最終的にこのようにした。Example contrib non-free と同じ。
# vi /etc/apt/sources.list
deb http://httpredir.debian.org/debian jessie main contrib non-free
deb-src http://httpredir.debian.org/debian jessie main contrib non-free
deb http://httpredir.debian.org/debian jessie-updates main contrib non-free
deb-src http://httpredir.debian.org/debian jessie-updates main contrib non-free
deb http://security.debian.org/ jessie/updates main contrib non-free
deb-src http://security.debian.org/ jessie/updates main contrib non-free
aptのカタログリストをアップデートし、Debianを最新の状態にする。
# apt-get update
# apt-get upgrade
やっと、gpartedをインストールする。
# apt-get install gparted
- 仮想ハードディスクイメージをRAWイメージに変換する
VBoxManage clonehd sdb.vdi sdb.img --format RAW
こうした。
VBoxManage clonehd ~/VritualBox\ VMs/recover.vdi ~/recover.img --format RAW
復元先のMicro SDをカードリーダにセットし、
dd if=recover.img of=/dev/rdisk2 bs=1024000
を実行する。 Raspberry Piに刺し、Raspberry Piが起動できれば復元終了