Rassberry Pi OS イメージ カスタム&縮小 - ぶやかー

OSのコピーをお手軽にやるなら

Desktop環境でRaspberry Pi標準の”SD Card Copier”使う。
なければ入れる

sudo apt-get install piclone

でもやっぱり、最小イメージを作りたい

やること
* イメージ作成
– 必要なソフト入れたり環境設定したりしておく
* イメージ縮小
– Raspberry Pi公式のPerl Scriptでイメージサイズ縮小
* Disk仕様領域の縮小
– 方法その1
* 起動前に不要な領域用のパーティション追加して割り当てておく
– 最小イメージ作る前提でSDにOS焼いた後パーティションを最小サイズにしておく。
が、これはこれで方法その2とあまり手数は変わらない。
– 方法その2
* Linux系にSD挿して現在の使用量確認して出来る限り領域縮小する
– 汎用性が一番高そう。
– 方法その3
* SDまるっとイメージ化してから、Raspberry Pi公式のPerl Scriptでイメージサイズ縮小
– https://www.raspberrypi.org/forums/viewtopic.php?f=91&t=58069
– このスクリプトだとraspberry pi osしか対応してない
– 方法その4
* オンラインのままパーティション縮小してDDで書き出す
– いまいち手順がまとまらないので試行錯誤中

方法2の場合

インストールされているパッケージを確認し、不要なのあったら削除しておく

dpkg-query -W -f='${Installed-Size}\t${Package}\n'  |sort -n -r

イメージ化の前にapt updateしておく。あとインストールしておきたいパッケージは一通りいれておく

sudo apt-update && sudo apt -y upgrade

パッケージのキャッシュ消しておく

sudo apt -y autoremove && sudo apt -y clean

次回起動時にパーティション拡張を設定しておく
/// これうまく動かなそうなので、raspi初期起動時のスクリプト追加で対応

sudo raspi-config --expand-rootfs

せっかくなので.bash_historyとかもきれいにしておく

rm ~/.bash_history ; HISTCONTROL=ignorespace sudo shutdown -h now

シャットダウンしたらSDカードを別のLinux系マシンに挿して作業

マウントされているか確認

pi@lab-berry:~ $ sudo mount | grep sd
cgroup2 on /sys/fs/cgroup/unified type cgroup2 (rw,nosuid,nodev,noexec,relatime,nsdelegate)
gvfsd-fuse on /run/user/1000/gvfs type fuse.gvfsd-fuse (rw,nosuid,nodev,relatime,user_id=1000,group_id=1000)
/dev/sda2 on /media/pi/rootfs type ext4 (rw,nosuid,nodev,relatime,uhelper=udisks2)
/dev/sda1 on /media/pi/boot type vfat (rw,nosuid,nodev,relatime,uid=1000,gid=1000,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,showexec,utf8,flush,errors=remount-ro,uhelper=udisks2)

マウントされていなかったらマウントする

初回起動時のパーティション拡張を入れておく

sudo vi /media/pi/boot/cmdline.txt

quitesplashの間ぐらいに追加

init=/usr/lib/raspi-config/init_resize.sh

/etc/init.d/resize2fs_onceを追加

cat << 'EOF' | sudo tee /media/pi/rootfs/etc/init.d/resize2fs_once
#!/bin/sh
### BEGIN INIT INFO
# Provides:          resize2fs_once
# Required-Start:
# Required-Stop:
# Default-Start: 3
# Default-Stop:
# Short-Description: Resize the root filesystem to fill partition
# Description:
### END INIT INFO
. /lib/lsb/init-functions
case "$1" in
  start)
    log_daemon_msg "Starting resize2fs_once"
    ROOT_DEV=$(findmnt / -o source -n) &&
    resize2fs $ROOT_DEV &&
    update-rc.d resize2fs_once remove &&
    rm /etc/init.d/resize2fs_once &&
    log_end_msg $?
    ;;
  *)
    echo "Usage: $0 start" >&2
    exit 3
    ;;
esac
EOF
sudo chmod 755 /media/pi/rootfs/etc/init.d/resize2fs_once

準備ができたのでunmountする

sudo umount /media/pi/boot /media/pi/rootfs

ディスクのサイズ確認

pi@lab-berry:~ $ sudo fdisk -l /dev/sda
Disk /dev/sda: 14.9 GiB, 15987638272 bytes, 31225856 sectors
Disk model: Storage Device
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: 0x998b0266

