15
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

「RaspberryPiをROM化する」をシェルスクリプト化した

Last updated at Posted at 2019-08-07

「RaspberryPiをROM化する」にシェルスクリプトをかぶせただけの投稿。
https://qiita.com/felis_silv/items/e69f3490091bee9fe619
簡単にかいつまんで説明すると書き込みをONにしておくと、破損しやすいSDカードを読み取り専用にすることによって、破損しにくくなるスクリプト。

これを /usr/local/sbinにでも置いておいてください。

使い方

最初に rom installを実行してください。
その後、読み取り専用を解除したい場合はrom rw、読み取り専用にしたい場合はrom roです。

スクリプト

ご自由に煮るなり焼くなりどうぞ。

rom
#!/bin/sh

#RaspberryPiをROM化/解除するシェルスクリプト
#options:
# rw: 読み書き可能に
# ro: 読み取り専用に
# status: 現在の読み取り専用かどうかの状態を表示
# install: このスクリプトを使える状態にする。最初に実行
#https://qiita.com/felis_silv/items/e69f3490091bee9fe619

help(){
    echo "Usage: $CMDNAME ro|rw|status|install" 1>&2
}
# Read / Write
toRW(){
    if [ -e /mnt/boot-ro/config.txt ]; then
        sudo mount -o remount,rw /dev/mmcblk0p1
        sudo grep -v initramfs /mnt/boot-ro/config.txt >/tmp/config.txt
        sudo cp /tmp/config.txt /mnt/boot-ro/config.txt
        echo "Write enabled now."
        echo "done. please reboot."
    else
        echo "Already write enabled"
    fi
}
# Readonly
toRO(){
    if [ -e /mnt/boot-ro/config.txt ]; then
        echo "Already write protected"
    else
        sudo grep -v initramfs /boot/config.txt >/tmp/config.txt
        sudo echo initramfs initrd.gz >> /tmp/config.txt
        sudo cp /tmp/config.txt /boot/config.txt
        echo "Write protected now."
        echo "done. please reboot."
    fi
}
#show status
status(){
    if [ -e /mnt/boot-ro/config.txt ]; then
        echo "Write protected now"
    else
        echo "Write enabled now"
    fi
}
install(){
    echo "Install start... Aftre install, will be reboot."
    sudo bash
    apt-get install -y git rsync gawk busybox bindfs
    dphys-swapfile swapoff
    dphys-swapfile uninstall
    update-rc.d dphys-swapfile disable
    systemctl disable dphys-swapfile
    git clone https://github.com/josepsanzcamp/root-ro.git
    rsync -va root-ro/etc/initramfs-tools/* /etc/initramfs-tools/
    mkinitramfs -o /boot/initrd.gz
    echo initramfs initrd.gz >> /boot/config.txt
    echo "install end."
    echo "done. please reboot."
}

case $@ in
    ro)
        toRO
        ;;
    rw)
        toRW
         ;;
    status)
        status
        ;;
    install)
        install
        ;;
    *)
        help
        ;;
esac
15
16
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
15
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?