OPKG 軟件包管理


OPKG 軟件包管理

來源 https://openwrt.org/zh/docs/techref/opkg

 

opkg 工具 (一個 ipkg 變種) 是一個用來從本地軟件倉庫或互聯網軟件倉庫上下載並安裝 OpenWrt 軟件包的輕量型軟件包管理器。

GNU/Linux 用戶可能會對 apt-getaptitudepacmanyum 等比較熟悉,也會看出其相似之處。它與 NSLU2 上同樣用於嵌入式設備的 Optware 也有相似之處。OPKG 沒有僅僅將軟件安裝到一個單獨的路徑(如:/opt),而是根文件系統上的一個完整的包管理器。它也包含了增加內核模塊與驅動的可能性。OPKG 有時被稱為 Entware ,但這主要是針對為嵌入式設備准備的 Entware 倉庫。

opkg 試圖在軟件包倉庫內來解決依賴關系。如果失敗了,它將會報告一個錯誤並停止安裝該軟件包。

如果丟失第三方包的依賴關系,源碼包依然可用的話,為了忽略依賴關系的錯誤可以使用 –force-depends 選項。

請注意: 如果你在使用一個 snapshot 、trunk 或 bleeding edge 版本,在倉庫中的軟件包適用內核版本比你的 Flash 上的內核更高,opkg install <pkg> 可能會失敗。這種情況下,會報錯『Cannot satisfy the following dependencies for…』。參見OpenWrt FAQ

調用

opkg 必須有一個子命令參數:

用法: opkg [選項…] 子命令 [參數]

其中的子命令可以是:

可以使用  glob 模式.

軟件包管理

update 更新可用軟件包列表
此命令直接為你安裝的 OpenWrt 獲取一個類似樣例的文件,保存到內存分區的 /tmp/opkg-lists/snapshots 中。現在大約占用 1.3MB 空間。OPKG 需要它的內容才能安裝、升級軟件包或打印軟件包信息。此文件內容須體現軟件倉庫中當前的可用軟件包。為釋放內存空間,你可以安全地刪除這個文件,但不要忘記在安裝軟件前重新獲取一個。
upgrade <pkgs> 升級軟件包
要升級一組軟件包,運行 opkg upgrade 軟件包名1 軟件包名2 即可。
命令 opkg list-upgradable 可以獲取一個可升級軟件包的列表。
通常我們不推薦普通用戶去升級軟件包,因為一個典型的 OpenWrt 系統是存儲在只讀的 SquashFS 文件系統中。同時當升級工作做完后,使用了遠超出在 SquashFS 文件系統或 JFFS2 文件系統默認安裝基本軟件包的容量。
因此,推薦用刷一個更新的 OpenWrt 固件來代替升級軟件包。當然,升級已安裝的軟件包沒有這種問題。Keep in mind though that for OpenWrt releases upgrading is for the most part not possible, since there is nothing to upgrade without changing the package repository. This is because the package repositories for OpenWrt's releases are generally not updated. However, the package repository in the trunk snapshots are updated by the build bots to new versions very often, as this is where the packages are updated, like the OpenWrt builds themselves. Note however that for kernel packages updating can be a risky business as it may brick the device if the trunk build kernel is incompatible with the new upgraded kernel package. You should therefore only upgrade non-kernel packages.
install <pkgs|FQDN> 安裝一個或多個軟件包
例如:
opkg install hiawatha
opkg install http://downloads.openwrt.org/snapshots/trunk/ar71xx/packages/hiawatha_7.7-2_ar71xx.ipk
opkg install /tmp/hiawatha_7.7-2_ar71xx.ipk
configure <pkgs> 配置一個或多個未安裝的包
remove <pkgs|globp> 移除一個或多個軟件包
flag <flag> <pkgs> 標記一個或多個軟件包
每次調用僅允許一個標記。可用標記有:
hold • noprune • user • ok • installed • unpacked

閱讀有關 正則表達式http://en.wikipedia.org/wiki/Regular_expression

信息命令

list [pkg|globp] 列出可用軟件包
Package name - Version - Description

描述信息 (Description) 可能包含換行,so using merely grep is inapt since grep is line-based.

