Pacman Tips (日本語)
| 概要 |
|---|
| pacman を新しく使い始めた人向けのヒント集。 |
| 関連項目 |
| pacman (日本語) |
| Mirrors (日本語) |
| Creating Packages (日本語) |
| Custom local repository |
外観と利便性の向上
カラー出力
バージョン 4.1 から Pacman にはカラーオプションが付きました。pacman.conf の中の "Color" という行をアンコメントしてください。
ショートカット
以下では、よく使われる pacman コマンドの入力の手間を省くスクリプトエイリアスを説明しています。
シェルの設定
以下の例を追加して下さい、Bash と Zsh のどちらでも動作します:
# Pacman alias examples alias pacupg='sudo pacman -Syu' # Synchronize with repositories before upgrading packages that are out of date on the local system. alias pacin='sudo pacman -S' # Install specific package(s) from the repositories alias pacins='sudo pacman -U' # Install specific package not from the repositories but from a file alias pacre='sudo pacman -R' # Remove the specified package(s), retaining its configuration(s) and required dependencies alias pacrem='sudo pacman -Rns' # Remove the specified package(s), its configuration(s) and unneeded dependencies alias pacrep='pacman -Si' # Display information about a given package in the repositories alias pacreps='pacman -Ss' # Search for package(s) in the repositories alias pacloc='pacman -Qi' # Display information about a given package in the local database alias paclocs='pacman -Qs' # Search for package(s) in the local database # Additional pacman alias examples alias pacupd='sudo pacman -Sy && sudo abs' # Update and refresh the local package and ABS databases against repositories alias pacinsd='sudo pacman -S --asdeps' # Install given package(s) as dependencies of another package alias pacmir='sudo pacman -Syy' # Force refresh of all package lists after updating /etc/pacman.d/mirrorlist
使用方法
エイリアスの名前を入力するだけでそれぞれのコマンドが実行できます。例えば、リポジトリを同期して古くなっているパッケージをアップグレードするには:
$ pacupg
リポジトリからパッケージをインストール:
$ pacin <package1> <package2> <package3>
カスタムビルドパッケージをインストール:
$ pacins /path/to/<package>
インストールしたパッケージを完全に削除:
$ pacrem <package>
リポジトリからパッケージを検索:
$ pacreps <keywords>
リポジトリにあるパッケージの情報 (サイズ、依存関係など) を表示:
$ pacrep <keywords>
ノート
The aliases used above are merely examples. By following the syntax samples above, rename the aliases as convenient. For example:
alias pacrem='sudo pacman -Rns' alias pacout='sudo pacman -Rns'
In the case above, the commands pacrem and pacout both call your shell to execute the same command.
オペレーションと Bash 構文
In addition to pacman's standard set of features, there are ways to extend its usability through rudimentary Bash commands/syntax.
- To install a number of packages sharing similar patterns in their names -- not the entire group nor all matching packages; eg. kde:
# pacman -S kde-{applets,theme,tools}
- Of course, that is not limited and can be expanded to however many levels needed:
# pacman -S kde-{ui-{kde,kdemod},kdeartwork}
- Sometimes,
-s's builtin ERE can cause a lot of unwanted results, so it has to be limited to match the package name only; not the description nor any other field:
# pacman -Ss '^vim-'
- pacman has the
-qoperand to hide the version column, so it is possible to query and reinstall packages with "compiz" as part of their name:
# pacman -S $(pacman -Qq | grep compiz)
- Or install all packages available in a repository (kde-unstable for example):
# pacman -S $(pacman -Slq kde-unstable)
メンテナンス
House keeping, in the interest of keeping a clean system and following The Arch Way
全てのインストールしたパッケージを容量と一緒にリストアップ
- 容量でソートしたインストール済みパッケージの一覧を見ることができます、これはハードドライブの空き容量を増やしたいときに役立ちます。
- pacman パッケージに入っている
pacsyscleanを使って下さい。 - expac をインストールして
expac -s "%-30n %m" | sort -rhk 2を実行して下さい。 - pacgraph に -c オプションを付けて実行することで全てのインストールしたパッケージと容量のリストを作成できます。pacgraph は [community] からインストールできます。
-
pacman -Qi | egrep "Name|Installed Size" | sed -e 'N;s/\n/ /' | awk '{ print $7, $3}' | sort -n(note that some packages with "Name" in their descriptions will disrupt this (e.g. idnkit))
インストールしたパッケージをバージョンと一緒にリストアップ
- You may want to get the list of installed packages with their version, which is useful when reporting bugs or discussing installed packages.
- List all explicitly installed packages:
pacman -Qe. - List all foreign packages (typically manually downloaded and installed):
pacman -Qm. - List all native packages (installed from the sync database(s)):
pacman -Qn. - List packages by regex:
pacman -Qs <regex> | awk 'BEGIN { RS="\n" ; FS="/" } { print $2 }' | awk '{ if(NF > 0) print $1, $2 }' - Install expac and run
expac -s "%-30n %v"
ファイルがどのパッケージにも所有されていないことを確認
Periodic checks for files outside of pacman database are recommended. These files are often some 3rd party applications installed using the usual procedure (e.g. ./configure && make && make install). Search the file-system for these files (or symlinks) using this simple script:
pacman-disowned
#!/bin/sh
tmp=${TMPDIR-/tmp}/pacman-disowned-$UID-$$
db=$tmp/db
fs=$tmp/fs
mkdir "$tmp"
trap 'rm -rf "$tmp"' EXIT
pacman -Qlq | sort -u > "$db"
find /bin /etc /sbin /usr \
! -name lost+found \
\( -type d -printf '%p/\n' -o -print \) | sort > "$fs"
comm -23 "$fs" "$db"
To generate the list:
$ pacman-disowned > non-db.txt
Note that one should not delete all files listed in non-db.txt without confirming each entry. There could be various configuration files, logs, etc., so use this list responsibly and only proceed after extensively searching for cross-references using grep.
孤立したパッケージの削除
再帰的に孤立したパッケージを削除するには:
# pacman -Rs $(pacman -Qtdq)
The following alias is easily inserted into ~/.bashrc and removes orphans if found:
~/.bashrc
# '[r]emove [o]rphans' - recursively remove ALL orphaned packages alias pacro="/usr/bin/pacman -Qtdq > /dev/null && sudo /usr/bin/pacman -Rs \$(/usr/bin/pacman -Qtdq | sed -e ':a;N;\$!ba;s/\n/ /g')"
The following function is easily inserted into ~/.bashrc and removes orphans if found:
~/.bashrc
orphans() {
if [[ ! -n $(pacman -Qdt) ]]; then
echo "No orphans to remove."
else
sudo pacman -Rs $(pacman -Qdtq)
fi
}
base グループ以外の全てのパッケージを削除する
base グループを除く全てのパッケージを削除する必要がある場合は、このワンライナーを試して下さい:
# pacman -Rs $(comm -23 <(pacman -Qeq|sort) <((for i in $(pacman -Qqg base); do pactree -ul $i; done)|sort -u|cut -d ' ' -f 1))
ノート:
-
commrequires sorted input otherwise you get e.g.comm: file 1 is not in sorted order. -
pactreeprints the package name followed by what it provides. For example:
$ pactree -lu logrotate
logrotate popt glibc linux-api-headers tzdata dcron cron bash readline ncurses gzip
The dcron cron line seems to cause problems, that is why cut -d ' ' -f 1 is needed - to keep just the package name.
公式にインストールされたパッケージのみを一覧する
pacman -Qqn
同期データベースに存在するパッケージを一覧します。非公式のリポジトリを設定している場合は、そのリポジトリからインストールしたパッケージも表示されます。
複数のパッケージの依存パッケージ一覧を取得
Dependencies are alphabetically sorted and doubles are removed.
Note that you can use pacman -Qi to improve response time a little. But
you won't be able to query as many packages. Unfound packages are simply skipped
(hence the 2>/dev/null).
You can get dependencies of AUR packages as well if you use yaourt -Si,
but it will slow down the queries.
$ pacman -Si $@ 2>/dev/null | awk -F ": " -v filter="^Depends" \ '$0 ~ filter {gsub(/[>=<][^ ]*/,"",$2) ; gsub(/ +/,"\n",$2) ; print $2}' | sort -u
Alternatively, you can use expac: expac -l '\n' %E -S $@ | sort -u.
複数のパッケージの容量を取得
You can use (and tweak) this little shell function:
~/.bashrc
pacman-size()
{
CMD="pacman -Si"
SEP=": "
TOTAL_SIZE=0
RESULT=$(eval "${CMD} $@ 2>/dev/null" | awk -F "$SEP" -v filter="^Size" -v pkg="^Name" \
'$0 ~ pkg {pkgname=$2} $0 ~ filter {gsub(/\..*/,"") ; printf("%6s KiB %s\n", $2, pkgname)}' | sort -u -k3)
echo "$RESULT"
## Print total size.
echo "$RESULT" | awk '{TOTAL=$1+TOTAL} END {printf("Total : %d KiB\n",TOTAL)}'
}
As told for the dependencies list, you can use pacman -Qi instead, but
not yaourt since AUR's PKGBUILD do not have size information.
A nice one-liner:
$ pacman -Si "$@" 2>/dev/null | awk -F ": " -v filter="Size" -v pkg="Name" '$0 ~ pkg {pkgname=$2} $0 ~ filter {gsub(/\..*/,"") ; printf("%6s KiB %s\n", $2, pkgname)}' | sort -u -k3 | tee >(awk '{TOTAL=$1+TOTAL} END {printf("Total : %d KiB\n",TOTAL)}')
You should replace "$@" with packages, or put this line in a shell function.
変更された設定ファイルを一覧する
If you want to backup your system configuration files you could copy all files in /etc/, but usually you're only interested in the files that you have changed. In this case you want to list those changed configuration files, we can do this with the following command:
# pacman -Qii | awk '/^MODIFIED/ {print $2}'
The following script does the same. You need to run it as root or with sudo.
changed-files.sh
#!/bin/bash for package in /var/lib/pacman/local/*; do sed '/^%BACKUP%$/,/^%/!d' $package/files | tail -n+2 | grep -v '^$' | while read file hash; do [ "$(md5sum /$file | (read hash file; echo $hash))" != "$hash" ] && echo $(basename $package) /$file done done
Listing all packages that nothing else depends on
If you want to generate a list of all installed packages that nothing else depends on, you can use the following script. This is very helpful if you are trying to free hard drive space and have installed a lot of packages that you may not remember. You can browse through the output to find packages which you no longer need.
clean
#!/bin/bash
# This script is designed to help you clean your computer from unneeded
# packages. The script will find all packages that no other installed package
# depends on. It will output this list of packages excluding any you have
# placed in the ignore list. You may browse through the script's output and
# remove any packages you do not need.
# Enter groups and packages here which you know you wish to keep. They will
# not be included in the list of unrequired packages later.
ignoregrp="base base-devel"
ignorepkg=""
# Temporary file locations
tmpdir=/tmp
ignored=$tmpdir/ignored
installed=$tmpdir/installed
# Generate list of installed packages and packages you wish to keep.
echo $(pacman -Sg $ignoregrp | awk '{print $2}') $ignorepkg | tr ' ' '\n' | sort | uniq > $ignored
pacman -Qq | sort > $installed
# Do not loop packages you are keeping
loop=$(comm -13 $ignored $installed)
# Check each remaining package. If package is not required by anything and
# is not on your ignore list, print the package name to the screen.
for line in $loop; do
check=$(pacman -Qi $line | awk '/Required By/ {print $4}')
if [ "$check" == 'None' ]; then echo $line; fi
done
# Clean up $tmpdir
rm $ignored $installed
If you install expac you can run expac "%n %N" -Q $(expac "%n %G" | grep -v ' base') | awk '$2 == "" {print $1}' which should give the same results but much faster.
The following script has the option to exclude files like above, but uses expac:
clean
#!/bin/bash
# Generate list of installed packages (leaves in package dependency tree).
# Give it a list with packages that should be ignored in the final list, each
# package on a separate line.
# Temporary files
IGNORED=$(mktemp)
[ -n "$1" ] && cat "$1" >$IGNORED
LIST="$(mktemp)"
expac "%n %N" -Q $(expac "%n %G" | grep -v ' base') | awk '$2 == "" {print $1}' > "$LIST"
# Sort both lists, so they can be diffed.
TMPF=$(mktemp)
sort "$IGNORED" | grep -v '^$' > "$TMPF"
sort -o "$LIST" "$LIST"
IGNORED="$TMPF"
# Diff the lists.
comm -13 "$IGNORED" "$LIST"
Systemd を使ってローカルデータベースをバックアップ
Systemd は pacman のローカルデータベースのスナップショットを(データベースが変更される度に)作成することができます。
#!/bin/bash declare -r pakbak="/pakbak.tar.xz"; ## set backup location tar -cJf "$pakbak" "/var/lib/pacman/local"; ## compress & store pacman local database in $pakbak
[Unit] Description=Back up pacman database [Service] Type=oneshot ExecStart=/bin/bash /usr/lib/systemd/scripts/pakbak_script RemainAfterExit=no
[Unit] Description=Back up pacman database [Path] PathChanged=/var/lib/pacman/local Unit=pakbak.service [Install] WantedBy=multi-user.target
インストールとリカバリー
Alternative ways of getting and restoring packages.
パッケージを CD/DVD や USB スティックからインストールする
パッケージや、パッケージグループをダウンロードするには:
# cd ~/Packages # pacman -Syw base base-devel grub-bios xorg gimp --cachedir . # repo-add ./custom.db.tar.gz ./*
ダウンロードしたら "Packages" フォルダを CD/DVD に焼くか USB スティック、外部 HDD などにコピーしてください。
インストールするには:
1. メディアをマウントする:
# mkdir /mnt/repo # mount /dev/sr0 /mnt/repo # CD/DVD の場合 # mount /dev/sdxY /mnt/repo # USB スティックの場合。
2. pacman.conf を編集して他のリポジトリ (例: extra, core, etc.) の前にリポジトリを追加してください。この手順は重要です。これで標準のリポジトリに優先して CD/DVD/USB のファイルがインストールされるようになります:
# nano /etc/pacman.conf
[custom] SigLevel = PackageRequired Server = file:///mnt/repo/Packages
3. 最後に、pacman データベースを同期して新しいリポジトリを使えるようにしてください:
# pacman -Sy
カスタムローカルリポジトリ
pacman 3 では個人的なリポジトリのデータベースの作成をより簡単にするため repo-add という名前の新しいスクリプトが導入されました。詳しい使い方は repo-add --help を実行して見て下さい。
Simply store all of the built packages to be included in the repository in one directory, and execute the following command (where repo is the name of the custom repository):
$ repo-add /path/to/repo.db.tar.gz /path/to/*.pkg.tar.xz
Note that when using repo-add, the database and the packages do not need to be in the same directory. But when using pacman with that database, they should be together.
To add a new package (and remove the old if it exists), run:
$ repo-add /path/to/repo.db.tar.gz /path/to/packagetoadd-1.0-1-i686.pkg.tar.xz
Once the local repository has been made, add the repository to pacman.conf. The name of the db.tar.gz file is the repository name. Reference it directly using a file:// url, or access it via FTP using ftp://localhost/path/to/directory.
If willing, add the custom repository to the list of unofficial user repositories, so that the community can benefit from it.
Read-only cache
If you're looking for a quick and dirty solution, you can simply run a standalone webserver which other computers can use as a first mirror: darkhttpd /var/cache/pacman/pkg. Just add this server at the top of your mirror list. Be aware that you might get a lot of 404 errors, due to cache misses, depending on what you do, but pacman will try the next (real) mirrors when that happens.
Read-write cache
In order to share packages between multiple computers, simply share /var/cache/pacman/ using any network-based mount protocol. This section shows how to use shfs or sshfs to share a package cache plus the related library-directories between multiple computers on the same local network. Keep in mind that a network shared cache can be slow depending on the file-system choice, among other factors.
First, install any network-supporting filesystem; for example sshfs, shfs, ftpfs, smbfs or nfs.
Then, to share the actual packages, mount /var/cache/pacman/pkg from the server to /var/cache/pacman/pkg on every client machine.
Preventing unwanted cache purges
By default, pacman -Sc removes package tarballs from the cache that correspond to packages that are not installed on the machine the command was issued on. Because pacman cannot predict what packages are installed on all machines that share the cache, it will end up deleting files that should not be.
To clean up the cache so that only outdated tarballs are deleted, add this entry in the [options] section of /etc/pacman.conf:
CleanMethod = KeepCurrent
インストールしたパッケージのリストのバックアップと復旧
pacman によってインストールしたパッケージのバックアップを定期的に行うのはグッドプラクティスです。何らかの理由でリカバリーできないシステムクラッシュが発生した時、pacman を使って全く同じパッケージを簡単に新しい環境に再インストールすることができるようになります。
- First, backup the current list of non-local packages:
- pacman -Qqen > pkglist.txt
- Store the
pkglist.txton a USB key or other convenient medium or gist.github.com or Evernote, Dropbox, etc.
- Copy the
pkglist.txtfile to the new installation, and navigate to the directory containing it.
- Issue the following command to install from the backup list:
- # pacman -S $(< pkglist.txt)
In the case you have a list which was not generated like mentioned above, there may be foreign packages in it (i.e. packages not belonging to any repos you have configured, or packages from the AUR).
In such a case, you may still want to install all available packages from that list:
# pacman -S --needed $(comm -12 <(pacman -Slq|sort) <(sort badpkdlist) )
Explanation:
-
pacman -Slqlists all available softwares, but the list is sorted by repository first, hence thesortcommand. - Sorted files are required in order to make the
commcommand work. - The
-12parameter display lines common to both entries. - The
--neededswitch is used to skip already installed packages.
You may also try to install all unavailable packages (those not in the repos) from the AUR using yaourt (not recommended unless you know exactly what you are doing):
$ yaourt -S --needed $(comm -13 <(pacman -Slq|sort) <(sort badpkdlist) )
Finally, you may want to remove all the packages on your system that are not mentioned in the list.
# pacman -Rsu $(comm -23 <(pacman -Qq|sort) <(sort pkglist))
base や base-devel に存在しないダウンロード済みパッケージを一覧
The following command will list any installed packages that are not in base/base-devel, and as such were likely installed manually by the user:
$ comm -23 <(pacman -Qeq|sort) <(pacman -Qgq base base-devel|sort)
全てのパッケージの再インストール
To reinstall all native packages, use:
# pacman -Qenq | pacman -S -
Foreign (AUR) packages must be reinstalled separately; you can list them with pacman -Qemq.
Pacman preserves the installation reason by default.
pacman のローカルデータベースを復元する
Signs that pacman needs a local database restoration:
-
pacman -Qgives absolutely no output, andpacman -Syuerroneously reports that the system is up to date. - When trying to install a package using
pacman -S package, and it outputs a list of already satisfied dependencies. - When
testdb(part of pacman) reports database inconsistency.
Most likely, pacman's database of installed software, /var/lib/pacman/local, has been corrupted or deleted. While this is a serious problem, it can be restored by following the instructions below.
Firstly, make sure pacman's log file is present:
$ ls /var/log/pacman.log
If it does not exist, it is not possible to continue with this method. You may be able to use Xyne's package detection script to recreate the database. If not, then the likely solution is to re-install the entire system.
ログフィルタースクリプト
pacrecover
#!/bin/bash -e
. /etc/makepkg.conf
PKGCACHE=$((grep -m 1 '^CacheDir' /etc/pacman.conf || echo 'CacheDir = /var/cache/pacman/pkg') | sed 's/CacheDir = //')
pkgdirs=("$@" "$PKGDEST" "$PKGCACHE")
while read -r -a parampart; do
pkgname="${parampart[0]}-${parampart[1]}-*.pkg.tar.xz"
for pkgdir in ${pkgdirs[@]}; do
pkgpath="$pkgdir"/$pkgname
[ -f $pkgpath ] && { echo $pkgpath; break; };
done || echo ${parampart[0]} 1>&2
done
Make the script executable:
$ chmod +x pacrecover
Generating the package recovery list
Run the script (optionally passing additional directories with packages as parameters):
$ paclog-pkglist /var/log/pacman.log | ./pacrecover >files.list 2>pkglist.orig
This way two files will be created: files.list with package files, still present on machine and pkglist.orig, packages from which should be downloaded. Later operation may result in mismatch between files of older versions of package, still present on machine, and files, found in new version. Such mismatches will have to be fixed manually.
Here is a way to automatically restrict second list to packages available in a repository:
$ { cat pkglist.orig; pacman -Slq; } | sort | uniq -d > pkglist
Check if some important base package are missing, and add them to the list:
$ comm -23 <(pacman -Sgq base) pkglist.orig >> pkglist
Proceed once the contents of both lists are satisfactory, since they will be used to restore pacman's installed package database; /var/lib/pacman/local/.
Performing the recovery
Define bash alias for recovery purposes:
# recovery-pacman() {
pacman "$@" \
--log /dev/null \
--noscriptlet \
--dbonly \
--force \
--nodeps \
--needed \
#
}
--log /dev/null allows to avoid needless pollution of pacman log, --needed will save some time by skipping packages, already present in database, --nodeps will allow installation of cached packages, even if packages being installed depend on newer versions. Rest of options will allow pacman to operate without reading/writing filesystem.
Populate the sync database:
# pacman -Sy
Start database generation by installing locally available package files from files.list:
# recovery-pacman -U $(< files.list)
Install the rest from pkglist:
# recovery-pacman -S $(< pkglist)
Update the local database so that packages that are not required by any other package are marked as explicitly installed and the other as dependences. You will need be extra careful in the future when removing packages, but with the original database lost is the best we can do.
# pacman -D --asdeps $(pacman -Qq) # pacman -D --asexplicit $(pacman -Qtq)
Optionally check all installed packages for corruption:
# pacman -Qk
Optionally #Identify files not owned by any package.
Update all packages:
# pacman -Su
Recovering a USB key from existing install
If you have Arch installed on a USB key and manage to mess it up (e.g. removing it while it is still being written to), then it is possible to re-install all the packages and hopefully get it back up and working again (assuming USB key is mounted in /newarch)
# pacman -S $(pacman -Qq --dbpath /newarch/var/lib/pacman) --root /newarch --dbpath /newarch/var/lib/pacman
.pkg ファイルの中身を展開する
.xz で終わっている .pkg ファイルは tar で固められた圧縮ファイルであり、次のコマンドで解凍できます:
$ tar xvf package.tar.xz
If you want to extract a couple of files out of a .pkg file, this would be a way to do it.
Viewing a single file inside a .pkg file
For example, if you want to see the contents of /etc/systemd/logind.conf supplied within the systemd package:
$ tar -xOf /var/cache/pacman/pkg/systemd-204-3-x86_64.pkg.tar.xz etc/systemd/logind.conf
Or you can use vim, then browse the archive:
$ vim /var/cache/pacman/pkg/systemd-204-3-x86_64.pkg.tar.xz