mirror of
https://github.com/flatcar/scripts.git
synced 2025-09-24 07:01:13 +02:00
Allow specifying environment variables w/ enter_chroot again.
Examples of how people might be using enter_chroot: 1. ./enter_chroot [chroot_flags] VAR1=val1 VAR2=val2 -- cmd arg1 arg2 Set env vars and run cmd w/ args 2. ./enter_chroot [chroot_flags] VAR1=val1 VAR2=val2 Set env vars and run shell 3. ./enter_chroot [chroot_flags] -- cmd arg1 arg2 Run cmd w/ args 4. ./enter_chroot [chroot_flags] VAR1=val1 VAR2=val2 cmd arg1 arg2 Like #1 _if_ args aren't flags (if they are, enter_chroot will claim them) 5. ./enter_chroot [chroot_flags] cmd arg1 arg2 Like #3 _if_ args aren't flags (if they are, enter_chroot will claim them) I also updated the help to indicate that whole-command quoting is no longer supported. If you really need whole-command quoting (maybe you want in- chroot redirection), you'll need to use sh -c like: ./enter_chroot.sh -- sh -c "echo \"Save me\" > /tmp/save.txt" You should avoid single quotes in the command and arguments. This isn't a new limitation: it's shflags related. Change-Id: I0452a8730ac9b8197834edc753b9eece69896135 BUG=chromium-os:7072 TEST=Tried a whole bunch of these commands. Review URL: http://codereview.chromium.org/5840003
This commit is contained in:
parent
4228f22c97
commit
9362fa85c7
@ -34,23 +34,53 @@ DEFINE_boolean unmount $FLAGS_FALSE "Only tear down mounts."
|
|||||||
DEFINE_boolean ssh_agent $FLAGS_TRUE "Import ssh agent."
|
DEFINE_boolean ssh_agent $FLAGS_TRUE "Import ssh agent."
|
||||||
|
|
||||||
# More useful help
|
# More useful help
|
||||||
FLAGS_HELP="USAGE: $0 [flags] [VAR=value] [-- \"command\"]
|
FLAGS_HELP="USAGE: $0 [flags] [VAR=value] [-- command [arg1] [arg2] ...]
|
||||||
|
|
||||||
One or more VAR=value pairs can be specified to export variables into
|
One or more VAR=value pairs can be specified to export variables into
|
||||||
the chroot environment. For example:
|
the chroot environment. For example:
|
||||||
|
|
||||||
$0 FOO=bar BAZ=bel
|
$0 FOO=bar BAZ=bel
|
||||||
|
|
||||||
If [-- \"command\"] is present, runs the command inside the chroot,
|
If [-- command] is present, runs the command inside the chroot,
|
||||||
after changing directory to /$USER/trunk/src/scripts. Note that the
|
after changing directory to /$USER/trunk/src/scripts. Note that neither
|
||||||
command should be enclosed in quotes to prevent interpretation by the
|
the command nor args should include single quotes. For example:
|
||||||
shell before getting into the chroot. For example:
|
|
||||||
|
|
||||||
$0 -- \"./build_platform_packages.sh\"
|
$0 -- ./build_platform_packages.sh
|
||||||
|
|
||||||
Otherwise, provides an interactive shell.
|
Otherwise, provides an interactive shell.
|
||||||
"
|
"
|
||||||
|
|
||||||
|
# Double up on the first '--' argument. Why? For enter_chroot, we want to
|
||||||
|
# emulate the behavior of sudo for setting environment vars. That is, we want:
|
||||||
|
# ./enter_chroot [flags] [VAR=val] [-- command]
|
||||||
|
# ...but shflags ends up eating the '--' out of the command line and gives
|
||||||
|
# us back "VAR=val" and "command" together in one chunk. By doubling up, we
|
||||||
|
# end up getting what we want back from shflags.
|
||||||
|
#
|
||||||
|
# Examples of how people might be using enter_chroot:
|
||||||
|
# 1. ./enter_chroot [chroot_flags] VAR1=val1 VAR2=val2 -- cmd arg1 arg2
|
||||||
|
# Set env vars and run cmd w/ args
|
||||||
|
# 2. ./enter_chroot [chroot_flags] VAR1=val1 VAR2=val2
|
||||||
|
# Set env vars and run shell
|
||||||
|
# 3. ./enter_chroot [chroot_flags] -- cmd arg1 arg2
|
||||||
|
# Run cmd w/ args
|
||||||
|
# 4. ./enter_chroot [chroot_flags] VAR1=val1 VAR2=val2 cmd arg1 arg2
|
||||||
|
# Like #1 _if_ args aren't flags (if they are, enter_chroot will claim them)
|
||||||
|
# 5. ./enter_chroot [chroot_flags] cmd arg1 arg2
|
||||||
|
# Like #3 _if_ args aren't flags (if they are, enter_chroot will claim them)
|
||||||
|
_FLAGS_FIXED=''
|
||||||
|
_SAW_DASHDASH=0
|
||||||
|
while [ $# -gt 0 ]; do
|
||||||
|
_FLAGS_FIXED="${_FLAGS_FIXED:+${_FLAGS_FIXED} }'$1'"
|
||||||
|
if [ $_SAW_DASHDASH -eq 0 ] && [[ "$1" == "--" ]]; then
|
||||||
|
_FLAGS_FIXED="${_FLAGS_FIXED:+${_FLAGS_FIXED} }'--'"
|
||||||
|
_SAW_DASHDASH=1
|
||||||
|
fi
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
eval set -- "${_FLAGS_FIXED}"
|
||||||
|
|
||||||
|
|
||||||
# Parse command line flags
|
# Parse command line flags
|
||||||
FLAGS "$@" || exit 1
|
FLAGS "$@" || exit 1
|
||||||
eval set -- "${FLAGS_ARGV}"
|
eval set -- "${FLAGS_ARGV}"
|
||||||
@ -303,9 +333,9 @@ git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all user.email \
|
|||||||
# Run command or interactive shell. Also include the non-chrooted path to
|
# Run command or interactive shell. Also include the non-chrooted path to
|
||||||
# the source trunk for scripts that may need to print it (e.g.
|
# the source trunk for scripts that may need to print it (e.g.
|
||||||
# build_image.sh).
|
# build_image.sh).
|
||||||
sudo chroot "$FLAGS_chroot" sudo -i -u $USER $CHROOT_PASSTHRU \
|
sudo -- chroot "$FLAGS_chroot" sudo -i -u $USER $CHROOT_PASSTHRU \
|
||||||
EXTERNAL_TRUNK_PATH="${FLAGS_trunk}" LANG=C SSH_AGENT_PID="${SSH_AGENT_PID}" \
|
EXTERNAL_TRUNK_PATH="${FLAGS_trunk}" LANG=C SSH_AGENT_PID="${SSH_AGENT_PID}" \
|
||||||
SSH_AUTH_SOCK="${SSH_AUTH_SOCK}" -- "$@"
|
SSH_AUTH_SOCK="${SSH_AUTH_SOCK}" "$@"
|
||||||
|
|
||||||
# Remove trap and explicitly unmount
|
# Remove trap and explicitly unmount
|
||||||
trap - EXIT
|
trap - EXIT
|
||||||
|
Loading…
x
Reference in New Issue
Block a user