Published On: 2017-06-22|Last Updated: 2018-06-13|Categories: Debian, Linux|Tags: , , , , , , |

Linux のブートプロセスに明るくないので勉強の一環としてこちらのページを主に参考にして Debian 9 で GPT/md RAID1/LVM/ext4 から GPT/md RAID1/btrfs への変換を試みました。

結論としては chroot が少し面倒なので rescue boot や live environment などで作業した方が楽だと思います。

Add disk and create partitions.

今回 btrfs-convert は使用せずに新しいディスクに環境を移行させます。

起動可能なレイアウトでパーティションを作成します。

例. 8GB のディスク (/dev/sdb) を GPT、512MB の swap と残りにレイアウトします。

root@debian:~# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.29.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): g

Created a new GPT disklabel (GUID: 503BC0B6-FBC6-4C02-B050-5D39108825C9).

Command (m for help): n
Partition number (1-128, default 1):
First sector (34-16777182, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-16777182, default 16777182): +1M

Created a new partition 1 of type 'Linux filesystem' and of size 1 MiB.

Command (m for help): n
Partition number (2-128, default 2):
First sector (4096-16777182, default 4096):
Last sector, +sectors or +size{K,M,G,T,P} (4096-16777182, default 16777182): 15728606

Created a new partition 2 of type 'Linux filesystem' and of size 7.5 GiB.

Command (m for help): n
Partition number (3-128, default 3):
First sector (15728607-16777182, default 15728640):
Last sector, +sectors or +size{K,M,G,T,P} (15728640-16777182, default 16777182):

Created a new partition 3 of type 'Linux filesystem' and of size 512 MiB.

Command (m for help): t
Partition number (1-3, default 3): 1
Hex code (type L to list all codes): 4

Changed type of partition 'Linux filesystem' to 'BIOS boot'.

Command (m for help): t
Partition number (1-3, default 3): 2
Hex code (type L to list all codes): 29

Changed type of partition 'Linux filesystem' to 'Linux RAID'.

Command (m for help): t
Partition number (1-3, default 3): 3
Hex code (type L to list all codes): 29

Changed type of partition 'Linux server data' to 'Linux RAID'.

Command (m for help): p
Disk /dev/sdb: 8 GiB, 8589934592 bytes, 16777216 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: gpt
Disk identifier: 503BC0B6-FBC6-4C02-B050-5D39108825C9

Device        Start      End  Sectors  Size Type
/dev/sdb1      2048     4095     2048    1M BIOS boot
/dev/sdb2      4096 15728606 15724511  7.5G Linux RAID
/dev/sdb3  15728640 16777182  1048543  512M Linux RAID

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.

Configure RAID/LVM.

必要に応じて構成します。

例. RAID1 デバイスを degraded で構成します。

root@debian:~# mdadm --create /dev/md1 --level=1 --raid-devices=2 --run /dev/sdb2 missing
mdadm: Note: this array has metadata at the start and
    may not be suitable as a boot device.  If you plan to
    store '/boot' on this device please ensure that
    your boot-loader understands md/v1.x metadata, or use
    --metadata=0.90
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md1 started.
root@debian:~# mdadm --create /dev/md2 --level=1 --raid-devices=2 --run /dev/sdb3 missing
mdadm: Note: this array has metadata at the start and
    may not be suitable as a boot device.  If you plan to
    store '/boot' on this device please ensure that
    your boot-loader understands md/v1.x metadata, or use
    --metadata=0.90
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md2 started.

Create and configure filesystem.

root@debian:~# mkfs.btrfs /dev/md1
btrfs-progs v4.7.3
See http://btrfs.wiki.kernel.org for more information.

Label:              (null)
UUID:
Node size:          16384
Sector size:        4096
Filesystem size:    7.49GiB
Block group profiles:
  Data:             single            8.00MiB
  Metadata:         DUP             383.69MiB
  System:           DUP               8.00MiB
SSD detected:       no
Incompat features:  extref, skinny-metadata
Number of devices:  1
Devices:
   ID        SIZE  PATH
    1     7.49GiB  /dev/md1

root@debian:~# mkswap /dev/md2
Setting up swapspace version 1, size = 511.7 MiB (536539136 bytes)
no label, UUID=72400a6f-325b-4716-992b-99d0718bcc38
root@debian:~# mount -o compress=zlib /dev/md1 /mnt/
root@debian:~# cd /mnt/
root@debian:/mnt# btrfs subvolume create rootfs
Create subvolume './rootfs'
root@debian:/mnt# btrfs subvolume list /mnt
ID 257 gen 46 top level 5 path rootfs
root@debian:/mnt# btrfs subvolume set-default 257 /mnt
root@debian:/mnt# btrfs subvolume get-default /mnt
ID 257 gen 190 top level 5 path rootfs
root@debian:/mnt# cd rootfs/
root@debian:/mnt/rootfs# for k in home root run srv tmp var; do btrfs subvolume create $k; done
Create subvolume './home'
Create subvolume './root'
Create subvolume './run'
Create subvolume './srv'
Create subvolume './tmp'
Create subvolume './var'
root@debian:/mnt# btrfs subvolume list /mnt
ID 257 gen 46 top level 5 path rootfs
ID 258 gen 15 top level 257 path home
ID 259 gen 34 top level 257 path root
ID 260 gen 45 top level 257 path run
ID 261 gen 17 top level 257 path srv
ID 262 gen 17 top level 257 path tmp
ID 263 gen 32 top level 257 path var
root@debian:/mnt# cd ~

