FedoraのRISC-V portを試す

FedoraRISC-VポートはPorting Fedora to RISC-Vで記事になってて気になってたのと、RISC-V原典オープンアーキテクチャのススメを買ったしarch/riscvを読んでみようかな〜ということでqemuで動かしてみました。

(´-`).。oO(FedoraRISC-V関連の情報はArchitectures/RISC-V - Fedora Project Wikiに集まるんだと思います。

qemuのセットアップ

FedoraのドキュメントとしてはArchitectures/RISC-V/Installing - Fedora Project Wikiだと思うんですが、ナイトリービルドのサーバが繋がらなくなってました。ということで、QEMUDocumentation/Platforms/RISCV - QEMUを参考にしました。Fedoraqemurisc-vが使えるのでqemu-system-riscvパッケージをインストールするだけでOKです。

あとはドキュメントの通りでできます。

boot

起動もドキュメントの通りにやればOKです。こんな感じでログインプロンプトがでます。

f:id:masami256:20181231134107p:plain
riscv

初期状態でsshdが動いていて、rootでのsshも可能になっています。

RISC-V原典  オープンアーキテクチャのススメ

RISC-V原典 オープンアーキテクチャのススメ

Linuxカーネルソースに付属のscripts/の探索

この記事はLinux Advent Calendar 2018の12日目の記事です。

カーネルソースコードに含まれるscripts/を探索してみます。カーネルのビルド中で使うようなものもあるし、それ以外の時に使うものもあります。今回は後者の方を探してみましょう。

diffconfig

ファイルはsrcipts/diffconfigです。カーネルの.configのdiffを見やすくするツールです。

普通にdiffを取るとこんな感じです。

masami@saga:~$ diff -u /boot/config-4.19.9-300.fc29.x86_64 /boot/config-4.19.12-301.fc29.x86_64
--- /boot/config-4.19.9-300.fc29.x86_64 2018-12-14 02:44:50.000000000 +0900
+++ /boot/config-4.19.12-301.fc29.x86_64        2018-12-24 11:20:56.000000000 +0900
@@ -1,10 +1,10 @@
 #
 # Automatically generated file; DO NOT EDIT.
-# Linux/x86_64 4.19.9-300.fc29.x86_64 Kernel Configuration
+# Linux/x86_64 4.19.12-301.fc29.x86_64 Kernel Configuration
 #
 
 #
-# Compiler: gcc (GCC) 8.2.1 20181105 (Red Hat 8.2.1-5)
+# Compiler: gcc (GCC) 8.2.1 20181215 (Red Hat 8.2.1-6)
 #
 CONFIG_CC_IS_GCC=y
 CONFIG_GCC_VERSION=80201
@@ -20,7 +20,7 @@
 # CONFIG_COMPILE_TEST is not set
 CONFIG_LOCALVERSION=""
 # CONFIG_LOCALVERSION_AUTO is not set
-CONFIG_BUILD_SALT="4.19.9-300.fc29.x86_64"
+CONFIG_BUILD_SALT="4.19.12-301.fc29.x86_64"
 CONFIG_HAVE_KERNEL_GZIP=y
 CONFIG_HAVE_KERNEL_BZIP2=y
 CONFIG_HAVE_KERNEL_LZMA=y
@@ -93,6 +93,7 @@
 CONFIG_VIRT_CPU_ACCOUNTING=y
 CONFIG_VIRT_CPU_ACCOUNTING_GEN=y
 CONFIG_IRQ_TIME_ACCOUNTING=y
+CONFIG_HAVE_SCHED_AVG_IRQ=y
 CONFIG_BSD_PROCESS_ACCT=y
 CONFIG_BSD_PROCESS_ACCT_V3=y
 CONFIG_TASKSTATS=y
@@ -4655,7 +4656,7 @@
 # CONFIG_DVB_MMAP is not set
 CONFIG_DVB_NET=y
 CONFIG_TTPCI_EEPROM=m
-CONFIG_DVB_MAX_ADAPTERS=8
+CONFIG_DVB_MAX_ADAPTERS=16
 CONFIG_DVB_DYNAMIC_MINORS=y
 # CONFIG_DVB_DEMUX_SECTION_LOSS_LOG is not set
 # CONFIG_DVB_ULE_DEBUG is not set

diifconfigを使うとこのようになります。

masami@saga:~$ ./linux-kernel/scripts/diffconfig /boot/config-4.19.9-300.fc29.x86_64 /boot/config-4.19.12-301.fc29.x86_64
 BUILD_SALT "4.19.9-300.fc29.x86_64" -> "4.19.12-301.fc29.x86_64"
 DVB_MAX_ADAPTERS 8 -> 16
+HAVE_SCHED_AVG_IRQ y

通常のdiffと違って何がどう変わったのかが見やすくなりますね。

objdiff

ファイルはscripts/objdiffです。これはobjectファイル間のdiffを取ります。カーネル以外でも使えます。

例えば、コミット間で以下のようなファイルの変更が有った場合、

masami@saga:~/codes/objdiff-test (test %)$ git diff master a.c
diff --git a/a.c b/a.c
index ab39f2e..a0068b8 100644
--- a/a.c
+++ b/a.c
@@ -7,7 +7,7 @@ struct test {
 int main(int argc, char **argv)
 {
        struct test t = {
-               .s = "hello",
+               .s = "hello\n",
        };
 
        printf("%s\n", t.s);

こんな感じになります。

masami@saga:~/codes/objdiff-test (test %)$ ~/linux-kernel/scripts/objdiff diff
which: no colordiff in (/usr/share/Modules/bin:/usr/lib64/ccache:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/home/masami/bin:/home/masami/.gem/ruby/2.2.0/bin:/home/masami/go/bin:/home/masami/ltp:/home/masami/ltp/bin:/home/masami/.local/bin:/home/masami/bin:/home/masami/bin:/home/masami/.gem/ruby/2.2.0/bin:/home/masami/go/bin:/home/masami/ltp:/home/masami/ltp/bin:/home/masami/.local/bin:/home/masami/bin)
diff -Nurd /home/masami/codes/objdiff-test/.tmp_objdiff/b5b839d/a.dis /home/masami/codes/objdiff-test/.tmp_objdiff/374a3c2/a.dis
--- /home/masami/codes/objdiff-test/.tmp_objdiff/b5b839d/a.dis  2018-12-30 23:36:28.063937470 +0900
+++ /home/masami/codes/objdiff-test/.tmp_objdiff/374a3c2/a.dis  2018-12-30 23:37:13.345071254 +0900
@@ -10,8 +10,8 @@
 :      48 81 ec 10 01 00 00    sub    $0x110,%rsp
 :      89 bd fc fe ff ff       mov    %edi,-0x104(%rbp)
 :      48 89 b5 f0 fe ff ff    mov    %rsi,-0x110(%rbp)
-:      48 b8 68 65 6c 6c 6f    movabs $0x6f6c6c6568,%rax
-:      00 00 00 
+:      48 b8 68 65 6c 6c 6f    movabs $0xa6f6c6c6568,%rax
+:      0a 00 00 
 :      ba 00 00 00 00          mov    $0x0,%edx
 :      48 89 85 00 ff ff ff    mov    %rax,-0x100(%rbp)
 :      48 89 95 08 ff ff ff    mov    %rdx,-0xf8(%rbp)
Binary files /home/masami/codes/objdiff-test/.tmp_objdiff/b5b839d/a.stripped and /home/masami/codes/objdiff-test/.tmp_objdiff/374a3c2/a.stripped differ

bootgraph.pl

ファイルはscripts/bootgraph.plです。 カーネルの起動時にprintk.time=1とinitcall_debugをカーネルコマンドラインオプションに付けて起動する必要があります。dmesgの経過時間のところから何にどれだけ時間を使ったかを見る感じですね。

全体像はこんな感じです。

f:id:masami256:20181230224632p:plain
boot graph

拡大するとこうなってます。

f:id:masami256:20181230225324p:plain
bootgraph2

show_delta

ファイルはscripts/show_deltaです。 これも実行時間の計測ですね。前回のprintkの時刻から今回までの時間を表示してくれます。

('[0.280670 < 0.003444 >] pci 0000:00:06.0: reg 0x20: [io  0xc0a0-0xc0bf]\n',)
('[0.281820 < 0.001150 >] pci 0000:00:06.1: [8086:2935] type 00 class 0x0c0300\n',)
('[0.306576 < 0.000002 >] initcall xen_setup_shutdown_event+0x0/0x30 returned -19 after 0 usecs\n',)
('[0.307587 < 0.000000 >] calling  power_supply_class_init+0x0/0x40 @ 1\n',)
('[0.309641 < 0.000000 >] NetLabel:  domain hash size = 128\n',)
('[0.309642 < 0.000001 >] NetLabel:  protocols = UNLABELED CIPSOv4 CALIPSO\n',)
('[0.359181 < 0.000003 >] initcall proc_version_init+0x0/0x22 returned 0 after 1 usecs\n',)
('[0.359707 < 0.000090 >] pnp 00:02: Plug and Play ACPI device, IDs PNP0f13 (active)\n',)
('[0.365880 < 0.000002 >] calling  thermal_init+0x0/0xe1 @ 1\n',)
('[0.370580 < 0.000040 >] initcall intel_pmc_ipc_init+0x0/0x5f returned 0 after 37 usecs\n',)
('[0.370701 < 0.000006 >] initcall ipv4_offload_init+0x0/0x74 returned 0 after 0 usecs\n',)
('[0.890552 < 0.000001 >] calling  crypto_module_init+0x0/0x11 @ 1\n',)

faddr2line

ファイルはscripts/faddr2lineです。oopsにある関数名+オフセット/関数サイズからそれがどのファイルの何行目かを調べることができます。自作したカーネルモジュールでは使えなかったけど。。

こんな風に使えます。

masami@kerntest:~/a2l$ ~/linux-kernel/scripts/faddr2line ~/linux-kernel/vmlinux do_init_module+0x22/0x210
do_init_module+0x22/0x210:
do_init_module at kernel/module.c:3436

decode_stacktrace.sh

ファイルはscripts/decode_stacktrace.shです。カーネルoopsを読み込んでデコードしてくれます。使い方はlinux-3.16で入ったdecode_stacktrace.shを試してみる - φ(・・*)ゞ ウーン カーネルとか弄ったりのメモを参照してください。

decodecode

ファイルはscripts/decodecodeです。decode_stacktrace.shと似たような感じです。使い方はlinuxカーネルデバッグめも decodecodeでoopsの機械語列を逆アセンブル - φ(・・*)ゞ ウーン カーネルとか弄ったりのメモを参照してください。

ver_linux

ファイルはscripts/ver_linuxです。カーネル、ビルドとか諸々のツール類の情報表示します。

masami@saga:~/linux-kernel (test %)$ ./scripts/ver_linux 
If some fields are empty or look unusual you may have an old version.
Compare to the current minimal requirements in Documentation/Changes.

Linux saga 4.19.12-301.fc29.x86_64 #1 SMP Mon Dec 24 01:58:57 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

GNU Make                4.2.1
Binutils                2.31.1
Util-linux              2.32.1
Mount                   2.32.1
Module-init-tools       25
E2fsprogs               1.44.3
Xfsprogs                4.17.0
Quota-tools             4.04
PPP                     2.4.7
Nfs-utils               2.3.3
Linux C Library         2.28
Dynamic linker (ldd)    2.28
Linux C++ Library       6.0.25
Procps                  3.3.15
Net-tools               2.10
Kbd                     2.0.4
Console-tools           2.0.4
Sh-utils                8.30
Udev                    239
Wireless-tools          29
Modules Loaded          binfmt_misc bridge coretemp crc32c_intel crc32_pclmul crct10dif_pclmul devlink drm drm_kms_helper e1000e ebtable_filter ebtable_nat ebtables fat fuse ghash_clmulni_intel hid_logitech_dj hid_logitech_hidpp i2c_algo_bit i2c_i801 intel_cstate intel_powerclamp intel_rapl intel_rapl_perf intel_uncore intel_wmi_thunderbolt ip6table_filter ip6table_mangle ip6table_nat ip6table_raw ip6_tables ip6table_security ip6t_REJECT ip6t_rpfilter ip_set iptable_mangle iptable_nat iptable_raw iptable_security ipt_MASQUERADE irqbypass iTCO_vendor_support iTCO_wdt kvm kvm_intel libcrc32c llc lpc_ich mei mei_me mxm_wmi nf_conntrack nf_conntrack_broadcast nf_conntrack_netbios_ns nf_defrag_ipv4 nf_defrag_ipv6 nf_nat nf_nat_ipv4 nf_nat_ipv6 nfnetlink nf_reject_ipv6 nouveau pcc_cpufreq snd snd_hda_codec snd_hda_codec_ca0132 snd_hda_codec_hdmi snd_hda_core snd_hda_intel snd_hwdep snd_pcm snd_seq snd_seq_device snd_seq_dummy snd_timer soundcore stp sunrpc tap ttm tun uas usb_storage vfat vhost vhost_net video wmi x86_pkg_temp_thermal xfs xt_CHECKSUM xt_conntrack xt_CT

spelling.txt

ファイルはscripts/spelling.txtです。このファイルは単体で使うと言うか、checkpatch.plでスペルチェックに使っています。ファイルの書式はこんな感じで、左側がよく有る間違いで右側が正解です。

abov||above

まとめ

他にもスクリプトは色々有ります。たとえばscripts/gdbカーネルgdbデバッグする時のスクリプトで.gdbinitで add-auto-load-safe-path /home/masami/codes/slmb/scripts/gdb/vmlinux-gdb.py という感じに読み込ませます。

ということで、なんとなくscripts/を探索してみました( ´ー`)フゥー...

ハッキング・ラボのつくりかた 仮想環境におけるハッカー体験学習

ハッキング・ラボのつくりかた 仮想環境におけるハッカー体験学習

