分類
Linux 软件

Fedora 35 與 Nvidia GPU 驅動

TL;NR

如果電腦開啓了 Secure Boot, 又不想頭痛地處理簽名,可以關掉。然後跟隨這個網頁的方法安裝開源的 Nvidia 驅動: Howto/NVIDIA。或者跟隨這個網頁安裝 Nvidia 官方的驅動:Fedora 36/35/34 NVIDIA [515.57 / 510.73.05 / 470.129.06 / 390.151 / 340.108] Drivers Install Guide。這個網頁裏也有介紹如果禁用 Fedora 自帶的 Nouveau 驅動的方法。

The Story

打算嘗試在 Fedora 上編輯視頻,一番搜索後發現 DaVinci Resolve 好像很強大,而且還有 Linux 的免費版本。下載安裝很順利,但是打開後歡迎頁面是黑屏。禁用 Secure Boot 裝了 rpmfusion 推薦的驅動後可以打開了,但是 GPU 設置成 CUDA 模式提示無法渲染圖像,錯誤編碼 999,改成 OpenGL 模式不報錯,但是連 JPG 圖像的預覽都是花的,我以爲是 rpmfusion 的驅動不行(實際不是)。於是去裝官網驅動,禁用了 Nouveau 驅動後安裝成功,但是 DaVinci 還是無法使用,後來發現可能是筆記本自動選擇顯卡 Optimus 有關,可以參考 OptimusHow to Set Nvidia as Primary GPU on Optimus-based Laptops。我並沒有把 Nvidia 設置爲默認,因爲覺得平常板載就夠了。所以後來我使用環境變量的方法成功運行了 DaVinci Resolve

__NV_PRIME_RENDER_OFFLOAD=1 __GLX_VENDOR_LIBRARY_NAME=nvidia /opt/resolve/bin/resolve

最後我發現相機拍攝的 mov 視頻文件導入後還是只有音頻,想要編輯的話要先對視頻進行轉碼。而且導出格式也有蠻多限制,所以就放棄了 DaVinci Resolve。

然後我搜索到 Linux 平臺另一款「強大」的視頻編輯器 Lightworks,到官網下載免費版體驗,發現需要註冊,註冊時發現有 recaptcha 人機驗證,我換了好幾個網絡居然都無法通過。無奈從網上找了個安裝包,安裝後打開軟件發現需要登錄才能使用所以還是要註冊。最後是第二天在手機瀏覽器裏註冊成功了。然後發現導出 1080p 的視頻居然是付費版本才有的功能,2022年了喂~

最後我去看 Fedora 的手冊,上面推薦 Kdenlive,從官網直接下載 AppImage 檔直接運行,視頻和圖片都順利導入,導出也都沒有什麼限制,h264、h265、ProRes 全都 OK!而且也支持視頻穩定和關鍵幀調整。雖然兩個視頻漸變過度的 Mixes 操作很怪異,但是至少能達到想要的效果。時間軸的操作可能由於還不會用,所有也感覺不太順手,但是至少可以正常導入、剪輯、調整顏色、加字幕和出片了。

分類
Linux

Fedora 35 安裝 VirtualBox

沒想到給 Fedora 35 安裝 VirtualBox 會遇到這麼一個坑,所以記錄下。如果你的電腦沒有啓用 EFI Secure Boot 應該不會遇到這個問題。報錯信息如下:

vboxdrv.sh: failed: modprobe vboxdrv failed. Please use 'dmesg' to find out why.

There were problems setting up VirtualBox.  To re-start the set-up process, run
  /sbin/vboxconfig
as root.  If your system is using EFI Secure Boot you may need to sign the
kernel modules (vboxdrv, vboxnetflt, vboxnetadp, vboxpci) before you can load
them. Please see your Linux system's documentation for more information.

如果你不想關閉安全啓動,那麼可以按如下步驟來解決:

1, 安裝 mokutil

sudo dnf update
sudo dnf install mokutil

2, 在新文件夾裏創建 RSA 密鑰

sudo -i
mkdir /root/signed-modules
cd /root/signed-modules
openssl req -new -x509 -newkey rsa:2048 -keyout MOK.priv -outform DER -out MOK.der -nodes -days 36500 -subj "/CN=VirtualBox/"
chmod 600 MOK.priv

3, 準備導入密鑰的密碼,等下重啓電腦後會詢問你設置的密碼。

sudo mokutil --import MOK.der

4, 重啓電腦,電腦會進入一個藍屏,選擇 Enroll MOK --> Continue --> 輸入你剛剛設置的密碼,然後你的電腦會重啓。

5, 進入剛剛的文件夾新建一個腳本。

cd /root/signed-modules
nano sign-virtual-box

腳本內容如下:

#!/bin/bash

for modfile in $(dirname $(modinfo -n vboxdrv))/*.ko; do
  echo "Signing $modfile"
  /usr/src/kernels/$(uname -r)/scripts/sign-file sha256 \
                                /root/signed-modules/MOK.priv \
                                /root/signed-modules/MOK.der "$modfile"
done

如果腳本執行失敗了,可以嘗試運行下面命令,然後根據結果修改上面腳本。

find /usr/src -name signfile

5, 爲腳本增加執行權限並運行。

chmod 700 sign-virtual-box
./sign-virtual-box 

6, 把簽名後的驅動加入內核中就可以了。

modprobe vboxdrv

Ubuntu 用戶可以參考這篇 https://stegard.net/2016/10/virtualbox-secure-boot-ubuntu-fail/。以上解決辦法由 Younes LAB 作答與 Sign virtual box modules (vboxdrv, vboxnetflt, vboxnetadp, vboxpci) Centos 8 - Stack Overflow


從 VirtualBox RPM 源安裝 VirtualBox

由於之前沒有搜到上面的辦法,還以爲是安裝的問題,嘗試了從 VirtualBox RPM 源安裝 VirtualBox,其實是不能解決問題的,但是這裏也記錄一下。

1, 安裝依賴。

sudo dnf -y install @development-tools
sudo dnf -y install kernel-headers kernel-devel dkms elfutils-libelf-devel qt5-qtx11extras

2, 添加VirtualBox RPM 源。

Fedora 36 / Fedora 35:

cat <<EOF | sudo tee /etc/yum.repos.d/virtualbox.repo 
[virtualbox]
name=Fedora $releasever - $basearch - VirtualBox
baseurl=http://download.virtualbox.org/virtualbox/rpm/fedora/35/\$basearch
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://www.virtualbox.org/download/oracle_vbox.asc
EOF

Fedora 34:

cat <<EOF | sudo tee /etc/yum.repos.d/virtualbox.repo 
[virtualbox]
name=Fedora $releasever - $basearch - VirtualBox
baseurl=http://download.virtualbox.org/virtualbox/rpm/fedora/34/\$basearch
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://www.virtualbox.org/download/oracle_vbox.asc
EOF

Fedora 33:

cat <<EOF | sudo tee /etc/yum.repos.d/virtualbox.repo 
[virtualbox]
name=Fedora $releasever - $basearch - VirtualBox
baseurl=http://download.virtualbox.org/virtualbox/rpm/fedora/33/\$basearch
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://www.virtualbox.org/download/oracle_vbox.asc
EOF

3, 導入 VirtualBox GPG 密鑰,確認指紋匹配後按 y。

sudo dnf search virtualbox
Fedora  -  - VirtualBox                                 58  B/s | 181  B     00:03
Fedora  -  - VirtualBox                                906  B/s | 1.7 kB     00:01
Importing GPG key 0x98AB5139:
 Userid     : "Oracle Corporation (VirtualBox archive signing key) <[email protected]>"
 Fingerprint: 7B0F AB3A 13B9 0743 5925 D9C9 5442 2A4B 98AB 5139
 From       : https://www.virtualbox.org/download/oracle_vbox.asc
Is this ok [y/N]: y

4, 安裝 VirtualBox。

sudo dnf install VirtualBox-6.1

5, 把當前用戶添加到 vboxusers 用戶組。

sudo usermod -a -G vboxusers $USER
newgrp vboxusers

然後就可以通過命令 virtualbox 或應用菜單 》System 》Oracle VM VirtualBox來啓動了。但是這個時候還沒有裝 Extension Pack,表現爲虛擬機只支持 USB 1.1,屏幕無法縮放。好在擴展包直接從官網下載就可以,然後直接雙擊就能順利運行安裝。

此安裝教程來自 How To Install VirtualBox 6 on Fedora 36/35/34/33/32 - ComputingForGeeks

分類
Linux

Install Fedora 35 on Dell Inspiron 1525

The Inspiron 1525 is Emanon's first (and only) laptop, which brought almost 13 years ago. When it got laggy and hard to run Adobe software, we brought an Acer desktop. Several years later, we got some video tasks to do, so we gave the Acer desktop to Emanon's sister, who didn't have a desktop. From that on, we started using a Dell desktop in daily life. But we only got one PC, we had to take turns recently. I found the old desktop after a room tidy up and thought maybe it can still run for easy tasks in daily life.

My Inspiron 1525 has a 2 GB Memory, a 2-core CPU, a 120 GB hard disk and a 15.4" screen(which is much bigger than my phone). Fortunately, it meets the minimum hardware of Fedora 35. I use Xfce desktop environment for many years, it's fast and has almost everything I need. So I download Fedora 35 Xfce Desktop and burn it into my old 2 GB flash disk with the official tool Fedora Media Writer.

sudo dnf install mediawriter
mediawriter

Then insert the flash disk into 1525, press power button. When you see the progress bar go to the end, keep hitting F12 then select USB Storage Device. You'll boot into Fedora 35 Xfce in a moment. If you haven't back up your files, you could do it now. If you don't have a wired Internet connection, you should download and copy the two files below for fixing the Wi-Fi later. Run the Install to Hard Drive in the desktop to install Fedora OS. For more details, please refer to Installing Using Anaconda. After that, reboot your laptop and remove the flash disk.

You'll see the Wi-Fi is failed, even after you updated all the software. The good news is there are ways to fix. Download broadcom-wl-6.30.163.46 (backup link), extract it. Run the following commands to get the working driver and copy them to your system.

#sudo yum install b43-fwcutter
sudo b43-fwcutter -w /lib/firmware broadcom-wl-6.30.163.46/broadcom-wl-6.30.163.46.wl_apsta.o
sudo sync
sudo /sbin/modprobe -r b43
sudo /sbin/modprobe b43

Now you can browse the web or do some text work on it. If you want some multimedia pleasure, you might need VLC. And before you install VLC, you need to enable RPM Fusion first.

sudo dnf install https://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm

本文更新於 2022/03/21。

分類
Linux 程序

Django簡易搭建上傳文件

這裡使用Django+Gunicorn+Nginx的方式簡單運行一個小型webserver,實現一個簡單的上傳文件到服務器的功能(並不生成下載鏈接)。

啟動虛擬環境,安裝django和gunicorn:

pip install Django==2.0
pip install gunicorn
#進入要放置代碼的目錄並新建項目
django-admin startproject mysite
##或者在當前目錄建立項目
#django-admin startproject mysite .
cd mysite
#新建app
python manage.py startapp polls

先建立一個表格:

#polls/forms.py
from django import forms

class UploadFileForm(forms.Form):
    title = forms.CharField(label='密碼',max_length=20,widget=forms.PasswordInput)
    file = forms.FileField(label='文件',)

修改view:

#polls/views.py
import os
import subprocess
from django.core.files.storage import FileSystemStorage
from django.conf import settings
from django.shortcuts import render
from django.http import HttpResponse
from .forms import UploadFileForm

#handle file example with file
def handle_uploaded_file(f):
    with open('/file/should/be/saved/here/target.odt', 'wb+') as destination:
        for chunk in f.chunks():
            destination.write(chunk)

#another handle file example with filename
def handle_uploaded_file2(filename):
    msg=''
    try:
        targetZipFilePath = os.path.join(settings.BASE_DIR, filename)
        cmd1=subprocess.check_call(["unzip", "-o", targetZipFilePath, "-d", "/home/fred/workspace/"])
        if cmd1==0 :
            cmd2=subprocess.check_call(["cp", "-Rf", "/home/fred/workspace/dist", "/home/fred/"])
            if cmd2==0 :
                msg="deployed successfully"
            else:
                msg="error 2"
        else:
            msg="error 1"
    except:
        msg = 'error 0'
    return msg

def upload_file(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid() and request.POST['title']=='Secret':
#            handle_uploaded_file(request.FILES['file'])
#            return HttpResponse("上傳成功")
            myfile = request.FILES['file']
            fs = FileSystemStorage()
            filename = fs.save(myfile.name, myfile)
            uploaded_file_url = fs.url(filename)
            print(uploaded_file_url)
            res = handle_uploaded_file2(uploaded_file_url)
            return HttpResponse(res)
    else:
        form = UploadFileForm()
    return render(request, 'upload.html', {'form': form})

新建一個表格的模板:

#polls/templates/upload.html
<form enctype="multipart/form-data" action="/polls/upload/" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="上傳" />
</form>

新建一個url路由表:

#polls/urls.py
from django.urls import path

from . import views

urlpatterns = [
    path('upload/', views.upload_file, name='upload_file'),
]

修改項目路由:

#mysite/urls.py
from django.contrib import admin
from django.urls import include,path

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]

修改項目設置:

#mysite/settings.py
INSTALLED_APPS = [    
    'polls.apps.PollsConfig',
#    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
#    'django.contrib.messages',
#    'django.contrib.staticfiles',
]
LANGUAGE_CODE = 'zh-Hant'
TIME_ZONE = 'Asia/Taipei'

然後在項目目錄(最上層)運行gunicorn就可以訪問了:

gunicorn mysite.wsgi --bind 127.0.0.1:3040

nginx中增加如下server即可在外網訪問了(鏈接應該是http://YourPublicIP:8081/polls/upload/):

server {
        listen 8081;
        server_name 127.0.0.1;
        charset utf-8;
        keepalive_timeout 60s;
        #access_log logs/django2a.access.log combined if=$loggable;
        
        location / {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_redirect off;
    
            if (!-f $request_filename) {
                proxy_pass http://127.0.0.1:3040;
                break;
            }
    }
}

CentOS6+Django2+MySql

yum install MySQL-python
pip install mysqlclient
#如果import MySQLdb提示無libmysqlclient.so.18
#則建立軟連接如下
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18
分類
Linux

Fedora 31 L2TP PSK 連接失敗

有個預共享密鑰的 L2TP VPN,在安卓和 Windows上,不做額外設置的情況下都能連接上。但是 Fedora 31 上卻不行,試着修改下加密選項,也不成功。後來搜索到說是因為自帶的 libreswan 不在支持弱加密導致的。

解決方法

刪除已有的 VPN 配置
dnf remove libreswan
dnf remove strongswan
dnf install strongswan
新建 VPN 配置

錯誤現象

Started the VPN service, PID 17688
Saw the service appear; activating connection
VPN connection: (ConnectInteractive) reply received
VPN plugin: state changed: stopped (6)
VPN service disappeared
VPN connection: failed to connect: 'Remote peer disconnected'

原因

IKE DH algorithm ‘modp1024’ is not supported in libreswan with 5.5.7-200 kernel in FC31.

其他有用信息

#將網絡日誌調至調試級別(更多)
nmcli general logging level DEBUG
#將網絡日誌調至信息級別(默認)
nmcli general logging level INFO
#查看網絡連接日誌
journalctl -u NetworkManager
#服務的啟動與停止
systemctl stop strongswan.service
systemctl stop xl2tpd.service
#查看本機的網絡連接
nmcli con show
#啟動指定 UUID 的網絡連接
nmcli con up uuid 40c58e49-5b99-4432-85ae-a6dc9c1c37a3
#把指定 UUID 網絡連接的日誌保存到 t.txt
journalctl -xe NM_CONNECTION=40c58e49-5b99-4432-85ae-a6dc9c1c37a3 + NM_DEVICE=enp0s3 > t.txt

成功連接的版本信息

strongswan-5.8.2-3.fc31.x86_64
Linux 5.8.17-100.fc31.x86_64 #1 SMP Thu Oct 29 18:58:48 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

參考資料:Cannot Connect to VPN 感謝 nrv 和 insomniacjunkie 的回復。

分類
Linux 软件

Linux上的離線字典——GoldenDict

GoldenDict是一款方便的字典應用,不僅支持離線字典和屏幕取詞,也支持在線辭典服務。Fedora直接從軟件倉庫安裝 goldendict 即可sudo dnf install goldendict,默認的屏幕取詞快捷鍵是Ctrl+C+C。

分享幾個字典的下載地址:https://1drv.ms/f/s!AiSujQyFSc-uab_ItF61BBKnLUs。GoldenDict中的字典順序也是下面順序。

  1. Babylon_English_Chinese_S_.BGL
  2. Babylon_Chinese_S_English.BGL
  3. Oxford_Advanced_Learner_English-Chinese_Dictionary-4th.bgl
  4. ConciseOxfordEnglishDictionary.dsl.dz
  5. Oxford English Dictionary (2nd Edition) .bgl
  6. 现代汉英词典(金山).dsl.dz

安卓也有GoldenDict可用,我用的免費版(谷歌市場:GoldenDict Free),有最大5個字典的限制,但是也夠用了。

使用 espeak 來發音

#安裝espeak
sudo dnf install espeak
#打開GoldenDict,菜單欄依次選擇
#編輯/字典/字典來源/程式/新增
#新增內容為:
#類型:音訊
#名稱:espeak
#命令列:/usr/bin/espeak -v en -s 120 %GDWORD%
#圖示:/usr/share/doc/espeak/html/images/lips.png
#最後勾選啟用框,保存並重啟GoldenDict就可以有英文發音了

##如果要聽法語發音,
##只需要把命令列中的 en 換成 fr 即可。

更多字典可以從這些地方獲得:http://download.huzheng.org/(來自:Good offline dictionaries for GoldenDict)

使用 Speech Dispatcher 來發音

來到 2023 年,Speech Dispatcher 是新的語音生成器。這裏記錄下在 Fedora 上安裝以及命令調用的方法:

#安裝
sudo dnf install speech-dispatcher speech-dispatcher-utils
#使用
spd-say hello

本文更新於 2023/08/12。

分類
Linux

Hourly Reminder

忙起來的時候不知不覺時間就過去了,而久坐對健康絕對不是什麼好事。那麼就產生了一個需求,提醒我時間的流逝。Android上有個不錯的開源應用Hourly Reminder,但是我是在工作的時候才需要提醒,所以最好是桌面彈窗。簡單搜索發現Fedora預裝的notify-send命令就能很好的實現toast效果,搭配crontab就能滿足需求了。

crontab -e
#單行Hourly Reminder簡潔版
0 9-18 * * 1-5 notify-send -t 6000 '整點咯' 'It is ? time.'
#Hourly Reminder python 豪華版
0 9-18 * * 1-5 /home/42/Programs/p37/bin/python /home/42/eclipse-workspace/scriptsP37/hourlyReminder.py

file:/home/42/eclipse-workspace/scriptsP37/hourlyReminder.py

#!/home/42/Programs/p37/bin/python
import time,datetime,subprocess,random

ts = int(time.time())
hour = datetime.datetime.fromtimestamp(ts).strftime('%H')
title = "現在時刻"+hour+"點整"
emojiList=['?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','☕️','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?']
content = '\r        '+random.choice(emojiList)+'\r'
subprocess.check_call(['notify-send','-t','6000',title,content])

定時任務參考自crontab guru。 Emoji複製自Get Emoji

代碼里的emoji被wordpress強制轉義,從段落里複製吧emojiList=['?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','☕️','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?']

本文更新於 2021/09/13。