顯示具有 yocto 標籤的文章。 顯示所有文章
顯示具有 yocto 標籤的文章。 顯示所有文章

2021年7月1日 星期四

Find your systems easily on a LAN with mDNS

Multicast DNS, or mDNS, lets systems broadcast queries on a local network to find other resources by name. Fedora users often own multiple Linux systems on a router without sophisticated name services. In that case, mDNS lets you talk to your multiple systems by name — without touching the router in most cases. You also don’t have to keep files like /etc/hosts in sync on all the local systems. This article shows you how to set it up.

mDNS is a zero-configuration networking service that’s been around for quite a while. Fedora ships Avahi, a zero-configuration stack that includes mDNS, as part of Workstation. (mDNS is also part of Bonjour, found on Mac OS.)

This article assumes you have two systems running supported versions of Fedora (27 or 28). Their host names are meant to be castor and pollux.

Installing packages

Make sure the nss-mdns and avahi packages are installed on your system. You might have a different version, which is fine:

$ rpm -q nss-mdns avahi
nss-mdns-0.14.1-1.fc28.x86_64
avahi-0.7-13.fc28.x86_64

Fedora Workstation provides both of these packages by default. If not present, install them:

$ sudo dnf install nss-mdns avahi

Make sure the avahi-daemon.service unit is enabled and running. Again, this is the default on Fedora Workstation.

$ sudo systemctl enable --now avahi-daemon.service

Although optional, you might also want to install the avahi-tools package. This package includes a number of handy utilities for checking how well the zero-configuration services on your system are working. Use this sudo command:

$ sudo dnf install avahi-tools

The /etc/nsswitch.conf file controls which services your system uses to resolve services, and in what order. You should see a line like this in that file:

hosts: files mdns4_minimal [NOTFOUND=return] dns myhostname

Notice the commands mdns4_minimal [NOTFOUND=return]. They tell your system to use the multicast DNS resolver to resolve a hostname to an IP address. Even if that service works, the remaining services are tried if the name doesn’t resolve.

If you don’t see a configuration similar to this, you can edit it (as the root user). However, the nss-mdns package handles this for you. Remove and reinstall that package to fix the file, if you’re uncomfortable editing it yourself.

Follow the steps above for both systems.

Setting host name and testing

Now that you’ve done the common configuration work, set up each host’s name in one of these ways:

  1. If you’re using Fedora Workstation, you can use this procedure.
  2. If not, use hostnamectl to do the honors. Do this for the first box:
    $ hostnamectl set-hostname castor
  3. You can also edit the /etc/avahi/avahi-daemon.conf file, remove the comment on the host-name setting line, and set the name there. By default, though, Avahi uses the system provided host name, so you shouldn’t need this method.

Next, restart the Avahi daemon so it picks up changes:

$ sudo systemctl restart avahi-daemon.service

Then set your other box properly:

$ hostnamectl set-hostname pollux
$ sudo systemctl restart avahi-daemon.service

As long as your network router is not disallowing mDNS traffic, you should now be able to login to castor and ping the other box. You should use the default .local domain name so resolution works correctly:

$ ping pollux.local
PING pollux.local (192.168.0.1) 56(84) bytes of data.
64 bytes from 192.168.0.1 (192.168.0.1): icmp_seq=1 ttl=64 time=3.17 ms
64 bytes from 192.168.0.1 (192.168.0.1): icmp_seq=2 ttl=64 time=1.24 ms
...

The same trick should also work from pollux if you ping castor.local. It’s much more convenient now to access your systems around the network!

Moreover, don’t be surprised if your router advertises services. Modern WiFi and wired routers often provide these services to make life easier for consumers.

This process works for most systems. However, if you run into trouble, use avahi-browse and other tools from the avahi-tools package to see what services are available. 

資料來源: https://fedoramagazine.org/find-systems-easily-lan-mdns/



yocto

IMAGE_INSTALL_APPEND = libnss-mdns avahi-daemon
avahi-daemon -f /etc/avahi/avahi-daemon.conf --no-chroot &

2021年6月21日 星期一

在Linux上使用rclone掛載串接雲端空間 Google Drive 、Onedrive、...

 使用Rclone串接雲端空間

資料來源: https://iservice.nchc.org.tw/download_file.php?f=5G7AkAdcLrsExQugX1yjaY6h1__l9FM_rN4sIIhOhaKvd4nSrAmTh9Ifu5_uLRnsvtXStxkPNcGXxFoLkEIJOw