RHEL 8 betaのsrpmをリビルドしてmockでshellでログインできるところまで作る

この記事はLinux Advent Calendarの13日目です。

いんとろ

Powering IT’s future while preserving the present: Introducing Red Hat Enterprise Linux 8 Beta キタ━━━━(゚∀゚)━━━━!!って感じですよね。

srpmも公開されています。Index of /redhat/rhel/rhel-8-beta/baseos/source/PackagesIndex of /redhat/rhel/rhel-8-beta/appstream/source/Packagesにあります。BaseOSとかAppstreamとは?といくところはIntroducing Application Streams in RHEL 8 - RHD Blogとかを参照してもらえればokかと思います。

今回はrhel8以外の環境でrhel8のsrpmをリビルドしてrhelクローン的なものを作って遊ぼうではないかというところになります。

ビルド環境

ホストはfedora 29でビルド環境としてはmockのfedora 28にしました。RHEL 8はfedora 28ベースらしい(8.0 BETA RELEASE NOTES)のでfedora 28でいいかーって感じです。

ちなみに、mockrpmパッケージをクリーンな環境でビルドするためのツールです。意味合い的にはdebianpbuilderに近いかも?mockはchrootの環境にsystemd-nspawnを使ってます。

ビルド手順

手順としてはこういう感じになります。最初はfedora 28の環境でパッケージをビルドして、それ以降はリビルドしたパッケージを使ってmock環境を作ってビルドしていくというブートストラップ方式です。

  1. mockに必要なパッケージをダウンロードしてくる
  2. 最初はfedora 28環境でrhel 8 betaのsrpmをビルドしてrpmパッケージとsrpmパッケージを作ります
  3. 1で作ったrpmパッケージでmock環境を作り、そこで1で作ったsrpmをビルドします
  4. 2で作ったrpmパッケージで2で作ったsrpmをリビルド

今回は2の途中までしか出来てません。

パッケージのビルド

stage 0

パッケージの確認

最初に最低限のビルド環境を作る必要があるので、この環境に必要なパッケージのsrpmをダウンロードします。必要なパッケージが何か調べる方法ですがmockを使いました。fedora 29に入ってるmockパッケージだとmock環境としてrhel 8 betaの環境があります。なので、この環境を作ってその時にインストールされるパッケージを確認しました。

masami@saga:~/codes/rhel_rebuild/stage0$ ls /etc/mock/
./                    default.cfg@         fedora-27-aarch64.cfg  fedora-28-ppc64.cfg    fedora-29-x86_64.cfg        fedora-rawhide-i386.cfg     mageia-cauldron-aarch64.cfg      rhelbeta-8-aarch64.cfg
../                   default.cfg.rpmnew@  fedora-27-armhfp.cfg   fedora-28-ppc64le.cfg  fedora-30-aarch64.cfg@      fedora-rawhide-ppc64.cfg    mageia-cauldron-armv7hl.cfg      rhelbeta-8-ppc64le.cfg
custom-1-aarch64.cfg  eol/                 fedora-27-i386.cfg     fedora-28-s390x.cfg    fedora-30-armhfp.cfg@       fedora-rawhide-ppc64le.cfg  mageia-cauldron-i586.cfg         rhelbeta-8-s390x.cfg
custom-1-armhfp.cfg   epel-6-i386.cfg      fedora-27-ppc64.cfg    fedora-28-x86_64.cfg   fedora-30-i386.cfg@         fedora-rawhide-s390x.cfg    mageia-cauldron-x86_64.cfg       rhelbeta-8-x86_64.cfg
custom-1-i386.cfg     epel-6-ppc64.cfg     fedora-27-ppc64le.cfg  fedora-29-aarch64.cfg  fedora-30-ppc64.cfg@        fedora-rawhide-x86_64.cfg   opensuse-leap-15.0-x86_64.cfg    site-defaults.cfg
custom-1-ppc64.cfg    epel-6-x86_64.cfg    fedora-27-s390x.cfg    fedora-29-armhfp.cfg   fedora-30-ppc64le.cfg@      logging.ini                 opensuse-tumbleweed-aarch64.cfg
custom-1-ppc64le.cfg  epel-7-aarch64.cfg   fedora-27-x86_64.cfg   fedora-29-i386.cfg     fedora-30-s390x.cfg@        mageia-6-armv5tl.cfg        opensuse-tumbleweed-i586.cfg
custom-1-s390.cfg     epel-7-ppc64.cfg     fedora-28-aarch64.cfg  fedora-29-ppc64.cfg    fedora-30-x86_64.cfg@       mageia-6-armv7hl.cfg        opensuse-tumbleweed-ppc64.cfg
custom-1-s390x.cfg    epel-7-ppc64le.cfg   fedora-28-armhfp.cfg   fedora-29-ppc64le.cfg  fedora-rawhide-aarch64.cfg  mageia-6-i586.cfg           opensuse-tumbleweed-ppc64le.cfg
custom-1-x86_64.cfg   epel-7-x86_64.cfg    fedora-28-i386.cfg     fedora-29-s390x.cfg    fedora-rawhide-armhfp.cfg   mageia-6-x86_64.cfg         opensuse-tumbleweed-x86_64.cfg

インストールされたパッケージ数は160パッケージほどでした。そして個々のrpmパッケージがそのsrpmパッケージから調べると123個ほどでした。

srpmのダウンロード

ということで、ここで必要なのはsrpmパッケージをダウンロードしてビルドするための準備をします。ダウンロードは以下のスクリプトで行いました。

#!/bin/bash

urls=$(cat ./rpm_list.txt)

rm -fr srpms
mkdir -p srpms/{baseos,appstream}

cd srpms

for url in $urls;
do
    name=$(basename $url)
    dl_dir="baseos"

    echo $url | grep appstream >/dev/null 2>&1
    if [ $? -eq 0 ]; then
       dl_dir="appstream"
    fi

    savefile="$dl_dir/$name"

    echo "[-]Download $name to $dl_dir"
    curl -L -o $savefile $url
done

rpm_list.txtは以下のようにsrpmパッケージのリンクを1行ずつ書いてるだけです。

http://downloads.redhat.com/redhat/rhel/rhel-8-beta/baseos/source/Packages/acl-2.2.53-1.el8.src.rpm
http://downloads.redhat.com/redhat/rhel/rhel-8-beta/baseos/source/Packages/attr-2.4.48-3.el8.src.rpm

そして次のスクリプトでダウンロードしたsrpmパッケージを展開して、個々のsrpmパッケージに含まれるファイルをgitで管理するようにしてます。

#!/bin/bash

pkgrepo="../pkgrepo"

rm -fr $pkgrepo
mkdir -p $pkgrepo/{baseos,appstream}

topdir=$(pwd)

setup_dir() {
    path=$1

    dir="srpms/$path"
    srpms=$(ls $dir)

    for srpm in $srpms;
    do
        spec=$(rpm -ql $dir/$srpm | grep '.spec$')
        name="${spec%.*}"
        echo $name

        pushd . >/dev/null 2>&1

        cd $pkgrepo/$path
        mkdir $name

        cd $name
        git init
        rpm2cpio ../../../stage0/srpms/$path/$srpm | cpio -iv
        git add .
        git commit -m "initial commit"

        popd >/dev/null 2>&1
    done

}

setup_dir "baseos"
setup_dir "appstream"

ここはmockとpackageのgitリポジトリとリビルド - φ(・・*)ゞ ウーン カーネルとか弄ったりのメモのおまけで紹介したことをやってます。

ここまでで各srpmパッケージについてspecとかパッチなんかをgitで管理できるようになりました。

stage 1

ビルド環境設定

次はパッケージをfedora 28のmock環境でビルドしていきます。最初にsrpmを作って次にrpmを作る流れです。この時にmockに渡すconfigファイルを自前で用意します。と言っても/etc/mockにあるfedora 28のファイルを名前を変えてコピーするだけなのでなくても良いんですが。それとmacros.distファイルを使います。これはそのままビルドするとディストリビューション名がfc28とかになってしまうのでこれを変更するためです。 macros.distはこんな感じです。

# dist macros.

%foobar              8
%dist                %{?distprefix}.fb8
%fb8                1

srpmの作成

srpmのリビルドはこんなスクリプトで行います。

#!/bin/bash

pkg_gitrepo="../pkg_gitrepo"
mockcfg="./foobar.cfg"
macrofile="./macros.dist"

pkgkind=("baseos" "appstream")

resultdir_root="./srpms"

if [ -d $resultdir_root ]; then
    rm -fr $resultdir_root
fi

mkdir -p $resultdir_root/{baseos,appstream}

for kind in ${pkgkind[@]};
do
    packages=$(echo $pkg_gitrepo/$kind/*)

    for pkgdir in ${packages[@]};
    do
        name=$(basename $pkgdir)
        resultdir="$resultdir_root/$kind/"

        echo "[-]$(date) Create $name srpm"
        mock -r $mockcfg --resultdir=$resultdir --macro-file=$macrofile --buildsrpm --spec $pkgdir/${name}.spec --sources $pkgdir >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            echo "[-]$(date) Success build $name srpm"
        else
            echo "[*]$(date) Fail build $name srpm"
        fi
        echo "[-]$(date) End $name srpm"
    done
done

find srpms/ -name "*.log" | xargs rm -f

echo "[-]$(date) Done."

rpmパッケージの作成

そして作成したsrpmをビルドします。

#!/bin/bash

pkg_gitrepo="../pkg_gitrepo"
mockcfg="./foobar.cfg"
macrofile="./macros.dist"

pkgkind=("baseos" "appstream")

srpmsdir="./srpms"
resultdir_root="./rpms"

if [ -d $resultdir_root ]; then
    rm -fr $resultdir_root
fi

mkdir -p $resultdir_root/{baseos,appstream}

echo "[-]$(date) build start"

for kind in ${pkgkind[@]};
do
    packages=$(echo $srpmsdir/$kind/*)

    for pkg in ${packages[@]};
    do
        srcrpm=$(basename $pkg)

        spec=$(rpm -ql $pkg | grep '.spec$')
        pkgname="${spec%.*}"

        echo "[-]$(date) Build $pkgname start"

        resultdir="$resultdir_root/$kind/$pkgname"
        mkdir -p $resultdir

        mock -r $mockcfg --resultdir=$resultdir --macro-file=$macrofile --nocheck --rebuild $pkg >/dev/null 2>&1
        ls $resultdir/*.rpm >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            echo "[-]$(date) Rebuild $pkgname success"
        else
            echo "[*]$(date) Rebuild $pkgname failed"
        fi
        echo "[-]$(date) Build $pkgname end"
    done
done

echo "[-]$(date) End"

これでビルドに成功したパッケージはヽ(=´▽`=)ノという感じで、失敗したものは原因を調べてspecファイルを修正するなどの作業が必要です。まあ。specファイルの修正があるのでgit管理できるようにしたんですね(ノω・)テヘ 単純にビルド成功させるためとしては5、6個のパッケージのspecファイルを修正したのと、いくつかのパッケージはrhel 8 betaのbinutilsが必要だったりしました。

binutilsの方はmockでビルドする前に--installオプションでリビルドしたbinutilsをインストールしてから--nocleanオプションを付けて事前に環境を綺麗にしないでビルドさせるようにしてパッケージのビルドを行いました。

こんな感じで全部のパッケージをビルドしました。

dnfのリポジトリ作成

createrepoコマンドでサクッと作りました。作り方としては、BaseOSとAppstreamは分ける必要が有るので分けてます。ディレクトリ構成としてはこんな感じにしました。repodata/はPackagesやsourcesの中に有る形です。

pkgrepo_for_stage2     ----    baseos    ----    x86_64    ----    Packages
                                     |                       |---     sources
                                     |---    appstream    ----      x86_64    ----    Packages
                                                                  |---       source

stage 2

mockの設定ファイル

stage 1で作ったパッケージを使った環境でmockを使えるようにします。ここでもやっとmock用の設定ファイルをちゃんと使うようになります。foobar.cfgとして以下のようにしました。パッケージグループは作ってないのでパッケージはベタに書いてます(´・ω・`)

config_opts['root'] = 'foobar8'
config_opts['target_arch'] = 'x86_64'
config_opts['legal_host_arches'] = ('x86_64',)
#config_opts['chroot_setup_cmd'] = 'install @buildsys-build'
config_opts['chroot_setup_cmd'] = 'install bash bzip2 coreutils cpio diffutils findutils gawk grep gzip info make patch redhat-release sed shadow-utils tar unzip util-linux which xz rpm binutils glibc-devel libgomp isl libmpc cpp'
config_opts['dist'] = 'fb8'  # only useful for --resultdir variable subst
config_opts['extra_chroot_dirs'] = [ '/run/lock', ]
config_opts['releasever'] = 'fb'
config_opts['package_manager'] = 'dnf'

config_opts['yum.conf'] = """
[main]
keepcache=1
debuglevel=2
reposdir=/dev/null
logfile=/var/log/yum.log
retries=20
obsoletes=1
gpgcheck=0
assumeyes=1
syslog_ident=mock
syslog_device=
install_weak_deps=0
metadata_expire=0
mdpolicy=group:primary
best=1

# repos

[foobar-base]
name=Foobar BaseOS
baseurl=file:///home/masami/rhel_rebuild/pkgrepo_for_stage2/baseos/x86_64/Packages
enabled=1
gpgcheck=0

[foobar-appstream]
name=Foobar Appstream
baseurl=file:///home/masami/rhel_rebuild/pkgrepo_for_stage2/appstream/x86_64/Packages
enabled=1
gpgcheck=0

[foobar-base-source]
name=Foobar BaseOS source
baseurl=file:///home/masami/rhel_rebuild/pkgrepo_for_stage2/baseos/source
enabled=0

[foobar-base-source]
name=Foobar BaseOS source
baseurl=file:///home/masami/rhel_rebuild/pkgrepo_for_stage2/appstream/source
enabled=0

mock環境にログイン

これを使ってmock環境にログインすることができます。

masami@rhel-build:~/rhel_rebuild/stage2$ mock -r ./foobar.cfg --shell /bin/bash
INFO: mock.py version 1.4.13 starting (python version = 3.7.1)...
Start: init plugins
INFO: selinux disabled
Finish: init plugins
Start: run
Start: chroot init
INFO: calling preinit hooks
INFO: enabled root cache
INFO: enabled dnf cache
Start: cleaning dnf metadata
Finish: cleaning dnf metadata
INFO: enabled HW Info plugin
Start: dnf install
Foobar Appstream                                                                                                                                                                    18 kB/s |  36 kB     00:02
Foobar BaseOS                                                                                                                                                                      491 kB/s | 1.0 MB     00:02
Dependencies resolved.
===================================================================================================================================================================================================================
 Package                                             Arch                                   Version                                                         Repository                                        Size
===================================================================================================================================================================================================================
Installing:
 isl                                                 x86_64                                 0.16.1-6.fb8                                                    foobar-appstream                                 841 k
 libmpc                                              x86_64                                 1.0.2-9.fb8                                                     foobar-appstream                                  57 k
 bash                                                x86_64                                 4.4.19-6.fb8                                                    foobar-base                                      1.5 M
 binutils                                            x86_64                                 2.30-49.fb8                                                     foobar-base                                      5.6 M
 bzip2                                               x86_64                                 1.0.6-26.fb8                                                    foobar-base                                       59 k
 coreutils                                           x86_64                                 8.30-4.fb8                                                      foobar-base                                      1.2 M
 cpio                                                x86_64                                 2.12-8.fb8                                                      foobar-base                                      264 k
 cpp                                                 x86_64                                 8.2.1-3.3.fb8                                                   foobar-base                                       10 M
 diffutils                                           x86_64                                 3.6-5.fb8                                                       foobar-base                                      358 k
 findutils                                           x86_64                                 1:4.6.0-19.fb8                                                  foobar-base                                      526 k
 gawk                                                x86_64                                 4.2.1-1.fb8                                                     foobar-base                                      1.1 M
 glibc-devel                                         x86_64                                 2.28-18.fb8                                                     foobar-base                                      1.0 M
 grep                                                x86_64                                 3.1-6.fb8                                                       foobar-base                                      272 k
 gzip                                                x86_64                                 1.9-4.fb8                                                       foobar-base                                      164 k
 info                                                x86_64                                 6.5-4.fb8                                                       foobar-base                                      197 k
 libgomp                                             x86_64                                 8.2.1-3.3.fb8                                                   foobar-base                                      187 k
 make                                                x86_64                                 1:4.2.1-9.fb8                                                   foobar-base                                      496 k
 patch                                               x86_64                                 2.7.6-7.fb8                                                     foobar-base                                      136 k
 redhat-release                                      x86_64                                 8.0-0.34.fb8                                                    foobar-base                                       44 k
 rpm                                                 x86_64                                 4.14.2-4.fb8                                                    foobar-base                                      534 k
 sed                                                 x86_64                                 4.5-1.fb8                                                       foobar-base                                      296 k
 shadow-utils                                        x86_64                                 2:4.6-4.fb8                                                     foobar-base                                      1.2 M
 tar                                                 x86_64                                 2:1.30-4.fb8                                                    foobar-base                                      837 k
 unzip                                               x86_64                                 6.0-38.fb8                                                      foobar-base                                      190 k
 util-linux                                          x86_64                                 2.32.1-6.fb8                                                    foobar-base                                      2.5 M
 which                                               x86_64                                 2.21-10.fb8                                                     foobar-base                                       47 k
 xz                                                  x86_64                                 5.2.4-2.fb8                                                     foobar-base                                      151 k
Installing dependencies:
 audit-libs                                          x86_64                                 3.0-0.5.20180831git0047a6c.fb8                                  foobar-base                                      114 k
 basesystem                                          noarch                                 11-5.fb8                                                        foobar-base                                      9.3 k
 bzip2-libs                                          x86_64                                 1.0.6-26.fb8                                                    foobar-base                                       47 k
 ca-certificates                                     noarch                                 2018.2.24-6.fb8                                                 foobar-base                                      393 k
 chkconfig                                           x86_64                                 1.11-1.fb8                                                      foobar-base                                      188 k
 coreutils-common                                    x86_64                                 8.30-4.fb8                                                      foobar-base                                      2.0 M
 cracklib                                            x86_64                                 2.9.6-14.fb8                                                    foobar-base                                       92 k
 crypto-policies                                     noarch                                 20181026-1.gitcc78cb7.fb8                                       foobar-base                                       48 k
 curl                                                x86_64                                 7.61.1-5.fb8                                                    foobar-base                                      350 k
 elfutils-libelf                                     x86_64                                 0.174-1.fb8                                                     foobar-base                                      208 k
 expat                                               x86_64                                 2.2.5-3.fb8                                                     foobar-base                                      109 k
 filesystem                                          x86_64                                 3.8-2.fb8                                                       foobar-base                                      1.1 M
 glibc                                               x86_64                                 2.28-18.fb8                                                     foobar-base                                      3.7 M
 glibc-all-langpacks                                 x86_64                                 2.28-18.fb8                                                     foobar-base                                       25 M
 glibc-common                                        x86_64                                 2.28-18.fb8                                                     foobar-base                                      796 k
 glibc-headers                                       x86_64                                 2.28-18.fb8                                                     foobar-base                                      460 k
 gmp                                                 x86_64                                 1:6.1.2-8.fb8                                                   foobar-base                                      318 k
 kernel-headers                                      x86_64                                 4.18.0-32.fb8                                                   foobar-base                                      1.3 M
 keyutils-libs                                       x86_64                                 1.5.10-6.fb8                                                    foobar-base                                       32 k
 krb5-libs                                           x86_64                                 1.16.1-19.fb8                                                   foobar-base                                      842 k
 libacl                                              x86_64                                 2.2.53-1.fb8                                                    foobar-base                                       34 k
 libarchive                                          x86_64                                 3.3.2-3.fb8                                                     foobar-base                                      356 k
 libattr                                             x86_64                                 2.4.48-3.fb8                                                    foobar-base                                       26 k
 libblkid                                            x86_64                                 2.32.1-6.fb8                                                    foobar-base                                      211 k
 libcap                                              x86_64                                 2.25-9.fb8                                                      foobar-base                                       55 k
 libcap-ng                                           x86_64                                 0.7.9-3.fb8                                                     foobar-base                                       32 k
 libcom_err                                          x86_64                                 1.44.3-1.fb8                                                    foobar-base                                       47 k
 libcurl-minimal                                     x86_64                                 7.61.1-5.fb8                                                    foobar-base                                      279 k
 libdb                                               x86_64                                 5.3.28-33.fb8                                                   foobar-base                                      757 k
 libdb-utils                                         x86_64                                 5.3.28-33.fb8                                                   foobar-base                                      147 k
 libfdisk                                            x86_64                                 2.32.1-6.fb8                                                    foobar-base                                      246 k
 libffi                                              x86_64                                 3.1-17.fb8                                                      foobar-base                                       35 k
 libgcc                                              x86_64                                 8.2.1-3.3.fb8                                                   foobar-base                                       75 k
 libgcrypt                                           x86_64                                 1.8.3-2.fb8                                                     foobar-base                                      457 k
 libgpg-error                                        x86_64                                 1.31-1.fb8                                                      foobar-base                                      240 k
 libmetalink                                         x86_64                                 0.1.3-7.fb8                                                     foobar-base                                       31 k
 libmount                                            x86_64                                 2.32.1-6.fb8                                                    foobar-base                                      228 k
 libnghttp2                                          x86_64                                 1.33.0-1.fb8                                                    foobar-base                                       76 k
 libnsl2                                             x86_64                                 1.2.0-2.20180605git4a062cf.fb8                                  foobar-base                                       56 k
 libpkgconf                                          x86_64                                 1.4.2-1.fb8                                                     foobar-base                                       33 k
 libpwquality                                        x86_64                                 1.4.0-8.fb8                                                     foobar-base                                      101 k
 libselinux                                          x86_64                                 2.8-5.fb8                                                       foobar-base                                      177 k
 libsemanage                                         x86_64                                 2.8-3.1.fb8                                                     foobar-base                                      160 k
 libsepol                                            x86_64                                 2.8-1.fb8                                                       foobar-base                                      337 k
 libsigsegv                                          x86_64                                 2.11-5.fb8                                                      foobar-base                                       29 k
 libsmartcols                                        x86_64                                 2.32.1-6.fb8                                                    foobar-base                                      171 k
 libstdc++                                           x86_64                                 8.2.1-3.3.fb8                                                   foobar-base                                      446 k
 libtasn1                                            x86_64                                 4.13-3.fb8                                                      foobar-base                                       75 k
 libtirpc                                            x86_64                                 1.1.4-3.fb8                                                     foobar-base                                      111 k
 libutempter                                         x86_64                                 1.1.6-14.fb8                                                    foobar-base                                       30 k
 libuuid                                             x86_64                                 2.32.1-6.fb8                                                    foobar-base                                       91 k
 libverto                                            x86_64                                 0.3.0-5.fb8                                                     foobar-base                                       23 k
 libxcrypt                                           x86_64                                 4.1.1-4.fb8                                                     foobar-base                                       72 k
 libxcrypt-devel                                     x86_64                                 4.1.1-4.fb8                                                     foobar-base                                       24 k
 libxml2                                             x86_64                                 2.9.7-5.fb8                                                     foobar-base                                      694 k
 lua-libs                                            x86_64                                 5.3.4-10.fb8                                                    foobar-base                                      117 k
 lz4-libs                                            x86_64                                 1.8.1.2-4.fb8                                                   foobar-base                                       54 k
 mpfr                                                x86_64                                 3.1.6-1.fb8                                                     foobar-base                                      220 k
 ncurses                                             x86_64                                 6.1-5.20180224.fb8                                              foobar-base                                      377 k
 ncurses-base                                        noarch                                 6.1-5.20180224.fb8                                              foobar-base                                       79 k
 ncurses-libs                                        x86_64                                 6.1-5.20180224.fb8                                              foobar-base                                      333 k
 openssl-libs                                        x86_64                                 1:1.1.1-6.fb8                                                   foobar-base                                      1.4 M
 p11-kit                                             x86_64                                 0.23.14-2.fb8                                                   foobar-base                                      270 k
 p11-kit-trust                                       x86_64                                 0.23.14-2.fb8                                                   foobar-base                                      137 k
 pam                                                 x86_64                                 1.3.1-3.fb8                                                     foobar-base                                      741 k
 pcre                                                x86_64                                 8.42-4.fb8                                                      foobar-base                                      207 k
 pcre2                                               x86_64                                 10.31-11.fb8                                                    foobar-base                                      237 k
 pkgconf                                             x86_64                                 1.4.2-1.fb8                                                     foobar-base                                       37 k
 pkgconf-m4                                          noarch                                 1.4.2-1.fb8                                                     foobar-base                                       16 k
 pkgconf-pkg-config                                  x86_64                                 1.4.2-1.fb8                                                     foobar-base                                       14 k
 popt                                                x86_64                                 1.16-14.fb8                                                     foobar-base                                       60 k
 readline                                            x86_64                                 7.0-10.fb8                                                      foobar-base                                      198 k
 rpm-libs                                            x86_64                                 4.14.2-4.fb8                                                    foobar-base                                      331 k
 rpm-plugin-selinux                                  x86_64                                 4.14.2-4.fb8                                                    foobar-base                                       69 k
 setup                                               noarch                                 2.12.2-1.fb8                                                    foobar-base                                      179 k
 systemd-libs                                        x86_64                                 239-8.fb8                                                       foobar-base                                      524 k
 tzdata                                              noarch                                 2018e-2.fb8                                                     foobar-base                                      458 k
 xz-libs                                             x86_64                                 5.2.4-2.fb8                                                     foobar-base                                       93 k
 zlib                                                x86_64                                 1.2.11-10.fb8                                                   foobar-base                                      100 k

Transaction Summary
===================================================================================================================================================================================================================
Install  106 Packages

Total size: 79 M
Installed size: 378 M
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Running scriptlet: filesystem-3.8-2.fb8.x86_64                                                                                                                                                               1/1
  Preparing        :                                                                                                                                                                                           1/1
  Installing       : libgcc-8.2.1-3.3.fb8.x86_64                                                                                                                                                             1/106
  Running scriptlet: libgcc-8.2.1-3.3.fb8.x86_64                                                                                                                                                             1/106
  Installing       : tzdata-2018e-2.fb8.noarch                                                                                                                                                               2/106
  Installing       : redhat-release-8.0-0.34.fb8.x86_64                                                                                                                                                      3/106
  Installing       : setup-2.12.2-1.fb8.noarch                                                                                                                                                               4/106
  Running scriptlet: setup-2.12.2-1.fb8.noarch                                                                                                                                                               4/106
warning: /etc/hosts created as /etc/hosts.rpmnew

  Installing       : filesystem-3.8-2.fb8.x86_64                                                                                                                                                             5/106
  Installing       : basesystem-11-5.fb8.noarch                                                                                                                                                              6/106
  Installing       : pkgconf-m4-1.4.2-1.fb8.noarch                                                                                                                                                           7/106
  Installing       : ncurses-base-6.1-5.20180224.fb8.noarch                                                                                                                                                  8/106
  Installing       : pcre2-10.31-11.fb8.x86_64                                                                                                                                                               9/106
  Installing       : libselinux-2.8-5.fb8.x86_64                                                                                                                                                            10/106
  Installing       : ncurses-libs-6.1-5.20180224.fb8.x86_64                                                                                                                                                 11/106
  Installing       : glibc-all-langpacks-2.28-18.fb8.x86_64                                                                                                                                                 12/106
  Installing       : glibc-common-2.28-18.fb8.x86_64                                                                                                                                                        13/106
  Running scriptlet: glibc-2.28-18.fb8.x86_64                                                                                                                                                               14/106
  Installing       : glibc-2.28-18.fb8.x86_64                                                                                                                                                               14/106
  Running scriptlet: glibc-2.28-18.fb8.x86_64                                                                                                                                                               14/106
  Installing       : bash-4.4.19-6.fb8.x86_64                                                                                                                                                               15/106
  Running scriptlet: bash-4.4.19-6.fb8.x86_64                                                                                                                                                               15/106
  Installing       : libsepol-2.8-1.fb8.x86_64                                                                                                                                                              16/106
  Running scriptlet: libsepol-2.8-1.fb8.x86_64                                                                                                                                                              16/106
  Installing       : zlib-1.2.11-10.fb8.x86_64                                                                                                                                                              17/106
  Installing       : info-6.5-4.fb8.x86_64                                                                                                                                                                  18/106
  Installing       : bzip2-libs-1.0.6-26.fb8.x86_64                                                                                                                                                         19/106
  Installing       : xz-libs-5.2.4-2.fb8.x86_64                                                                                                                                                             20/106
  Installing       : gmp-1:6.1.2-8.fb8.x86_64                                                                                                                                                               21/106
  Running scriptlet: gmp-1:6.1.2-8.fb8.x86_64                                                                                                                                                               21/106
  Installing       : libcap-2.25-9.fb8.x86_64                                                                                                                                                               22/106
  Installing       : libdb-5.3.28-33.fb8.x86_64                                                                                                                                                             23/106
  Running scriptlet: libdb-5.3.28-33.fb8.x86_64                                                                                                                                                             23/106
  Installing       : libattr-2.4.48-3.fb8.x86_64                                                                                                                                                            24/106
  Installing       : libacl-2.2.53-1.fb8.x86_64                                                                                                                                                             25/106
  Installing       : sed-4.5-1.fb8.x86_64                                                                                                                                                                   26/106
  Running scriptlet: sed-4.5-1.fb8.x86_64                                                                                                                                                                   26/106
  Installing       : libuuid-2.32.1-6.fb8.x86_64                                                                                                                                                            27/106
  Running scriptlet: libuuid-2.32.1-6.fb8.x86_64                                                                                                                                                            27/106
  Installing       : libxcrypt-4.1.1-4.fb8.x86_64                                                                                                                                                           28/106
  Installing       : popt-1.16-14.fb8.x86_64                                                                                                                                                                29/106
  Installing       : mpfr-3.1.6-1.fb8.x86_64                                                                                                                                                                30/106
  Running scriptlet: mpfr-3.1.6-1.fb8.x86_64                                                                                                                                                                30/106
  Installing       : elfutils-libelf-0.174-1.fb8.x86_64                                                                                                                                                     31/106
  Installing       : libcom_err-1.44.3-1.fb8.x86_64                                                                                                                                                         32/106
  Running scriptlet: libcom_err-1.44.3-1.fb8.x86_64                                                                                                                                                         32/106
  Installing       : lua-libs-5.3.4-10.fb8.x86_64                                                                                                                                                           33/106
  Installing       : chkconfig-1.11-1.fb8.x86_64                                                                                                                                                            34/106
  Installing       : libcap-ng-0.7.9-3.fb8.x86_64                                                                                                                                                           35/106
  Installing       : audit-libs-3.0-0.5.20180831git0047a6c.fb8.x86_64                                                                                                                                       36/106
  Running scriptlet: audit-libs-3.0-0.5.20180831git0047a6c.fb8.x86_64                                                                                                                                       36/106
  Installing       : libffi-3.1-17.fb8.x86_64                                                                                                                                                               37/106
  Installing       : p11-kit-0.23.14-2.fb8.x86_64                                                                                                                                                           38/106
  Running scriptlet: p11-kit-0.23.14-2.fb8.x86_64                                                                                                                                                           38/106
  Installing       : lz4-libs-1.8.1.2-4.fb8.x86_64                                                                                                                                                          39/106
  Installing       : libsemanage-2.8-3.1.fb8.x86_64                                                                                                                                                         40/106
  Installing       : libmpc-1.0.2-9.fb8.x86_64                                                                                                                                                              41/106
  Running scriptlet: libmpc-1.0.2-9.fb8.x86_64                                                                                                                                                              41/106
  Installing       : libdb-utils-5.3.28-33.fb8.x86_64                                                                                                                                                       42/106
  Installing       : libxml2-2.9.7-5.fb8.x86_64                                                                                                                                                             43/106
  Installing       : coreutils-common-8.30-4.fb8.x86_64                                                                                                                                                     44/106
  Running scriptlet: coreutils-common-8.30-4.fb8.x86_64                                                                                                                                                     44/106
  Installing       : readline-7.0-10.fb8.x86_64                                                                                                                                                             45/106
  Running scriptlet: readline-7.0-10.fb8.x86_64                                                                                                                                                             45/106
  Installing       : expat-2.2.5-3.fb8.x86_64                                                                                                                                                               46/106
  Installing       : libmetalink-0.1.3-7.fb8.x86_64                                                                                                                                                         47/106
  Installing       : keyutils-libs-1.5.10-6.fb8.x86_64                                                                                                                                                      48/106
  Installing       : libgpg-error-1.31-1.fb8.x86_64                                                                                                                                                         49/106
  Installing       : libgcrypt-1.8.3-2.fb8.x86_64                                                                                                                                                           50/106
  Running scriptlet: libgcrypt-1.8.3-2.fb8.x86_64                                                                                                                                                           50/106
  Installing       : libnghttp2-1.33.0-1.fb8.x86_64                                                                                                                                                         51/106
  Installing       : libpkgconf-1.4.2-1.fb8.x86_64                                                                                                                                                          52/106
  Installing       : pkgconf-1.4.2-1.fb8.x86_64                                                                                                                                                             53/106
  Installing       : pkgconf-pkg-config-1.4.2-1.fb8.x86_64                                                                                                                                                  54/106
  Installing       : libsigsegv-2.11-5.fb8.x86_64                                                                                                                                                           55/106
  Installing       : gawk-4.2.1-1.fb8.x86_64                                                                                                                                                                56/106
  Installing       : libsmartcols-2.32.1-6.fb8.x86_64                                                                                                                                                       57/106
  Running scriptlet: libsmartcols-2.32.1-6.fb8.x86_64                                                                                                                                                       57/106
  Installing       : libstdc++-8.2.1-3.3.fb8.x86_64                                                                                                                                                         58/106
  Running scriptlet: libstdc++-8.2.1-3.3.fb8.x86_64                                                                                                                                                         58/106
  Installing       : libtasn1-4.13-3.fb8.x86_64                                                                                                                                                             59/106
  Running scriptlet: libtasn1-4.13-3.fb8.x86_64                                                                                                                                                             59/106
  Installing       : p11-kit-trust-0.23.14-2.fb8.x86_64                                                                                                                                                     60/106
  Running scriptlet: p11-kit-trust-0.23.14-2.fb8.x86_64                                                                                                                                                     60/106
  Installing       : libverto-0.3.0-5.fb8.x86_64                                                                                                                                                            61/106
  Installing       : ncurses-6.1-5.20180224.fb8.x86_64                                                                                                                                                      62/106
  Installing       : pcre-8.42-4.fb8.x86_64                                                                                                                                                                 63/106
  Installing       : grep-3.1-6.fb8.x86_64                                                                                                                                                                  64/106
  Running scriptlet: grep-3.1-6.fb8.x86_64                                                                                                                                                                  64/106
  Installing       : openssl-libs-1:1.1.1-6.fb8.x86_64                                                                                                                                                      65/106
  Running scriptlet: openssl-libs-1:1.1.1-6.fb8.x86_64                                                                                                                                                      65/106
  Installing       : coreutils-8.30-4.fb8.x86_64                                                                                                                                                            66/106
  Installing       : crypto-policies-20181026-1.gitcc78cb7.fb8.noarch                                                                                                                                       67/106
  Running scriptlet: crypto-policies-20181026-1.gitcc78cb7.fb8.noarch                                                                                                                                       67/106
  Running scriptlet: ca-certificates-2018.2.24-6.fb8.noarch                                                                                                                                                 68/106
  Installing       : ca-certificates-2018.2.24-6.fb8.noarch                                                                                                                                                 68/106
  Running scriptlet: ca-certificates-2018.2.24-6.fb8.noarch                                                                                                                                                 68/106
  Installing       : libblkid-2.32.1-6.fb8.x86_64                                                                                                                                                           69/106
  Running scriptlet: libblkid-2.32.1-6.fb8.x86_64                                                                                                                                                           69/106
  Installing       : libmount-2.32.1-6.fb8.x86_64                                                                                                                                                           70/106
  Running scriptlet: libmount-2.32.1-6.fb8.x86_64                                                                                                                                                           70/106
  Installing       : krb5-libs-1.16.1-19.fb8.x86_64                                                                                                                                                         71/106
  Installing       : libtirpc-1.1.4-3.fb8.x86_64                                                                                                                                                            72/106
  Running scriptlet: libtirpc-1.1.4-3.fb8.x86_64                                                                                                                                                            72/106
  Installing       : libnsl2-1.2.0-2.20180605git4a062cf.fb8.x86_64                                                                                                                                          73/106
  Running scriptlet: libnsl2-1.2.0-2.20180605git4a062cf.fb8.x86_64                                                                                                                                          73/106
  Installing       : libcurl-minimal-7.61.1-5.fb8.x86_64                                                                                                                                                    74/106
  Installing       : curl-7.61.1-5.fb8.x86_64                                                                                                                                                               75/106
  Installing       : systemd-libs-239-8.fb8.x86_64                                                                                                                                                          76/106
  Running scriptlet: systemd-libs-239-8.fb8.x86_64                                                                                                                                                          76/106
  Installing       : libfdisk-2.32.1-6.fb8.x86_64                                                                                                                                                           77/106
  Running scriptlet: libfdisk-2.32.1-6.fb8.x86_64                                                                                                                                                           77/106
  Installing       : gzip-1.9-4.fb8.x86_64                                                                                                                                                                  78/106
  Running scriptlet: gzip-1.9-4.fb8.x86_64                                                                                                                                                                  78/106
  Installing       : cracklib-2.9.6-14.fb8.x86_64                                                                                                                                                           79/106
  Installing       : libpwquality-1.4.0-8.fb8.x86_64                                                                                                                                                        80/106
  Installing       : pam-1.3.1-3.fb8.x86_64                                                                                                                                                                 81/106
  Running scriptlet: pam-1.3.1-3.fb8.x86_64                                                                                                                                                                 81/106
  Installing       : shadow-utils-2:4.6-4.fb8.x86_64                                                                                                                                                        82/106
  Running scriptlet: libutempter-1.1.6-14.fb8.x86_64                                                                                                                                                        83/106
  Installing       : libutempter-1.1.6-14.fb8.x86_64                                                                                                                                                        83/106
  Installing       : libarchive-3.3.2-3.fb8.x86_64                                                                                                                                                          84/106
  Installing       : rpm-4.14.2-4.fb8.x86_64                                                                                                                                                                85/106
  Installing       : rpm-libs-4.14.2-4.fb8.x86_64                                                                                                                                                           86/106
  Running scriptlet: rpm-libs-4.14.2-4.fb8.x86_64                                                                                                                                                           86/106
  Installing       : rpm-plugin-selinux-4.14.2-4.fb8.x86_64                                                                                                                                                 87/106
  Installing       : kernel-headers-4.18.0-32.fb8.x86_64                                                                                                                                                    88/106
  Running scriptlet: glibc-headers-2.28-18.fb8.x86_64                                                                                                                                                       89/106
  Installing       : glibc-headers-2.28-18.fb8.x86_64                                                                                                                                                       89/106
  Installing       : libxcrypt-devel-4.1.1-4.fb8.x86_64                                                                                                                                                     90/106
  Installing       : glibc-devel-2.28-18.fb8.x86_64                                                                                                                                                         91/106
  Running scriptlet: glibc-devel-2.28-18.fb8.x86_64                                                                                                                                                         91/106
  Installing       : util-linux-2.32.1-6.fb8.x86_64                                                                                                                                                         92/106
  Running scriptlet: util-linux-2.32.1-6.fb8.x86_64                                                                                                                                                         92/106
  Installing       : binutils-2.30-49.fb8.x86_64                                                                                                                                                            93/106
  Running scriptlet: binutils-2.30-49.fb8.x86_64                                                                                                                                                            93/106
  Installing       : xz-5.2.4-2.fb8.x86_64                                                                                                                                                                  94/106
  Installing       : cpp-8.2.1-3.3.fb8.x86_64                                                                                                                                                               95/106
  Running scriptlet: cpp-8.2.1-3.3.fb8.x86_64                                                                                                                                                               95/106
  Installing       : tar-2:1.30-4.fb8.x86_64                                                                                                                                                                96/106
  Running scriptlet: tar-2:1.30-4.fb8.x86_64                                                                                                                                                                96/106
  Installing       : patch-2.7.6-7.fb8.x86_64                                                                                                                                                               97/106
  Installing       : isl-0.16.1-6.fb8.x86_64                                                                                                                                                                98/106
  Running scriptlet: isl-0.16.1-6.fb8.x86_64                                                                                                                                                                98/106
  Installing       : bzip2-1.0.6-26.fb8.x86_64                                                                                                                                                              99/106
  Installing       : unzip-6.0-38.fb8.x86_64                                                                                                                                                               100/106
  Installing       : diffutils-3.6-5.fb8.x86_64                                                                                                                                                            101/106
  Running scriptlet: diffutils-3.6-5.fb8.x86_64                                                                                                                                                            101/106
  Installing       : findutils-1:4.6.0-19.fb8.x86_64                                                                                                                                                       102/106
  Running scriptlet: findutils-1:4.6.0-19.fb8.x86_64                                                                                                                                                       102/106
  Installing       : libgomp-8.2.1-3.3.fb8.x86_64                                                                                                                                                          103/106
  Running scriptlet: libgomp-8.2.1-3.3.fb8.x86_64                                                                                                                                                          103/106
  Installing       : make-1:4.2.1-9.fb8.x86_64                                                                                                                                                             104/106
  Running scriptlet: make-1:4.2.1-9.fb8.x86_64                                                                                                                                                             104/106
  Installing       : cpio-2.12-8.fb8.x86_64                                                                                                                                                                105/106
  Installing       : which-2.21-10.fb8.x86_64                                                                                                                                                              106/106
  Running scriptlet: filesystem-3.8-2.fb8.x86_64                                                                                                                                                           106/106
  Running scriptlet: glibc-all-langpacks-2.28-18.fb8.x86_64                                                                                                                                                106/106
  Running scriptlet: glibc-common-2.28-18.fb8.x86_64                                                                                                                                                       106/106
  Running scriptlet: info-6.5-4.fb8.x86_64                                                                                                                                                                 106/106
  Verifying        : isl-0.16.1-6.fb8.x86_64                                                                                                                                                                 1/106
  Verifying        : libmpc-1.0.2-9.fb8.x86_64                                                                                                                                                               2/106
  Verifying        : audit-libs-3.0-0.5.20180831git0047a6c.fb8.x86_64                                                                                                                                        3/106
  Verifying        : basesystem-11-5.fb8.noarch                                                                                                                                                              4/106
  Verifying        : bash-4.4.19-6.fb8.x86_64                                                                                                                                                                5/106
  Verifying        : binutils-2.30-49.fb8.x86_64                                                                                                                                                             6/106
  Verifying        : bzip2-1.0.6-26.fb8.x86_64                                                                                                                                                               7/106
  Verifying        : bzip2-libs-1.0.6-26.fb8.x86_64                                                                                                                                                          8/106
  Verifying        : ca-certificates-2018.2.24-6.fb8.noarch                                                                                                                                                  9/106
  Verifying        : chkconfig-1.11-1.fb8.x86_64                                                                                                                                                            10/106
  Verifying        : coreutils-8.30-4.fb8.x86_64                                                                                                                                                            11/106
  Verifying        : coreutils-common-8.30-4.fb8.x86_64                                                                                                                                                     12/106
  Verifying        : cpio-2.12-8.fb8.x86_64                                                                                                                                                                 13/106
  Verifying        : cpp-8.2.1-3.3.fb8.x86_64                                                                                                                                                               14/106
  Verifying        : cracklib-2.9.6-14.fb8.x86_64                                                                                                                                                           15/106
  Verifying        : crypto-policies-20181026-1.gitcc78cb7.fb8.noarch                                                                                                                                       16/106
  Verifying        : curl-7.61.1-5.fb8.x86_64                                                                                                                                                               17/106
  Verifying        : diffutils-3.6-5.fb8.x86_64                                                                                                                                                             18/106
  Verifying        : elfutils-libelf-0.174-1.fb8.x86_64                                                                                                                                                     19/106
  Verifying        : expat-2.2.5-3.fb8.x86_64                                                                                                                                                               20/106
  Verifying        : filesystem-3.8-2.fb8.x86_64                                                                                                                                                            21/106
  Verifying        : findutils-1:4.6.0-19.fb8.x86_64                                                                                                                                                        22/106
  Verifying        : gawk-4.2.1-1.fb8.x86_64                                                                                                                                                                23/106
  Verifying        : glibc-2.28-18.fb8.x86_64                                                                                                                                                               24/106
  Verifying        : glibc-all-langpacks-2.28-18.fb8.x86_64                                                                                                                                                 25/106
  Verifying        : glibc-common-2.28-18.fb8.x86_64                                                                                                                                                        26/106
  Verifying        : glibc-devel-2.28-18.fb8.x86_64                                                                                                                                                         27/106
  Verifying        : glibc-headers-2.28-18.fb8.x86_64                                                                                                                                                       28/106
  Verifying        : gmp-1:6.1.2-8.fb8.x86_64                                                                                                                                                               29/106
  Verifying        : grep-3.1-6.fb8.x86_64                                                                                                                                                                  30/106
  Verifying        : gzip-1.9-4.fb8.x86_64                                                                                                                                                                  31/106
  Verifying        : info-6.5-4.fb8.x86_64                                                                                                                                                                  32/106
  Verifying        : kernel-headers-4.18.0-32.fb8.x86_64                                                                                                                                                    33/106
  Verifying        : keyutils-libs-1.5.10-6.fb8.x86_64                                                                                                                                                      34/106
  Verifying        : krb5-libs-1.16.1-19.fb8.x86_64                                                                                                                                                         35/106
  Verifying        : libacl-2.2.53-1.fb8.x86_64                                                                                                                                                             36/106
  Verifying        : libarchive-3.3.2-3.fb8.x86_64                                                                                                                                                          37/106
  Verifying        : libattr-2.4.48-3.fb8.x86_64                                                                                                                                                            38/106
  Verifying        : libblkid-2.32.1-6.fb8.x86_64                                                                                                                                                           39/106
  Verifying        : libcap-2.25-9.fb8.x86_64                                                                                                                                                               40/106
  Verifying        : libcap-ng-0.7.9-3.fb8.x86_64                                                                                                                                                           41/106
  Verifying        : libcom_err-1.44.3-1.fb8.x86_64                                                                                                                                                         42/106
  Verifying        : libcurl-minimal-7.61.1-5.fb8.x86_64                                                                                                                                                    43/106
  Verifying        : libdb-5.3.28-33.fb8.x86_64                                                                                                                                                             44/106
  Verifying        : libdb-utils-5.3.28-33.fb8.x86_64                                                                                                                                                       45/106
  Verifying        : libfdisk-2.32.1-6.fb8.x86_64                                                                                                                                                           46/106
  Verifying        : libffi-3.1-17.fb8.x86_64                                                                                                                                                               47/106
  Verifying        : libgcc-8.2.1-3.3.fb8.x86_64                                                                                                                                                            48/106
  Verifying        : libgcrypt-1.8.3-2.fb8.x86_64                                                                                                                                                           49/106
  Verifying        : libgomp-8.2.1-3.3.fb8.x86_64                                                                                                                                                           50/106
  Verifying        : libgpg-error-1.31-1.fb8.x86_64                                                                                                                                                         51/106
  Verifying        : libmetalink-0.1.3-7.fb8.x86_64                                                                                                                                                         52/106
  Verifying        : libmount-2.32.1-6.fb8.x86_64                                                                                                                                                           53/106
  Verifying        : libnghttp2-1.33.0-1.fb8.x86_64                                                                                                                                                         54/106
  Verifying        : libnsl2-1.2.0-2.20180605git4a062cf.fb8.x86_64                                                                                                                                          55/106
  Verifying        : libpkgconf-1.4.2-1.fb8.x86_64                                                                                                                                                          56/106
  Verifying        : libpwquality-1.4.0-8.fb8.x86_64                                                                                                                                                        57/106
  Verifying        : libselinux-2.8-5.fb8.x86_64                                                                                                                                                            58/106
  Verifying        : libsemanage-2.8-3.1.fb8.x86_64                                                                                                                                                         59/106
  Verifying        : libsepol-2.8-1.fb8.x86_64                                                                                                                                                              60/106
  Verifying        : libsigsegv-2.11-5.fb8.x86_64                                                                                                                                                           61/106
  Verifying        : libsmartcols-2.32.1-6.fb8.x86_64                                                                                                                                                       62/106
  Verifying        : libstdc++-8.2.1-3.3.fb8.x86_64                                                                                                                                                         63/106
  Verifying        : libtasn1-4.13-3.fb8.x86_64                                                                                                                                                             64/106
  Verifying        : libtirpc-1.1.4-3.fb8.x86_64                                                                                                                                                            65/106
  Verifying        : libutempter-1.1.6-14.fb8.x86_64                                                                                                                                                        66/106
  Verifying        : libuuid-2.32.1-6.fb8.x86_64                                                                                                                                                            67/106
  Verifying        : libverto-0.3.0-5.fb8.x86_64                                                                                                                                                            68/106
  Verifying        : libxcrypt-4.1.1-4.fb8.x86_64                                                                                                                                                           69/106
  Verifying        : libxcrypt-devel-4.1.1-4.fb8.x86_64                                                                                                                                                     70/106
  Verifying        : libxml2-2.9.7-5.fb8.x86_64                                                                                                                                                             71/106
  Verifying        : lua-libs-5.3.4-10.fb8.x86_64                                                                                                                                                           72/106
  Verifying        : lz4-libs-1.8.1.2-4.fb8.x86_64                                                                                                                                                          73/106
  Verifying        : make-1:4.2.1-9.fb8.x86_64                                                                                                                                                              74/106
  Verifying        : mpfr-3.1.6-1.fb8.x86_64                                                                                                                                                                75/106
  Verifying        : ncurses-6.1-5.20180224.fb8.x86_64                                                                                                                                                      76/106
  Verifying        : ncurses-base-6.1-5.20180224.fb8.noarch                                                                                                                                                 77/106
  Verifying        : ncurses-libs-6.1-5.20180224.fb8.x86_64                                                                                                                                                 78/106
  Verifying        : openssl-libs-1:1.1.1-6.fb8.x86_64                                                                                                                                                      79/106
  Verifying        : p11-kit-0.23.14-2.fb8.x86_64                                                                                                                                                           80/106
  Verifying        : p11-kit-trust-0.23.14-2.fb8.x86_64                                                                                                                                                     81/106
  Verifying        : pam-1.3.1-3.fb8.x86_64                                                                                                                                                                 82/106
  Verifying        : patch-2.7.6-7.fb8.x86_64                                                                                                                                                               83/106
  Verifying        : pcre-8.42-4.fb8.x86_64                                                                                                                                                                 84/106
  Verifying        : pcre2-10.31-11.fb8.x86_64                                                                                                                                                              85/106
  Verifying        : pkgconf-1.4.2-1.fb8.x86_64                                                                                                                                                             86/106
  Verifying        : pkgconf-m4-1.4.2-1.fb8.noarch                                                                                                                                                          87/106
  Verifying        : pkgconf-pkg-config-1.4.2-1.fb8.x86_64                                                                                                                                                  88/106
  Verifying        : popt-1.16-14.fb8.x86_64                                                                                                                                                                89/106
  Verifying        : readline-7.0-10.fb8.x86_64                                                                                                                                                             90/106
  Verifying        : redhat-release-8.0-0.34.fb8.x86_64                                                                                                                                                     91/106
  Verifying        : rpm-4.14.2-4.fb8.x86_64                                                                                                                                                                92/106
  Verifying        : rpm-libs-4.14.2-4.fb8.x86_64                                                                                                                                                           93/106
  Verifying        : rpm-plugin-selinux-4.14.2-4.fb8.x86_64                                                                                                                                                 94/106
  Verifying        : sed-4.5-1.fb8.x86_64                                                                                                                                                                   95/106
  Verifying        : setup-2.12.2-1.fb8.noarch                                                                                                                                                              96/106
  Verifying        : shadow-utils-2:4.6-4.fb8.x86_64                                                                                                                                                        97/106
  Verifying        : systemd-libs-239-8.fb8.x86_64                                                                                                                                                          98/106
  Verifying        : tar-2:1.30-4.fb8.x86_64                                                                                                                                                                99/106
  Verifying        : tzdata-2018e-2.fb8.noarch                                                                                                                                                             100/106
  Verifying        : unzip-6.0-38.fb8.x86_64                                                                                                                                                               101/106
  Verifying        : util-linux-2.32.1-6.fb8.x86_64                                                                                                                                                        102/106
  Verifying        : which-2.21-10.fb8.x86_64                                                                                                                                                              103/106
  Verifying        : xz-5.2.4-2.fb8.x86_64                                                                                                                                                                 104/106
  Verifying        : xz-libs-5.2.4-2.fb8.x86_64                                                                                                                                                            105/106
  Verifying        : zlib-1.2.11-10.fb8.x86_64                                                                                                                                                             106/106

Installed:
  isl-0.16.1-6.fb8.x86_64                      libmpc-1.0.2-9.fb8.x86_64                           bash-4.4.19-6.fb8.x86_64                               binutils-2.30-49.fb8.x86_64
  bzip2-1.0.6-26.fb8.x86_64                    coreutils-8.30-4.fb8.x86_64                         cpio-2.12-8.fb8.x86_64                                 cpp-8.2.1-3.3.fb8.x86_64
  diffutils-3.6-5.fb8.x86_64                   findutils-1:4.6.0-19.fb8.x86_64                     gawk-4.2.1-1.fb8.x86_64                                glibc-devel-2.28-18.fb8.x86_64
  grep-3.1-6.fb8.x86_64                        gzip-1.9-4.fb8.x86_64                               info-6.5-4.fb8.x86_64                                  libgomp-8.2.1-3.3.fb8.x86_64
  make-1:4.2.1-9.fb8.x86_64                    patch-2.7.6-7.fb8.x86_64                            redhat-release-8.0-0.34.fb8.x86_64                     rpm-4.14.2-4.fb8.x86_64
  sed-4.5-1.fb8.x86_64                         shadow-utils-2:4.6-4.fb8.x86_64                     tar-2:1.30-4.fb8.x86_64                                unzip-6.0-38.fb8.x86_64
  util-linux-2.32.1-6.fb8.x86_64               which-2.21-10.fb8.x86_64                            xz-5.2.4-2.fb8.x86_64                                  audit-libs-3.0-0.5.20180831git0047a6c.fb8.x86_64
  basesystem-11-5.fb8.noarch                   bzip2-libs-1.0.6-26.fb8.x86_64                      ca-certificates-2018.2.24-6.fb8.noarch                 chkconfig-1.11-1.fb8.x86_64
  coreutils-common-8.30-4.fb8.x86_64           cracklib-2.9.6-14.fb8.x86_64                        crypto-policies-20181026-1.gitcc78cb7.fb8.noarch       curl-7.61.1-5.fb8.x86_64
  elfutils-libelf-0.174-1.fb8.x86_64           expat-2.2.5-3.fb8.x86_64                            filesystem-3.8-2.fb8.x86_64                            glibc-2.28-18.fb8.x86_64
  glibc-all-langpacks-2.28-18.fb8.x86_64       glibc-common-2.28-18.fb8.x86_64                     glibc-headers-2.28-18.fb8.x86_64                       gmp-1:6.1.2-8.fb8.x86_64
  kernel-headers-4.18.0-32.fb8.x86_64          keyutils-libs-1.5.10-6.fb8.x86_64                   krb5-libs-1.16.1-19.fb8.x86_64                         libacl-2.2.53-1.fb8.x86_64
  libarchive-3.3.2-3.fb8.x86_64                libattr-2.4.48-3.fb8.x86_64                         libblkid-2.32.1-6.fb8.x86_64                           libcap-2.25-9.fb8.x86_64
  libcap-ng-0.7.9-3.fb8.x86_64                 libcom_err-1.44.3-1.fb8.x86_64                      libcurl-minimal-7.61.1-5.fb8.x86_64                    libdb-5.3.28-33.fb8.x86_64
  libdb-utils-5.3.28-33.fb8.x86_64             libfdisk-2.32.1-6.fb8.x86_64                        libffi-3.1-17.fb8.x86_64                               libgcc-8.2.1-3.3.fb8.x86_64
  libgcrypt-1.8.3-2.fb8.x86_64                 libgpg-error-1.31-1.fb8.x86_64                      libmetalink-0.1.3-7.fb8.x86_64                         libmount-2.32.1-6.fb8.x86_64
  libnghttp2-1.33.0-1.fb8.x86_64               libnsl2-1.2.0-2.20180605git4a062cf.fb8.x86_64       libpkgconf-1.4.2-1.fb8.x86_64                          libpwquality-1.4.0-8.fb8.x86_64
  libselinux-2.8-5.fb8.x86_64                  libsemanage-2.8-3.1.fb8.x86_64                      libsepol-2.8-1.fb8.x86_64                              libsigsegv-2.11-5.fb8.x86_64
  libsmartcols-2.32.1-6.fb8.x86_64             libstdc++-8.2.1-3.3.fb8.x86_64                      libtasn1-4.13-3.fb8.x86_64                             libtirpc-1.1.4-3.fb8.x86_64
  libutempter-1.1.6-14.fb8.x86_64              libuuid-2.32.1-6.fb8.x86_64                         libverto-0.3.0-5.fb8.x86_64                            libxcrypt-4.1.1-4.fb8.x86_64
  libxcrypt-devel-4.1.1-4.fb8.x86_64           libxml2-2.9.7-5.fb8.x86_64                          lua-libs-5.3.4-10.fb8.x86_64                           lz4-libs-1.8.1.2-4.fb8.x86_64
  mpfr-3.1.6-1.fb8.x86_64                      ncurses-6.1-5.20180224.fb8.x86_64                   ncurses-base-6.1-5.20180224.fb8.noarch                 ncurses-libs-6.1-5.20180224.fb8.x86_64
  openssl-libs-1:1.1.1-6.fb8.x86_64            p11-kit-0.23.14-2.fb8.x86_64                        p11-kit-trust-0.23.14-2.fb8.x86_64                     pam-1.3.1-3.fb8.x86_64
  pcre-8.42-4.fb8.x86_64                       pcre2-10.31-11.fb8.x86_64                           pkgconf-1.4.2-1.fb8.x86_64                             pkgconf-m4-1.4.2-1.fb8.noarch
  pkgconf-pkg-config-1.4.2-1.fb8.x86_64        popt-1.16-14.fb8.x86_64                             readline-7.0-10.fb8.x86_64                             rpm-libs-4.14.2-4.fb8.x86_64
  rpm-plugin-selinux-4.14.2-4.fb8.x86_64       setup-2.12.2-1.fb8.noarch                           systemd-libs-239-8.fb8.x86_64                          tzdata-2018e-2.fb8.noarch
  xz-libs-5.2.4-2.fb8.x86_64                   zlib-1.2.11-10.fb8.x86_64

Complete!
Finish: dnf install
Start: creating root cache
Finish: creating root cache
Finish: chroot init
INFO: Installed packages:
Start: shell
[root@990c9a5364794a4788e69f8b4eb784ff /]#

ビルド環境としてのmock

ちょっと依存関係の解決が必要でまだ解決してないので綺麗なビルド環境としては使えません(´・ω・`) 

Error: 
 Problem: conflicting requests
  - nothing provides libgcc_s.so.1 needed by gcc-8.2.1-3.3.fb8.x86_64
ERROR: Command failed: 
 # /usr/bin/dnf --installroot /var/lib/mock/foobar8/root/ --releasever fb --disableplugin=local --setopt=deltarpm=False install bash bzip2 coreutils cpio diffutils findutils gawk grep gzip info make patch redhat-release sed shadow-utils tar unzip util-linux which xz rpm binutils glibc-devel libgomp isl libmpc cpp gcc
Foobar Appstream                                                                                                                                                                   3.0 kB/s | 3.0 kB     00:01    
Foobar BaseOS                                                                                                                                                                      3.0 kB/s | 3.0 kB     00:01    
Error: 
 Problem: conflicting requests
  - nothing provides libgcc_s.so.1 needed by gcc-8.2.1-3.3.fb8.x86_64

まとめ

RHELクローン的な物を作る遊びでした。段階を踏んで自分自身で自分をビルドしていくというブートストラップ方式はコンパイラ的な感じですね。

mockとpackageのgitリポジトリとリビルド

この記事はLinux Advent Calendarの19日目です。

概要

今回はsrpmをダウンロードしてきて何かをするのではなくて、パッケージのgitリポジトリからcloneしてきてビルドするときのメモです。パッケージのビルド方法としてはfedpkgを使うかmockを使うかの2通りがあります。両者の方法を見ていきましょう。

fedpkgの場合

masterブランチとかf29と言ったディストリビューションのバージョンのブランチをそのまま使わずに、ローカルにブランチを作って作業するとしましょう。こんな感じですね。

masami@saga:~/fedora-bash (master=)$ git checkout -b f29 origin/f29
Branch 'f29' set up to track remote branch 'f29' from 'origin'.
Switched to a new branch 'f29'
masami@saga:~/fedora-bash (f29=)$ git checkout -b build-test
Switched to a new branch 'build-test'

この場合、fedpkgで普通にmockbuildをするとエラーになります。これはローカルのブランチがリモートにも有ることを期待しているためです。で、--releaseオプションを使えと言われます。

masami@saga:~/fedora-bash (build-test)$ fedpkg mockbuild
Downloading bash-4.4.tar.gz
######################################################################## 100.0%
Could not execute mockbuild: Unable to find remote branch.  Use --release

--releaseオプションはmockbuildオプションのオプションではなくて、fedpkgのオプションです。ということでこのようにするとresults_bash/というディレクトリが出来て、その下にrpmパッケージが置かれます。

masami@saga:~/fedora-bash (build-test)$ fedpkg --release f29 mockbuild
warning: Macro expanded in comment on line 16: %{version}.tar.gz



warning: Macro expanded in comment on line 16: %{version}.tar.gz

Wrote: /home/masami/fedora-bash/bash-4.4.23-5.fc29.src.rpm
INFO: mock.py version 1.4.13 starting (python version = 3.7.1)...
Start: init plugins
〜略〜

mockの場合

mockを使う場合は一旦srpmを作ってからビルドします。specとpatch諸々が有るディレクトリを指定し、srpmパッケージをresultdirオプションで指定した場所に作ります。

masami@saga:~/fedora-bash (build-test %)$ mock -r /etc/mock/fedora-29-x86_64.cfg --buildsrpm --spec ./bash.spec --sources=. --resultdir=.
INFO: mock.py version 1.4.13 starting (python version = 3.7.1)...
Start: init plugins
INFO: selinux disabled
Finish: init plugins
〜略〜

そしてsrpmファイルをビルドしてrpmパッケージを作ります。

masami@saga:~/fedora-bash (build-test %)$ mock -r /etc/mock/fedora-29-x86_64.cfg --rebuild ./bash-4.4.23-5.fc29.src.rpm
INFO: mock.py version 1.4.13 starting (python version = 3.7.1)...
Start: init plugins
〜略〜

おまけ srpmを展開してgitリポジトリと同じ構造で扱う

rpmコマンドで~/rpmbuildにファイルを展開するのではなくてrpm2cpioとcpioで展開すればgitリポジトリと同じ構造で.specやpatch等を取り出せます。あとはmockの場合の方法でビルドできます。

masami@saga:~/tmp/bash$ rpm2cpio ~/fedora-bash/bash-4.4.23-5.fc29.src.rpm | cpio -iv
bash-2.02-security.patch
bash-2.03-paths.patch
bash-2.03-profile.patch
〜略〜
dot-bash_profile
dot-bashrc
18621 blocks
masami@saga:~/tmp/bash$ ls
./                            bash-3.2-ssh_source_bash.patch     bash-4.3-noecho.patch                 bash-4.4-patch-16.patch  bash-4.4-patch-4.patch                 bash-setlocale.patch
../                           bash-4.0-nobits.patch              bash-4.4-assignment-error.patch       bash-4.4-patch-17.patch  bash-4.4-patch-5.patch                 bash.spec
bash-2.02-security.patch      bash-4.1-broken_pipe.patch         bash-4.4-case-in-command-subst.patch  bash-4.4-patch-18.patch  bash-4.4-patch-6.patch                 bash-tty-tests.patch
bash-2.03-paths.patch         bash-4.1-defer-sigchld-trap.patch  bash-4.4-coverity.patch               bash-4.4-patch-19.patch  bash-4.4-patch-7.patch                 dot-bash_logout
bash-2.03-profile.patch       bash-4.1-examples.patch            bash-4.4-no-loadable-builtins.patch   bash-4.4-patch-1.patch   bash-4.4-patch-8.patch                 dot-bash_profile
bash-2.05a-interpreter.patch  bash-4.2-coverity.patch            bash-4.4-patch-10.patch               bash-4.4-patch-20.patch  bash-4.4-patch-9.patch                 dot-bashrc
bash-2.05b-debuginfo.patch    bash-4.2-manpage_trap.patch        bash-4.4-patch-11.patch               bash-4.4-patch-21.patch  bash-4.4.tar.gz
bash-2.05b-manso.patch        bash-4.2-rc2-logout.patch          bash-4.4-patch-12.patch               bash-4.4-patch-22.patch  bash-4.4-unset-nonblock-stdin.patch
bash-2.05b-pgrp_sync.patch    bash-4.2-size_type.patch           bash-4.4-patch-13.patch               bash-4.4-patch-23.patch  bash-4.5-test-modification-time.patch
bash-2.05b-xcc.patch          bash-4.3-man-ulimit.patch          bash-4.4-patch-14.patch               bash-4.4-patch-2.patch   bash-infotags.patch
bash-3.2-audit.patch          bash-4.3-memleak-lc_all.patch      bash-4.4-patch-15.patch               bash-4.4-patch-3.patch   bash-requires.patch

まとめ

mockやfedpkgを使う利点は~/rpmbuildを使わないのでファイルがゴチャゴチャにならずにすみます😃

Linuxカーネルをgdbでデバッグ(またはディストリビューションのカーネルを使うときは当たってるパッチにも注意しよう)

この記事はLinux Advent Calendar 2018の1日目ですΣ(゚∀゚ノ)ノキャー

イントロ

ほんとは別の内容にしようと思ってたのですが、進めてる途中でカーネルデバッグをするハメになったのでカーネルデバッグをネタにしてみました。カーネルデバッグと言っても普通のデバッグと変わらないよね〜というところがわかると思います。(`・ω・´)<コワクナイヨー

デバッグの環境としてはlibvirt(qemu)で動いてるゲスト環境にホスト側からgdbデバッグする感じです。ディストリビューションFedora 29です。デバッグするカーネルFedoraカーネルで4.19.2-300.fc29.x86_64です。

テストコード

テストコードは↓です。これはdebugfsのディレクトリ(大概は/sys/kernel/debug/だと思います)にopen-testってファイルを作って、そのファイルを読むとhello, world!\nって読めるだけの単純なものです。

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/debugfs.h>

MODULE_DESCRIPTION("debugfs open test module");
MODULE_AUTHOR("masami266");
MODULE_LICENSE("GPL");

#define OPEN_TEST_FILE "open-test"

static struct dentry *open_test_file;

static char open_test_file_buf[] = "hello, world!\n";

static ssize_t open_test_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
{
        pr_info("%s\n", __func__);
        return simple_read_from_buffer(buf, len, ppos, open_test_file_buf, sizeof(open_test_file_buf));
}

static struct file_operations open_test_fops = {
        .owner = THIS_MODULE,
        .read = open_test_read,
};

static int open_test_init(void)
{
        open_test_file = debugfs_create_file(OPEN_TEST_FILE, 0644,
                            NULL, open_test_file_buf,
                            &open_test_fops);

        if (!open_test_file) {
                pr_info("failed to create debugfs file\n");
                return -ENODEV;
        }

        pr_info("open_test module has been initialized\n");
        return 0;
}

static void open_test_exit(void)
{
        if (open_test_file)
                debugfs_remove(open_test_file);

        pr_info("good bye\n");
}

module_init(open_test_init);
module_exit(open_test_exit);

Makefileはこうです。

KERNDIR := /lib/modules/`uname -r`/build
BUILD_DIR := $(shell pwd)
VERBOSE = 0

obj-m := open_test.o
smallmod-objs := open_test.o

all:
        make -C $(KERNDIR) SUBDIRS=$(BUILD_DIR) KBUILD_VERBOSE=$(VERBOSE) modules

clean:
        rm -f *.o
        rm -f *.ko
        rm -f *.mod.c
        rm -f *~

実行してみる

これをメインラインのカーネルで実行するとこうなります。予想通りの挙動ですね〜

masami@kerntest:~/open-test$ uname -a
Linux kerntest 4.20.0-rc3-test+ #8 SMP Fri Nov 23 10:15:41 JST 2018 x86_64 x86_64 x86_64 GNU/Linux
masami@kerntest:~/open-test$ sudo insmod ./open_test.ko
masami@kerntest:~/open-test$ sudo cat /sys/kernel/debug/open-test
hello, world!

だがしかし、fedoraカーネルで実行するとエラーになります。。。これをデバッグするのが今回のネタとなります。

masami@kerntest:~/open-test$ uname -a
Linux kerntest 4.19.2-300.fc29.x86_64 #1 SMP Wed Nov 14 19:05:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
masami@kerntest:~/open-test$ sudo insmod ./open_test.ko
masami@kerntest:~/open-test$ sudo cat /sys/kernel/debug/open-test
cat: /sys/kernel/debug/open-test: Operation not permitted

デバッグ

状況確認

まずはdmesgを見てみます。

[  155.906829] open_test: loading out-of-tree module taints kernel.
[  155.906874] open_test: module verification failed: signature and/or required key missing - tainting kernel
[  155.907409] open_test: open_test module has been initialized

open_test_read()のpr_info()の部分は実行されていないのがわかります。そしたらstrace(1)を使ってみます。

openat(AT_FDCWD, "/sys/kernel/debug/open-test", O_RDONLY) = -1 EPERM (Operation not permitted)

straceを使うとopenat(2)でEPERMが返ってきてるのがわかります。ファイルをreadするにはopenする必要が有るから当然な感じですね。ということで、ファイルを開く処理のどこかでEPERMが返ってるわけですが、これをコードを読んでいって調べるのは大変なのでgdbでも使いましょう。

configの内容を合わせてみる

メインラインのカーネルも一旦config-4.19.2-300.fc29.x86_64を使ってビルドしてみましたが、問題ありませんでした。ということでコンフィグ的な差分では無いというのがわかりました。

デバッグの準備

gdbで接続するための準備

今回の環境ではlibvirtを使っているのでlibvirtgdbの接続ができるようにします。設定ファイルの変更ですがこれはvrishコマンドでできます。

masami@saga:~$ sudo virsh edit kerntest

エディタが立ち上がって編集可能になるので、まずは先頭の部分を以下のように変えます。

<domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>

そして、一番下にでも以下の行を追加して上書き保存してエディタを終了しましょう。

  <qemu:commandline>
    <qemu:arg value='-s'/>
  </qemu:commandline>

基本的にはこれだけでOKなんですが、デバッグされるホストのカーネルのほうでKASLRが有効になっていてリモートデバッグするときにはこのままだと適切な場所にブレークポイントを張ったりができないので、これを無効にする必要があります。詳細はこちらを参照してください。

kernhack.hatenablog.com

KASLRを無効にするのはカーネルコマンドラインでオプションを渡せばできます。起動時に毎回設定するのは面倒なのでgrubのほうでカーネルコマンドラインに追記しましょう。/etc/default/grubというファイルがgrub.cfgを作る時の設定ファイルです。fedoraの場合は以下の行がありますので、

GRUB_CMDLINE_LINUX="rhgb quiet"

こんな感じにします。

GRUB_CMDLINE_LINUX="rhgb quiet nokaslr"

そして、sudo grub2-mkconfig -o /boot/grub2/grub.cfgな感じでgrubの設定ファイルを作り直してから再起動しましょう。 これでtcpポート1234を使ってリモートでバッグできるようになりました。

デバッグ用のvmlinuxとソースの準備

デバッグするのにデバッグシンボルがないと不便ですし、ソースもみたいですよね。ということでこれも用意しましょう。これはgdbを実行するホストのほうで必要になります。 デバッグシンボル付きのvmlinuxはkernel-debuginfoパッケージでインストールすることができます。ホストとゲストで同じfedoraを使ってるならホスト側でsudo dnf debuginfo-install -y kernelを実行しても良いですね。もしくはゲスト側でインストールしてvmlinuxをscp等でダウンロードするのも有りです。vmlinuxが有る場所は以下の場所です。

/usr/lib/debug/lib/modules/4.19.2-300.fc29.x86_64/vmlinux

ソールはkernel-debuginfo-commonパッケージにあります。なのでsudo dnf debuginfo-install -y kernelを実行すればvmlinuxとソースの両方がインストールできます。ソースは以下の場所にインストールされます。

/usr/src/debug/kernel-4.19.fc29/linux-4.19.2-300.fc29.x86_64/

自分はゲストのほうでパッケージをインストールしてvmlinuxとソースコードディレクトリはscpでホスト側に持っていきました。

これでgdbを使う準備は完了です。

gdbで動作を追う

gdbを使うにしてもブレークポイントを設定する場所とかは確認したいので、まずはopenat()を見てみます。

SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
        umode_t, mode)
{
    if (force_o_largefile())
        flags |= O_LARGEFILE;

    return do_sys_open(dfd, filename, flags, mode);
}

do_sys_open()が呼ばれているのでこちらにブレークポイントを張って追いかけていきましょう。しかし、ファイルのオープンは色んな所で行われるため条件付きのブレークポイントにしないと面倒です。引数のfilenameが開きたいファイル名なのでこれを条件にしましょう。組み込み関数の$_streq()を使うと文字列の比較ができます。

gdbを起動するときのソースコードのパスも一緒に指定しました。

masami@saga:~$ gdb --directory=./linux-4.19.2-300.fc29.x86_64/ ./vmlinux  
Reading symbols from ./vmlinux...done.
gdb-peda$ 

そしてリモート接続します。あ、その前に別のターミナルからssh接続してモジュールのロードまで済ませて置きましょう。

gdb-peda$ target remote :1234
Remote debugging using :1234
Warning: not running or target is remote
0xffffffff819294a2 in native_safe_halt () at ./arch/x86/include/asm/irqflags.h:57
warning: Source file is more recent than executable.
57              asm volatile("sti; hlt": : :"memory");

ブレークポイントをセットしたら実行を再開させましょう。

gdb-peda$ b do_sys_open if $_streq(filename, "/sys/kernel/debug/open-test") == 1
Breakpoint 1 at 0xffffffff812ad830: file fs/open.c, line 1049.
gdb-peda$ c
Continuing.

以下のようにアクセスできない〜って言われた場合は無視してcを押して処理を継続しましょう。

Thread 1 hit Breakpoint 1, do_sys_open (dfd=0xffffff9c, filename=0x7f55d9b808b0 <error: Cannot access memory at address 0x7f55d9b808b0>, flags=0x88000, mode=0x0) at fs/open.c:1049
1049    {

これが設定した条件に引っかった時です。

Thread 1 hit Breakpoint 1, do_sys_open (dfd=0xffffff9c, filename=0x7ffe8f308807 "/sys/kernel/debug/open-test", flags=0x8000, mode=0x0) at fs/open.c:1049
1049    {
gdb-peda$ 

ここからは普通のgdbの使い方と一緒でnextとかstepを使ってどこでEPERMが返ってくるか調べていくとfull_proxy_open()がEPERMを返すことが分かりました。そんなわけでブレークポイントの条件をこちらに変えてもokです。

b full_proxy_open if $_streq(filp->f_path->dentry->d_iname, "open-test") == 1

良いところで止まったら、ゆっくり見ていきましょう。

gdb-peda$ c
Continuing.
[Switching to Thread 1]
Warning: not running or target is remote

Thread 1 hit Breakpoint 1, full_proxy_open (inode=inode@entry=0xffff8802261744b0, filp=filp@entry=0xffff88022fe10100) at fs/debugfs/file.c:288
warning: Source file is more recent than executable.
288     {

debugfs_file_get()は成功したようですね。

Warning: not running or target is remote
294             r = debugfs_file_get(dentry);
gdb-peda$ n
Warning: not running or target is remote
295             if (r)
gdb-peda$ p r
$1 = 0x0

しばらく先に進んでopen()が設定されているか調べていて、テストプログラムではreadしか設定していないのでここはNULLになってますね。

320             if (real_fops->open) {
gdb-peda$ p real_fops->open
$2 = (int (*)(struct inode *, struct file *)) 0x0 <irq_stack_union>

実行を続けるとdentryを片付けてエラーコード-1が返ってます。

gdb-peda$ n
Warning: not running or target is remote
338             debugfs_file_put(dentry);
gdb-peda$ n
Warning: not running or target is remote
339             return r;
gdb-peda$ p r
$3 = 0xffffffff

nで実行を継続していったときはrを最後に確認したのはdebugfs_file_get()の実行時なのでその後はgdb上では出てきてません(´・ω・`) ということでソースを見てみましょう。

static int full_proxy_open(struct inode *inode, struct file *filp)
{
    struct dentry *dentry = F_DENTRY(filp);
    const struct file_operations *real_fops = NULL;
    struct file_operations *proxy_fops = NULL;
    int r;

    r = debugfs_file_get(dentry);
    if (r)
        return r == -EIO ? -ENOENT : r;

    real_fops = debugfs_real_fops(filp);
    r = -EPERM;
    if (debugfs_is_locked_down(inode, filp, real_fops))
        goto out;

    real_fops = fops_get(real_fops);
    if (!real_fops) {
        /* Huh? Module did not cleanup after itself at exit? */
        WARN(1, "debugfs file owner did not clean up at exit: %pd",
            dentry);
        r = -ENXIO;
        goto out;
    }

    proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL);
    if (!proxy_fops) {
        r = -ENOMEM;
        goto free_proxy;
    }
    __full_proxy_fops_init(proxy_fops, real_fops);
    replace_fops(filp, proxy_fops);

    if (real_fops->open) {
        r = real_fops->open(inode, filp);
        if (r) {
            replace_fops(filp, d_inode(dentry)->i_fop);
            goto free_proxy;
        } else if (filp->f_op != proxy_fops) {
            /* No protection against file removal anymore. */
            WARN(1, "debugfs file owner replaced proxy fops: %pd",
                dentry);
            goto free_proxy;
        }
    }

    goto out;
