LoginSignup
0
1

More than 3 years have passed since last update.

Raspberry PiのRaspbianをSSDに移行し、Swap領域も拡大しました。

Posted at

はじめに

全然、目新しい情報ではありませんが、メモとして残しておきます。
SDカードは書換回数に制限があり、ラズパイで使う場合に、データベースやログ、Swap等頻繁に書換が発生するため、信頼性に限界があります。 そこで、Raspberry Piの rootを SSDに移行して、安定して使えるようにします。今回の移行では、/bootパーティションは、SDカードに残したままとしました(/bootのファイルは、基本的に書換は発生しないので)。

参考にさせて頂いた記事は、 Raspberry Pi 3B+ SSD起動ディスク化 MicroSD必要と不要 両方試してみたです。ありがとうございます。

SSDの準備

SSDを購入してから、最初にWindowsに接続して、パーティションを作ったり、読み書きを確かめたのが後になって、少し厄介な事になりましたが、結局、再度Windowsの 「ディスクの管理」で、完全にパーティションを無くしてから再度、Raspberryでの設定を進めたらすんなり進みましたので、手順を残しておきます。

SSDの初期化

まずは、SSDにLinux用のパーティションを作る必要があります。

$ sudo fdisk /dev/sda

dでパーティション削除、nで新規作成、pで状態表示、最後にwで書き込みです。
前述のようにWindows側で全部パーティションを消して置けば、nの作成から始められます。(中途半端に残っていた時に、ここで消しても次のmkfsでエラーが出ました)
パーティションは、linux用に一つあればいいので、結果として /dev/sda1のみになります。(下記情報は、全部終わったあとの状態なので、ご容赦)

$ 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: 55.9 GiB, 60022480896 bytes, 117231408 sectors
Disk model: 2105
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: 71BFAD4F-E232-4647-B7DD-7099B87CB9CA

Device     Start       End   Sectors  Size Type
/dev/sda1   2048 117231374 117229327 55.9G Linux filesystem

フォーマット

/dev/sda1のパーティションを ext4でフォーマットします。

$ sudo mkfs.ext4 /dev/sda1

SDカードのLinuxパーティションと、SSDをマウント

$ sudo mkdir /tmp/usd # 作業用
$ sudo mount /dev/mmcblk0p7 /tmp/usd #SDカードのLinuxパーティションをmount
$ sudo mkdir /tmp/ssd #作業用
$ sudo mount /dev/sda1 /tmp/ssd #SSDを mount

tarをつかってSDカードの中味を全部コピー

これは便利ですね。

$ (cd /tmp/usd; sudo tar --backup -c *)|sudo tar -C /tmp/ssd -xv

最終的な各種設定

/boot/cmdline.txt
console=serial0,115200 console=tty1 root=/dev/sda1 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles
$ lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0 55.9G  0 disk
mqsda1        8:1    0 55.9G  0 part /mnt/ssd
mmcblk0     179:0    0 14.9G  0 disk
tqmmcblk0p1 179:1    0  256M  0 part /boot
mqmmcblk0p2 179:2    0 14.6G  0 part
$

もともとのSDカード上のLinuxシステムは、/dev/mmcblk0p2に入っていました。この中味を全部、SSDにtarでコピーしました。

$ sudo fdisk -l
・・・略・・・
Device         Boot  Start      End  Sectors  Size Id Type
/dev/mmcblk0p1        8192   532479   524288  256M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      532480 31116287 30583808 14.6G 83 Linux

Disk /dev/sda: 55.9 GiB, 60022480896 bytes, 117231408 sectors
Disk model: 2105
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: 71BFAD4F-E232-4647-B7DD-7099B87CB9CA

Device     Start       End   Sectors  Size Type
/dev/sda1   2048 117231374 117229327 55.9G Linux filesystem
$

全部終わった後の、確認した際の/etc/fstabファイルです。実際に書き込んだ時には、SSDのprocのところは、/dev/sda1と書いたつもりだったのですが、リブート後に、UUIDに変わったような気がします。このあたりは、良く分らない。

