Do not try to auto-mount containers logical volume during lxc-create

Written by - 0 comments

Published on - Listed in Linux LXC


For a fully automated deployment using LXC (Linux Containers), I tried to automate the mounting of the containers logical volume at the end of the LXC template:

# If LVM was used, mount it
if [[ -n $(cat $config  | egrep '^lxc.rootfs' | grep dev) ]]; then
  lxclv=$(egrep '^lxc.rootfs' $config | awk -F'=' '{print $2}' | sed 's/ //')
  echo mount -v $lxclv $path/rootfs
  mount -v $lxclv $path/rootfs
  echo "$lxclv $path/rootfs  ext4  defaults  0  0" >> /etc/fstab
fi

Without loosing too many words at the begin: It doesn't work.

As soon as the LXC is created, the rootfs (/var/lib/mytestlxc/rootfs) is empty, although it appears mounted in the mount command:

mount | grep mytestlxc
/dev/mapper/vglxc-mytestlxc on /var/lib/lxc/mytestlxc/rootfs type ext4 (rw)

Even stranger when I try to unmount the supposedly mounted lv:

umount /var/lib/lxc/mytestlxc/rootfs
umount: /var/lib/lxc/mytestlxc/rootfs: not mounted

The LXC template is called by lxc-create (therefore its a child process). Once lxc-create finishes the job, it unmounts the containers logical volume.
Why? During the creation of an LXC container, the rootfs is written into a temporary mounted location:

[...]
clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7fc45ff1fb90) = 13264
wait4(13264, File descriptor 3 (/var/lib/lxc/mytestlxc/partial) leaked on lvcreate invocation. Parent PID 13264: lxc-create
  Logical volume "mytestlxc" created
[{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 13264
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=13264, si_status=0, si_utime=0, si_stime=0} ---
[...]
Copy /var/cache/lxc/trusty/rootfs-amd64 to /usr/lib/x86_64-linux-gnu/lxc ...
Copying rootfs to /usr/lib/x86_64-linux-gnu/lxc ...
[...]

 At the end of lxc-create the "partial" file system is unlinked again:

[...]
close(3)                                = 0
unlink("/var/lib/lxc/mytestlxc/partial") = 0
exit_group(0)                           = ?
+++ exited with 0 +++

To solve this in the automation task, I now mount the LV right after the lxc-start command:

lxc-create -n mytestlxc -B lvm --vgname=vglxc --fstype=ext4 --fssize=2G -t ubuntu-ck && lxc-start -n mytestlxc -d && mount /dev/vglxc/mytestlxc /var/lib/lxc/mytestlxc/rootfs && echo "/dev/vglxc/mytestlxc /var/lib/lxc/mytestlxc/rootfs" >> /etc/fstab



Add a comment

Show form to leave a comment

Comments (newest first)

No comments yet.

RSS feed

Blog Tags:

  AWS   Android   Ansible   Apache   Apple   Atlassian   BSD   Backup   Bash   Bluecoat   CMS   Chef   Cloud   Coding   Consul   Containers   CouchDB   DB   DNS   Database   Databases   Docker   ELK   Elasticsearch   Filebeat   FreeBSD   Galera   Git   GlusterFS   Grafana   Graphics   HAProxy   HTML   Hacks   Hardware   Icinga   Icingaweb   Icingaweb2   Influx   Internet   Java   KVM   Kibana   Kodi   Kubernetes   LVM   LXC   Linux   Logstash   Mac   Macintosh   Mail   MariaDB   Minio   MongoDB   Monitoring   Multimedia   MySQL   NFS   Nagios   Network   Nginx   OSSEC   OTRS   Office   PGSQL   PHP   Perl   Personal   PostgreSQL   Postgres   PowerDNS   Proxmox   Proxy   Python   Rancher   Rant   Redis   Roundcube   SSL   Samba   Seafile   Security   Shell   SmartOS   Solaris   Surveillance   Systemd   TLS   Tomcat   Ubuntu   Unix   VMWare   VMware   Varnish   Virtualization   Windows   Wireless   Wordpress   Wyse   ZFS   Zoneminder   


Update cookies preferences