Unix/Arch Linux

SMB를 automount 하기 (systemd.mount 이용)

ForceCore 2021. 7. 24. 14:55

하는 이유: NAS가 있어서

https://wiki.archlinux.org/title/autofs 가 있기는 한데 systemd가 기왕 있으니 써보자. 역시 최신 시스템이라 그런지 설정하기가 훨씬 더 쉽고 깔끔하다.

cifs-video.mount 작성

/cifs/video 라고 마운트 하려면 파일 이름이 저래야 한다.
/etc/systemd/system/cifs-video.mount 파일을 만든다. 내용은,

neddy가 NAS 호스트명. /etc/hosts 에 IP주소 등록해줬음.

[Unit]
Description=video

[Mount]
What=//neddy/video
Where=/cifs/video
Type=cifs
TimeoutSec=10
Options=_netdev,credentials=/home/forcecore/.ssh/neddy,iocharset=utf8,vers=2.0,uid=1000,gid=1000,file_mode=0644,dir_mode=0755

[Install]
WantedBy=multi-user.target
  • cifs.mount를 참조하고, Options=에 들어가는 것은 /etc/fstab에 들어간다고 생각하고 작성하면 된다. 골치아팠던 부분이, vers=1.0이면 권한 옵션들이 먹히지 않아서 힘들었다. vers=2.0을 사용해야한다.
  • _netdev는 network mount라고 알려주는 부분이다. 자동감지를 하긴 할텐데 https://www.freedesktop.org/software/systemd/man/systemd.mount.html 에 의하면 수동으로 네트워크 마운트라고 확실히 지정할 수 있다는 식으로 나와있다.

id, 비번을 저장하는 곳이 있는데, credentials=... 으로 지정된 파일 안이다.

username={CIFS_USER_ID}
password={CIFS_USER_PASSWORD}

내용물은 이렇게 생긴 파일이다.

cifs-video.automount 작성

[Unit]
Description=video

[Automount]
What=/cifs/video

[Install]
WantedBy=multi-user.target

이러면 autofs로 골치아프게 설정해야했던 시절에 비하면 훨씬 간단하게 작업 끝!
이후 sudo systemctl start cifs-video.automount, sudo systemctl enable cifs-video.automount 하면 작동하기 시작한다.

디버그

#!/bin/bash
sudo systemctl stop cifs-video.automount
ls -l
umount /cifs/video
umount /cifs/video
umount /cifs/video

ls -l
sudo systemctl daemon-reload
sudo systemctl start cifs-video.automount
ls -l

Options= 설정을 바꾸면 그게 제대로 된 옵션인지 아닌지 시행착오가 필요하다. 위 스크립트를 실행하면 마운트가 원하는 옵션으로 되었나 안 되었나, 마운트를 풀고 새 옵션으로 마운트를 해준다.