/etc/fstab
$ cat /etc/fstab
proc            /proc           proc    defaults          0       0
PARTUUID=d9b3f436-01  /boot           vfat    defaults          0       2
PARTUUID=d9b3f436-02  /               ext4    defaults,noatime  0       1
UUID=6766df05-a8f7-4839-af55-2a71d0334740 /mnt/ssd ext4 defaults 0 2
# a swapfile is not a swap partition, no line here
#   use  dphys-swapfile swap[on|off]  for that
$

Swap領域を拡大する

これまで、SDカードでブートし、Linux(Raspbian)も全てSDカードに入っていた状態では、Swap領域も狭く、SSDへの移行前は一時的にSwap usageが 90%以上になっていることもありました。SWAP領域のすすめという記事を参考に、追加したSSDの領域を使い、Swap領域を拡大してみます。

現在のSwap領域を確認

現在のSwap領域は、freeコマンドで確認できます。

$ free
              total        used        free      shared  buff/cache   available
Mem:         948280      474284       38708       48252      435288      479116
Swap:        102396        5120       97276
$ free -h
              total        used        free      shared  buff/cache   available
Mem:          926Mi       463Mi        36Mi        47Mi       426Mi       467Mi
Swap:          99Mi       5.0Mi        94Mi
$ 

99MiB (メビバイト:こういう表記がある事を知りませんでした)が全体のSwap領域で、5MiBが使用済みのようです。(昨日の夜、REBOOTしたため、きれいになった)

SWAP領域のマウント状況を確認

swaponコマンドで確認します。 /var/swapにマウントされています。

$ swapon -s
Filename                                Type            Size    Used    Priority
/var/swap                               file            102396  5632    -2
$

SWAP領域の設定

/etc/dphys-swapfileで設定します。このCONF_SWAPSIZE=100の行と #CONF_MAXSWAP=2048の行を変更します。CONF_SWAPSIZEを1024とし、CONF_MAXSWAPのコメント外して2048とします。

$ sudo vi /etc/dphys-swapfile
# /etc/dphys-swapfile - user settings for dphys-swapfile package
# author Neil Franklin, last modification 2010.05.05
# copyright ETH Zuerich Physics Departement
#   use under either modified/non-advertising BSD or GPL license

# this file is sourced with . so full normal sh syntax applies

# the default settings are added as commented out CONF_*=* lines


# where we want the swapfile to be, this is the default
#CONF_SWAPFILE=/var/swap

# set size to absolute value, leaving empty (default) then uses computed value
#   you most likely don't want this, unless you have an special disk situation
CONF_SWAPSIZE=100

# set size to computed value, this times RAM size, dynamically adapts,
#   guarantees that there is enough swap without wasting disk space on excess
#CONF_SWAPFACTOR=2

# restrict size (computed and absolute!) to maximally this limit
#   can be set to empty for no limit, but beware of filled partitions!
#   this is/was a (outdated?) 32bit kernel limit (in MBytes), do not overrun it
#   but is also sensible on 64bit to prevent filling /var or even / partition
#CONF_MAXSWAP=2048

リブートして確認

一度、リブートしないと変更が反映されないようなので、sudo rebootを実行した後で、再度、free -h コマンドで確認します。以下のように、1ギガに変更されました。

$ free -h
              total        used        free      shared  buff/cache   available
Mem:          926Mi       422Mi        45Mi       7.0Mi       458Mi       433Mi
Swap:         1.0Gi          0B       1.0Gi
$

Telegrafで取得したRaspberry PiのメトリクスをGrafanaのダッシュボードで、可視化していますが、そこでも確かに 1024MiB になっていました。
image.png

おわりに

raspberry piを、工場のIoTの大事なところに設置する予定ですが、そのためには、色々注意すべき事が他にもありそうです。信頼性とかセキュリティーとか、リブート時の処置とか、接続先の停電復帰とか・・・。 これから、一つずつ整えていきたいと思います。

0
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
0
1