list-installed 列出已安裝軟件包
list-upgradable 列出可升級的已安裝軟件包
list-changed-conffiles 列出用戶修改過的配置文件
files <pkg> 列出屬於軟件包 <pkg> 的文件
僅適用於已安裝的軟件包。例如:
opkg files asterisk18
Package asterisk18 (1.8.4.4-1) is installed on root and has the following files:
/usr/lib/asterisk/modules/res_rtp_multicast.so
/usr/lib/asterisk/modules/codec_ulaw.so
/etc/asterisk/features.conf
/usr/lib/asterisk/modules/format_wav_gsm.so
/usr/lib/asterisk/modules/app_macro.so
/usr/lib/asterisk/modules/chan_sip.so
/usr/lib/asterisk/modules/app_dial.so
/usr/lib/asterisk/modules/app_playback.so
/usr/lib/asterisk/modules/format_gsm.so
/usr/lib/asterisk/modules/func_callerid.so
/usr/lib/asterisk/modules/func_timeout.so
/etc/asterisk/asterisk.conf
/etc/asterisk/modules.conf
/usr/lib/asterisk/modules/format_wav.so
/etc/asterisk/extensions.conf
/etc/init.d/asterisk
/etc/asterisk/manager.conf
/usr/lib/asterisk/modules/res_rtp_asterisk.so
/etc/asterisk/logger.conf
/etc/asterisk/rtp.conf
/usr/lib/asterisk/modules/codec_gsm.so
/etc/asterisk/indications.conf
/usr/lib/asterisk/modules/func_strings.so
/usr/lib/asterisk/modules/app_echo.so
/usr/lib/asterisk/modules/format_pcm.so
/etc/asterisk/sip_notify.conf
/etc/asterisk/sip.conf
/etc/default/asterisk
/usr/sbin/asterisk
/usr/lib/asterisk/modules/pbx_config.so
/usr/lib/asterisk/modules/func_logic.so
search <file|globp> 列出包含 <file>
info [pkg|globp] 顯示軟件包 <pkg> 的所有信息
Package: horst
Version: 2.0-rc1-2
Depends: libncurses
Provides:
Status: install user installed
Section: net
Architecture: ar71xx
Maintainer: Bruno Randolf <br1@einfach.org>
MD5Sum: 378cea9894ec971c419876e822666a6a
Size: 19224
Filename: horst_2.0-rc1-2_ar71xx.ipk
Source: feeds/packages/net/horst
Description: [horst] is a scanning and analysis tool for 802.11 wireless networks and
 especially IBSS (ad-hoc) mode and mesh networks (OLSR).

注1: size 是 gzip 壓縮后的 tar 包尺寸。安裝階段,軟件包會被展開解壓縮,JFFS2 會再次將之壓縮。
注2: 因 JFFS2 壓縮是『透明』的,像 ls 之類的命令仍會返回未壓縮的文件尺寸。

status [pkg|globp] 顯示軟件包 <pkg> 的狀態
download <pkg> 下載軟件包 <pkg> 到當前目錄
compare-versions <v1> <op> <v2> 使用關系符 <=<>>==<< 或 >> 來比較兩個版本 v1 和 v2 。
print-architecture 列出可安裝軟件包的結構
whatdepends [-A] [pkgname|pat]+ 僅適用於已安裝的軟件包。所以如果你想知道一個軟件包及其所有依賴占用的空間,你只能將這個選項的輸出分拆后傳給 info 選項來計算。
whatdependsrec [-A] [pkgname|pat]+ 僅適用於已安裝的軟件包。所以所以如果你想知道一個軟件包及其所有依賴占用的空間,你只能將這個選項的輸出分拆后傳給 info 選項來計算。
whatprovides [-A] [pkgname|pat]+  
whatconflicts [-A] [pkgname|pat]+  
whatreplaces [-A] [pkgname|pat]+  

選項

短選項 長選項 描述
-A   查詢全部軟件包(不僅是已安裝的)
-V[<level>] --verbosity[=<level>] 設置『嘮叨』級別為 <level> 。
可用的級別有:
0 僅錯誤
1 普通消息(缺省情況)
2 有用的消息
3 調試
4 調試等級2
-f <conf_file> --conf <conf_file> 設置 opkg 配置文件為 <conf_file> 。缺省為 /etc/opkg.conf
  --cache <directory> 指定包緩存
-d <dest_name> --dest <dest_name> 設置軟件包安裝、刪除、升級的根目錄為 <dest_name> 。
<dest_name> 應為已在配置文件中定義的目的名稱(but can also be a directory name in a pinch)。
-o <dir> --offline-root <dir> 指定離線安裝軟件包的根目錄。
  --add-arch <arch>:<prio> 注冊架構及其優先級
  --add-dest <name>:<path> 注冊目的名及其路徑
