Operate LXC from Python

Overview

I installed ubuntu 13.10 with vagrant and tested it on it.

First, install Python 3.3

sudo apt-get install libsqlite3-dev
sudo apt-get install sqlite3 # for the command-line client
sudo apt-get install bzip2 libbz2-dev

wget http://python.org/ftp/python/3.3.3/Python-3.3.3.tar.bz2
tar jxf Python-3.3.3.tar.bz2
./configure --prefix=/opt/python3.3
make
sudo make install

sudo ln -s /opt/python3.3/bin/python3.3 /usr/bin/python

wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py
sudo python3.3 ez_setup.py
sudo easy_install pip

LXC installation

sudo apt-get -y install lxc
sudo apt-get -y install lxctl

Try to operate LXC virtual container from Python interpreter

sudo python3
import lxc
c=lxc.Container("C")
c.create("ubuntu")

#If successful, you will get a message like this at the end.
#If you make a mistake while installing the module, you can start over from create and it will succeed.
Processing triggers for ureadahead ...
Can not write log, openpty() failed (/dev/pts not mounted?)
Setting up libdrm2:i386 (2.4.46-1ubuntu1) ...
Setting up libprocps0:i386 (1:3.3.3-2ubuntu9) ...
Setting up libudev1:i386 (204-0ubuntu19) ...
Setting up udev (204-0ubuntu19) ...
invoke-rc.d: policy-rc.d denied execution of restart.
Removing 'diversion of /bin/udevadm to /bin/udevadm.upgrade by fake-udev'
update-initramfs: deferring update (trigger activated)
Setting up initramfs-tools-bin (0.103ubuntu1.1) ...
Setting up initramfs-tools (0.103ubuntu1.1) ...
update-initramfs: deferring update (trigger activated)
Setting up procps (1:3.3.3-2ubuntu9) ...
invoke-rc.d: policy-rc.d denied execution of start.
Setting up openssh-client (1:6.2p2-6ubuntu0.1) ...
Setting up openssh-server (1:6.2p2-6ubuntu0.1) ...
invoke-rc.d: policy-rc.d denied execution of restart.
Setting up ssh (1:6.2p2-6ubuntu0.1) ...
Processing triggers for libc-bin ...
Processing triggers for initramfs-tools ...
Download complete
Copy /var/cache/lxc/saucy/rootfs-i386 to /usr/lib/i386-linux-gnu/lxc ... 
Copying rootfs to /usr/lib/i386-linux-gnu/lxc ...
Generating locales...
  en_US.UTF-8... up-to-date
Generation complete.
Creating SSH2 RSA key; this may take some time ...
Creating SSH2 DSA key; this may take some time ...
Creating SSH2 ECDSA key; this may take some time ...
invoke-rc.d: policy-rc.d denied execution of start.

##
# The default user is 'ubuntu' with password 'ubuntu'!
# Use the 'sudo' command to run tasks as root in the container.
##

#Start the container
c.start()

#Location of configuration files
c.config_file_name
'/var/lib/lxc/C/config'

#List of containers
lxc.list_containers()
['C']

#Write settings
c.append_config_item("lxc.network.ipv4", "192.168.1.10/24")
c.append_config_item("lxc.network.ipv4.gateway", "192.168.1.1")
c.save_config()

#IP address confirmation
c.get_ips()

#Attach to the console.
c.console()

A login shell like this will be launched.

  Ubuntu 13.10 C tty1

  C login: 
  Password: 

The username and password is ubuntu. Ctrl-a q returns to the python interpreter.

Below is the continuation of the interpreter.


#Create a clone.
clone = lxc.Container("CLONE_NAME")
clone.clone(c)
clone.start()
clone.stop()
clone.destroy()

#Discard and exit
c.destroy()

A little more detailed sample can be found here, so please refer to it. https://github.com/lxc/lxc/blob/master/src/python-lxc/examples/api_test.py

Recommended Posts

Operate LXC from Python
Operate Filemaker from Python
Operate neutron from Python!
Operate an I2C-connected display from Python
Operate DynamoDB from Python like SQL.
sql from python
MeCab from Python
Operate Sakura's cloud object storage from Python
Use thingsspeak from python
Touch MySQL from Python 3
Operate Redmine using Python Redmine
Use fluentd from python
Access bitcoind from python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Python from or import
Run python from excel
Install python from source
Operate Kinesis with Python
Execute command from Python
Use MySQL from Python
Operate Blender with Python
Operate the schedule app using python from iphone
Manipulate riak from python
Force Python from Fortran
Use BigQuery from python.
Execute command from python
Operate Excel with Python (1)
[Python] Read From Stdin
Use mecab-ipadic-neologd from python
Operate Excel with Python (2)
Operate mongoDB from python in ubuntu environment ① Introduction of mongoDB
[AWS] Operate SQS from SDK (send / receive) [Python] [Node.js]
Flatten using Python yield from
Call CPLEX from Python (DO cplex)
Deep Python learned from DEAP
Post from Python to Slack
Operate TwitterBot with Lambda, Python
Grammar features added from Python3.6
Cheating from PHP to Python
Make MeCab available from Python3
Information obtained from tweet_id (Python)
OCR from PDF in Python
Run illustrator script from python
Use MySQL from Anaconda (python)
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
Study from Python Hour4: Object-oriented ②
Query Athena from Lambda Python
Access Oracle DB from Python
Study from Python Hour3: Functions
Start / stop GCE from python
Stop Omxplayer from Python code
Switch from python2.7 to python3.6 (centos7)
Connect to sqlite from python
Install pyenv from Homebrew, install Python from pyenv
Study from Python Hour4: Object-oriented ①
Python naming convention (from PEP8)
[Python] [SQLite3] Operate SQLite with Python (Basic)
With skype, notify with skype from python!
Use e-Stat API from Python
Register redmine issue from Python