Proxmox VE中的vm新增硬碟檔qcow2, 卻因相同UUID無法掛載

出事了阿伯! pve 某linux vm系統裡面有重要檔案誤刪,於是我把NAS上的snapshot還原(qcow2), 想說掛載到原vm成為另一個硬碟,再提取裡面的檔案使用。 這種土炮做法, 我記錄一下:

  1. snapshot還原成另一個qcow2, 名稱先改成temp.qcow2,避免覆蓋或衝突。
  2. vm新增一顆hdd,想辦法讓這顆新硬碟與原硬碟一模一樣(含partition)。
  3. 關掉vm,刪除步驟2新增的hdd檔案;再將temp.qcow2改名取代刪掉的檔案名稱,然後重開vm。
  4. 如此一來,新檔案就有被誤刪的資料,此時可將誤刪檔案, 複製到原本的硬碟裡面,再關機,移除snapshots檔案即可。

步驟4掛載的同時,出現以下問題
該partition為xfs

wrong fs type, bad option, bad superblock on /dev/vdd1, missing codepage or helper program, or other error.

這問題應該是該還原的檔案, 與原本的檔案有相同UUID才導致無法掛載,

解法如下:

  1. 先確認UUID是否正常。
  2. 再用無UUID的方式掛載,確認是否正常
  3. 若正常,重新產生新UUID。
# xfs_db -c uuid /dev/vdc1
UUID = 56283b3b-c2f3-525e-90db-e9d26def629d

# 用無UUID方式掛載
# mount -t xfs -o nouuid /dev/vdc1 /mnt

# xfs_admin -U generate /dev/vdc1
Clearing log and setting UUID
writing all SBs
new UUID = 1eb81512-3f22-4b79-9a35-f22f29745c60

安裝derp(笨蛋伺服器),讓 tailscale 網路更安全順暢

前一篇文章 Tailscale+Headscale 自架VPN全紀錄 讓 tailscale node 能夠透過 headscale 管控, 互相連線, 但是有些 tailscale node 隱藏在區域網路中, 無法互相p2p(peer to peer)直接連線, 這時候就需要 derp 伺服器提供轉接, derp 伺服器預設有很多, 若需要效率更好的,更無資安疑慮的, 可以自架.

我想也因為 derp 只做資料轉接, 不會偷看內容, 因此才會叫這個名字 DERP(笨蛋) XD.
derp 伺服器有很多, 可以參考tailscale官網 .

此外若derp轉接延遲時間太長, 就需要自架 , 可參考 https://tailscale.com/kb/1118/custom-derp-servers/

本文提供CentOS 7 安裝derp過程

  1. 安裝go 1.20以上
  2. 安裝derper程式
  3. 執行指令
  4. 讓headscale納入derper伺服器
  • 安裝 go 1.20
wget https://golang.google.cn/dl/go1.20.linux-amd64.tar.gz
 
tar -C /usr/local -xzf go1.20.linux-amd64.tar.gz 
 
export GOROOT=/usr/local/go
export GOPATH=/usr/local/gopath
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
export PATH=$PATH:$GOPATH/bin
 
source /etc/profile

#安裝完畢, 執行
 /usr/local/go/bin/go version
  • 安裝 derper
go install tailscale.com/cmd/derper@main
#安裝完畢, 應該會在
/usr/local/gopath/bin/derper
  • 啟動derp

先搞定letscrypt證書, 因為我選擇derp手動設定, 所以文件要對應好.

ln -s /etc/letsencrypt/live/derp.test.com/cert.pem /etc/letsencrypt/live/derp.test.com/derp.test.com.crt

ln -s /etc/letsencrypt/live/derp.test.com/privkey.pem /etc/letsencrypt/live/derp.test.com/derp.test.com.key

執行指令

#若要防止別人使用, 需先把derp server主機也加入tailscale 端點, 才可使用 --verify-clients 這個參數
/usr/local/gopath/bin/derper --hostname=derp.test.com --verify-clients -a=:8445 -http-port=-1 -certdir=/etc/letsencrypt/live/derp.test.com  -certmode=manual -stun 
#這樣就可以輸入 https://derp.test.com:8445 測試是否正常
  • 讓headscale納入derper伺服器
    修改headscale設定, 打開headscale yaml 設定檔案, 找到 derp , 將https:/derp.test.com:8445 加上去, 並且新增derp.yaml檔案, 重開headscale就可以了

