Skip to content

Instantly share code, notes, and snippets.

@ssato
Last active May 2, 2017 02:23
Show Gist options
  • Save ssato/5bc6fcb318964c83fadcf478abc7ada2 to your computer and use it in GitHub Desktop.
Save ssato/5bc6fcb318964c83fadcf478abc7ada2 to your computer and use it in GitHub Desktop.
yum のリポジトリメタデータに含まれる RPM パッケージファイルのサイズ情報などからリポジトリ容量を概算
#! /bin/bash
#
# Copyright(C) 2016 Red Hat, Inc.
# Author: Satoru SATOH <[email protected]>
# License: MIT
#
# Example:
# [root@rhel-7-client-1 ~]# ./yum-repo-size-info-list.sh \
# > -r rhel-7-server-rpms -r rhel-7-server-optional-rpms \
# > -r rhel-7-server-rh-common-rpms -r rhel-7-server-extras-rpms \
# > -r rhel-7-server-supplementary-rpms -r rhel-server-rhscl-7-rpms
# Loaded plugins: changelog, product-id, subscription-manager
# repo id repo name status
# rhel-7-server-extras-rpms/x86_64 Red Hat Enterprise Linux 7 Server - Extras (RPMs) 188
# rhel-7-server-optional-rpms/7Server/x86_64 Red Hat Enterprise Linux 7 Server - Optional (RPMs) 8,516
# rhel-7-server-rh-common-rpms/7Server/x86_64 Red Hat Enterprise Linux 7 Server - RH Common (RPMs) 186
# rhel-7-server-rpms/7Server/x86_64 Red Hat Enterprise Linux 7 Server (RPMs) 10,697
# rhel-7-server-supplementary-rpms/7Server/x86_64
# Red Hat Enterprise Linux 7 Server - Supplementary (R 129
# rhel-server-rhscl-7-rpms/7Server/x86_64 Red Hat Software Collections RPMs for Red Hat Enterp 4,594
# repolist: 24,310
# Loaded plugins: changelog, product-id, subscription-manager
# rhel-7-server-extras-rpms | 2.9 kB 00:00:00
# rhel-7-server-optional-rpms | 3.5 kB 00:00:00
# rhel-7-server-rh-common-rpms | 3.8 kB 00:00:00
# rhel-7-server-rpms | 3.7 kB 00:00:00
# rhel-7-server-supplementary-rpms | 3.7 kB 00:00:00
# rhel-server-rhscl-7-rpms | 3.1 kB 00:00:00
# Metadata Cache Created
# [Info] rhel-7-server-rpms: 10697 rpms, 13658590872 bytes (rpms), 238828 bytes (metadata)
# rhel-7-server-rpms,10697,13658590872,238828,13658829700
# [Info] rhel-7-server-extras-rpms: 188 rpms, 417264100 bytes (rpms), 1532 bytes (metadata)
# rhel-7-server-extras-rpms,188,417264100,1532,417265632
# [Info] rhel-7-server-optional-rpms: 8516 rpms, 11226677420 bytes (rpms), 103260 bytes (metadata)
# rhel-7-server-optional-rpms,8516,11226677420,103260,11226780680
# [Info] rhel-7-server-rh-common-rpms: 186 rpms, 4807985928 bytes (rpms), 1508 bytes (metadata)
# rhel-7-server-rh-common-rpms,186,4807985928,1508,4807987436
# [Info] rhel-7-server-supplementary-rpms: 129 rpms, 3418212204 bytes (rpms), 3872 bytes (metadata)
# rhel-7-server-supplementary-rpms,129,3418212204,3872,3418216076
# [Info] rhel-server-rhscl-7-rpms: 4594 rpms, 4004597412 bytes (rpms), 68988 bytes (metadata)
# rhel-server-rhscl-7-rpms,4594,4004597412,68988,4004666400
# All repo data size: 37533745924 bytes = 35794 MB = 34 GB
# [root@rhel-7-client-1 ~]#
set -e
REPOS=""
YUM_REPO_DIR="/var/cache/yum"
YUM_REPO_ENABLE_OPTS=""
usage () {
cat << EOU
Usage: $0 [Options]
Options:
-r REPO Specify yum repo ID[s] to limit yum repos to get result.
It can be given multiple times.
Example:
$0 -r rhel-7-server-rpms -r rhel-7-server-optional-rpms
EOU
exit 0
}
while getopts "r:hv" OPT; do
case $OPT in
r) REPOS="$REPOS $OPTARG"
YUM_REPO_ENABLE_OPTS="${YUM_REPO_ENABLE_OPTS} --enablerepo=$OPTARG";;
\?|h) usage ;;
esac
done
shift $(( $OPTIND - 1 ))
# Primary DB path Examples:
# rhel-5: /var/cache/yum/*/*-primary*.sqlite
# rhel-6: /var/cache/yum/x86_64/6Server/*/*-primary*.sqlite
# rhel-7: /var/cache/yum/x86_64/7Server/*/gen/primary_db.sqlite
sum_of_data_size_min=0
if test "x${YUM_REPO_ENABLE_OPTS}" = "x"; then
yum repolist
yum makecache
else
yum repolist --disablerepo='*' ${YUM_REPO_ENABLE_OPTS}
yum makecache --disablerepo='*' ${YUM_REPO_ENABLE_OPTS}
fi
for repo_pkgdb in $(find ${YUM_REPO_DIR} -regex '.*primary.*\.sqlite$')
do
if test -d /etc/systemd; then
repo_name=${repo_pkgdb/\/gen*/}
repo_name=${repo_name##*/}
else
repo_name=$(echo ${repo_pkgdb} | sed -r "s,${YUM_REPO_DIR}/.+/.+/([^/]+)/.*,\1,")
fi
if test "x${REPOS}" != "x"; then
found=0
for repo in $REPOS; do
test "x${repo}" = "x${repo_name}" && found=1 || :
done
test ${found} -eq 1 || continue
fi
repo_num_of_rpms=$(sqlite3 ${repo_pkgdb} "select count(name) from packages")
repo_sum_of_rpmsize=$(sqlite3 ${repo_pkgdb} "select sum(size_package) from packages")
repo_metadata_size=$(du -s ${repo_pkgdb%/*} | sed -r 's/[[:blank:]].*//g')
repo_data_size_min=$(( ${repo_sum_of_rpmsize} + ${repo_metadata_size} ))
sum_of_data_size_min=$(( ${sum_of_data_size_min} + ${repo_data_size_min} ))
echo "[Info] ${repo_name}: ${repo_num_of_rpms} rpms, ${repo_sum_of_rpmsize} bytes (rpms), ${repo_metadata_size} bytes (metadata)"
echo "${repo_name},${repo_num_of_rpms},${repo_sum_of_rpmsize},${repo_metadata_size},${repo_data_size_min}"
done
echo "All repo data size: ${sum_of_data_size_min} bytes = $(( ${sum_of_data_size_min} / 1024 / 1024 )) MB = $(( ${sum_of_data_size_min} / 1024 / 1024 / 1024 )) GB"
# vim:sw=4:ts=4:et:
@ssato
Copy link
Author

ssato commented May 2, 2017

Maybe I should extend it to get size data from primary.xml.gz:

<?xml version="1.0" encoding="UTF-8"?>
<metadata packages="10675" xmlns="http://linux.duke.edu/metadata/common" xmlns:rpm="http://linux.duke.edu/metadata/rpm"><package type="rpm">
  <name>at-sysvinit</name>
             ...
  <size package="6440" installed="2122" archive="2380"/><!-- size[@package] is size of the RPM package file -->
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment