Technology Engineering

178inaba の技術ブログ

Raspberry Piでいろいろ。【その2 -> Swap領域設定、パスワード変更、パッケージ更新】

inabaです。
昨日に引き続き、Raspberry Piの設定をしていきます。

やること

  • SSH接続 OK
  • 起動ドライブをHDDにする OK
  • 無線でつなぐ OK
  • Swap領域設定 New
  • パスワード変更
  • パッケージ更新
  • GitLabインストール

Swap領域設定

GitLabのRaspberry Piへのインストールについてのページに

We recommend adding at least 1GB of swap

とあります。
「1GBはスワップ領域作ったほうがいいよ。」という感じでしょうか。

about.gitlab.com

また、Requirementsには

1GB RAM + 3GB of swap is the absolute minimum but we strongly advise against this amount of memory. See the unicorn worker section below for more advice.

とあります。
「メモリが1GBなら3GBはスワップ領域あったほうがいいよ。」という感じでしょうか。

docs.gitlab.com

昨日起動ディスクにしたHDDは1TBあるので容量の心配はありません。
なので、どーんと16GB割り当ててみることにします。

Swap領域の割り当てにはパーティションを割り当てる方法とファイルを割り当てる方法があるが、調べたところパフォーマンスに差はなく、ディスクフォーマットをしなくてよいのでファイルを使うことにする。
ちなみにRaspberry PiはもともとファイルのSwap領域がある。

$ swapon -s
Filename                Type        Size    Used    Priority
/var/swap                               file        102396  0   -1

単位はkBだそうなので、だいたい100MBくらいだろうか。

それでは実際に設定していく。

# Swap領域管理サービスを停止。
$ service dphys-swapfile stop

# Swap領域設定ファイルを編集。
$ sudo nano /etc/dphys-swapfile
$ cat /etc/dphys-swapfile
...
CONF_SWAPSIZE=16384
...
# ↑元々は「CONF_SWAPSIZE=1024」

# Swap領域管理サービスを開始。
# (ちょい時間かかる。)
$ sudo service dphys-swapfile start

# 確認
$ swapon -s
Filename                Type        Size    Used    Priority
/var/swap                               file        2097148 0   -1

あれ?
2097148kbという事だから2GBくらいにしかなってない。

調べた。

a244.hateblo.jp

CONF_MAXSWAP=2048という行のコメントアウト外して、倍の4096にすると良いらしい。

上の行のコメントは

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

Google翻訳にかけると

最大限にこの制限にサイズ(計算されたと絶対!)を制限する
   制限なしで空にすることができますが、塗りつぶされたパーティションには注意してください!
   これは(古い?)32ビットカーネルの制限(MBytes)であり、それをオーバーランさせない
   しかし、/varやeven / partitionの埋め込みを防ぐために64ビットでも有効です

重要そうなのは

制限なしで空にすることができますが(can be set to empty for no limit)

ということはCONF_MAXSWAP=というように空を設定すれば制限無しにできるのかな?

再度設定

# Swap領域管理サービスを停止。
$ service dphys-swapfile stop

# Swap領域設定ファイルを編集。
$ cat /etc/dphys-swapfile
...
#CONF_MAXSWAP=2048
...
$ sudo nano /etc/dphys-swapfile
$ cat /etc/dphys-swapfile
...
CONF_MAXSWAP=
...

# Swap領域管理サービスを開始。
# (まぁまぁ時間かかる。)
$ sudo service dphys-swapfile start
Job for dphys-swapfile.service failed. See 'systemctl status dphys-swapfile.service' and 'journalctl -xn' for details.
# 失敗したようだ。

$ systemctl status dphys-swapfile.service
● dphys-swapfile.service - LSB: Autogenerate and use a swap file
   Loaded: loaded (/etc/init.d/dphys-swapfile)
   Active: failed (Result: timeout) since Mon 2016-12-12 16:49:03 UTC; 1min 20s ago # timeoutと出ている。
  Process: 15078 ExecStop=/etc/init.d/dphys-swapfile stop (code=exited, status=0/SUCCESS)
  Process: 15135 ExecStart=/etc/init.d/dphys-swapfile start (code=killed, signal=TERM)

試しにCONF_SWAPSIZEを1024に戻して開始してみる。

# 念のため停止コマンドを実行
$ sudo service dphys-swapfile stop
$ cat /etc/dphys-swapfile 
...
CONF_SWAPSIZE=16384
...
$ sudo nano /etc/dphys-swapfile
$ cat /etc/dphys-swapfile
...
CONF_SWAPSIZE=1024
...
$ sudo service dphys-swapfile start
$

成功した。

今度はCONF_SWAPSIZEとCONF_MAXSWAP両方に16384を設定してみる。

$ sudo service dphys-swapfile stop
$ cat /etc/dphys-swapfile 
...
CONF_SWAPSIZE=1024
...
CONF_MAXSWAP=
...
$ sudo nano /etc/dphys-swapfile
$ cat /etc/dphys-swapfile
...
CONF_SWAPSIZE=16384
...
CONF_MAXSWAP=16384
...
$ sudo service dphys-swapfile start
Job for dphys-swapfile.service failed. See 'systemctl status dphys-swapfile.service' and 'journalctl -xn' for details.
# 失敗した。

$ systemctl status dphys-swapfile.service
● dphys-swapfile.service - LSB: Autogenerate and use a swap file
   Loaded: loaded (/etc/init.d/dphys-swapfile)
   Active: failed (Result: timeout) since Mon 2016-12-12 17:07:04 UTC; 36s ago # 同じくtimeout
  Process: 15307 ExecStop=/etc/init.d/dphys-swapfile stop (code=exited, status=0/SUCCESS)
  Process: 15403 ExecStart=/etc/init.d/dphys-swapfile start (code=killed, signal=TERM)