free_proxy:
    kfree(proxy_fops);
    fops_put(real_fops);
out:
    debugfs_file_put(dentry);
    return r;
}

debugfs_is_locked_down()の呼び出し前に以下の処理がありますね。ということは、real_fops->openがNULLの場合はrはそのままなので-1が返るってのがわかります。

r = -EPERM;

しかし、メインラインのカーネルではエラーにならなかったんですが??? まあ、メインラインのコードも見るしか無いですよね。ということで、4.19.2のfs/debugfs/file.cを見てみましょう。

static int full_proxy_open(struct inode *inode, struct file *filp)
{
    struct dentry *dentry = F_DENTRY(filp);
    const struct file_operations *real_fops = NULL;
    struct file_operations *proxy_fops = NULL;
    int r;

    r = debugfs_file_get(dentry);
    if (r)
        return r == -EIO ? -ENOENT : r;

    real_fops = debugfs_real_fops(filp);
    real_fops = fops_get(real_fops);
    if (!real_fops) {
        /* Huh? Module did not cleanup after itself at exit? */
        WARN(1, "debugfs file owner did not clean up at exit: %pd",
            dentry);
        r = -ENXIO;
        goto out;
    }

    proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL);
    if (!proxy_fops) {
        r = -ENOMEM;
        goto free_proxy;
    }
    __full_proxy_fops_init(proxy_fops, real_fops);
    replace_fops(filp, proxy_fops);

    if (real_fops->open) {
        r = real_fops->open(inode, filp);
        if (r) {
            replace_fops(filp, d_inode(dentry)->i_fop);
            goto free_proxy;
        } else if (filp->f_op != proxy_fops) {
            /* No protection against file removal anymore. */
            WARN(1, "debugfs file owner replaced proxy fops: %pd",
                dentry);
            goto free_proxy;
        }
    }

    goto out;
