LoginSignup
1
1

More than 1 year has passed since last update.

Raspberry Pi 4B 8GB版にRaspberry Pi OS(32bit)をUSBメモリにインストールしRAMDISKからブート

Last updated at Posted at 2021-11-10

USBメモリからのブートでルートディスクをtmpfsで作成し、そこへOSをコピーして起動させるということをやっています
参考資料はhttps://gist.github.com/avinash-oza/9791c4edd78a03540dc69d6fbf21bd9c
です。

準備

Raspberry Pi 4B 8GB
USBメモリ 16GB以上 アクセスランプ付き
Raspberry Pi Imager が動作するPC、USBメモリにRaspberry Pi OS を環境があればOKです
ラズパイでUSBメモリに書き込んでもかまいません

手順1

10月30日版ではうまくいかなかったので、5月7日版で試してみてください
https://ftp.jaist.ac.jp/pub/raspberrypi/raspios_lite_armhf/images/raspios_lite_armhf-2021-05-28/2021-05-07-raspios-buster-armhf-lite.zip
をダウンロードしてください

一番下の Use custom からダウンロードしたラズパイイメージを選択してください
screen.jpg

CHOOSE STRAGE で USBメモリ を選択して書き込んでください

手順2

USBメモリをラズパイに接続して起動します
CUI 環境からセットアップしていきます
root で作業します

apt update
apt full-upgrade
reboot

apt install xorg lxde lightdm

手順3

tar の static 版を作成します
tar のバージョンを確認します

root@raspberrypi:~# tar --version
tar (GNU tar) 1.30
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

作者: John Gilmore, Jay Fenlason.

1.34のソースコードをダウンロードします
https://ftp.gnu.org/gnu/tar/tar-1.30.tar.gz
tar をstasic link でコンパイルします

root@raspberrypi:~/top# ls
tar-1.34.tar.gz
root@raspberrypi:~/top# tar zxvf tar-1.30.tar.gz
展開したファイル名が表示されます
tar-1.30/tests/sigpipe.at
tar-1.30/configure
tar-1.30/THANKS
root@raspberrypi:~/top#cd tar-1.30/
root@raspberrypi:~/top/tar-1.30# ./configure --prefix=/root/top LDFLAGS=-static
最後にエラーメッセージが表示されてしまいましたので
root@raspberrypi:~/top/tar-1.30# ./configure --prefix=/root/top LDFLAGS=-static FORCE_UNSAFE_CONFIGURE=1
を実行します
root@raspberrypi:~/top/tar-1.30# make
root@raspberrypi:~/top/tar-1.30# cd src
root@raspberrypi:~/top/tar-1.30/src# mv tar /mnt
root@raspberrypi:~/top/tar-1.30# cd
root@raspberrypi:~# rm -rf top

手順4

fstabを編集します

root@raspberrypi:~# cd /etc
現状のバックアップを作成します
root@raspberrypi:/etc# cp fstab fstab.org

私のラズパイでは以下のようです

/etc/fstab
proc            /proc           proc    defaults          0       0
PARTUUID=60100126-01  /boot           vfat    defaults          0       2
PARTUUID=60100126-02  /               ext4    defaults,noatime  0       1
# a swapfile is not a swap partition, no line here
#   use  dphys-swapfile swap[on|off]  for that

/ のところを編集します
/tmp を tmpfs に変更します

/etc/fstab

proc                  /proc           proc    defaults          0       0
PARTUUID=60100126-01  /boot           vfat    defaults          0       2
ramdisk                  /               tmpfs   defaults,noatime  0       0
tmpfs                 /tmp tmpfs     defaults,noatime,nodev,mode=1777 0 0
#PARTUUID=60100126-02  /               ext4   defaults,noatime  0       1
# a swapfile is not a swap partition, no line here
#   use  dphys-swapfile swap[on|off]  for that

保存して、再起動します
fstab を編集してもUSBメモリが / でマウントされていると思います

手順5

initramfs-tools を編集します

