Easy way to transfer files between host and LXC container on LVM
This is an easy task in the case of containers that share the same filesystem but I'm not sure what would be the proper approach for containers that use LVM disks.
I know I could use rsync
or scp
but I would like to know if it is possible to do this without setting up ssh
/ftp
/http
servers. Additionally, it would be nice to be able to transfer the files without modifying container's config since it would require a container restart. It would be a good idea to avoid persistent mounted shared folders since these might bring a potential security risk (easy mount/umount option should be available).
An option that popped up in my mind would be to use a WebDAV server on the host and mount it in the container but I haven't yet analyzed it and I'm not sure about the performance penalty that it might bring. Other option that I though of is to mount the LVM partition on the host but I'm not sure whether this is safe.
Update
In my configuration I'm creating the containers using the following command:
lxc-create -t ubuntu -n "${NAME}" -B lvm --vgname lxc-vg --fssize "${SIZE}"
I'm using the default configuration except for the autostart feature which is achieved by adding the following lines to /var/lib/lxc/${NAME}/config
file:
lxc.start.auto = 1
lxc.start.delay = 0
Solution 1:
Revised answer: LXC containers share the same kernel as the host, so any filesystem they mount should be accessible from outside.
If you do a cat /proc/mounts
on the host, can you see the container filesystems?
If you see a line like /dev/mapper/... /var/lib/lxc/o1/rootfs ext4 ...
then you should be able to access /var/lib/lxc/o1/rootfs
from the host, without any further commands.
Solution 2:
I know that this is an old question, but for someone ending up here while searching for how to copy files between host and container, this might help.
To pull a file 'my-file' from the container 'container-name' to the current folder, use:
lxc file pull container-name/any-path/my-file .
To push 'my-file', use:
lxc file push my-file container-name/any-path/
To push a folder 'my-dir' recursive, use:
lxc file push -r my-dir container-name/any-path/
Solution 3:
A better, somehow built-in way, of transfering data from host to lxc-container is line #4 in below code:
1 $ mkdir /tmp/transferDir
2 $ cp <some files> /tmp/transferDir/<some files>
3 $ cd /tmp
4 $ tar -C transferDir -c . | lxc-attach -n <container_name> -- /bin/sh -c "tar -C /tmp/ -vx; chmod 1777 /tmp;"
5 $ rm -r /transferDir
The chmod 1777 /tmp
is important, otherwise after copying rights on that folder (the containers /tmp) are changed and you would run into problems like this frequent one about xyz-sql server not being able to restart and others.