Device     Boot  Start      End  Sectors  Size Id Type
/dev/sda1         8192   532479   524288  256M  c W95 FAT32 (LBA)
/dev/sda2       532480 31225855 30693376 14.7G 83 Linux
pi@lab-berry:~ $
pi@lab-berry:~ $ sudo resize2fs -P /dev/sda2
resize2fs 1.44.5 (15-Dec-2018)
Estimated minimum size of the filesystem: 821191

ファイルシステム > パーティションの順番で縮小する

ファイルシステムの必要サイズは821191だけど、念のためのバッファとして100M分(25400)追加した容量846591に縮小する。
/// ぎりぎりで縮小して何度か失敗した・・・

pi@lab-berry:~ $ sudo e2fsck -f /dev/sda2
e2fsck 1.44.5 (15-Dec-2018)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
rootfs: 107059/960992 files (0.3% non-contiguous), 794996/3836672 blocks
pi@lab-berry:~ $ sudo resize2fs -p /dev/sda2 846591
resize2fs 1.44.5 (15-Dec-2018)
Resizing the filesystem on /dev/sda2 to 846591 (4k) blocks.
Begin pass 2 (max = 2)
Relocating blocks             XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 3 (max = 118)
Scanning inode table          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 4 (max = 8335)
Updating inode references     XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/sda2 is now 846591 (4k) blocks long.

ファイルシステムの必要分パーティションを拡張
ファイルシステムの1blockは4kで、パーティションの1blockは512biteなので
ファイルシステムのブロック数 * 8以上のパーティションが必要。
expr 846591 ‘*’ 8 = 6772728これを切り上げて6780000確保する
/// これもぎりぎりで縮小して何度か失敗した・・・・

でsda2のstartが532480なのでこれに確保分の6780000を追加すると
endは7312480になる。

ext4のシグネチャ消すか聞かれるけど、消しちゃダメ、絶対

pi@lab-berry:~ $ sudo fdisk /dev/sda

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


Command (m for help): p
Disk /dev/sda: 14.9 GiB, 15987638272 bytes, 31225856 sectors
Disk model: Storage Device
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: 0x998b0266

Device     Boot  Start      End  Sectors  Size Id Type
/dev/sda1         8192   532479   524288  256M  c W95 FAT32 (LBA)
/dev/sda2       532480 31225855 30693376 14.7G 83 Linux

Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (2048-31225855, default 2048): 532480
Last sector, +/-sectors or +/-size{K,M,G,T,P} (532480-31225855, default 31225855): 7312480

Created a new partition 2 of type 'Linux' and of size 3.2 GiB.
Partition #2 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: N

Command (m for help): p

Disk /dev/sda: 14.9 GiB, 15987638272 bytes, 31225856 sectors
Disk model: Storage Device
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: 0x998b0266

Device     Boot  Start     End Sectors  Size Id Type
/dev/sda1         8192  532479  524288  256M  c W95 FAT32 (LBA)
/dev/sda2       532480 7312480 6780001  3.2G 83 Linux

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

で、これをddで必要分書き出す。

縮小後のブロック位置は7312480

ddはcountで書き出すブロック数+1を指定しなければいけないので
countは7312481とする。
* ブロックサイズはパーティションと同じ512を指定しておけばcountの計算が楽
– このサイズならブロックサイズ1Mとかにしなくてもそれほど時間かからん
* 途中不安にならないようにstatus=progressもつけておく

sudo dd bs=512 if=/dev/sda count=7312481 status=progress of=./raspi64_custom.img

USBを抜く。USBストレージ系はやっぱりunbindしてから取り外したほうが安全。

sudo udisksctl power-off -b /dev/sda

unbindでもできるけど使い勝手が・・・・

pi@lab-berry:~ $ lsusb -t
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/4p, 5000M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/1p, 480M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/4p, 480M
        |__ Port 1: Dev 3, If 0, Class=Mass Storage, Driver=usb-storage, 480M
pi@lab-berry:~ $ echo -n "1-1" | sudo tee /sys/bus/usb/drivers/usb/unbind
1-1pi@lab-berry:~ $
pi@lab-berry:~ $ lsusb -t
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/4p, 5000M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/1p, 480M

この記事を書いた人 Wrote this article

kmatsunuma

TOP