free_proxy:
    kfree(proxy_fops);
    fops_put(real_fops);
out:
    debugfs_file_put(dentry);
    return r;
}

差が有りますね。。。これが差分です。

+        r = -EPERM;
+        if (debugfs_is_locked_down(inode, filp, real_fops))
+                goto out;
+

fedoraカーネルではreal_fops->openが設定されていないと -EPERMが返る仕様になっています。ということはディストリビューション側でパッチを当ててますよね。ってことで調べてみましょう。fedoraカーネルのgitリポジトリこちらです。カーネルと言うかカーネルパッケージのgitリポジトリです。

ブランチをf29に変えてパッチを調べるとefi-lockdown.patchに該当のコードが見つかります。このパッチファイルの1560行目からが該当する部分です。まさにありますね。

 
@@ -272,6 +296,10 @@ static int full_proxy_open(struct inode *inode, struct file *filp)
        return r == -EIO ? -ENOENT : r;
 
    real_fops = debugfs_real_fops(filp);
+   r = -EPERM;
+   if (debugfs_is_locked_down(inode, filp, real_fops))
+       goto out;
+

ここまで分かったのでテストプログラムを修正しましょう。

修正

file_operations構造体のopen()を設定します。openの処理自体には特別な処理は不要なのでlinux/fs.hで宣言されているsimple_open()を使いましょう。

        .open = simple_open,

