Beenden Sie chroot über das Skript

Beenden Sie chroot über das Skript

Mein Skript erstellt einen chrootKäfig zur Installation von GRUB auf einem USB-Stick, natürlich als sudo ausführen:

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

Führen Sie dann einige Befehle darin aus chroot:

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

Aber wenn ich den Befehl ausführen möchteexit

chroot ${boot_partition}/ exit

Ich bekomme:

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

Warum passiert das und gibt es eine Möglichkeit, das Problem zu beheben?

Antwort1

exitist eine in die Shell integrierte und keine eigenständige ausführbare Datei, was bedeutet, dass es nicht von ausgeführt werden kann chroot. Aber selbst wenn es möglich wäre, würde Ihr Befehl nichts bewirken.

Dieser Befehl wird /executableim Kontext eines /pathChroots ausgeführt:

chroot /path /executable

Der Anrufer bleibt nicht in diesem Chroot; es erfolgt ein impliziter Ausgang, sobald die /executableAusführung beendet ist:

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"

Antwort2

Ich habe dieselbe Frage erhalten. Meine Lösung besteht darin, beim Ausführen von chroot „exit“ danach anzuhängen:

chroot /chroot_path && exit

Wenn der Benutzer dann chroot verlässt, wird die gesamte Shell beendet

verwandte Informationen