[lxc-users] Custom lxc container hangs in lxc-console

manik sheeri sheeri185adidam at gmail.com
Wed Oct 12 00:13:42 UTC 2016


I am trying to run my LFS (Linux from scratch) inside a container. I
created LFS from its stable version book and using that as a rootfs for my
container.

I created the LFS rootfs at /home/manik/toolchain/lfs . I am using this as
a rootfs source for my container. I will copy the directories inside my
container rootfs from this location.

Below is the template that I created from busybox template of lxc package.I
named this tempalate as lxc-lfs and copied into the directory
/usr/share/lxc/templates

#!/bin/bash

LXC_MAPPED_UID=
LXC_MAPPED_GID=

# Make sure the usual locations are in PATH
export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin

am_in_userns() {
    [ -e /proc/self/uid_map ] || { echo no; return; }
    [ "$(wc -l /proc/self/uid_map | awk '{ print $1 }')" -eq 1 ] || {
echo yes; return; }
    line=$(awk '{ print $1 " " $2 " " $3 }' /proc/self/uid_map)
    [ "$line" = "0 0 4294967295" ] && { echo no; return; }
    echo yes
}

configure_lfs() {
    rootfs=$1
    name=$2
    lfsroot=$3
    res=0

tree=( \
bin \
boot \
dev \
etc \
home \
lib \
media \
mnt \
opt \
proc \
root \
run \
sbin \
srv \
sys \
tmp \
usr \
var )


for d in ${tree[@]}
do
    mkdir -p $rootfs/$d
    cp -r --preserve $lfsroot/$d/. $rootfs/$d
done

pushd $rootfs/dev > /dev/null || return 1

mknod -m 666 tty c 5 0       || res=1
mknod -m 666 console c 5 1   || res=1
mknod -m 666 tty0 c 4 0      || res=1
mknod -m 666 tty1 c 4 0      || res=1
mknod -m 666 tty5 c 4 0      || res=1
mknod -m 600 ram0 b 1 0      || res=1
mknod -m 666 null c 1 3      || res=1
mknod -m 666 zero c 1 5      || res=1
mknod -m 666 urandom c 1 9   || res=1

mkdir pts || res=1

popd > /dev/null

cat <<EOF >> $rootfs/etc/init.d/rcS
#!/bin/sh
/bin/syslogd
/bin/mount -a
EOF

# executable
chmod 744 $rootfs/etc/init.d/rcS || res=1

# launch rcS first then make a console available
# and propose a shell on the tty, the last one is
# not needed
cat <<EOF >> $rootfs/etc/inittab
::sysinit:/etc/init.d/rcS
tty1::respawn:/sbin/agetty -L tty1 115200 vt100
console::askfirst:/bin/sh
EOF
# writable and readable for other
chmod 644 $rootfs/etc/inittab || res=1

# /etc/fstab must exist for "mount -a"
touch $rootfs/etc/fstab || res=1

return $res
}

copy_configuration() {

    path=$1
    rootfs=$2
    name=$3

    grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs
= $rootfs" >> $path/config
    cat <<EOF >> $path/config
lxc.haltsignal = SIGUSR1
lxc.rebootsignal = SIGTERM
lxc.utsname = $name
lxc.tty = 1
lxc.pts = 1
lxc.cap.drop = sys_module mac_admin mac_override sys_time

# When using LXC with apparmor, uncomment the next line to run unconfined:
#lxc.aa_profile = unconfined

lxc.mount.auto = cgroup:mixed proc:mixed sys:mixed
lxc.mount.entry = shm /dev/shm tmpfs defaults 0 0
EOF

echo "lxc.mount.entry = /sys/kernel/security sys/kernel/security none
ro,bind,optional 0 0" >>$path/config
return 0
}

in_userns=0
[ $(am_in_userns) = "yes" ] && in_userns=1

usage()
{
    cat <<EOF
$1 -h|--help -p|--path=<path> -n|--name=<name>
--lfsroot=<lfsroot>[Mandatory     option]
EOF
    return 0
}


options=$(getopt -o hp:n: -l help,rootfs:,path:,name:,lfsroot: -- "$@")
if [ $? -ne 0 ]; then
    usage $(basename $0)
    exit 1
fi
eval set -- "$options"

while true
do
    case "$1" in
        -h|--help)      usage $0 && exit 0;;
        -p|--path)      path=$2; shift 2;;
        --rootfs)       rootfs=$2; shift 2;;
        --lfsroot)      lfsroot=$2; shift 2;;
        -n|--name)      name=$2; shift 2;;
        --)             shift 1; break ;;
       *)              break ;;
    esac
done

if [ -z "$lfsroot" ]; then
    echo "--lfsroot argument is required."
    exit 1
fi

if [ "$(id -u)" != "0" ]; then
    echo "This script should be run as 'root'"
    exit 1
fi

if [ -z "$path" ]; then
    echo "'path' parameter is required"
    exit 1
fi

# detect rootfs
config="$path/config"
rootfs="$path/rootfs"

configure_lfs $rootfs $name $lfsroot
if [ $? -ne 0 ]; then
    echo "failed to configure LFS's rootfs"
    exit 1
fi

copy_configuration $path $rootfs $name
if [ $? -ne 0 ]; then
    echo "failed to write lfs-lxc configuration file"
    exit 1
fi

I used the below command to create my container:

sudo lxc-create -t lfs -n lfs-lxc-container --lxcpath=/home/manik/toolchain

I can successfully use the following command:

sudo lxc-attach -n lfs-lxc-container --lxcpath=/home/manik/toolchain

I get root bash prompt which is understandable as the man pages for
lxc-attach says to run the shell of the user if no specific command is
given.

However, lxc-console command hangs. I am triggering the command like:

sudo lxc-console -n lfs-lxc-container --lxcpath=/home/manik/toolchain

Using the above lxc-attach since I get the bash prompt, I executed the
command:

bash-4.3# ps -efa
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 22:37 ?        00:00:00 init boot
root        29     0  0 23:27 ?        00:00:00 /bin/bash
root        30    29  0 23:27 ?        00:00:00 ps -efa

Looks like getty is not working/running.

Anyone got any idea ? I am not an expert in terminals or its services, so
if you know what is wrong please help.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linuxcontainers.org/pipermail/lxc-users/attachments/20161011/5dd475de/attachment.html>


More information about the lxc-users mailing list