grubbyコマンドを使ったSELinuxの設定変更

概要

システムの起動時にSELinuxモードの変更をおこなう場合は、 /etc/default/grubGRUB_CMDLINE_LINUX のカーネルパラメータを変更することで設定が出来る。

SELinuxを設定する例

パラメータの enforcing=1 を追記する。

GRUB_CMDLINE_LINUX="enforcing=1"

設定ファイルを手動で修正することでも変更が可能だが、 以下のように grubby コマンドを用いることでもカーネルパラメータの変更が可能となる。

grubby コマンドを用いたSELinuxの設定

Enforcing

$ sudo grubby --update-kernel ALL --args enforcing=1

OS再起動後、設定の確認

$ getenforce
Enforcing

設定の削除

$ sudo grubby --update-kernel ALL --remove-args 'enforcing'


Permissive

$ sudo grubby --update-kernel ALL --args enforcing=0

OS再起動後、設定の確認

$ getenforce
Permissive

設定の削除

$ sudo grubby --update-kernel ALL --remove-args 'enforcing'


Disabled

$ sudo grubby --update-kernel ALL --args selinux=0

OS再起動後、設定の確認

$ getenforce
Disabled

設定の削除

$ sudo grubby --update-kernel ALL --remove-args 'selinux'

UbuntuのVagrant(VirtualBox)環境で出たエラーの対応メモ

概要

Ubuntu上で動かしているVagrant(VirtualBox)環境で、 vagrant up しようとしたところエラーが出たためその対応をおこなった際のメモです。

OSバージョン

$ cat /etc/issue
Ubuntu 22.04.3 LTS


カーネルバージョン

$ uname -r
6.5.0-14-generic


Vagrantバージョン

$ vagrant -v
Vagrant 2.4.0


VirtualBoxバージョン

$ VBoxManage --version
7.0.14r161095


エラー出力内容と対応

出力されたエラー

$ vagrant up
The provider 'virtualbox' that was requested to back the machine
'node' is reporting that it isn't usable on this system. The
reason is shown below:

VirtualBox is complaining that the kernel module is not loaded. Please
run `VBoxManage --version` or open the VirtualBox GUI to see the error
message which should contain instructions on how to fix this error.


VBoxManage --version を実行

エラーに VBoxManage --version を実行するようメッセージが出力されたため実行します。

$ VBoxManage --version
WARNING: The vboxdrv kernel module is not loaded. Either there is no module
         available for the current kernel (6.5.0-14-generic) or it failed to
         load. Please recompile the kernel module and install it by

           sudo /sbin/vboxconfig

         You will not be able to start VMs until this problem is fixed.
7.0.14r161095


sudo /sbin/vboxconfig を実行

次に sudo /sbin/vboxconfig を実行するようメッセージが出力されたため実行します。

$ sudo /sbin/vboxconfig
vboxdrv.sh: Stopping VirtualBox services.
vboxdrv.sh: Starting VirtualBox services.
vboxdrv.sh: Building VirtualBox kernel modules.
vboxdrv.sh: failed: Look at /var/log/vbox-setup.log to find out what went wrong.

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


/var/log/vbox-setup.log を確認

エラーに /var/log/vbox-setup.log を確認するようメッセージが出力されたため内容を確認します。