これで実行するとちゃんと動きました(∩´∀`)∩ワーイ

masami@kerntest:~/open-test$ sudo insmod ./open_test.ko
masami@kerntest:~/open-test$ uname -a
Linux kerntest 4.19.2-300.fc29.x86_64 #1 SMP Wed Nov 14 19:05:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
masami@kerntest:~/open-test$ sudo cat /sys/kernel/debug/open-test
hello, world!
masami@kerntest:~/open-test$ 

もちろんメインラインのカーネルでも動きますヽ(=´▽`=)ノ

masami@kerntest:~/open-test$ sudo insmod ./open_test.ko
masami@kerntest:~/open-test$ uname -a
Linux kerntest 4.20.0-rc3-test+ #8 SMP Fri Nov 23 10:15:41 JST 2018 x86_64 x86_64 x86_64 GNU/Linux
masami@kerntest:~/open-test$ sudo cat /sys/kernel/debug/open-test
hello, world!
masami@kerntest:~/open-test$ 

まとめ

gdbを使ったカーネルデバッグを説明しました。カーネルと言っても普通のcプログラムと変わらずにデバッグできるし、こわくないよーという感じではないでしょうか。 あと、ディストリビューションカーネルを使っているときはもしかしたらメインラインに入っていない機能が有る場合も有るので、使われているパッチにも気をつけないといけないですね。。。