新增 derp.yaml

重開 headscale 就可以,進行測試了

# 隨便找一個tailscale端點
tailscale netcheck
# 然後  tailscale ping <另一個端點的ip (100.64....)>
tailscale ping 100.64.0.2

在台灣, 我自己覺得架設derp除非有資安考量, 否則不見得需要.

Tailscale+Headscale 自架VPN全紀錄

Wireguard 與最近大熱門的ChatGPT一樣, 是邪惡的存在, 以往公司網管還能勉強管控員工自架VPN, P2P, 與遠端連線, 雖說辛苦,但員工相關網路行為終究掌握在手中.

現在不得了了, 使用wireguard技術的 Tailscale 已攔不住, 隨隨便便就能翻牆, 穿透內網. 我們網管就像是, 治水用阻擋的方式不再可行 , 該是要全面開放, 採用全面零信任的思維+員工契約進行資通安全的管控.

“新暗網時代” , 用wireguard, 能讓一個小群體, 自成一個暗網, 也可自行打造VPN暗黑王國, 市面上網紅推薦的乾爹乾爸VPN, 未來亦將勢微.

本文著重在翻牆, 與簽入公司內部網路, 若用來做site to site請參考以下 tailscale 建議, 直接用Wireguard 比較好.

Using WireGuard directly offers better performance than using Tailscale. Tailscale does more than WireGuard, so that will always be true. We aim to minimize that gap, and Tailscale generally offers good bandwidth and excellent latency, particularly compared to non-WireGuard VPNs.

環境

  1. Headscale
    headscale是開源軟體, 使用docker自行架設後, 可對安裝tailscale的端點進行權限控管, 也是整個VPN當中, 最主要的控制伺服器(control server).
  2. 公司內部架設 tailscale 端點
    Linux tailscale安裝了N次, 推薦安裝ubuntu.
    讓用戶端進行vpn連線, 可存取內網, 或是當作跳板,進行翻牆
  3. PC 用戶端
    windows 10/11 用戶端.
    安裝tailscale windows client , 透過headscale註冊, 即可串接到公司tailscale端點, 存取公司內網工作.
  4. Android
    搜尋tailscale並且安裝後, 修改server url , 導到自架的headscale server ,登入後, 透過公司架設之tailscale端點, 進行翻牆.
    **目前測試,使用台灣之星, 翻牆方面不是很穩定, 但是連到公司內網穩定**
  5. iphone
    安裝tailscale後, 不可修改server, 因此無法連接自架headscale server, 所以GG.

架設Headscale+UI

  • Headscale伺服器, 須提供http對外網址, 以便讓端點登入
    ex: http://headscale.test.com:8081 或是 https://headscale.test.com
    可用proxy reverse反向代理(但我失敗了, 很像是headscale的bug, 因此https我先pass, 改用 http )
    後來發現是apache httpd 版本太舊(2.4.6),不支援 upgrade=any 語法 , 只要讓proxy server 改成2.4.47以上就好了
  • 下載headscale yaml設定檔案
  • docker安裝(本範例vm為centos 7+docker)
    請務必用0.21版, 0.20有bug不能刪除路由表
    紅色請自行依照環境修改
# 存放headscale設定檔按
mkdir -p /root/headscale/config
mkdir -p /root/headscale/data

# 虛擬網路
docker network create reverseproxy-nw

vi /root/headscale/docker-compose.yml
###############################################################################
version: '3.5'
services:
  headscale:
    image: headscale/headscale:0.21
    container_name: headscale
    volumes:
      - '~/config:/etc/headscale'
      - '~/data:/var/lib/headscale'
    ports:
      - 8081:8080
      - 50443:50443
    command: headscale serve
    restart: unless-stopped
    networks:
      reverseproxy-nw:
  headscale-ui:
    image: ghcr.io/gurucomputing/headscale-ui:latest
    restart: unless-stopped
    container_name: headscale-ui
    ports:
      - 9443:443
    networks:
      reverseproxy-nw:
networks:
  reverseproxy-nw:
    external: true
volumes:
  data:
    driver: local
  config:
    driver: local
###############################################################################
  • 下載headscale設定檔
wget https://github.com/juanfont/headscale/raw/main/config-example.yaml -O /root/headscale/config/config.yaml
# 下載完畢, 修改yaml檔案中以下參數
server_url: https://headscale.test.com
listen_addr: 0.0.0.0:8080
base_domain: test.com

#是否改寫用戶端dns,請自行測試, 依照需求決定
override_local_dns: true

# 提供(expose)給用戶端dns
  nameservers:
    - 1.1.1.1
    - 8.8.4.4
# 公司內部專用網址,可分別參考不同dns
  restricted_nameservers:
     test.com:
       - 192.168.1.1
# 停用magic dns
magic_dns: false

#其他如derp, 有機會再另外寫文章
  • 啟用container
cd /root/headscale
docker-compose up -d
  • 設定apache httpd proxy server (版本務必2.4.47以上)
<VirtualHost *:443>
  ServerName headscale.test.com

  ErrorLog /var/log/httpd/ssl_error_log
  TransferLog /var/log/httpd/ssl_access_log

  SSLEngine On
  SSLCertificateFile /etc/letsencrypt/live/headscale.test.com/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/headscale.test.com/privkey.pem
  SSLCACertificateFile /etc/letsencrypt//live/headscale.test.com/fullchain.pem

  SSLProxyEngine On
  ProxyPreserveHost On
  Header add Access-Control-Allow-Origin "test.com";
  SSLProxyCheckPeerName  off
  ProxyPass /web https://localhost:9443/web
  ProxyPassReverse /web https://localhost:9443/web
  ProxyPass / http://localhost:8081/ upgrade=any
  ProxyPassReverse / http://localhost:8081/ upgrade=any
</VirtualHost>
  • 等個1分鐘, 測試
https://headscale.test.com/windows
  • 下指令產生apikey
 docker exec -it headscale headscale apikey  create
  • 將headscale 伺服器網址, 以及apiky , 輸入到 UI 介面
    https://headscale.test.com/web
    請注意,這網址沒有帳號驗證就能用, 要考慮加上帳號密碼認證, 或是限定ip連線
  • 可以開始到UI介面進行管理了
  • 建立使用者,並且提供authkey, 讓公司架設的tailscale 端點使用
# 建立使用者
# docker exec headscale headscale users create <使用者>
docker exec headscale headscale users create hlmtvpn
# 產生authkey, 讓tailscale端點使用
docker exec headscale  headscale --user hlmtvpn preauthkeys create --reusable --expiration 24h
  • 相關指令
1. 列出 tailscale 端點
docker exec headscale headscale node list

2. 刪除 tailscale 端點
docker exec headscale headscale node delete -i <ID>
docker exec headscale headscale node delete -i 1
3. 列出 tailsacle 路由
docker exec headscale headscale route list
4. 啟用路由功能
docker exec headscale headscale route enable -r <ID>
5. 刪除路由表
docker exec headscale headscale route delete -r <ID> --force

公司架設tailscale 端點

  • 建議安裝ubuntu
    之前一直習慣用CentOS, 或是RockyLinux , 但這次有點踢到鐵板, site to site vpn一直不成功, 改由ubuntu就成功了.
  • 請先決定此端點提供site to site 或是 nat
    1. 若是與外地子公司site to site連線, 參數就要加上–snat-subnet-routes=false , 但無法讓外面裝置當作跳板翻牆.
    2. 若是讓外面設備簽入使用, 不需要反向回連, 就使用 –snat-subnet-routes=true (這是預設值), 若外面裝置要用tiptop gp 5.x 連到公司, 則不能使用此設定.

    結論: 要翻牆, 就要設定成true, 要使用tiptop erp , 或是與子公司互相連線就要設定成 false
  • 決定端點當作翻牆或是site to site後, 複製好上一步驟 headscale 伺服器所產生出來的authkey
curl -fsSL https://pkgs.tailscale.com/stable/ubuntu/jammy.noarmor.gpg | sudo tee /usr/share/keyrings/tailscale-archive-keyring.gpg >/dev/null

curl -fsSL https://pkgs.tailscale.com/stable/ubuntu/jammy.tailscale-keyring.list | sudo tee /etc/apt/sources.list.d/tailscale.list

sudo apt-get update
sudo apt-get install tailscale
  • 啟用/設定tailscale端點
# http://headscale.test.com:8080是 headscale對外的URL
# d72e7351e24bbd21ecc99bbf5004c3e1f7cffa0631f8f5d5 是 authkey

# nat , 翻牆
tailscale up --reset --accept-routes --advertise-exit-node --snat-subnet-routes=true --accept-dns=false --advertise-routes=192.168.1.0/24 --login-server=https://headscale.test.com  --authkey=d72e7351e24bbd21ecc99bbf5004c3e1f7cffa0631f8f5d5 

# site to site , 不翻牆
tailscale up --reset --accept-routes --snat-subnet-routes=false --accept-dns=false --advertise-routes=192.168.1.0/24 --login-server=https://headscale.hlmt.com.tw  --authkey=d72e7351e24bbd21ecc99bbf5004c3e1f7cffa0631f8f5d5 

# 主機為vp, 不需要對外ip, 只要內網ip, 可上網, 要加上forward功能
# https://tailscale.com/kb/1019/subnets/
echo 'net.ipv4.ip_forward = 1' | sudo tee -a /etc/sysctl.conf
echo 'net.ipv6.conf.all.forwarding = 1' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p /etc/sysctl.conf
  • 回到headscale,啟用路由
#列出路由表
docker exec headscale headscale route list
#啟用路由
 docker exec headscale headscale route enable -r <ID>

公司主要路由(router), 須額外設定靜態路由

若需要做到site to site

PC 用戶端安裝tailscale端點(可翻牆)

  • 先決條件: 公司tailscale端點已設定成nat(當作翻牆用)
  • 執行指令登入到headscale
    打開dos, 或是powershell (希望不用做這動作, 不然麻瓜會生氣)
    ** powershell or cmd run as administrator
cd C:\Program Files\Tailscale
tailscale login --login-server https://headscale.test.com --exit-node=<headscale ip 可為headscale server local ip> --exit-node-allow-lan-access=true

此時網頁會出現一串指令, 請將該node key 給管理人員註冊
**這邊要注意的是網址指令裡面的 USERNAME , 是一開始 headscale 新增的使用者帳號. 

# 管理人員到headscale主機註冊
docker exec headscale  headscale nodes register --user hlmtvpn --key nodekey:32a95789aa71c2dc7ccfa43a16cf1cff575e318c0a67117370ec9c847ff76828

若管理人員註冊完成, PC上面的tailscale會自動登入, 代表註冊成功, 此時可以選擇變身成公司tailscale 端點(如下圖 william-standard-pc-i440fx-piix-1996) , 就可以以公司ip的名義上網翻牆, 當然也可也存取公司內網(tiptop erp不行).

Android

  • play商店
    搜尋 tailscale後安裝並且執行, 執行後要連續按右上三個點, 連按四次, 會出現隱藏的Change server選項, 然後輸入 https://headscale.test.com:8081
  • 設定完headscale server 網址, 回到首頁, 點選 Sign in with other , 此時會出現網址顯示指令,內含一串node key , 如同上個步驟, 將auth key給網管人員註冊, 經網管通知註冊完畢, 即可登入使用.
    **這邊要注意的是網址指令裡面的 USERNAME , 指的是一開始我們在 headscale 伺服器中新增的使用者帳號.
  • 啟用可以翻牆的VPN惹, 要選擇exit node為可翻牆的主機
    實際測試,我自己手機(android 13)翻牆會秀逗不是很穩定, 有時候等1,2分鐘, 有時候秒連,有時候就GG

安裝自己的照片管理軟體-piwigo

這陣子研究清末明初古幣, 於是想起乾脆架設一個照片管理服務器(self hosting), 功能也不用太多, 有hash tag, 搜尋功能也不是很需要, 試用了一款最有名的 https://photoprism.app/ 發現功能太多, 還有AI自動辨識功能, 不符合我的需求.

最後終於找到簡單好用的 https://piwigo.org/ piwigo也有docker版本, 真是太棒了.

我架設了錢幣照片管理網站 – 布書 , 有不輸之意, 古代為錢之意.

安裝piwigo非常簡單

  • 先準備好mysql 這個container, 底下範例,名字叫做 my-mariadb
  • 指令如下, 請注意紅色部分需修改
docker volume create piwigo_config
docker volume create piwigo_gallery

docker run -d  --restart always --name=piwigo --link  my-mariadb:mysql -e PUID=1000 -e PGID=1000 -e TZ=Asia/Taipei -p 2342:80 -v piwigo_config:/config -v  piwigo_gallery:/gallery --restart unless-stopped  lscr.io/linuxserver/piwigo:12.3.0
  • 登入 http://ip:2342
    數據庫輸入mysql , 對應到以上指令中的 –link my-mariadb:mysql
    再輸入該資料庫的帳號密碼, 以及資料庫名稱(這部分我是先創建好資料庫, 沒試過未創建是否自己建立)
    加上 httpd 反向代理, 這樣就大功告成了

更改預設頁面,例如改成tags, docker 進入container, 修改設定檔案

cd /gallery/local/config
vi /gallery/local/config/config.inc.php
############################
$conf['random_index_redirect'] = array(
PHPWG_ROOT_PATH.'tags.php' => 'return true;',
);
############################

2022/10/15

piwigo釋出13.0.0 , 我迫不急待進行升級,結果失敗, 原因應該是我使用非官方的docker版本.
最後只能硬著頭皮,
1. 把舊版(12.3.0) piwigo_images 資料表匯出到新版本(13.0.0)去,
2.並且把舊版本/gallery/_data/i 目錄, 以及/gallery/upload 目錄複製到新版本去, 這樣新版本就告訴你有未處裡的照片, 再自己手動重新建立相本吧, 這也是無可奈何的事情, 希望官方能有解決方案.

python起手式,建立python虛擬環境

網管人員學python, 看到虛擬環境,第六感覺得這很重要

  1. 安裝python
    https://www.python.org/downloads/
    ps. windows安裝,記得要把python程式路徑加到PATH的選項打勾
  1. 安裝virtualenv建立虛擬環境程式
windows:
  pip3 install virtualenv
linux:
  sudo pip3 install virtualenv
  or
  sudo apt install python3-virtualenv
  1. 使用virtualenv建立虛擬環境
virtualenv -p <python程式位置>  <虛擬環境名稱>
or 
sudo -p <python程式位置>  <虛擬環境名稱>

#範例
windows :
  virtualenv -p C:\Users\x\AppData\Local\Programs\Python\Python310\python.exe my-env
linux:
  sudo virtualenv -p /usr/bin/python3 my-env
  1. 進入虛擬環境

    Windows:
    .\xxx\Scripts\activate.bat

Linux:
source ./xxx/bin/activate

william@william-Inspiron-3891:~$ source ./my-env/bin/activate
(my-env) william@william-Inspiron-3891:~$ 

升級docker images, 舊版images變成none

最近升級moodle到4.0.1版本, 發現有問題, 想用回舊版, 發現舊版的變成 moodle:<none> , 這樣一來不就不能使用舊版了, 後來發現只要知道舊版images id , 就可以改成想要的tag, 也就能繼續使用了

docker tag <images id> <images name>:x.x
docker tag 43b9473f010d bitnami/moodle:3.10

找出當初docker 容器(container)執行的指令(非compose)

 docker run --rm -v /var/run/docker.sock:/var/run/docker.sock:ro assaflavie/runlike  <容器名稱>
docker run --rm -i -v /var/run/docker.sock:/var/run/docker.sock nexdrew/rekcod <容器名稱>

但是這種方式屬於 reverse 方式, 跟原本會有一些失真,但真的沒留下當初執行的指令, 也只好使用這種方式了

1 2 3 4 ... 13