強制性選項
  --force-depends 在安裝、刪除軟件包時無視失敗的依賴
  --force-maintainer 覆蓋已經存在的配置文件
  --force-reinstall 重安裝軟件包
  --force-overwrite 覆蓋其它軟件包的文件
  --force-downgrade 允許 opkg 降級軟件包
  --force-space 禁用可用空間檢查
  --force-checksum 忽略校驗和失配
  --force-postinstall 離線模式下仍運行安裝后腳本
  --noaction 無操作 – 僅測試
  --download-only 無操作 – 僅下載
  --nodeps 不跟蹤依賴
  --force-removal-of-dependent-packages 移除軟件包的同時,移除其所有依賴軟件包
  --autoremove 移除自動安裝以滿足依賴的軟件包
-t --tmp-dir 指定臨時目錄

舉例

 

安裝

要安裝軟件包,執行下列命令。注意重啟設備會使可用軟件包列表丟失,所以在試圖安裝軟件包之前務必更新列表。

opkg update
opkg install <package>

搜索

  • opkg list 僅會顯示  包名 — 版本 — 描述
  • opkg info 會顯示全部可用信息

你可以直接使用 glob 模式,或者寫個小的 Shell 腳本來使用正則表達式或進一步的進程信息。使用管道(|)以及 grep 、awk 或 sed 指令來過濾輸出:

  • opkg list | grep pattern
  • opkg list | awk '/pattern/ {print $0}
  • opkg info kmod-ipt-* | awk '/length/ {print $0}'
  • opkg list-installed | awk '{print $1}' | sed ':M;N;$!bM;s#\n# #g'
  • var=“packagename1 packagename2 packagename2”; for i in $var; do opkg info $i; done;
  • opkg depends dropbear 並不會起作用。

配置

 

調整倉庫

唯一的配置文件是 /etc/opkg.conf ,它可能看起來是這樣:

src/gz snapshots http://downloads.openwrt.org/snapshots/trunk/ar71xx/packages
dest root /
dest ram /tmp
lists_dir ext /var/opkg-lists
option overlay_root /overlay

本地倉庫

你可以配置 opkg 從本地獲取軟件包:

src/gz local file:///path/to/packagesDirectory

Barrier_breaker 使用多個倉庫,每個參考需要個唯一的標識符。邏輯上使用倉庫原始的名稱。如:

...
src/gz base file:///path/to/packages/directory/packages/base
src/gz luci file:///path/to/packages/directory/packages/luci
src/gz packages file:///path/to/packages/directory/packages/packages
src/gz oldpackages file:///path/to/packages/directory/packages/oldpackages
... etc ...
實例
r=44685
search="http://downloads.openwrt.org/snapshots/trunk/ar71xx/generic"
replace="file:///mnt/sdcard/shared/users/www/r$r"
sed -i -e "s!$search!$replace!" /etc/opkg.conf

共享給第二個路由器:

ln -s /mnt/sdcard/shared/users/www /www/pendrive

在第二個路由器上:

r=44685
search="downloads.openwrt.org/snapshots/trunk/ar71xx/generic"
replace="192.168.1.1/pendrive/r$r"
sed -i -e "s!$search!$replace!" /etc/opkg.conf

修改處理器架構

缺省情況下,opkg 僅操作標記架構為 all(即架構無關)或與目標平台相同的軟件包。要從外引入兼容目標平台的軟件包,可以在 opkg.conf 中使用 arch 選項追加可用的架構。

arch all 100
arch brcm4716 200
arch brcm47xx 300

上面的例子將允許將 brcm47xx(即 soc 系列)軟件包安裝到 brcm4716(某特定的 SoC)平台。行尾的數字向 opkg 指定了優先級索引,以便在有多個可用包時選擇最合適的那個。

代理支持

要透過代理使用 opkg,在 /etc/opkg.conf 中添加:

option http_proxy http://proxy.example.org:8080/
option ftp_proxy ftp://proxy.example.org:2121/

使用以下設置來進行代理服務器的認證:

option proxy_username xxxx
option proxy_password xxxx

有時,因為 busybox 中 wget 的限制,代理服務器的認證可能失敗。這時,可以嘗試在代理服務器的 URL 中傳入用戶名和密碼:

option http_proxy http://username:password@proxy.example.org:8080/
option ftp_proxy http://username:password@proxy.example.org:2121/