では、8GBにする。

$ sudo service dphys-swapfile stop
$ cat /etc/dphys-swapfile 
...
CONF_SWAPSIZE=16384
...
CONF_MAXSWAP=16384
...
$ sudo nano /etc/dphys-swapfile 
$ cat /etc/dphys-swapfile 
...
CONF_SWAPSIZE=8192
...
CONF_MAXSWAP=
...
$ sudo service dphys-swapfile start
$

成功した。

結局スワップ領域は8GBに。

パスワード変更

デフォルトユーザpiでログインすると下記のメッセージが表示される。

SSH is enabled and the default password for the 'pi' user has not been changed.
This is a security risk - please login as the 'pi' user and type 'passwd' to set a new password.

ようは「パスワードがデフォルトだとセキュリティリスクがありますよ。」という話らしい。
なので変更する。

$ passwd
Changing password for pi.
(current) UNIX password:  # 現在のパスワード
Enter new UNIX password:  # 新しいパスワード
Retype new UNIX password: # 新しいパスワード確認
passwd: password updated successfully

念のため別コンソールでログインして反映されたか確認するとよい。

パッケージ更新

全てのパッケージを最新にする。

$ sudo apt-get clean && sudo apt-get update && sudo apt-get -y upgrade && sudo apt-get clean
Get:1 http://archive.raspberrypi.org jessie InRelease [13.2 kB]                     
Get:2 http://mirrordirector.raspbian.org jessie InRelease [14.9 kB]                 
Get:3 http://archive.raspberrypi.org jessie/main armhf Packages [128 kB]
Get:4 http://mirrordirector.raspbian.org jessie/main armhf Packages [8,981 kB]
Get:5 http://archive.raspberrypi.org jessie/ui armhf Packages [53.6 kB]        
Ign http://archive.raspberrypi.org jessie/main Translation-en_GB                                                                                                                 
Ign http://archive.raspberrypi.org jessie/main Translation-en                                                                                                                    
Ign http://archive.raspberrypi.org jessie/ui Translation-en_GB                                                                                                                   
Ign http://archive.raspberrypi.org jessie/ui Translation-en                                                                                                                      
Get:6 http://mirrordirector.raspbian.org jessie/contrib armhf Packages [37.5 kB]                                                                                                 
Get:7 http://mirrordirector.raspbian.org jessie/non-free armhf Packages [70.3 kB]                                                                                                
Get:8 http://mirrordirector.raspbian.org jessie/rpi armhf Packages [1,356 B]                                                                                                     
Ign http://mirrordirector.raspbian.org jessie/contrib Translation-en_GB                                                                                                          
Ign http://mirrordirector.raspbian.org jessie/contrib Translation-en
Ign http://mirrordirector.raspbian.org jessie/main Translation-en_GB
Ign http://mirrordirector.raspbian.org jessie/main Translation-en
Ign http://mirrordirector.raspbian.org jessie/non-free Translation-en_GB
Ign http://mirrordirector.raspbian.org jessie/non-free Translation-en
Ign http://mirrordirector.raspbian.org jessie/rpi Translation-en_GB
Ign http://mirrordirector.raspbian.org jessie/rpi Translation-en
Fetched 9,300 kB in 3min 18s (46.8 kB/s)
Reading package lists... Done
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Calculating upgrade... Done
The following packages will be upgraded:
  libdrm2 libicu52 raspberrypi-sys-mods raspi-config
4 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 6,615 kB of archives.
After this operation, 60.4 kB of additional disk space will be used.
Get:1 http://archive.raspberrypi.org/debian/ jessie/main libdrm2 armhf 2.4.71-1+rpi1 [32.4 kB]
Get:2 http://mirrordirector.raspbian.org/raspbian/ jessie/main libicu52 armhf 52.1-8+deb8u4 [6,556 kB]
Get:3 http://archive.raspberrypi.org/debian/ jessie/main raspberrypi-sys-mods all 20161208+1 [8,412 B]
Get:4 http://archive.raspberrypi.org/debian/ jessie/main raspi-config all 20161207 [17.7 kB]
Fetched 6,615 kB in 5s (1,267 kB/s)                                   
Reading changelogs... Done
(Reading database ... 31272 files and directories currently installed.)
Preparing to unpack .../libdrm2_2.4.71-1+rpi1_armhf.deb ...
Unpacking libdrm2:armhf (2.4.71-1+rpi1) over (2.4.65-3~bpo8+1) ...
Preparing to unpack .../libicu52_52.1-8+deb8u4_armhf.deb ...
Unpacking libicu52:armhf (52.1-8+deb8u4) over (52.1-8+deb8u3) ...
Preparing to unpack .../raspberrypi-sys-mods_20161208+1_all.deb ...
Unpacking raspberrypi-sys-mods (20161208+1) over (20161124) ...
Preparing to unpack .../raspi-config_20161207_all.deb ...
Unpacking raspi-config (20161207) over (20161114) ...
Processing triggers for systemd (215-17+deb8u5) ...
Setting up libdrm2:armhf (2.4.71-1+rpi1) ...
Setting up libicu52:armhf (52.1-8+deb8u4) ...
Setting up raspberrypi-sys-mods (20161208+1) ...
Installing new version of config file /etc/profile.d/sshpasswd.sh ...
Setting up raspi-config (20161207) ...
Processing triggers for libc-bin (2.19-18+deb8u6) ...

こんな感じかな。

今日はここまで。