$ cat /var/log/vbox-setup.log
Building the main VirtualBox module.
Error building the module:
make V=1 CONFIG_MODULE_SIG= CONFIG_MODULE_SIG_ALL= -C /lib/modules/6.5.0-14-generic/build M=/tmp/vbox.0 SRCROOT=/tmp/vbox.0 -j8 modules
make[1]: warning: -j8 forced in submake: resetting jobserver mode.
warning: the compiler differs from the one used to build the kernel
  The kernel was built by: x86_64-linux-gnu-gcc-12 (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0
  You are using:
make -f ./scripts/Makefile.build obj=/tmp/vbox.0 need-builtin=1 need-modorder=1
# cmd_mod /tmp/vbox.0/vboxdrv.mod
  printf '%s
'   linux/SUPDrv-linux.o SUPDrv.o SUPDrvGip.o SUPDrvSem.o SUPDrvTracer.o SUPLibAll.o common/string/strformatrt.o combined-agnostic1.o combined-agnostic2.o combined-os-specific.o | awk '!x[$0]++ { print("/tmp/vbox.0/"$0) }' > /tmp/vbox.0/vboxdrv.mod
# CC [M]  /tmp/vbox.0/linux/SUPDrv-linux.o
  gcc-12 -Wp,-MMD,/tmp/vbox.0/linux/.SUPDrv-linux.o.d -nostdinc -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h -I./ubuntu/include -include ./include/linux/compiler_types.h -D__KERNEL__ -std=gnu11 -fshort-wchar -funsigned-char -fno-common -fno-PIE -fno-strict-aliasing -Wall -Wundef -Werror=implicit-function-declaration -Werror=implicit-int -Werror=return-type -Werror=strict-prototypes -Wno-format-security -Wno-trigraphs -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -m64 -mno-80387 -mtune=generic -mno-red-zone -mcmodel=kernel -Wno-sign-compare -fno-asynchronous-unwind-tables -mfunction-return=thunk-extern -fno-jump-tables -mharden-sls=all -fpatchable-function-entry=16,16 -fno-delete-null-pointer-checks -O2 -Wframe-larger-than=1024 -fstack-protector-strong -Wno-main -fno-omit-frame-pointer -fno-optimize-sibling-calls -ftrivial-auto-var-init=zero -fzero-call-used-regs=used-gpr -pg -mrecord-mcount -falign-functions=16 -Wvla -Wno-pointer-sign -Wno-maybe-uninitialized -Wno-array-bounds -Wno-alloc-size-larger-than -Wimplicit-fallthrough=5 -fno-strict-overflow -fno-stack-check -fconserve-stack -Werror=date-time -g -gdwarf-5 -include /tmp/vbox.0/include/VBox/SUPDrvMangling.h -fno-omit-frame-pointer -fno-pie -Wno-declaration-after-statement -I./include -I/tmp/vbox.0/ -I/tmp/vbox.0/include -I/tmp/vbox.0/r0drv/linux -D__KERNEL__ -DMODULE -DRT_WITHOUT_PRAGMA_ONCE -DRT_OS_LINUX -DIN_RING0 -DIN_RT_R0 -DIN_SUP_R0 -DVBOX -DRT_WITH_VBOX -DVBOX_WITH_HARDENING -DSUPDRV_WITH_RELEASE_LOGGER -DVBOX_WITHOUT_EFLAGS_AC_SET_IN_VBOXDRV -DIPRT_WITHOUT_EFLAGS_AC_PRESERVING -DVBOX_WITH_64_BITS_GUESTS -DCONFIG_VBOXDRV_AS_MISC -DRT_ARCH_AMD64  -fsanitize=bounds-strict -fsanitize=shift -fsanitize=bool -fsanitize=enum  -DMODULE  -DKBUILD_BASENAME='"SUPDrv_linux"' -DKBUILD_MODNAME='"vboxdrv"' -D__KBUILD_MODNAME=kmod_vboxdrv -c -o /tmp/vbox.0/linux/SUPDrv-linux.o /tmp/vbox.0/linux/SUPDrv-linux.c   ; ./tools/objtool/objtool --hacks=jump_label --hacks=noinstr --hacks=skylake --retpoline --rethunk --sls --stackval --static-call --uaccess --prefix=16   --module /tmp/vbox.0/linux/SUPDrv-linux.o
/bin/sh: 1: gcc-12: not found
make[2]: *** [scripts/Makefile.build:251: /tmp/vbox.0/linux/SUPDrv-linux.o] Error 127
make[1]: *** [Makefile:2037: /tmp/vbox.0] Error 2
make: *** [/tmp/vbox.0/Makefile-footer.gmk:133: vboxdrv] Error 2

エラーメッセージ内に /bin/sh: 1: gcc-12: not found と出力され、 gcc-12 がインストールされていないため、ビルドエラーが出たと思われるため、次に現在インストールされているgccパッケージを確認します。

$ dpkg -l
ii  gcc                                        4:11.2.0-1ubuntu1                       amd64        GNU C compiler
ii  gcc-11                                     11.4.0-1ubuntu1~22.04                   amd64        GNU C compiler
ii  gcc-11-base:amd64                          11.4.0-1ubuntu1~22.04                   amd64        GCC, the GNU Compiler Collection (base package)
ii  gcc-12-base:amd64                          12.3.0-1ubuntu1~22.04                   amd64        GCC, the GNU Compiler Collection (base package)

gcc-12 パッケージがインストールされていないため、インストールします。

sudo apt -y install gcc-12

gcc-12 をインストールしたため、先程実行したコマンドを再実行します。


再度 sudo /sbin/vboxconfig を実行

$ sudo /sbin/vboxconfig
vboxdrv.sh: Stopping VirtualBox services.
vboxdrv.sh: Starting VirtualBox services.
vboxdrv.sh: Building VirtualBox kernel modules.

エラーは出力されませんでした。


再度 /var/log/vbox-setup.log を確認

ビルドが正常に完了していることが確認出来ました。

$ cat /var/log/vbox-setup.log
Building the main VirtualBox module.
Building the net filter module.
Building the net adapter module.

最後に vagrant up も正常に実行出来ることが確認出来ました。

GitHubのPull RequestをローカルへcheckoutするPythonスクリプト

概要

GItHubでPull Requestの内容を手元のPCへcheckoutしてその内容を確認する際は以下の手順を実施します。

  1. 最初は普通にclone
  2. PRのIDとブランチ名を指定してgit fetch を実行
git fetch origin pull/1/develop

1 はPRのID、develop はブランチ名 3. ブランチが作成されたか確認

git branch -a

4.先程作成したブランチをチェックアウト

git checkout test

※詳細はGitHubの公式ドキュメントをご参照ください。

Pythonスクリプト化

Pull Requestをレビューする際に毎回上記の手順で複数回コマンドを打つのが大変だったのでスクリプト化することにしました。

Pull RequestのIDやbranch名などをスクリプトへオプションとして渡したかったのでPythonのclickというライブラリを使用してスクリプトの作成をおこないました。

作成したPythonスクリプト

#! /usr/bin/env python3

import click
import subprocess

CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])