安裝目標

 

Extroot

使用 extroot_configuration 就足夠了,無需進一步配置。

掛載點

opkg 有個不為那些 apt 系工具使用者熟悉的有用特性,它可以為任一軟件包指定安裝目的位置。

許多軟件包不可重新定位,且可能無法干凈地安裝到非根目錄下!像 LuCI 就會找不到它的模塊,且若不手動修復就不能工作!使用 Extroot 吧!
別指望這個辦法能『拆箱即用』,大多軟件包需要額外的符號鏈接或 hack 才能在改變的路徑下正常工作!

事實上,默認的 opkg.conf 包含三個目的位置:

dest root /
dest ram /tmp
dest mnt /mnt

目的位置行的格式很簡單,關鍵字 dest 跟着目的位置名稱(可以隨便起),然后就是文件系統位置。任一如上配置好的目的位置都可以在 opkg 命令行中使用,如:

opkg install somepackage -d destination_name

dest 參數必須為 /etc/opkg.conf 中定義的目的位置名。如:-d ram 會將軟件包安裝到 /tmp/ 下。

若要在非根的目的位置安裝內核模塊,你可能要先讀下<red>這個</red>。

詳細說明

首先 mount 到外部文件系統,操作幫助見 Mounting Filesystems 。然后編輯 /etc/opkg.conf :

  • 在文件末尾加一行  dest usb /opt
  • 執行命令(假定你將外部文件系統掛載到了  /mnt/sda1 ):
    ln -s /mnt/sda1 /opt
  • 若你已通過  opkg -d ram 將軟件包安裝到了  tmp 文件夾,你需要加個新的 bin 和 lib 路徑:
    bash
    export PATH=$PATH:/tmp/usr/bin/
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/tmp/usr/lib/
  • 編輯  /etc/profile ,為你的  PATH 變量追加掛載點:
    export PATH=<current default path>:/opt/bin:/opt/sbin:/opt/usr/bin:/opt/usr/sbin export LD_LIBRARY_PATH=<current default LD library path>:/opt/lib:/opt/usr/lib
  • 到此,你應可向掛載點安裝新軟件包了:
    opkg update
    opkg -dest usb install asterisk14 # or whatever else you want…
  • 若你向外部文件系統安裝軟件包,而該文件系統的  /etc/init.d 及(若啟用) /etc/rc.d 下已有啟動腳本,你需要設置個符號鏈接到  /etc/init.d 。如:
     ln -s /usb/etc/init.d/openvpn /etc/init.d/openvpn
  • 跟隨軟件包安裝的庫也會被安裝到外部文件系統。這使得程序不會在引導時啟動。你需要在每個啟動腳本中手動設置  LD_LIBRARY_PATH :
     export LD_LIBRARY_PATH=/lib:/usr/lib:/tmp/lib:/tmp/usr/lib:/usb/lib:/usb/usr/lib
其它說明
  1. 掛載外部分區,見  Mounting Filesystems
  2. 修改  /etc/opkg.conf ,為預期的掛載點加入個新的目的位置或:
    1. 修改目的位置的根目錄,指向你的 USB 磁盤。
    2. 將  /usr/lib/opkg 目錄的內容復制到你的掛載點。比如 USB 磁盤。
    3. map directories where it is likely that they'll receive lots of files to your usb drive, for example with mount –bind during startup.  /etc/rc.local could be one place where to map the directories.

example:

mount --bind /mnt/usb/root /root
mount --bind /mnt/usb/usr/local /usr/local
mount --bind /mnt/usb/home  /home

對於包含庫的軟件包,若安裝到了外部磁盤,需要配置才能找到。 安裝 ldconfig 並在 /etc/ld.so.conf 中列出附加庫路徑會解決這個問題。在新庫安裝后,必須執行過 ldconfig 。它可以被安裝到外部磁盤。在啟動時執行一次它也許是個好主意。也許是在 /etc/rc.local 中。

內核模塊

安裝到非標准位置的內核模塊有可能不會自動加載,這時就需要手動插入內核。例如,在把 libdevmapper 安裝到 /mnt 下面后,插入此模塊:

insmod /mnt/lib/modules/2.6.36.4/dm-mod.ko

你可以把守護進程和『內核』服務安裝到內部 Flash 中,把可選的軟件包安裝到外部掛載點。啟動過程中,若外部磁盤被掛載,外部目錄的可執行文件(<掛載點>/[bin,sbin,usr/bin,usr/sbin])會被加進 PATH 變量。路由器通過這種方式繼續提供相關服務,即使外部存儲已經離線。

