Technology Engineering

178inaba の技術ブログ

Raspberry Piでいろいろ。【その1 -> SSH接続、起動ドライブHDD化、無線LAN設定】

ご無沙汰しております。
inabaです。

Raspberry Pi 3を夏頃に入手してLチカさせてほったらかしにしていました。
もったいないのでRaspberry PiでGitLabサーバでも作りたいと思います。

備忘録としてブログにも記しておこうとおもいます。

やること

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

ssh接続

推奨OSのraspbianは最初からsshdが立ち上がっているという話だったが、自分がインストールしたRASPBIAN JESSIE LITEでは立ち上がっていなかったのでsshdを立ち上げ。
これができないとRaspberry Piにキーボードを繋いで操作しなければならないので一番最初にやる。

  1. sudo raspi-configを実行。
  2. 5 Interfacing Optionsを選択。
  3. P2 SSHを選択。
  4. Yesを選択。
  5. Okを選択。
  6. Finishを選択。

追記(2017/04/03)
raspi-configが更新され、SSHの設定が7 Advanced Optionsから5 Interfacing Optionsに変更されました。

この後、同じネットワークにあるPCからssh pi@raspberrypi.localと打てば入れる。
再起動しても設定は生きているので大丈夫。
自分は面倒なのでsshの設定だけしたら、Raspberry Piのキーボードは外してmacから全部操作することにした。

起動ドライブをHDDに変更

初期状態から変更が少ない状態のままHDDに移行するほうが良いと思うので早めにやる。

理由としては、標準ではSDカードが起動ドライブになるのだが、SDカードは書き換えに弱く、壊れやすいと聞いたため。
ブートローダーはSDカードから変えられないそうなので、SD自体は必須となるのだが、書き込み回数自体は減るだろうと思う。

# ディスク名確認。
$ sudo fdisk -l
Disk /dev/sda: 931.5 GiB, 1000204886016 bytes, 1953525168 sectors

# 買ったばかりでパーティションが無いので作る。
$ sudo fdisk /dev/sda # 対話式コマンド。
Command (m for help): m # コマンド一覧を見る。

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table

Command (m for help): n # 新しくパーティションを作る。
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): # プライマリパーティションなのでデフォルトのままエンター。
Partition number (1-4, default 1): # パーティションは1つだけ作るのでデフォルトのままエンター。
First sector (2048-1953525167, default 2048): # 最大サイズで作るのでデフォルトのままエンター。
Last sector, +sectors or +size{K,M,G,T,P} (2048-1953525167, default 1953525167): # 同じくデフォルトのままエンター。

Created a new partition 1 of type 'Linux' and of size 931.5 GiB. # パーティションが作られた。

Command (m for help): p # パーティションができたことを確認。

Disk /dev/sda: 931.5 GiB, 1000204886016 bytes, 1953525168 sectors
# ...

Device     Boot Start        End    Sectors   Size Id Type
/dev/sda1        2048 1953525167 1953523120 931.5G 83 Linux # パーティション。

Command (m for help): w # ディスクに書き込み。
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

# OSの非同期書き込みを考慮してSync
$ sync

# パーティションのファイルシステムをext4にする。
$ sudo mkfs.ext4 /dev/sda1
mke2fs 1.42.12 (29-Aug-2014)
/dev/sda1 contains a ext4 file system
    last mounted on Sun Dec 11 11:49:33 2016
Proceed anyway? (y,n) y # すでに作成されていると、実行するか確認されるのでy。
Creating filesystem with 244190390 4k blocks and 61054976 inodes
Filesystem UUID: 50945b0a-3c30-4de5-a95d-223bb0f6415a
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
    4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 
    102400000, 214990848

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done     

# SDカードの起動ドライブのデータをHDDにコピー。
$ sudo dd if=/dev/mmcblk0p2 of=/dev/sda1 bs=32M conv=noerror,sync
939+1 records in
940+0 records out
31541166080 bytes (32 GB) copied, 1740.37 s, 18.1 MB/s

# ディスクをチェック。
$ sudo e2fsck -f /dev/sda1
e2fsck 1.42.12 (29-Aug-2014)
/dev/sda1: recovering journal
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
Free blocks count wrong (7376566, counted=7351276). # ブロック数に差異が出ている。
Fix<y>? yes 修正するかと聞かれたのでy。
Free inodes count wrong (1859519, counted=1859633).
Fix<y>? yes # 同じくy。

/dev/sda1: ***** FILE SYSTEM WAS MODIFIED *****
/dev/sda1: 35407/1895040 files (0.2% non-contiguous), 342292/7693568 blocks

# SDをコピーしたためSDのサイズになっているらしい。パーティションを最大化するためリサイズ。
$ sudo resize2fs /dev/sda1
resize2fs 1.42.12 (29-Aug-2014)
Resizing the filesystem on /dev/sda1 to 244190390 (4k) blocks.
The filesystem on /dev/sda1 is now 244190390 (4k) blocks long.

# fstabを書き換えるためマウント。
$ sudo mount -t ext4 -o defaults /dev/sda1 /mnt

# fstabの/dev/mmcblk0p2を/dev/sda1へ書き換え。
$ cat fstab 
proc            /proc           proc    defaults          0       0
/dev/mmcblk0p1  /boot           vfat    defaults          0       2
/dev/mmcblk0p2  /               ext4    defaults,noatime  0       1 # 変更前。
# a swapfile is not a swap partition, no line here
#   use  dphys-swapfile swap[on|off]  for that
$ sudo nano /mnt/etc/fstab # nanoで書き換え。
$ cat fstab 
proc            /proc           proc    defaults          0       0
/dev/mmcblk0p1  /boot           vfat    defaults          0       2
/dev/sda1       /               ext4    defaults,noatime  0       1 # 変更後。
# a swapfile is not a swap partition, no line here
#   use  dphys-swapfile swap[on|off]  for that

# ブートローダーの起動ドライブの設定を書き換え
# root=/dev/mmcblk0p2 -> root=/dev/sda1
$ cat /boot/cmdline.txt
dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
$ sudo nano /boot/cmdline.txt
$ cat /boot/cmdline.txt
dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/sda1 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait

# 再起動
$ sudo reboot

# 再起動後
$ df -kh
Filesystem      Size  Used Avail Use% Mounted on
/dev/root       918G  839M  879G   1% /

これでHDDを使うようになった。
ファイルシステムをCentOS7で使われているxfsにしようかとも思ったが、説明を読むと、メモリを大量に使うような仕様になっているようだったのでいつものext4にした。

Linux HDDフォーマット 参考サイト
Linux CentOS 外付けHDDのフォーマットとマウント

ファイルシステム 参考サイト
qiita.com

Raspberry Pi HDD起動 参考サイト
jyn.jp

無線で繋ぐ

$ sudo sh -c 'wpa_passphrase (SSID) (PASSPHRASE) >> /etc/wpa_supplicant/wpa_supplicant.conf'

上記を実行。
(SSID) (PASSPHRASE)は自分のSSIDとパスに変更して実行してください。

仮に

$ sudo sh -c 'wpa_passphrase foo bar >> /etc/wpa_supplicant/wpa_supplicant.conf'

を実行した場合の/etc/wpa_supplicant/wpa_supplicant.confが下記。

country=GB
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
    ssid="foo"
    #psk="bar"
    psk=baz
}

パスフレーズが平文でコメントアウトされているので、セキュリティ的に気になるのであれば削除しておいたほうがいい。
自分はテキストエディタnanoで削除した。

$ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

無線接続が遅いと感じたときの対処方法もあるようだったが、現状不満が無いので設定しなかった。
今後、不満があれば設定する。
Raspberry Piで無線LANの反応が悪い時の対処法 | ものづくりエクスペリメント

今日はここまで〜。