Copy files.

root@debian:~# rsync -avxHAX --progress / /mnt/rootfs
...

Edit /etc/fstab, /etc/mdadm/mdadm.conf.

root@debian:~# blkid /dev/md[12]
/dev/md1: UUID="a1d65ea6-ca59-44c5-8dd3-be109a1777fa" UUID_SUB="274993d2-ca46-4403-b8eb-8d87ff9d264f" TYPE="btrfs"
/dev/md2: UUID="9a622711-e2bc-4fe8-9497-5fd2045c2420" TYPE="swap"
root@debian:~# cat /mnt/rootfs/etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
#/dev/mapper/main-root /               ext4    errors=remount-ro 0       1
UUID=a1d65ea6-ca59-44c5-8dd3-be109a1777fa /               btrfs   subvol=/rootfs,noatime,nodiratime,relatime,compress=zlib 0       1
UUID=72400a6f-325b-4716-992b-99d0718bcc38 none            swap    sw              0       0
/dev/sr0        /media/cdrom0   udf,iso9660 user,noauto     0       0
root@debian:~# mdadm --detail --scan >> /mnt/rootfs/etc/mdadm/mdadm.conf
root@debian:~# cat /mnt/rootfs/etc/mdadm/mdadm.conf
# mdadm.conf
#
# Please refer to mdadm.conf(5) for information about this file.
#

# by default (built-in), scan all partitions (/proc/partitions) and all
# containers for MD superblocks. alternatively, specify devices to scan, using
# wildcards if desired.
#DEVICE partitions containers

# auto-create devices with Debian standard permissions
CREATE owner=root group=disk mode=0660 auto=yes

# automatically tag new arrays as belonging to the local system
HOMEHOST <system>

# instruct the monitoring daemon where to send mail alerts
MAILADDR root

# This configuration was auto-generated on Wed, 22 Mar 2017 09:18:35 +0900 by mkconf
ARRAY /dev/md1 metadata=1.2 name=debian:1 UUID=5683e444:59b7abc5:f99a4f87:6ac99adb
ARRAY /dev/md2 metadata=1.2 name=debian:2 UUID=c7b1afa8:ab906e9f:812087c6:c691efaf

remount, chroot, update-grub, grub-install

マウント状態によっては grub-probe: error: cannot find a device for / (is /dev mounted?) というエラーが発生します。

今回はここで update-initramfs を実行しなくても起動に成功しましたが、おそらく自動的に md デバイスが構成されていると思われます。

root@debian:~# umount /mnt
root@debian:~# mount -o compress=zlib /dev/md1 /mnt
root@debian:~# for fs in dev dev/pts proc run sys; do mount --bind /$fs /mnt/$fs; done
root@debian:~# chroot /mnt
root@debian:~# update-grub
root@debian:~# grub-install /dev/sdb
Installing for i386-pc platform.
grub-install: warning: Couldn't find physical volume `(null)'. Some modules may be missing from core image..
grub-install: warning: Couldn't find physical volume `(null)'. Some modules may be missing from core image..
Installation finished. No error reported.

reboot

アンマウントしてシャットダウンします。

省略してしまいましたが /dev などの umount に失敗したのでそのままシャットダウンしました。しかし正常に終了しなかったため、今回は強制的にシャットダウンしました。

root@debian:~# umount /mnt
root@debian:~# poweroff

シャットダウンしたら分かりやすいように古い環境のディスクの接続を切断します。

update-initramfs

/etc/initramfs-tools/conf.d/resume に古い環境の LVM への参照が残っていたため、起動するときに LVM を構成しようとして時間がかかる状態となってしまいました。起動には必要ないデバイスのため、max retry 後には起動が成功するのですが、新しい環境で再構成します。

chroot したときに適切に swap を設定して? update-initramfs を実行していればこの問題は発生しないと思われます。

root@debian:~# update-initramfs -u -k all
update-initramfs: Generating /boot/initrd.img-4.9.0-3-amd64
W: initramfs-tools configuration sets RESUME=/dev/mapper/main-swap
W: but no matching swap device is available.
I: The initramfs will attempt to resume from /dev/md126
I: (UUID=72400a6f-325b-4716-992b-99d0718bcc38)
I: Set the RESUME variable to override this.
update-initramfs: Generating /boot/initrd.img-3.16.0-4-amd64
W: initramfs-tools configuration sets RESUME=/dev/mapper/main-swap
W: but no matching swap device is available.
I: The initramfs will attempt to resume from /dev/md126
I: (UUID=72400a6f-325b-4716-992b-99d0718bcc38)
I: Set the RESUME variable to override this.

以上です。

BIOS/EFI –> GRUB –> /boot –> initramfs (initrd) –> Linux のような流れなので、GRUB/initramfs から適切にデバイスを構成、マウントできるようにすれば良いことが分かります。

関連