@click.command(context_settings=CONTEXT_SETTINGS)
@click.option(
    "-u",
    "--url",
    type=str,
    required=True,
    help="GitHub Repogitory URL",
)
@click.option(
    "-i",
    "--id",
    type=int,
    required=True,
    help="Pull Request ID",
)
@click.option(
    "-b",
    "--branch",
    type=str,
    required=True,
    help="Pull Request Branch",
)
def clone(
    url: str,
    id: int,
    branch: str,
):

    exec = f'git clone {url} && cd "$(basename "$_" .git)" && git fetch origin pull/{id}/head:{branch} && git checkout {branch}'

    try:
        subprocess.run(exec, shell=True)
    except BaseException:
        print("command failed")

Gitコマンドを実行する部分はsubprocessを利用しました。

スクリプト使い方

python3 github-pr-check.py --url git@github.com:example/test.git --id 1 --branch develop

上記のようにオプションを指定して実行することが可能です。

  • --url GitHubのリポジトリのURLを指定
  • --id Pull RequestのIDを指定
  • --branch branch名を指定

help

ヘルプの表示も可能です。 ライブラリのclickを使うことでヘルプ機能も簡単に実装することが出来ました。

Usage: github-pr-check.py [OPTIONS]

Options:
  -u, --url TEXT     GitHub Repogitory URL  [required]
  -i, --id INTEGER   Pull Request ID  [required]
  -b, --branch TEXT  Pull Request Branch  [required]
  -h, --help         Show this message and exit.