在Linux上使用rclone掛載Google Drive 和 Onedrive

rclone可以幫助我們在Linux上掛載一些儲存服務,包括Google drive, onedrive, box, AWS S3等等.同時不會佔用硬盤空間
簡直就是小容量vps的福音

1.0 安裝 Rclone

參考rclone Linux 安裝描述

1.1 穩定版安裝

Script installation
To install rclone on Linux/macOS/BSD systems, run:

curl https://rclone.org/install.sh | sudo bash

1.2 Beta安裝

For beta installation, run:

curl https://rclone.org/install.sh | sudo bash -s beta

1.3 源碼安裝

從預編譯二進制文件安裝Linux 獲取並解壓縮
Linux installation from precompiled binary
Fetch and unpack

curl -O https://downloads.rclone.org/rclone-current-linux-amd64.zip
unzip rclone-current-linux-amd64.zip
cd rclone-*-linux-amd64

複製bin文件到/usr/bin/
Copy binary file

sudo cp rclone /usr/bin/
sudo chown root:root /usr/bin/rclone
sudo chmod 755 /usr/bin/rclone

安裝對應的man幫助
Install manpage

sudo mkdir -p /usr/local/share/man/man1
sudo cp rclone.1 /usr/local/share/man/man1/
sudo mandb 

Note:

“ 官方同時給出了docker方式部署,利用docker -v 掛載到OS上。個人感覺意義不大就介紹了

2.0 本地PC 上下載rclone (可選)

爲什麼要在本地win10 上下載rclone ?
若果你的VPS 可以開啓圖形化的可以忽略這個步驟

主要問題是客戶端授權,在運行rclone需要先授權。授權其中會跳轉到瀏覽器,google drive 授權是給出網址,可以直接複製到chrome上進行授權獲取認證,但是onedrive 通常是給出的網址是http://127.0.0.1:53682/auth 這個很操蛋vps沒有圖形化就不無法解決了

所以直接利用本地pc win10上獲取auth就省事多了.

2.1 在本地window下載rclone

https://downloads.rclone.org/rclone-current-windows-amd64.zip

解壓出來後,進入cmd,輸入

rclone authorize "onedrive"

之後會彈出窗口認證,然後複製token 記錄好,後面在linux 上會用到

Paste the following into your remote machine --->
{"access_token":"xxxx"}  #請複製{xx}整個內容(包括花括號)
<---End paste

3.0 初始化配置rclone

回到vps上

執行rclone config 配置google drive

[root@lab-test ~]# rclone config
2019/09/11 10:18:28 NOTICE: Config file "/root/.config/rclone/rclone.conf" not found - using defaults
No remotes found - make a new one
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n  #選擇新建一個配置
name> test #配置名稱
Type of storage to configure.
Enter a string value. Press Enter for the default ("").
Choose a number from below, or type in your own value
 1 / 1Fichier
   \ "fichier"
 2 / Alias for an existing remote
   \ "alias"
 3 / Amazon Drive
   \ "amazon cloud drive"
 4 / Amazon S3 Compliant Storage Provider (AWS, Alibaba, Ceph, Digital Ocean, Dreamhost, IBM COS, Minio, etc)
   \ "s3"
 5 / Backblaze B2
   \ "b2"
 6 / Box
   \ "box"
 7 / Cache a remote
   \ "cache"
 8 / Dropbox
   \ "dropbox"
 9 / Encrypt/Decrypt a remote
   \ "crypt"
10 / FTP Connection
   \ "ftp"
11 / Google Cloud Storage (this is not Google Drive)
   \ "google cloud storage"
12 / Google Drive
   \ "drive"
13 / Google Photos
   \ "google photos"
14 / Hubic
   \ "hubic"
15 / JottaCloud
   \ "jottacloud"
16 / Koofr
   \ "koofr"
17 / Local Disk
   \ "local"
18 / Mega
   \ "mega"
19 / Microsoft Azure Blob Storage
   \ "azureblob"
20 / Microsoft OneDrive
   \ "onedrive"
21 / OpenDrive
   \ "opendrive"
22 / Openstack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
   \ "swift"
23 / Pcloud
   \ "pcloud"
24 / Put.io
   \ "putio"
25 / QingCloud Object Storage
   \ "qingstor"
26 / SSH/SFTP Connection
   \ "sftp"
27 / Union merges the contents of several remotes
   \ "union"
28 / Webdav
   \ "webdav"
29 / Yandex Disk
   \ "yandex"
