Merge pull request #72 from fsquillace/issue_71_chroot

Issue #71: Use chroot to fallback in root mode
This commit is contained in:
Filippo Squillace 2015-05-02 22:48:50 +01:00
commit 03d687ed12
4 changed files with 53 additions and 17 deletions

View file

@ -1,5 +1,7 @@
language: bash
sudo: required
install:
- PATH=$PWD/bin:$PATH
- juju -f echo 'Installing juju'
@ -8,13 +10,14 @@ install:
- juju -f pacman --noconfirm -S base-devel
script:
- SKIP_ROOT_TESTS=1 ./tests/test_all.sh
- ./tests/test_all.sh
# Test on installing package from AUR
- juju -f yaourt --noconfirm -S tcptraceroute
- sudo $PWD/bin/juju -r tcptraceroute localhost
# Test on installing package from official repo
- juju -f pacman --noconfirm -S sysstat
- juju -f iostat
# Test on installing package from official repo with root access (TODO: may not work in ubuntu)
#- juju -f pacman --noconfirm -S iftop
#- sudo bin/juju -r iftop -t -s 5
- juju -f pacman --noconfirm -S tree
- juju -f tree
# Test on installing package from official repo with root access
- juju -f pacman --noconfirm -S iftop
- sudo bin/juju -r iftop -t -s 5
- yes | juju -d

View file

@ -96,6 +96,10 @@ Check out the proot options with:
juju -p "--help"
###Automatic fallback to classic chroot###
Since the [arch-chroot](https://wiki.archlinux.org/index.php/Chroot) may not work
on some distros, JuJu automatically tries to fallback to the classic chroot.
### JuJu as a container ###
Although JuJu has not been designed to be a complete container, it is even possible to
virtualize the process tree thanks to the [systemd container](https://wiki.archlinux.org/index.php/Systemd-nspawn).

View file

@ -75,9 +75,10 @@ PROOT_LINK=http://static.proot.me/proot-${ARCH}
SH=("/bin/sh" "--login")
CHROOT=${JUJU_HOME}/usr/bin/arch-chroot
CLASSIC_CHROOT=${JUJU_HOME}/usr/bin/chroot
TRUE=/usr/bin/true
ID="/usr/bin/id -u"
CHOWN="/usr/bin/chown"
CHOWN="${JUJU_HOME}/usr/bin/chown"
################################# MAIN FUNCTIONS ##############################
@ -164,8 +165,18 @@ function run_juju_as_root(){
trap - QUIT EXIT ABRT KILL TERM INT
trap "[ -z $uid ] || ${CHOWN} -R ${uid} ${JUJU_HOME}" EXIT QUIT ABRT KILL TERM INT
JUJU_ENV=1 ${CHROOT} $JUJU_HOME "${SH[@]}" "-c" "${cmd}"
local ret=$?
if ${CHROOT} $JUJU_HOME ${TRUE} &> /dev/null
then
JUJU_ENV=1 ${CHROOT} $JUJU_HOME "${SH[@]}" "-c" "${cmd}"
local ret=$?
elif ${CLASSIC_CHROOT} $JUJU_HOME ${TRUE} &> /dev/null
then
warn "Warning: The executable arch-chroot does not work, falling back to classic chroot"
JUJU_ENV=1 ${CLASSIC_CHROOT} $JUJU_HOME "${SH[@]}" "-c" "${cmd}"
local ret=$?
else
die "Error: Chroot does not work"
fi
# The ownership of the files in JuJu is assigned to the real user
[ -z $uid ] || ${CHOWN} -R ${uid} ${JUJU_HOME}
@ -270,7 +281,8 @@ function build_image_juju(){
info "Installing pacman and its dependencies..."
# The archlinux-keyring and libunistring are due to missing dependencies declaration in ARM archlinux
# yaourt requires sed
sudo pacstrap -G -M -d ${maindir}/root pacman arch-install-scripts binutils libunistring nano archlinux-keyring sed
# coreutils is needed for chown
sudo pacstrap -G -M -d ${maindir}/root pacman arch-install-scripts coreutils binutils libunistring nano archlinux-keyring sed
sudo bash -c "echo 'Server = $DEFAULT_MIRROR' >> ${maindir}/root/etc/pacman.d/mirrorlist"
info "Generating the locales..."

View file

@ -104,29 +104,46 @@ function test_run_juju_as_root(){
install_mini_juju
CHROOT="sudo $CHROOT"
CLASSIC_CHROOT="sudo $CLASSIC_CHROOT"
CHOWN="sudo $CHOWN"
local output=$(run_juju_as_root pwd)
assertEquals "$output" "/"
assertEquals "/" "$output"
run_juju_as_root [ -e /run/lock ]
assertEquals $? 0
assertEquals 0 $?
run_juju_as_root [ -e $HOME ]
assertEquals $? 0
assertEquals 0 $?
# test that normal user has ownership of the files created by root
run_juju_as_root touch /a_root_file
local output=$(run_juju_as_root stat -c '%u' /a_root_file)
assertEquals "$output" "$UID"
assertEquals "$UID" "$output"
SH=("sh" "--login" "-c" "type -t type")
local output=$(run_juju_as_root)
assertEquals "$output" "builtin"
assertEquals "builtin" "$output"
SH=("sh" "--login" "-c" "[ -e /run/lock ]")
run_juju_as_root
assertEquals $? 0
assertEquals 0 $?
SH=("sh" "--login" "-c" "[ -e $HOME ]")
run_juju_as_root
assertEquals $? 0
assertEquals 0 $?
}
function test_run_juju_as_classic_root(){
[ $SKIP_ROOT_TESTS -eq 1 ] && return
install_mini_juju
CHROOT="sudo unknowncommand"
CLASSIC_CHROOT="sudo $CLASSIC_CHROOT"
CHOWN="sudo $CHOWN"
local output=$(run_juju_as_root pwd 2> /dev/null)
assertEquals "/" "$output"
run_juju_as_root [ -e /run/lock ] 2> /dev/null
assertEquals 0 $?
run_juju_as_root [ -e $HOME ] 2> /dev/null
assertEquals 0 $?
}
function test_run_juju_as_user(){