root@raspberrypi:~# cd /usr/share/initramfs-tools/scripts
現状のバックアップを作成します
root@raspberrypi:/usr/share/initramfs-tools/scripts# cp local local.org

local の local_mount_root() を編集します

local_mount_root()

#       checkfs "${ROOT}" root "${FSTYPE}"

        # Mount root
        # shellcheck disable=SC2086
#        if ! mount ${roflag} ${FSTYPE:+-t "${FSTYPE}"} ${ROOTFLAGS} "${ROOT}" "${rootmnt?}"; then
#                panic "Failed to mount ${ROOT} as root file system."
#        fi
}

"#" でコメントにします
最後にtmpfsを作成し、そこへOSをコピーします

local_mount_root()

#       checkfs "${ROOT}" root "${FSTYPE}"

        # Mount root
        # shellcheck disable=SC2086
#        if ! mount ${roflag} ${FSTYPE:+-t "${FSTYPE}"} ${ROOTFLAGS} "${ROOT}" "${rootmnt?}"; then
#                panic "Failed to mount ${ROOT} as root file system."
#        fi

        mkdir /tmptop
        mount -t ext4 /dev/sda2 /tmptop
        if [ "$?" != 0 ]; then
              panic " USB mount error. "
        fi

        mount -t tmpfs -o "size=6g,defaults,noatime" ramdisk "${rootmnt}"
        if [ "$?" != 0 ]; then
              panic " tmpfs mount error. "
        fi

        /tmptop/mnt/tar zxf /tmptop/mnt/ramdisk.tar.gz -C "${rootmnt}"
        if [ "$?" != 0 ]; then
              panic " tar extract error. "
        fi
        umount /tmptop
        echo "ramdisk  / tmpfs  defaults,noatime  0 0" > "${rootmnt}"/etc/fstab
}

手順6

/boot/config.txt の最後に initramfs initrd-ramdisk を追加します
mkinitramfs -o /boot/initrd-ramdisk を実行します
/mnt/tar zcvf /mnt/ramdisk.tar.gz --one-file-system --exclude /mnt/ramdisk.tar.gz --exclude /usr/share/man --exclude /usr/share/doc --exclude /usr/lib/modules/5.10.63+ --exclude /usr/lib/modules/5.10.63-v7+ --exclude /usr/lib/modules/5.10.63-v8+ --exclude /usr/lib/debug /
を実行します

ブート

再起動して、RAMDISK ブートします
起動時間は5分〜10分かかります

USBメモリからの通常起動に戻すときは /boot/config.txt の最後の行を #initramfs initrd-ramdisk に修正して再起動してください

USBメモリを取り外して Diskless で使う場合はパネル上にイジェクトのアイコンが表示されていないか確認してください
piのユーザで df を実行してもUSBメモリのファイルシステムは表示されませんが root では表示されますので注意が必要です


pi@raspberrypi:~ $ df
ファイルシス   1K-ブロック    使用  使用可 使用% マウント位置
udev               3871952       0 3871952    0% /dev
tmpfs               808728   38064  770664    5% /run
ramdisk            6291456 2504784 3786672   40% /
tmpfs              4043636    8512 4035124    1% /dev/shm
tmpfs                 5120       4    5116    1% /run/lock
tmpfs              4043636       0 4043636    0% /sys/fs/cgroup
tmpfs               808724       8  808716    1% /run/user/1000
pi@raspberrypi:~ $ sudo -i
root@raspberrypi:~# df
ファイルシス   1K-ブロック    使用    使用可 使用% マウント位置
udev               3871952       0   3871952    0% /dev
tmpfs               808728   38064    770664    5% /run
ramdisk            6291456 2504784   3786672   40% /
tmpfs              4043636    8512   4035124    1% /dev/shm
tmpfs                 5120       4      5116    1% /run/lock
tmpfs              4043636       0   4043636    0% /sys/fs/cgroup
tmpfs               808724       8    808716    1% /run/user/1000
/dev/sda2        115092472 3552416 106830288    4% /media/pi/rootfs
/dev/sda1           258095   62883    195213   25% /media/pi/boot
root@raspberrypi:~# 

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1