30 / http Connection
   \ "http"
31 / premiumize.me
   \ "premiumizeme"
Storage> 12 #選擇對應的標號 這裏選擇的是Google Drive
** See help for drive backend at: https://rclone.org/drive/ **

Google Application Client Id
Setting your own is recommended.
See https://rclone.org/drive/#making-your-own-client-id for how to create your own.
If you leave this blank, it will use an internal key which is low performance.
Enter a string value. Press Enter for the default ("").
client_id> #一般留空
Google Application Client Secret
Setting your own is recommended.
Enter a string value. Press Enter for the default ("").
client_secret> #一般留空
Scope that rclone should use when requesting access from drive.
Enter a string value. Press Enter for the default ("").
Choose a number from below, or type in your own value
 1 / Full access all files, excluding Application Data Folder.
   \ "drive"
 2 / Read-only access to file metadata and file contents.
   \ "drive.readonly"
   / Access to files created by rclone only.
 3 | These are visible in the drive website.
   | File authorization is revoked when the user deauthorizes the app.
   \ "drive.file"
   / Allows read and write access to the Application Data folder.
 4 | This is not visible in the drive website.
   \ "drive.appfolder"
   / Allows read-only access to file metadata but
 5 | does not allow any access to read or download file content.
   \ "drive.metadata.readonly"
scope> 1 #選擇完全訪問所有文件
ID of the root folder
Leave blank normally.
Fill in to access "Computers" folders. (see docs).
Enter a string value. Press Enter for the default ("").
root_folder_id> #一般留空
Service Account Credentials JSON file path
Leave blank normally.
Needed only if you want use SA instead of interactive login.
Enter a string value. Press Enter for the default ("").
service_account_file> #一般留空
Edit advanced config? (y/n)
y) Yes
n) No
y/n> n #是否修改客戶端配置,沒有特殊就不改了
Remote config
Use auto config?
 * Say Y if not sure
 * Say N if you are working on a remote or headless machine
y) Yes
n) No
y/n> n #是否使用自動配置,這裏選擇n
If your browser doesn't open automatically go to the following link: https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=202264815644.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&state=cdadc76b05c67ecf139a03d81e3c8e2c
Log in and authorize rclone for access 
Enter verification code> #複製上面給出的認證地址到chrome,得到的認證碼粘貼到這裏

Configure this as a team drive?
y) Yes
n) No
y/n> n #不用team drive 選擇n
--------------------
[remote]
client_id = 
client_secret = 
scope = drive
root_folder_id = 
service_account_file =
token = {"access_token":"XXX","token_type":"Bearer","refresh_token":"XXX","expiry":"2014-03-16T13:57:58.955387075Z"}
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y #確認配置

完成後選擇退出

這時候可以使用再次使用rclone config就可以看到剛新建的配置名稱

[root@lab-test ~]# rclone config
Current remotes:

Name                 Type
====                 ====
test                 drive

e) Edit existing remote
n) New remote
d) Delete remote
r) Rename remote
c) Copy remote
s) Set configuration password
q) Quit config
e/n/d/r/c/s/q>

onedrive 是基本一樣的,就是在給出認證鏈接時候給的是http://127.0.0.1:53682/auth。
這時候就直接把在window 上獲得的認證粘貼到Enter verification code> 步驟就行了

4.0 掛載

# 先新建一個目錄,用於掛載
mkdir -p /mnt/google-drive-test
# 掛載命令
rclone mount test: /mnt/google-drive-test --allow-other --allow-non-empty --vfs-cache-mode writes &
# df 查看
df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            483M     0  483M   0% /dev
tmpfs            99M   13M   87M  13% /run
/dev/sda1       9.8G  7.6G  1.8G  82% /
tmpfs           495M     0  495M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           495M     0  495M   0% /sys/fs/cgroup
test:            7EB     0   7EB   0% /mnt/google-drive-test
# 使用dd 測速一下
dd if=/dev/zero of=/mnt/google-drive-test/1  bs=2048 count=1k
記錄了1024+0 的讀入
記錄了1024+0 的寫出
2097152字節(2.1 MB)已複製,1.88323 秒,1.1 MB/秒

1.1MB/s 速度還是不錯的

Note:

" 這裏需要注意的掛載時候命令默認是前臺展示,退出就卸載了.
使用 rclone mount --help 可以看到
When the program ends, either via Ctrl+C or receiving a SIGINT or SIGTERM signal,
the mount is automatically stopped.
所以使用使用還得加個& 掉到後臺

