Here is the bit of code we use to create a container with, it shells out
to lxc-create passing in a custom userdata file:

```
    @classmethod
    def create(cls, name, userdata):
        """ creates a container from ubuntu-cloud template
        """
        # NOTE: the -F template arg is a workaround. it flushes the lxc
        # ubuntu template's image cache and forces a re-download. It
        # should be removed after https://github.com/lxc/lxc/issues/381 is
        # resolved.
        flushflag = "-F"
        if os.getenv("USE_LXC_IMAGE_CACHE"):
            log.debug("USE_LXC_IMAGE_CACHE set, so not flushing in lxc-create")
            flushflag = ""
        out = utils.get_command_output(
            'sudo -E lxc-create -t ubuntu-cloud '
            '-n {name} -- {flushflag} '
            '-u {userdatafilename}'.format(name=name,
                                           flushflag=flushflag,
                                           userdatafilename=userdata))
        if out['status'] > 0:
            raise Exception("Unable to create container: "
                            "{0}".format(out['output']))
        return out['status']
```

We also setup a custom lxc config file:

```
    def create_container_and_wait(self):
        """ Creates container and waits for cloud-init to finish
        """
        self.tasker.start_task("Creating Container",
                               self.read_container_status)

        Container.create(self.container_name, self.userdata)

        with open(os.path.join(self.container_abspath, 'fstab'), 'w') as f:
            f.write("{0} {1} none bind,create=dir\n".format(
                self.config.cfg_path,
                'home/ubuntu/.cloud-install'))
            f.write("/var/cache/lxc var/cache/lxc none bind,create=dir\n")
            # Detect additional charm plugins and make available to the
            # container.
            charm_plugin_dir = self.config.getopt('charm_plugin_dir')
            if charm_plugin_dir \
               and self.config.cfg_path not in charm_plugin_dir:
                plug_dir = os.path.abspath(
                    self.config.getopt('charm_plugin_dir'))
                plug_base = os.path.basename(plug_dir)
                f.write("{d} home/ubuntu/{m} "
                        "none bind,create=dir\n".format(d=plug_dir,
                                                        m=plug_base))

            extra_mounts = os.getenv("EXTRA_BIND_DIRS", None)
            if extra_mounts:
                for d in extra_mounts.split(','):
                    mountpoint = os.path.basename(d)
                    f.write("{d} home/ubuntu/{m} "
                            "none bind,create=dir\n".format(d=d,
                                                            m=mountpoint))

        # update container config
        with open(os.path.join(self.container_abspath, 'config'), 'a') as f:
            f.write("lxc.mount.auto = cgroup:mixed\n"
                    "lxc.start.auto = 1\n"
                    "lxc.start.delay = 5\n"
                    "lxc.mount = {}/fstab\n".format(self.container_abspath))
```

Here is the userdata.yaml file we output and pass into the container:

