2017年9月6日水曜日

RPi_0030 partedとrestore

Raspbian のブートプロセスでrootマウントは、/boot/cmdline.txt と /etc/fstab で、"root=PARTUUID=12345678-02" と"PARTUUID=12345678-02 / ext4" などと、PARTUUIDを一致させて、ルートパーティションをマウントしている。

microSDの区画切りで、fdiskではPARTUUIDはフォーマット後に出現しない。
gdiskでは、Partition Table: gpt で区画するとPARTUUIDが現れるが、RaspberryPiは Partition Table: msdos でないと起動してくれない。

これが、partedだと、Partition Table: msdos にて、PARTUUIDは区画切り後に出現した。

partedで区画を作り、dumpバックアップからのrestoreしてmicroSDでの起動についての説明する。

(1) partedで区画を作成

(1-1)まず、8GBのmicroSDをfdiskで区画切りしてフォーマットでは、以下のようにblkidではPARTUUIDが出現しない。Disk identifier: 0x0になっている。
# fdisk -l /dev/sda

Disk /dev/sda: 7.4 GiB, 7985954816 bytes, 15597568 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000 #ここ値がセットできれば良いのだが

Device     Boot  Start      End  Sectors  Size Id Type
/dev/sda1         2048   104447   102400   50M  c W95 FAT32 (LBA)
/dev/sda2       104448 15597567 15493120  7.4G 83 Linux

# blkid | grep sda
/dev/sda1: SEC_TYPE="msdos" UUID="B7B2-351F" TYPE="vfat"
/dev/sda2: UUID="958651f6-3615-4ac6-afeb-bae165a936d9" TYPE="ext4"

(1-2)そのままmicroSDをpartedで、区画の削除をする。
# parted /dev/sda
GNU Parted 3.2
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p #区画の確認
Model: Mass Storage Device (scsi)
Disk /dev/sda: 7986MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  53.5MB  52.4MB  primary  fat32        lba
 2      53.5MB  7986MB  7932MB  primary  ext4

(parted) rm 2 #区画2削除
Warning: Partition /dev/sda2 is being used. Are you sure you want to continue?
Yes/No? Yes
Error: Partition(s) 2 on /dev/sda have been written, but we have been unable to
inform the kernel of the change, probably because it/they are in use.  As a
result, the old partition(s) will remain in use.  You should reboot now before
making further changes.
Ignore/Cancel? I
(parted) rm 1 #区画1削除
Warning: Partition /dev/sda1 is being used. Are you sure you want to continue?
Yes/No? Y
Error: Partition(s) 1, 2 on /dev/sda have been written, but we have been unable
to inform the kernel of the change, probably because it/they are in use.  As a
result, the old partition(s) will remain in use.  You should reboot now before
making further changes.
Ignore/Cancel? I
(parted) p #区画削除されたことの確認
Model: Mass Storage Device (scsi)
Disk /dev/sda: 7986MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start  End  Size  Type  File system  Flags

(parted)

(1-3)次に、mkpartで区画の作成をする。
(parted) mkpart #区画作成コマンド
Partition type?  primary/extended? p
File system type?  [ext2]? fat32 #fat32で区画の作成
Start? 0
End? 50M
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? I
Error: Partition(s) 1, 2 on /dev/sda have been written, but we have been unable
to inform the kernel of the change, probably because it/they are in use.  As a
result, the old partition(s) will remain in use.  You should reboot now before
making further changes.
Ignore/Cancel? I
(parted) mkpart
Partition type?  primary/extended? p
File system type?  [ext2]? ext4 #ext4で区画の作成
Start? 50M
End? 8G
Error: Partition(s) 1, 2 on /dev/sda have been written, but we have been unable
to inform the kernel of the change, probably because it/they are in use.  As a
result, the old partition(s) will remain in use.  You should reboot now before
making further changes.
Ignore/Cancel? I
 # # # インタラクティブ(interactive)な対話モードでなく、
 # (parted) mkpart p fat32 0% 50M
 # (parted) mkpart p ext4 50M 100%
 # とか、引数を指定して、start end は100%のように%も使えるようだ。
(parted) p #区画の確認
Model: Mass Storage Device (scsi)
Disk /dev/sda: 7986MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      512B    50.0MB  50.0MB  primary  fat32        lba
 2      50.3MB  7986MB  7936MB  primary  ext4         lba

(parted) quit #writeしなくてもquitで書き込んでくれるようだ。
Information: You may need to update /etc/fstab.

# 一旦このmicroSD8GBを取外して挿し直すか、再起動で区画の正常認識を確認する。
# partprobe -s
/dev/sda: msdos partitions 1 2
/dev/mmcblk0: msdos partitions 1 2