5.0 卸載

卸載很簡單就是直接umount 掉就行了

#卸載
umount /mnt/google-drive-test
#確認是否已卸載
df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            483M     0  483M   0% /dev
tmpfs            99M   13M   87M  13% /run
/dev/sda1       9.8G  7.6G  1.8G  82% /
tmpfs           495M     0  495M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           495M     0  495M   0% /sys/fs/cgroup

6.0 設置啓動腳本

這裏提供一個類似server 啓動的腳本

#!/bin/bash

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
NAME_BIN="rclone"
### BEGIN INIT INFO
# Provides:          rclone
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start rclone at boot time
# Description:       Enable rclone by daemon.
### END INIT INFO

NAME="OneDrive" #rclone name名
REMOTE='Download' #遠程文件夾
LOCAL='/home/OneDrive' #掛載地址

Green_font_prefix="\033[32m" && Red_font_prefix="\033[31m" && Green_background_prefix="\033[42;37m" && Red_background_prefix="\033[41;37m" && Font_color_suffix="\033[0m"
Info="${Green_font_prefix}[信息]${Font_color_suffix}"
Error="${Red_font_prefix}[錯誤]${Font_color_suffix}"
RETVAL=0

check_running(){
        PID="$(ps -C $NAME_BIN -o pid= |head -n1 |grep -o '[0-9]\{1,\}')"
        if [[ ! -z ${PID} ]]; then
                return 0
        else
                return 1
        fi
}
do_start(){
        check_running
        if [[ $? -eq 0 ]]; then
                echo -e "${Info} $NAME_BIN (PID ${PID}) 正在運行..." && exit 0
        else
                fusermount -zuq $LOCAL >/dev/null 2>&1
                mkdir -p $LOCAL
                sudo /usr/bin/rclone mount $NAME:$REMOTE $LOCAL --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000 >/dev/null 2>&1 &
                sleep 2s
                check_running
                if [[ $? -eq 0 ]]; then
                        echo -e "${Info} $NAME_BIN 啓動成功 !"
                else
                        echo -e "${Error} $NAME_BIN 啓動失敗 !"
                fi
        fi
}
do_stop(){
        check_running
        if [[ $? -eq 0 ]]; then
                kill -9 ${PID}
                RETVAL=$?
                if [[ $RETVAL -eq 0 ]]; then
                        echo -e "${Info} $NAME_BIN 停止成功 !"
                else
                        echo -e "${Error} $NAME_BIN 停止失敗 !"
                fi
        else
                echo -e "${Info} $NAME_BIN 未運行"
                RETVAL=1
        fi
        fusermount -zuq $LOCAL >/dev/null 2>&1
}
do_status(){
        check_running
        if [[ $? -eq 0 ]]; then
                echo -e "${Info} $NAME_BIN (PID $(echo ${PID})) 正在運行..."
        else
                echo -e "${Info} $NAME_BIN 未運行 !"
                RETVAL=1
        fi
}
do_restart(){
        do_stop
        do_start
}
case "$1" in
        start|stop|restart|status)
        do_$1
        ;;
        *)
        echo "使用方法: $0 { start | stop | restart | status }"
        RETVAL=1
        ;;
esac
exit $RETVAL

此腳本出自
https://raw.githubusercontent.com/x91270/Centos/master/rcloned

使用時候先改好裏面的變量
NAME=“OneDrive” #rclone name名
REMOTE=‘Download’ #遠程文件夾
LOCAL=’/home/OneDrive’ #掛載地址

但是隻能對應掛載一個盤,當我需要掛載幾個google drive 或者同時掛載onedrive 時候就麻煩極了
盡是些花裏胡哨的玩意
當對應多盤的時候直接編寫一個腳本到/usr/bin下方便多了

cat /usr/bin/rclone-server
rclone mount test: /mnt/google-drive-test --allow-other --allow-non-empty --vfs-cache-mode writes &
rclone mount test1: /mnt/google-drive-test1 --allow-other --allow-non-empty --vfs-cache-mode writes &

添加時候直接往裏面加就完事,不用的時候就直接umount.

總結

如果手裏有大容量的Google Drive或者OneDrive可以使用rclone掛載到VPS上,將不常用的文件丟進去可以很大程度上節約空間。當然要注意的是在保存重要文件的的盤不要翻車哦

資料來源:https://www.twblogs.net/a/5ee8144c29a78f6b011059d4