( ´ー`)フゥー...

Linuxでプロセス単位にaslrのon/offを切り替える仕組みめも

gdb turns off ASLR « codeblogを見ていてpersonality(2)を使えば良いのか〜と知ったのでめもです。

tl;dr

personality(2)でADDR_NO_RANDOMIZEをセットすればoffにできる。

personality(2)

aslr以外にも設定可能な項目はいくつか有ります。設定されている内容を見るだけなら /proc/<pid>/personality を読んで調べることができます。

テストコード

とりあえずこんな感じのコードを。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/personality.h>

static void show_pointer_address(void)
{
    int n;

    printf("[*] local variable address is %p\n", &n);
}

static void show_current_persona(void)
{
    int persona = personality(0xffffffff);
    printf("[*]current setting is 0x%x\n", persona);
}

int main(int argc, char **argv)
{
    show_current_persona();

    if (argc == 2 && !strcmp(argv[1], "show")) {
        show_pointer_address();
        return 0;
    } else if (argc == 2 && !strcmp(argv[1], "no_aslr")) {
        printf("[*]set aslr off\n");
        personality(ADDR_NO_RANDOMIZE);
    }

    pid_t pid = fork();

    if (pid < 0) {
        perror("fork");
        exit(-1);
    }

    if (!pid) {
        execl(argv[0], argv[0], "show",  NULL);
    } else {
        int status;
        waitpid(pid, &status, 0);
    }

    return 0;
}

動かしてみる

aslrを無効にしない場合

masami@saga:~/codes/personality_test$ for((i=0;i<10;i++)); do ./a.out ; done | grep local
[*] local variable address is 0x7fffa4082aac
[*] local variable address is 0x7ffc06ed6a4c
[*] local variable address is 0x7fff8bc1fbac
[*] local variable address is 0x7ffdd78eb9dc
[*] local variable address is 0x7ffe2c88ed3c
[*] local variable address is 0x7ffd32bde89c
[*] local variable address is 0x7fffa323205c
[*] local variable address is 0x7ffd22dc153c
[*] local variable address is 0x7ffe5613520c
[*] local variable address is 0x7ffdee4e8dec

aslrを無効にした場合

masami@saga:~/codes/personality_test$ for((i=0;i<10;i++)); do ./a.out no_aslr; done | grep local
[*] local variable address is 0x7fffffffd2dc
[*] local variable address is 0x7fffffffd2dc
[*] local variable address is 0x7fffffffd2dc
[*] local variable address is 0x7fffffffd2dc
[*] local variable address is 0x7fffffffd2dc
[*] local variable address is 0x7fffffffd2dc
[*] local variable address is 0x7fffffffd2dc
[*] local variable address is 0x7fffffffd2dc
[*] local variable address is 0x7fffffffd2dc
[*] local variable address is 0x7fffffffd2dc

libbfdのめも

libbfdの使い方のめもです。利用してるバージョンはbinutils-devel-2.31.1-13.fc29.x86_64です。

nmもどき

アドレスとセクション名、それにdebug情報があればファイル名と行数を表示。連想配列が使いたかったのでヘッダファイルだけで実装されてるuthashというライブラリを使ってます。

simple_nm.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <bfd.h>

#include "uthash.h"

struct section_info {
    char name[64];
    unsigned long vma;
    UT_hash_handle hh;
};

static struct section_info *sections_info = NULL;

static void delete_all_hash_values(void)
{
    struct section_info *pos, *tmp;

    HASH_ITER(hh, sections_info, pos, tmp) {
        HASH_DEL(sections_info, pos);
        free(pos);
    }

    HASH_CLEAR(hh, sections_info);
}

static struct section_info *add_section(bfd *abfd, const char *sname)
{
    struct section_info *info;

    asection *section = bfd_get_section_by_name(abfd, sname);
    if (!section)
        return NULL;

    info = malloc(sizeof(*info));
    if (!info) {
        fprintf(stderr, "malloc failed\n");
        exit(-1);
    }
    memset(info, 0x0, sizeof(*info));

    strcpy(info->name, sname);
    info->vma = section->vma;

    HASH_ADD_STR(sections_info, name, info);

    return info;
}

static struct section_info *get_section_info(bfd *abfd, const char *sname)
{
    struct section_info *info = NULL;

    HASH_FIND_STR(sections_info, sname, info);
    if (info)
        return info;

    return add_section(abfd, sname);
}

static void show_function_info(bfd *abfd, asection *debug_info, asymbol **symbols, int idx)
{
    const char *file, *func;
    unsigned int line;
    const char *sname = symbols[idx]->section->name;
    const char *name = symbols[idx]->name;
    struct section_info *info = get_section_info(abfd, sname);

    if (!info)
        return ;

    unsigned long addr = symbols[idx]->value + info->vma;
    if (bfd_find_nearest_line(abfd, debug_info, symbols, addr, &file, &func, &line))
        printf("0x%lx %s %s %s:%u\n", addr, sname, name, file, line);
    else
        printf("0x%lx %s %s\n", addr, sname, name);
}

static void show(char *prog)
{
    bfd *abfd;
    asection *debug_info;
    size_t nsyms;
    asymbol **symbols;
    int ret;

    bfd_init();

    abfd = bfd_openr(prog, NULL);
    if (!abfd) {
        fprintf(stderr, "failed to open %s\n", prog);
        exit(-1);
    }

    ret = bfd_check_format(abfd, bfd_object);
    if (!ret) {
        fprintf(stderr, "invalid object format\n");
        exit(-1);
    }

    debug_info = bfd_get_section_by_name(abfd, ".debug_info");

    symbols = malloc(bfd_get_symtab_upper_bound(abfd));
    if (!symbols) {
        fprintf(stderr, "malloc failed\n");
        exit(-1);
    }

    nsyms = bfd_canonicalize_symtab(abfd, symbols);

    for (int i = 0; i < nsyms; i++)
        show_function_info(abfd, debug_info, symbols, i);

    delete_all_hash_values();
    free(symbols);
    bfd_close(abfd);
}

int main(int argc, char **argv)
{
    if (argc != 2) {
        fprintf(stderr, "usage: %s <file>\n", argv[0]);
        exit(-1);
    }

    show(argv[1]);
    return 0;
}

Makefile

objs=simple_nm.o

prog=simple_nm

all: $(objs)
  $(CC) -O0 -g $(objs) -o $(prog) -lbfd

.c.o:
  $(CC) -O0 -I. -g -c -Wall $<

test:
  ./$(prog) $(prog)

clean:
  rm -f core* $(objs) $(prog)

実行例

0xffffffff829e0018 .bss panic_param
0xffffffff827347e3 .init.text unknown_bootoption /home/masami/linux-kernel/init/main.c:294
0xffffffff81002010 .text trace_initcall_finish_cb /home/masami/linux-kernel/init/main.c:841
0xffffffff810028e3 .text trace_initcall_start_cb /home/masami/linux-kernel/init/main.c:832
0xffffffff81002911 .text run_init_process /home/masami/linux-kernel/init/main.c:1011
0xffffffff8100294f .text try_to_run_init_process /home/masami/linux-kernel/init/main.c:1020
0xffffffff82734977 .init.text trace_event_define_fields_initcall_level /home/masami/linux-kernel/./include/trace/events/initcall.h:10
0xffffffff8273499f .init.text trace_event_define_fields_initcall_start /home/masami/linux-kernel/./include/trace/events/initcall.h:27
0xffffffff827349c4 .init.text trace_event_define_fields_initcall_finish /home/masami/linux-kernel/./include/trace/events/initcall.h:48
0xffffffff81002050 .text perf_trace_initcall_start /home/masami/linux-kernel/./include/trace/events/initcall.h:27
0xffffffff81002120 .text perf_trace_initcall_finish /home/masami/linux-kernel/./include/trace/events/initcall.h:48
0xffffffff81002200 .text trace_raw_output_initcall_level /home/masami/linux-kernel/./include/trace/events/initcall.h:10
0xffffffff81002250 .text trace_raw_output_initcall_start /home/masami/linux-kernel/./include/trace/events/initcall.h:27
0xffffffff81002290 .text trace_raw_output_initcall_finish /home/masami/linux-kernel/./include/trace/events/initcall.h:48
0xffffffff810022e0 .text __bpf_trace_initcall_level /home/masami/linux-kernel/./include/trace/events/initcall.h:10
0xffffffff810022f0 .text __bpf_trace_initcall_start /home/masami/linux-kernel/./include/trace/events/initcall.h:27
0xffffffff81002300 .text __bpf_trace_initcall_finish /home/masami/linux-kernel/./include/trace/events/initcall.h:48
0xffffffff82734a1c .init.text loglevel /home/masami/linux-kernel/init/main.c:230
0xffffffff81002310 .text initcall_blacklisted /home/masami/linux-kernel/init/main.c:790
0xffffffff82213020 .data blacklisted_initcalls
0xffffffff82329a50 .data descriptor.60757
0xffffffff82734a77 .init.text set_debug_rodata /home/masami/linux-kernel/ini

ptrace(2)で呼び出す関数を動的に変更

bfdで関数のアドレスを調べてptrace(2)でブレークポイントを設定して、ブレークポイントにきたらripを呼び出したい関数に変えて本来の関数は実行しない形。inline化されてるものには対応してませんけども。。

simple_break_point.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <sys/user.h>
#include <assert.h>
#include <bfd.h>

static void stub_function(void)
{
    printf("stub function is called\n");
}

static void say_hello(void)
{
    printf("hello, world\n");
}

void *get_function_address(char *prog_name, char *func_name)
{
    bfd *abfd;
    asection *text;
    size_t nsyms;
    asymbol **symbols;
    void *addr = NULL;
    int ret;

    bfd_init();

    abfd = bfd_openr(prog_name, NULL);
    assert(abfd != NULL);

    ret = bfd_check_format(abfd, bfd_object);
    assert(ret != 0);

    text = bfd_get_section_by_name(abfd, ".text");

    symbols = malloc(bfd_get_symtab_upper_bound(abfd));
    assert(symbols != NULL);

    nsyms = bfd_canonicalize_symtab(abfd, symbols);

    for (int i = 0; i < nsyms; i++) {
        if (!strcmp(symbols[i]->name, func_name)) {
            addr = (void *) (symbols[i]->value + text->vma);
            break;
        }
    }

    free(symbols);
    bfd_close(abfd);

    return addr;
}

int main(int argc, char **argv)
{
    pid_t pid;
    long ret;
    void *addr;

    if (argc != 2) {
        printf("usage: %s <function name>\n", argv[0]);
        exit(-1);
    }

    addr = get_function_address(argv[0], argv[1]);
    if (!addr) {
        printf("function %s is not found\n", argv[1]);
        exit(-1);
    }

    pid = fork();
    assert(pid >=  0);

    if (!pid) {
        say_hello();
    } else {
        int status;
        void *break_point;
        long orig_text;

        ret = ptrace(PTRACE_ATTACH, pid, NULL, NULL);
        assert(ret != -1);
        waitpid(pid, &status, 0);

        orig_text = ptrace(PTRACE_PEEKTEXT, pid, addr, NULL);
        break_point = (void *) (unsigned long) (orig_text | 0xcc);
        ret = ptrace(PTRACE_POKETEXT, pid, addr, break_point);
        assert(pid != -1);

        printf("[*] set break point at %p\n", addr);

        ret = ptrace(PTRACE_CONT, pid, NULL, NULL);
        assert(pid != -1);

        waitpid(pid, &status, 0);

        if (WIFEXITED(status)) {
            printf("program normally finishd\n");
            exit(0);
        } else if (WIFSTOPPED(status)) {
            struct user_regs_struct regs = { 0 };
            ret = ptrace(PTRACE_GETREGS, pid, 0, &regs);
            assert(ret != -1);
            regs.rip = (unsigned long long) stub_function;

            ret = ptrace(PTRACE_SETREGS, pid, 0, &regs);
            assert(ret != -1);

            ret = ptrace(PTRACE_CONT, pid, NULL, NULL);
            assert(pid != -1);
        } else {
            printf("unkown error\n");
            exit(-1);
        }
    }

    return 0;
}

Makefile

objs=simple_break_point.o

prog=simple_break_point

all: $(objs)
  $(CC) -O0 -g $(objs) -o $(prog) -lbfd

.c.o:
  $(CC) -O0 -g -c -Wall $<

test:
  ./$(prog) say_hello

clean:
  rm -f core* $(objs) $(prog)

実行例

ここでブレークポイントを設定する関数のアドレスはnmで見るとこうなってます。

masami@saga:~/codes/simple_break_point$ nm ./simple_break_point | grep "say_hello\|main"
                 U __libc_start_main@@GLIBC_2.2.5
0000000000402a69 T main
00000000004028c7 t say_hello
ブレークポイントにヒットする場合
masami@saga:~/codes/simple_break_point$ ./simple_break_point say_hello
[*] set break point at 0x4028c7
stub function is called
ブレークポイントにヒットしない場合

すでに実行されたmain()に設定することでブレークポイントにヒットしない感じで。

mi@saga:~/codes/simple_break_point$ ./simple_break_point main
[*] set break point at 0x402a69
hello, world
program normally finishd