軟件包若安裝到外部驅動器中,可能引發下列問題:

  • file
    It installs its dependency libmagic to external drive too, but looks for that lib on root device.
    Fix: a script with be name “file” in a directory in PATH which is searched before the directory which contains the executable “file”, which calls file (full path) with option -m and path to the libmagic:
    #!/bin/sh
    /mnt/usb/usr/bin/file -m /mnt/usb/usr/share/file/magic "$@"

    after saving the script, and making it executable (chmod +x /path/of/script/file), executing once “hash -r” may or may not be needed.

  • netcat
    Not really a problem, but the link nc (busybox?) was still pointing to a file with wrong location.
    Moving the nc link to external drive (to the dir containing netcat) fixes that.
  • nfsd
    /etc/init.d/nfsd contains hardcoded paths to executables on  /usr/sbin
    Fix: edit  /etc/init.d/nfsd, by changing those paths to updated location.
  • lvm2
    /etc/init.d/lvm2 contains hardcoded paths to executables on  /sbin
    Fix: edit  /etc/init.d/lvm2, by changing these paths to updated location.

More information on installing and using ldconfig can be found in this article.

Some programs need additional config files to run, and you will have to create soft links between the root filesystem and the external storage one (e.g. USB). As an example, to successfully run Midnight Commander after installing it on a USB stick, you must run:

ln -s $USB/usr/share/terminfo/ /usb/share/
ln -s $USB/etc/mc /etc/mc

Troubleshooting

 

磁盤空間耗盡

若 opkg 耗盡了存儲空間,它通常會報告個 Could not obtain administrative lock 錯誤,表示沒有干凈地恢復孤懸的文件鎖。可以調用 rm /usr/lib/opkg/lock 命令來刪除這個文件鎖。

除此之外,opkg 可能不會刪除之前安裝的文件。
處理此事的一種辦法是獲取一個之前安裝的文件的列表,然后刪除它們。

將 URL 替換為相應的包:

(cd /; \
 wget -qO- http://downloads.openwrt.org/snapshots/trunk/ar7/packages/6in4_10-1_all.ipk | \
 tar -Oxz ./data.tar.gz | tar -tz | xargs rm)

然而,上述代碼並沒有可靠地刪除那些伴隨軟件包安裝進去的依賴軟件包。它也遺留下了那些空目錄。這個腳本嘗試修正這些問題:

#!/bin/sh
#takes one argument/parameter: the name of the package which didn't install correctly and should be removed along with its dependencies
#do opkg update first
#example: ./opkgremovepartlyinstalledpackage.sh pulseaudio-daemon

#get list of all packages that would be installed along with package x
opkg update
PACKAGES=`opkg --force-space --noaction install $1 | grep "http:" | cut -f 2 -d ' ' | sed 's/.$//'`
for i in $PACKAGES
do
        LIST=`wget -qO- $i | tar -Oxz ./data.tar.gz | tar -tz | sort -r | sed 's/^./\/overlay/'`
        for f in $LIST
        do
                if [ -f $f ]
                then
                        echo "Removing file $f"
                        rm -f $f
                fi
                if [ -d $f ]
                then
                        echo "Try to remove directory $f (will only work on empty directories)"
                        rmdir $f
                fi
        done
done
echo "You may need to reboot for the free space to become visible"

將之保存在你的 OpenWrt 設備上,命名為 opkgclean.sh ,設置為可執行 chmod +x ./opkgclean.sh ,然后你可以這樣執行它 ./opkgclean.sh <package-name> 

其它

要使 opkg 的輸出有顏色,可以用 http://pastie.org/5464938.

命令

To find installed pkgs of a specific install target (ex. USB) (DRAWBACK!!! if any update availlable, it will update the package, just be warned!!!):

for pkg in `opkg list-installed | sed -e “s/^([0-9A-Za-z\-]+) - .*$/\1/p” -n`; do opkg install $pkg; done | grep -i installed\ in\ <TARGET>

備注

  Since Trunk r23173 respectively Backfire r23206 the kernel and kmod packages are flagged as hold.
The opkg upgrade command won't attempt to update them anymore.
This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.
zh/docs/techref/opkg.txt · Last modified: 2018/06/06 19:56 by tmomas
 

================== End

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM