LoginSignup
0
1

More than 3 years have passed since last update.

Raspi に接続した外付けのメディアを共有ディレクトリにする

Posted at

上記の記事の続きです。
やりたかったことの本命です。

外付けメディアを Raspi に接続し、OSMC で参照可能にします。
また、外付けメディアを samba で共有することで、外部から更新可能にします。

手順

1. 外付けメディアにパーティションを作成する

私の場合は外付けメディアは /dev/sda2 に接続されていました。
mac のバックアップに使っていたメディアを流用したので、空っぽからではありません。
適宜読み替えてください。
まずは fdisk コマンドでメディアにパーティションを切ります。

$ sudo fdisk /dev/sda2

Welcome to fdisk (util-linux 2.29.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device /dev/sda2 already contains a hfsplus signature.
The signature will be removed by a write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x552d6f76.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-3906357343, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-3906357343, default 3906357343): 

Created a new partition 1 of type 'Linux' and of size 1.8 TiB.

Command (m for help): 


Command (m for help): p

Disk /dev/sda2: 1.8 TiB, 2000054960128 bytes, 3906357344 sectors
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: dos
Disk identifier: 0x552d6f76

Device      Boot Start        End    Sectors  Size Id Type
/dev/sda2p1       2048 3906357343 3906355296  1.8T 83 Linux

Command (m for help): 


Command (m for help): w

The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: 無効な引数です

The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).

$ 

2. 外付けメディアを初期化する

mkfs コマンドで外付けメディアを初期化します。

$ mkfs -t ext4 /dev/sda2
mke2fs 1.43.4 (31-Jan-2017)
Found a dos partition table in /dev/sda2
Proceed anyway? (y,N) y
Creating filesystem with 488294668 4k blocks and 122077184 inodes
Filesystem UUID: 6d7bb6e7-74e7-41c7-a028-811e197f75e6
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 (262144 blocks): 
done
Writing superblocks and filesystem accounting information: done       

$ 

3. 外付けメディアを自動的にマウントするよう設定する

Raspi 再起動後に外付けメディアを自動的にマウントするよう設定します。
ここでは /media/usb にマウントするようにしました。

$ sudo cp -p /etc/fstab /etc/fstab_org
$ sudo vi /etc/fstab
$ diff /etc/fstab /etc/fstab_org 
4d3
< /dev/sda2 /media/usb  ext4    defaults,noatime    1   2
$ sudo mkdir /media/usb

4. 再起動する

ここまでの設定が正しく機能しているか、Raspi を再起動して確認します。

$ sudo shutdown -r now

5. 外付けメディアがマウントされているか確認する

手順 3 で設定したとおりマウントされているか確認します。

$ df -h
ファイルシス   サイズ  使用  残り 使用% マウント位置
〜〜〜省略〜〜〜
/dev/sda2        1.8T  6.4G  1.7T    1% /media/usb
〜〜〜省略〜〜〜
$ 

5. samba をインストールする

外付けメディアを共有可能にするために samba をインストールします。
途中、「続行しますか」と尋ねられるので Y と答えます。

$ sudo apt install samba
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています                
状態情報を読み取っています... 完了
以下の追加パッケージがインストールされます:
  libfile-copy-recursive-perl libgdbm3 libperl5.24 perl perl-modules-5.24 python-crypto python-dnspython python-ldb python-samba python-tdb samba-common-bin tdb-tools update-inetd
提案パッケージ:
  perl-doc libterm-readline-gnu-perl | libterm-readline-perl-perl make python-crypto-dbg python-crypto-doc python-gpgme bind9 bind9utils ctdb ldb-tools smbldap-tools winbind ufw heimdal-clients
推奨パッケージ:
  rename attr logrotate samba-dsdb-modules samba-vfs-modules
以下のパッケージが新たにインストールされます:
  libfile-copy-recursive-perl libgdbm3 libperl5.24 perl perl-modules-5.24 python-crypto python-dnspython python-ldb python-samba python-tdb samba samba-common-bin tdb-tools update-inetd
アップグレード: 0 個、新規インストール: 14 個、削除: 0 個、保留: 8 個。
8,902 kB のアーカイブを取得する必要があります。
この操作後に追加で 54.0 MB のディスク容量が消費されます。
続行しますか? [Y/n] 
〜〜〜省略〜〜〜
$ 

6. samba を設定する

マウントした外付けメディアを共有するよう samba を設定します。

$ sudo cp -p /etc/samba/smb.conf /etc/samba/smb.conf_org
$ 
$ sudo vi /etc/samba/smb.conf
$ 
$ diff /etc/samba/smb.conf /etc/samba/smb.conf_org 
25,28d24
< interfaces 192.168.1. 127.0.0. wlan0
< bind interfaces only = yes
< hosts allow = 192.168.1.
< security = user
259,264d254
< [public]
< comment = public
< path = /media/usb
< public = yes
< read only = no
< browsable = yes
$ 

7. samba を起動する

設定した内容が正しいか testparm コマンドで確認し、samba を起動します。
samba も Raspi の再起動後に自動的に起動するように設定してしまうと、楽ですね。

$ testparm 
Load smb config files from /etc/samba/smb.conf
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
WARNING: The "syslog" option is deprecated
Processing section "[homes]"
Processing section "[printers]"
Processing section "[print$]"
Processing section "[public]"
Loaded services file OK.
Server role: ROLE_STANDALONE

Press enter to see a dump of your service definitions

# Global parameters
[global]
    bind interfaces only = Yes
    log file = /var/log/samba/log.%m
    max log size = 1000
    syslog = 0
    panic action = /usr/share/samba/panic-action %d
    usershare allow guests = Yes
    map to guest = Bad User
    obey pam restrictions = Yes
    pam password change = Yes
    passwd chat = *Enter\snew\s*\spassword:* %n\n *Retype\snew\s*\spassword:* %n\n *password\supdated\ssuccessfully* .
    passwd program = /usr/bin/passwd %u
    security = USER
    server role = standalone server
    unix password sync = Yes
    dns proxy = No
    idmap config * : backend = tdb
    hosts allow = 192.168.1.


[homes]
    comment = Home Directories
    browseable = No
    create mask = 0700
    directory mask = 0700
    valid users = %S


[printers]
    comment = All Printers
    path = /var/spool/samba
    browseable = No
    printable = Yes
    create mask = 0700


[print$]
    comment = Printer Drivers
    path = /var/lib/samba/printers


[public]
    comment = public
    path = /media/usb
    guest ok = Yes
    read only = No
$ 
$ sudo systemctl restart smbd nmbd
$ sudo systemctl enable smbd nmbd

以上で設定は完了です。

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