Permalink
Browse files

ONLY create new user when chroot env is newly created.

  • Loading branch information...
1 parent ee28584 commit 9f1ecaa04d4aa13299a006e1815abfdcc8b4b59c @ngty committed Jan 20, 2012
Showing with 31 additions and 10 deletions.
  1. +31 −10 bin/s/change-root
View
@@ -5,6 +5,7 @@ PACKAGES=( base base-devel sudo git openssh )
CONFIGS=( /etc/resolv.conf /etc/sudoers )
LOGIN=`whoami`
PROMPT="($0:$1)"
+ALREADY_EXIST=1
if [[ -z "$1" ]]; then
echo "$PROMPT USAGE: $ $0 <NAME>"
@@ -14,6 +15,8 @@ fi
# Create chroot dir if it isn't already present,
# see https://wiki.archlinux.org/index.php/DeveloperWiki:Building_in_a_Clean_Chroot#Classic_Way
if [[ ! -d $CHROOT ]]; then
+ ALREADY_EXIST=''
+
echo "$PROMPT preparing fresh chroot environment ... "
sudo mkdir -p $CHROOT
sudo pacman -S --needed devtools
@@ -32,21 +35,39 @@ sudo mount -t tmpfs /tmp $CHROOT/root/tmp
# Copy whatever files we need
for c in ${CONFIGS[@]}; do sudo cp $c $CHROOT/root/$c ; done
-# Automatically create $LOGIN & assign him to "sudo"
-sudo sh -c "cat <<EOF > $CHROOT/root/root/.bashrc
- groupadd sudo 2>&1
- useradd --home /home/$LOGIN --groups sudo --create-home $LOGIN
+# 1st time access, automatically create $LOGIN & assign him to "sudo"
+if [[ -z $ALREADY_EXIST ]]; then
+ sudo sh -c "cat <<EOF > $CHROOT/root/root/.bashrc
+groupadd sudo 2>&1
+useradd --home /home/$LOGIN --groups sudo --create-home $LOGIN
+
+echo \"$PROMPT setting passwd for user '$LOGIN' ... \"
+passwd $LOGIN
- echo \"$PROMPT setting passwd for user '$LOGIN' ... \"
- passwd $LOGIN
+# Create a flag to ease checking of whether user is chrooted
+echo $PROJECT > /tmp/.chrootid
- echo \"$PROMPT logging in as '$LOGIN' ... \"
- login $LOGIN TERM=screen-256color
+echo \"$PROMPT logging in as '$LOGIN' ... \"
+login $LOGIN TERM=screen-256color
- echo \"$PROMPT exiting chroot ... \"
- exit
+echo \"$PROMPT exiting chroot ... \"
+exit
EOF"
+# Nth access, automatically login to user
+else
+ sudo sh -c "cat <<EOF > $CHROOT/root/root/.bashrc
+# Create a flag to ease checking of whether user is chrooted
+echo $PROJECT > /tmp/.chrootid
+
+echo \"$PROMPT logging in as '$LOGIN' ... \"
+login $LOGIN TERM=screen-256color
+
+echo \"$PROMPT exiting chroot ... \"
+exit
+EOF"
+fi
+
# Changing root
sudo chroot $CHROOT/root bash

0 comments on commit 9f1ecaa

Please sign in to comment.