
chroot
作業ディレクトリを設定できるコマンドのラッパーを作成したいとします。そうでなければ、chroot
意味体系が維持される。したがって、意味は次のようになります。
chroot.sh <chroot-dir> <working-dir> <command> [arg]...
私の無邪気な試みは次のとおりです
#!/bin/sh
chroot_dir=$1
working_dir=$2
shift 2
chroot "$chroot_dir" sh -c "cd $working_dir; $*"
ただし、これは正しく処理されません。
chroot.sh /path/to/chroot /tmp touch 'filename with space'
正しく実装する方法がわかりません。 bashしか使えませんか?
私のCentOS 6システムでは、コマンドはchroot
作業ディレクトリの設定をサポートしていません。他のシステムではそうではないかもしれません。
答え1
これを処理する方法は、パラメータを参照リストに渡すことです。後ろに以下で呼び出されるコマンドchroot
:
chroot "$chroot_dir" sh -c 'cd "$1" && shift && "$@"' - "$working_dir" "$@"
以下は、一般的な環境設定を含む実際の例ですchroot
。
設定
# Where
chroot="$HOME/chroot"
# Directory structure
mkdir -p -m755 "$chroot"/{bin,dev,lib,tmp}
chmod 1777 "$chroot"/tmp
# Shell
cp -a /bin/dash "$chroot"/bin/sh
# Devices
cp -a /dev/{null,tty} "$chroot"/dev
# Libraries
cp -p $(ldd /bin/dash | sed 's/^.*=> //' | awk '!/vdso/ {print $1}' ) "$chroot"/lib/
# Demonstration executable
cat >"$chroot"/bin/args <<'EOF'
#!/bin/sh
#
echo "Got $# arg(s): $*"
echo "CWD: $(pwd)"
printf '> %s <\n' "$@"
EOF
chmod a+x "$chroot"/bin/args
実行(アプリケーションargs
)
working_dir=/tmp
chroot "$chroot" /bin/sh -c 'cd "$1" && shift && "$@"' - "$working_dir" args 'one two' three
出力
Got 2 arg(s): one two three
CWD: /tmp
> one two <
> three <
出力が示すように、スペースとパラメータは正しく保持されます。
答え2
ついに私は次の解決策を思いついた。
#!/bin/sh
chroot_dir=$1
working_dir=$2
shift 2
printf -v CMDS " %q" "$@"
chroot "$chroot_dir" sh -c "cd \"$working_dir\";$CMDS"
答え3
これはあなたが望むことをしているようです:
#!/bin/bash
chroot_dir="$1"
shift
working_dir="$1"
shift
cmd="$@"
chroot "${chroot_dir}" sh -c "cd ${working_dir} ; ${cmd}"
/ tmpはスクリプトでのみ始まります。
# ls /tmp
chroot.sh
/mnt/foo
バインドマウント付きのchrootがあります/
。
# /tmp/chroot.sh /mnt/foo /tmp touch '"filename with spaces"'
次に、ファイルがfilename with spaces
存在することを確認してください。
# ls -1 /tmp
chroot.sh
'filename with spaces'