[lxc-users] Making a container behave like a VM(ish)

Craig Ruff cruff at ucar.edu
Tue Mar 11 15:32:55 UTC 2014


On Tue, Mar 11, 2014 at 03:18:52PM +0000, Tom Taylor wrote:
> Secondly there is no root device when you do a df.  I guess I need to generate
> an mtab somewhow but I'm not sure what to put in there.

I use a script (/etc/lxc.init inside the container) to fake up mtab entries so
df works.  It reads the container's lxc.fstab file that has itself been bind
mounted into the container during the processing of the lxc.mount option to
make the appropriate mtab entries.  It strips off the path to the container's
rootfs where appropriate.  It handles the entry for the loop back mounted file
system of the container's /tmp, as well as any additional file systems that are
managed outside the container but end up bind mounted inside it via mount
propagation for which I want df to work (the lines starting with "#df").

sample lxc.fstab
----------------
/containers/strings/lxc.fstab /containers/strings/rootfs/etc/lxc.fstab none bind,ro 0 0
#/containers/strings/tmpfs /containers/strings/rootfs/tmp ext4 defaults 0 0
/containers/strings/rootfs/tmp /containers/strings/rootfs/var/tmp ext4 bind 0 0
/containers/glade /containers/strings/rootfs/glade none rbind,create=dir 0 0
#df /glade/p gpfs bind 0 0
#df /glade/scratch gpfs bind 0 0
#df /glade/u gpfs bind 0 0


lxc.init
--------
#!/bin/bash

lxctab=/etc/lxc.fstab
mtab=/etc/mtab

[ -e $tab ] || return 0;

first=1
cat <<EOF > $mtab
none   /        ext4   rw                  0 0
proc   /proc    proc   nodev,noexec,nosuid 0 0
devpts /dev/pts devpts defaults            0 0
sysfs  /sys     sysfs  defaults            0 0
EOF

while read src dst type opts x y; do
	if [ $first = 1 ]; then
		first=0
		prefix=${dst%/etc/lxc.fstab}
		continue
	fi

	case $src in
	\#df) src=none ;;
	esac

	dst=${dst#$prefix}
	case $dst in
	/tmp)
		src=none
		chmod 1777 /tmp
		;;

	/var/tmp)
		src=none
		;;
	esac

	echo "$src $dst $type $opts $x $y" >> $mtab
done < $lxctab

umount $lxctab
exit 0


More information about the lxc-users mailing list