```
#cloud-config

write_files:
  - content: |
      #!/bin/sh
      mkdir -p /dev/net || true
      mknod /dev/kvm c 10 232
      mknod /dev/net/tun c 10 200
      exit 0
    path: /etc/rc.local
    permissions: '0755'
packages:
  - software-properties-common
groups:
  - libvirtd: [ubuntu]
  - sudo: [ubuntu]
apt_proxy: http://10.0.3.1:3142
apt_https_proxy: http://10.0.3.1:3142
apt_get_wrapper:
  command: eatmydata
  enabled: auto
apt_sources:
  - source: deb http://ppa.launchpad.net/juju/stable/ubuntu $RELEASE main
    key: |
      -----BEGIN PGP PUBLIC KEY BLOCK-----
      Version: SKS 1.1.4
      Comment: Hostname: keyserver.ubuntu.com

      mI0ETDzUZwEEAK3pnEmDZFVTehdKEN6jWs5UBfiEuyqHOmeINREQVn2ue+3bvFb+pYnjaPoz
      CxLRDa4gitS8Wt/rtLvC/Gl+0UNXl71yVdQoPCOPTjfcc4WfPe5EaVNUqNZsEou31jRLvuq9
      D/047ZjUbEFjy/oMY5I/zBA5X0BjNc30Tlm9NoMRABEBAAG0FkxhdW5jaHBhZCBFbnNlbWJs
      ZSBQUEGItgQTAQIAIAUCTDzUZwIbAwYLCQgHAwIEFQIIAwQWAgMBAh4BAheAAAoJEDdqKQ7I
      BosRT50EAJQVKoSFSxyIlx4fdH9vhftKwvvmZK6KHYHqVY8Hee3exU/wdPCH+nssG2s/9/oW
      ReHiRNgIhc+iIDr+OTh/9TNPoClybkvrlWBgqUh6CIUNXbJQMigZlly90gkaLtUMGfsfW2+1
      u9j+vBKs9toPKDN1ybKu61k4lCrxoDUo9QDp
      =4LP9
      -----END PGP PUBLIC KEY BLOCK-----
  
  - source: deb http://ppa.launchpad.net/cloud-installer/stable/ubuntu $RELEASE 
main
    key: |
      -----BEGIN PGP PUBLIC KEY BLOCK-----
      Version: SKS 1.1.4
      Comment: Hostname: keyserver.ubuntu.com

      mI0EUwap0AEEAOcvGspxrV0/83Xa6grEAplHLuuvj3CrOH26XwSleu/wzzTmNqKDnZLWjBkA
      qdbfYzQ3T4aD2uWfMJ2doqemBmLZWKrkMInLcS2o3cqraFHJaZDAvJHg7wiDBYiuq0cCrj/m
      enC351p/DLQ+QuiHE1hgX6JV4nF527rCmGbWSSTRABEBAAG0KExhdW5jaHBhZCBQUEEgZm9y
      IFVidW50dSBDbG91ZCBJbnN0YWxsZXKIuAQTAQIAIgUCUwap0AIbAwYLCQgHAwIGFQgCCQoL
      BBYCAwECHgECF4AACgkQ3eaD+NL/Ce1RuQQApA4AaFlsujGJ4i2lN1qLbxivXu2fs+D9aXAV
      MgYINVj13/xxtn+1PqV7x7I9ej+Adhy9uLc+7T6mfx7Ahn791hbNr/gycSZEkvdp3gZRFWHS
      MqPZ3uOvWieRR347tMTh/BgxEsTdTUcLbt7jZkk6NypGO+ej8WtWugJ9kwOPb9s=
      =aDcI
      -----END PGP PUBLIC KEY BLOCK-----
  
  - source: deb http://ppa.launchpad.net/cloud-installer/testing/ubuntu 
$RELEASE main
    key: |
      -----BEGIN PGP PUBLIC KEY BLOCK-----
      Version: SKS 1.1.4
      Comment: Hostname: keyserver.ubuntu.com

      mI0EUwap0AEEAOcvGspxrV0/83Xa6grEAplHLuuvj3CrOH26XwSleu/wzzTmNqKDnZLWjBkA
      qdbfYzQ3T4aD2uWfMJ2doqemBmLZWKrkMInLcS2o3cqraFHJaZDAvJHg7wiDBYiuq0cCrj/m
      enC351p/DLQ+QuiHE1hgX6JV4nF527rCmGbWSSTRABEBAAG0KExhdW5jaHBhZCBQUEEgZm9y
      IFVidW50dSBDbG91ZCBJbnN0YWxsZXKIuAQTAQIAIgUCUwap0AIbAwYLCQgHAwIGFQgCCQoL
      BBYCAwECHgECF4AACgkQ3eaD+NL/Ce1RuQQApA4AaFlsujGJ4i2lN1qLbxivXu2fs+D9aXAV
      MgYINVj13/xxtn+1PqV7x7I9ej+Adhy9uLc+7T6mfx7Ahn791hbNr/gycSZEkvdp3gZRFWHS
      MqPZ3uOvWieRR347tMTh/BgxEsTdTUcLbt7jZkk6NypGO+ej8WtWugJ9kwOPb9s=
      =aDcI
      -----END PGP PUBLIC KEY BLOCK-----
ssh_authorized_keys:
  - ssh-rsa 
AAAAB3NzaC1yc2EAAAADAQABAAABAQCjInOUu3bhxOod18NUG5yF86z4FDLY78mzD53mYNEw52J8UT71/kzJgJZfGEkS5cE6UO/XazYIKDZtTsjK1euHOg/nQL93bJ+hwjlCjUINsXtkvul5odxqwlVB7BiJxXWxJqURYgpURR3GwUDGLx5W1gO8mA9sWzbTt5NXfE0gbdIXeb0TVUI6CcgNj+e9uW0xl/0kSA6VFDqyz9kidHm/dLGoakg8V3h08tbIjG9PfGz/M0L2j345iZP816NVpE0k2UAn1dS415wJwvUjZGF/vFoPqemdP0ecp/UECFAz0wt/CVOtdcnh/XGhiz4aNK0a/y0HNxgjzuN+jrx6fZ5h
 stokachu@cabeiri

random_seed:
  command: ['env', 'http_proxy=http://squid.internal:3128', 
'https_proxy=http://squid.internal:3128', 'pollinate', '-q']
apt_mirror: http://us.archive.ubuntu.com/ubuntu/
package_update: true
password: ubuntu
chpasswd: { expire: False }
ssh_pwauth: True
manage_etc_hosts: localhost
resize_rootfs: False

# Make sure we load our modules on first creation
runcmd:
  - [ sh, /etc/rc.local ]
  - echo "export PATH=$PATH:/usr/sbin" >> /home/ubuntu/.bashrc
  - echo "export http_proxy=http://squid.internal:3128"; >> /home/ubuntu/.bashrc
  - echo "export HTTP_PROXY=http://squid.internal:3128"; >> /home/ubuntu/.bashrc
  - echo "export https_proxy=http://squid.internal:3128"; >> /home/ubuntu/.bashrc
  - echo "export HTTPS_PROXY=http://squid.internal:3128"; >> /home/ubuntu/.bashrc
```

And this is our lxc config output:

```
# Template used to create this container: 
/usr/share/lxc/templates/lxc-ubuntu-cloud
# Parameters passed to the template: -u 
/home/stokachu/.cloud-install/userdata.yaml
# For additional config options, please look at lxc.container.conf(5)

# Common configuration
lxc.include = /usr/share/lxc/config/ubuntu-cloud.common.conf

# Container specific configuration
lxc.rootfs = /var/lib/lxc/openstack-single-stokachu/rootfs
lxc.utsname = openstack-single-stokachu
lxc.arch = amd64

# Network configuration
lxc.network.type = veth
lxc.network.link = lxcbr0
lxc.network.flags = up
lxc.network.hwaddr = 00:16:3e:c8:95:cc
lxc.mount.auto = cgroup:mixed
lxc.start.auto = 1
lxc.start.delay = 5
lxc.mount = /var/lib/lxc/openstack-single-stokachu/fstab
```

-- 
You received this bug notification because you are a member of Ubuntu
Bugs, which is subscribed to Ubuntu.
https://bugs.launchpad.net/bugs/1486296

Title:
  Unable to start VMs under a lxc container

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/libvirt/+bug/1486296/+subscriptions

-- 
ubuntu-bugs mailing list
ubuntu-bugs@lists.ubuntu.com
https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs

Reply via email to