Description: ubuntu template: install bound user's shell
 If a host user is bound into the container (-b), make sure that his
 shell is installed in the container.
 This patch will be forwarded upstream.
Author: Serge Hallyn <serge.hallyn@ubuntu.com>
Bug-Ubuntu: https://bugs.launchpad.net/ubuntu/+source/lxc/+bug/936762
Forwarded: no

Index: lxc/templates/lxc-ubuntu.in
===================================================================
--- lxc.orig/templates/lxc-ubuntu.in	2012-02-20 14:13:47.883696005 -0600
+++ lxc/templates/lxc-ubuntu.in	2012-02-20 14:44:30.228831716 -0600
@@ -478,20 +478,31 @@
     user=$2
 
     # copy /etc/passwd, /etc/shadow, and /etc/group entries into container
-    pwd=`getent passwd $user`
-    if [ $? -ne 0 ]; then
-        echo 'Warning: failed to copy password entry for $user'
-	return
-    else
-        echo $pwd >> $rootfs/etc/passwd
+    pwd=`getent passwd $user` || { echo "Failed to copy password entry for $user"; false; }
+    echo $pwd >> $rootfs/etc/passwd
+
+    # make sure user's shell exists in the container
+    shell=`echo $pwd | cut -d: -f 7`
+    if [ ! -x $rootfs/$shell ]; then
+        echo "shell $shell for user $user was not found in the container."
+        pkg=`dpkg -S $(readlink -m $shell) | cut -d ':' -f1`
+        echo "Installing $pkg"
+        chroot $rootfs apt-get --force-yes -y install $pkg
     fi
+
     shad=`getent shadow $user`
-    echo $shad >> $rootfs/etc/shadow
+    echo "$shad" >> $rootfs/etc/shadow
 
     # bind-mount the user's path into the container's /home
     h=`getent passwd $user | cut -d: -f 6`
     mkdir -p $rootfs/$h
     echo "$h $rootfs/$h none bind 0 0" >> $path/fstab
+
+    # Make sure the group exists in container
+    chroot $rootfs getent group $user || { \
+        grp=`getent group $user`
+        echo "$grp" >> $rootfs/etc/group
+    }
 }
 
 usage()
