Skip to content

Instantly share code, notes, and snippets.

@akihiro
Last active January 9, 2020 10:24
Show Gist options
  • Save akihiro/7b9525153b939b8ca0b0baa3f6af6be3 to your computer and use it in GitHub Desktop.
Save akihiro/7b9525153b939b8ca0b0baa3f6af6be3 to your computer and use it in GitHub Desktop.
#!/bin/bash
[[ -z $STATE ]] && export STATE=init
case "$STATE" in
init)
export STATE=unshare
sudo -E unshare -mpf "$0"
# cleanup が不要ならexecでも良い
exit 0
;;
unshare)
export STATE=pivot_root
# pivot_rootとか
exit
;;
pivot_root)
export STATE=sudo
# デフォルトの作業ディレクトリとかにcdしたり環境変数設定したり
# mountとか
sudo -E -i -u container "$0"
exit
;;
sudo)
# enter interactive shell
bash
esac
## chrootなどする場合はfiledescriptorを持ち回すとか?
# 10<$0
# sudo -E -i -u container /dev/fd/10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment