LoginSignup
10
10

More than 5 years have passed since last update.

LXCをPythonから操作する

Last updated at Posted at 2013-12-04

概要

vagrantでubuntu13.10をインストールして、その上でテストしました。

まずはPython3.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のインストール

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

PythonのインタプリタからLXCの仮想コンテナを操作してみる

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

# 成功すると最後にこのようなメッセージが出ます。
# モジュールのインストールで途中で失敗したらcreateからやり直せば成功します。
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.
##

# コンテナを起動する
c.start()

# 設定ファイルの場所
c.config_file_name
'/var/lib/lxc/C/config'

# コンテナの一覧
lxc.list_containers()
['C']

# 設定の書き込み
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アドレス確認
c.get_ips()

# コンソールにアタッチする。
c.console()

このようなログインシェルが立ち上がります。

  Ubuntu 13.10 C tty1

  C login: 
  Password: 

ユーザー名とパスワードはubuntuです。
Ctrl-a qでpythonインタープリタに戻ります。

以下、インタープリタの続き。


# クローンを作成する。
clone = lxc.Container("CLONE_NAME")
clone.clone(c)
clone.start()
clone.stop()
clone.destroy()

# 破棄して終了
c.destroy()

もう少し詳しいサンプルはこちらに載っていますので、是非参考に。
https://github.com/lxc/lxc/blob/master/src/python-lxc/examples/api_test.py

10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10