LoginSignup
7
8

More than 5 years have passed since last update.

Dockerコンテナ上でnbdモジュールを読み込む

Last updated at Posted at 2017-08-23

こんにちは、@hico_horiuchiです。
Dockerコンテナ上でqow2イメージを展開するためにnbdモジュールを読み込んだので、その方法をまとめておきます。
nbd以外のカーネルモジュールでも同様の方法で読み込むことができるはずです。

Dockerホストのセットアップ

Ubuntu Server 16.04をインストールしたVMに、個人用のAnsible Playbookを使ってDockerの最新版をインストールしています。
手動でインストールする場合は Get Docker CE for Ubuntu | Docker Documentation を参照。

$ git clone git@github.com:hico-horiuchi/hiconyan-com-ansible.git
$ cd hiconyan-com-ansible
$ virtualenv venv
$ source venv/bin/activate
$ pip install -r requirements.txt
$ source venv/bin/activate
$ ansible-playbook -i hosts -t common_apt,common_ssh,docker site.yml

Dockerfileの書き方

Ubuntu 16.04のイメージをベースに、検証用のDockerfileを作成します。
標準で lsmodmodprobe コマンドが入っていないので、kmod パッケージを入れます。
また、qemu-nbd コマンドを使うために qemu-utils パッケージも必要です。

FROM ubuntu:16.04
MAINTAINER Akihiko Horiuchi <12ff5b8@gmail.com>

RUN sed -i -e 's/archive\.ubuntu\.com/ftp\.jaist\.ac\.jp/g' /etc/apt/sources.list && \
    apt-get update && \
    apt-get -y upgrade && \
    apt-get -y install bzip2 curl kmod qemu-utils

RUN apt-get -y autoremove && \
    apt-get clean && \
    rm -rf /var/cache/apt/archives/* /var/lib/apt/lists/* /tmp/* /var/tmp/*

CMD /bin/bash
$ docker build -t nbd .

nbdモジュールの読み込み・qcow2の展開

カーネルモジュールの読み込みや、デバイスへのアクセスのため、--privileged オプションでコンテナの権限を拡張します。
/dev/lib/modules のディレクトリもマウントする必要があるので、注意してください。

$ docker run -it --rm --privileged -v /dev:/dev -v /lib/modules:/lib/modules:ro --name nbd nbd /bin/bash

後は、コンテナ上でnbdモジュールを読み込み、適当なqcow2を展開してみるだけです。
(今回はOpenStack用のCoreOSイメージを使用しました → OpenStack。)

$ modprobe nbd
$ curl -O https://stable.release.core-os.net/amd64-usr/current/coreos_production_openstack_image.img.bz2
$ bunzip2 coreos_production_openstack_image.img.bz2
$ mkdir /mnt/tmp
$ qemu-nbd --connect /dev/nbd0 coreos_production_openstack_image.img
$ mount -o ro /dev/nbd0p1 /mnt/tmp
$ ls /mnt/tmp
$ umount /mnt/tmp
$ qemu-nbd --disconnect /dev/nbd0
$ exit

意外と簡単でしたね。

参考文献

7
8
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
7
8