Skip to content

Instantly share code, notes, and snippets.

@jeffdyke
Last active May 2, 2024 19:49
Show Gist options
  • Save jeffdyke/d48dbaea067ee9bb3d947a132e55073e to your computer and use it in GitHub Desktop.
Save jeffdyke/d48dbaea067ee9bb3d947a132e55073e to your computer and use it in GitHub Desktop.
Mount folder in lxc container to the host machine

There is a lot of misleading information out there to solve a very simple problem.
This will allow you to create what would normally be a nfs share between two machines.
There have been many updates to lxc and many ways reported to work, this way worked perfectly for me on the first try and the directories where writable from both machines.

Containers

  • stagingweb01
  • stagingweb02

Shares:

  • /mnt/nfs/tmp/media
  • /mnt/nfs/media*

Command structure as seen from:

lxc config help
  lxc config device add [remote:]container1 <device-name> disk source=/share/c1 path=opt

Commands to run on the host machine note there is NO need to create these directories on the containers, but if they exist it will work the same way:

Commands to run on the host

  • sudo lxc config device add stagingweb01 sdb disk source=/mnt/nfs/tmp/media path=mnt/nfs/tmp/media
  • sudo lxc config device add stagingweb01 sdb1 disk source=/mnt/nfs/media path=mnt/nfs/media
  • sudo lxc config device add stagingweb02 sdb disk source=/mnt/nfs/tmp/media path=mnt/nfs/tmp/media
  • sudo lxc config device add stagingweb02 sdb1 disk source=/mnt/nfs/media path=mnt/nfs/media

Very important Due to a change in LXC on Sept of 2015 the paths need to be relative on the host side, but they will be created on the root

@hnagri
Copy link

hnagri commented Sep 30, 2016

I am not able to create/update any file on the shared/mounted directory. I know I can make the directory a Read/Write all of 777 and that solves the problem but I dont want to do that as one folder belongs to one container only and I want to maintain some level of security. Is there any solution for my particular problem ?

@jeffbondlink
Copy link

I've stopped using lxc for this particular use, but let me check and i'll respond back

@jeffbondlink
Copy link

sorry for not seeing this earlier are you still having the issue without having to 777 everything (b/c that is bad)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment