wiki:TipAndDoc/Linux/LXC

Version 57 (modified by mitty, 8 years ago) (diff)

--

  • LinuX Container
  • ホスト側のvethデバイス名を指定する
    • lxc-chroot-linux-containers - Re: (Lxc-users) veth name - msg#00058 - Recent Discussion OSDir.com
    • man 5 lxc.conf

      By default lxc choose a name for the network device belonging to the outside of the container, this name is handled by lxc, but if you wish to handle this name yourself, you can tell lxc to set a specific name with the lxc.network.veth.pair option.

    • 複数のコンテナで同じ名前のpairを指定すると、二つ目は起動しない(下記の例ではtest1, test2コンテナでlxc.network.veth.pair = testと指定)
    • $ sudo lxc-start -n test2
      lxc-start: failed to create test-vethNdql6o : File exists
      lxc-start: failed to create netdev
      lxc-start: failed to create the network
      lxc-start: failed to spawn 'test2'
      lxc-start: No such file or directory - failed to remove cgroup '/sys/fs/cgroup/cpu/sysdefault/lxc/test2'
      
  • 指定しない場合、mktemp("vethXXXXXX")で決定される
    • SourceForge - lxc/lxc/blob - src/lxc/conf.c

      static int instanciate_veth(struct lxc_handler *handler, struct lxc_netdev *netdev)

    • ホスト側の物理NIC(lxc.network.link)とリンクするveth1の名前やifindexを外部から知る手段が無い模様
      • $ sudo lxc-start -d -n test -o test.log -l DEBUG
              lxc-start 1356615687.534 DEBUG    lxc_conf - instanciated veth 'vethtgqRWu/vethnwRgNf', index is '99'
        
      • lxc.network.veth.pair = vtest とすると以下の様になる
              lxc-start 1356615810.522 DEBUG    lxc_conf - instanciated veth 'vtest/vethA3QwQ9', index is '102'
        

two NICs in one container

example

  • 通常のbridge(veth)ではなくmacvlanであるが、vethでも同じはず
  • /var/lib/lxc/test/config
    lxc.network.type=macvlan
    lxc.network.macvlan.mode=bridge
    lxc.network.link=em1
    lxc.network.flags=up
    lxc.network.hwaddr = 00:16:3e:85:2e:da
    lxc.network.type=macvlan
    lxc.network.macvlan.mode=bridge
    lxc.network.link=em1
    lxc.network.flags=up
    lxc.network.hwaddr = 00:16:3e:85:2e:db
    
  • mitty@test:~$ ifconfig -a | egrep 'addr|Link'
    eth0      Link encap:Ethernet  HWaddr 00:16:3e:85:2e:da
              inet addr:192.168.83.207  Bcast:192.168.83.255  Mask:255.255.255.0
              inet6 addr: fe80::216:3eff:fe85:2eda/64 Scope:Link
    eth1      Link encap:Ethernet  HWaddr 00:16:3e:85:2e:db
              inet addr:192.168.83.212  Bcast:192.168.83.255  Mask:255.255.255.0
              inet6 addr: fe80::216:3eff:fe85:2edb/64 Scope:Link
    lo        Link encap:Local Loopback
              inet addr:127.0.0.1  Mask:255.0.0.0
              inet6 addr: ::1/128 Scope:Host
    
  • mitty@test:~$ ip route
    default via 192.168.83.243 dev eth0
    192.168.83.0/24 dev eth0  proto kernel  scope link  src 192.168.83.207
    192.168.83.0/24 dev eth1  proto kernel  scope link  src 192.168.83.212
    

mount bind

  • /var/lib/lxc/>container name>/fstab を用いて、ホストの特定ディレクトリ以下をゲストにbind出来る
  • fstab
    /media mnt none bind 0 0
    
    • host:/media -> guest:/mnt とマウントされる
    • mntに「/」が無いことに注意
    • mitty@lxc:~$ mount
      /dev/disk/by-uuid/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX on /mnt type ext4 (rw,relatime,errors=remount-ro,user_xattr,barrier=1,data=ordered)
      
    • mitty@lxc:~$ ls -l /mnt/
      total 0
      -rw-r--r-- 1 root root 0 Oct 23 22:01 host-media
      
  1. mitty@lxc:~$ sudo rm /mnt/host-media
    removed `/mnt/host-media'
    
  2. mitty@host:~$ ls -l /media/host-media
    ls: cannot access /media/host-media: No such file or directory
    

read-only bind

  • fstab
    /media mnt none bind,ro 0 0
    
  • mountコマンドの出力結果は「(rw,relatime,errors=remount-ro,user_xattr,barrier=1,data=ordered)」で変わらないが、read-only化される
  • mitty@lxc:~$ sudo rm /mnt/host-media
    rm: cannot remove `/mnt/host-media': Read-only file system
    

X11 with VNC

  • 12.04上のLXCでは、configのcgroupの設定やmknodなどは特に必要ない模様
  • vnc自体については => network/vnc
  • 基本的にaptitude install -Rでインストールしている
  • エラーメッセージについては ~/.vnc/ 以下に保存されるログファイルから

common packages

  • vnc4server
  • xfonts-base
    could not open default font 'fixed'
    

GNOME

  • ~/.vnc/xstartup
    xsetroot -solid grey
    exec gnome-session &
    

Xfce

  • xfce4
  • ~/.vnc/xstartup
    xsetroot -solid grey
    exec startxfce4 &
    
  • もしくは
    xsetroot -solid grey
    exec xfce4-session &
    
  • 後者だとTrashディレクトリなどがFile Managerに表示されない
    • ファイルアイコンが表示されない

LXDE

  • lxde
  • ~/.vnc/xstartup
    xsetroot -solid grey
    exec lxsession -s LXDE &
    
    • -s LXDEは無くても良い模様

mount inside container

  • コンテナの中ではAppArmorによってmountが禁止されているため、直接NFSマウントするにはAppArmorの再設定が必要

ホストで直接mountし、コンテナには--bindでアクセスさせる

  1. host# mount nfsserver:/path/to/export /mnt/somewhere
  2. /var/lib/lxc/CONTAINER/fstab
    /mnt/somewhere   path/to/mountdir    none bind 0 0
    
  3. host# lxc-start -d -n CONTAINER
  4. lxc$ mount
    nfsserver:/path/to/export on /path/to/mountdir type nfs4 (rw,relatime,...)
    
  • ホスト上でread-onlyマウントしていても、/var/lib/lxc/CONTAINER/fstabでのread/write権限設定によって上書きされるので注意
  • cifsも同じやりかたでマウントできる
    • cifsではホスト上でread-onlyマウントしていると、設定は一部(?)引き継がれる模様
    • touch hogeすると、touch: cannot touch `/path/to/mountdir/hoge': Read-only file systemとエラーになるが、実際にはファイルは生成される
    • 0バイトのファイルは生成されるが、データの書き込みは出来ない模様
    • 削除は出来ないため、削除できないファイルが作成されてしまう点に注意
  • ホスト上のマウントディレクトリを直接コンテナのディレクトリにbindする必要がある。つまり、以下のようなことは出来ない
    1. host# mount nfsserver:/path/to/export/dir1 /mnt/somewhere/dir1
    2. lxc/CONTAINER/fstab
      /mnt/somewhere   path/to/mountdir    none bind 0 0
      
    3. lxc$ ls -l /path/to/mountdir/dir1
      • nfsserver:/path/to/export/dir1 が見えることが期待されるが、実際にはマウントされていない

backup

Attachments (4)

Download all attachments as: .zip