(1-4)フォーマットする
# fdisk -l /dev/sda

Disk /dev/sda: 7.4 GiB, 7985954816 bytes, 15597568 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x88584BBB  ###これがPARTUUIDのもとになるようだ。

Device     Boot Start      End  Sectors  Size Id Type
/dev/sda1           1    98303    98303   48M  c W95 FAT32 (LBA)
/dev/sda2       98304 15597567 15499264  7.4G 83 Linux

# mkfs.vfat /dev/sda1 #vfatでフォーマット
mkfs.fat 3.0.27 (2014-11-12)
# mkfs.ext4 -j /dev/sda2 #ext4ジャーナル付きでフォーマット
mke2fs 1.43.3 (04-Sep-2016)
Creating filesystem with 1937408 4k blocks and 484800 inodes
Filesystem UUID: 1de1421d-55c1-4fd1-94d4-e5b1d266dAAA
Superblock backups stored on blocks:
 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

# blkid | grep sda #こんどは以下のようにblkidでPARTUUIDが出現している。
/dev/sda1: SEC_TYPE="msdos" UUID="1A96-82FB" TYPE="vfat" PARTUUID="88584BBB-01"
/dev/sda2: UUID="1de1421d-55c1-4fd1-94d4-e5b1d266dAAA" TYPE="ext4" PARTUUID="88584BBB-02"
#

(2) dumpバックアップからのrestore

restore対象のmountポイントにcdしてrestoreする。
# mount /dev/sdb1 /dsk/sdb1 #/boot区画マウント
# mount /dev/sdb2 /dsk/sdb2 #ルート区画マウント
# cd /dsk/sdb1
# rsync -a /dsk/sda4/Backup/rapia/RootBz/boot/. . #/boot領域のコピー
# cd ../sdb2
# ls
lost+found
 #以下でrestoreの実行
# time bzip2 -dc /dsk/sda4/Backup/rapia/RootBz/b_mmcblk0_rapia_2017_0902/mmcblk0p2_2017_0902.dump.bz2| restore rf -

restore: ./lost+found: File exists

bzip2 -dc   953.43s user 33.54s system 85% cpu 19:19.90 total
restore rf -  15.04s user 67.21s system 7% cpu 19:21.66 total
#

1.5GBのリストアに953秒の約16分かかった。
結構まつので、進捗ぐあいの確認には、
while [ true ]; do echo -n "`date "+%H:%M:%S"` "; df -H |grep /dev/sdb2|awk '{print $1,$2,$3,$4}'; sleep 30; done
...とかで、
22:31:44 /dev/sdb2 7.8G 946M 6.4G
22:32:14 /dev/sdb2 7.8G 985M 6.4G
22:32:44 /dev/sdb2 7.8G 1.1G 6.4G
...などと見張る。

(3) cmdline.txtとfstabの修正

PARTUUIDの部分をblkidのものと、それぞれ一致させる。
# cat /dsk/sdb1/cmdline.txt
dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=PARTUUID=88584BBB-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
# cat /dsk/sdb2/etc/fstab
proc            /proc           proc    defaults          0       0
PARTUUID=88584BBB-01  /boot           vfat    defaults          0       2
PARTUUID=88584BBB-02  /               ext4    defaults,noatime  0       1
# a swapfile is not a swap partition, no line here
#   use  dphys-swapfile swap[on|off]  for that

◎ /dsk/sdb1と/dsk/sdb2をアンマウントして再起動する。
◎ restore後のmicroSD8GBの起動では、以下のようなPARTUUIDの状態になっている。
# ls -l /dev/disk/by-partuuid
total 0
lrwxrwxrwx 1 root root 15 Sep  2 14:17 88584BBB-01 -> ../../mmcblk0p1
lrwxrwxrwx 1 root root 15 Sep  2 14:17 88584BBB-02 -> ../../mmcblk0p2
# blkid
/dev/mmcblk0p1: SEC_TYPE="msdos" UUID="1A96-82FB" TYPE="vfat" PARTUUID="88584BBB-01"
/dev/mmcblk0p2: UUID="1de1421d-55c1-4fd1-94d4-e5b1d266dAAA" TYPE="ext4" PARTUUID="88584BBB-02"
/dev/mmcblk0: PTUUID="88584BBB" PTTYPE="dos"
# grep PARTUUID /etc/fstab
PARTUUID=88584BBB-01  /boot           vfat    defaults          0       2
PARTUUID=88584BBB-02  /               ext4    defaults,noatime  0       1
# grep PARTUUID /boot/cmdline.txt
dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=PARTUUID=88584BBB-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
#