スクリプトからchrootを終了する

スクリプトからchrootを終了する

私のスクリプトは、chrootGRUB を USB にインストールするためのケージを作成します。もちろん、sudo として実行されます。

SYSTEM_DIRS=(etc bin sbin var lib lib64 usr proc sys dev tmp)

boot_partition=/media/user/boot

for dir in ${SYSTEM_DIRS[@]}; do
  mount --bind /$dir ${boot_partition}/${dir}
done

次に、 内でいくつかのコマンドを実行しますchroot

chroot ${boot_partition}/ touch foo # works fine
...

しかし、コマンドを実行したいときexit

chroot ${boot_partition}/ exit

次のような結果になります:

chroot: failed to execute the command <<exit>>: No such file or directory

なぜこのようなことが起こるのでしょうか?また、修正方法はあるのでしょうか?

答え1

exitは、スタンドアロンの実行可能ファイルではなく、シェルに組み込まれているため、 では実行できませんchroot。ただし、実行できたとしても、コマンドは何も実行しません。

このコマンドはchroot/executableのコンテキストで実行されます/path

chroot /path /executable

呼び出し元は chroot 内に留まりません。/executable実行が終了するとすぐに暗黙的に終了します。

mkdir -p /tmp/cr/{bin,lib,lib64}
cp -p /bin/pwd /tmp/cr/bin
cp -p $(find /lib* /usr/lib* -name 'libc.so*') /tmp/cr/lib
cp -p $(find /lib* /usr/lib* -name 'ld-linux-x86-64.so*') /tmp/cr/lib64

/bin/pwd                   # "/root"
chroot /tmp/cr /bin/pwd    # "/"
/bin/pwd                   # "/root"

答え2

私も同じ質問に遭遇しましたが、私の解決策は、chroot を実行するときに、その後に exit を追加することです。

chroot /chroot_path && exit

ユーザーがchrootを終了すると、シェル全体が終了します。

関連情報