Software development of explosion! -夢の破片(カケラ)たちの日々-

ソフトウェア開発を中心としたコンピューター関連のネタを扱ったブログです

Software development is passion and explosion!

Vagrant で Tiny Core Linux!

boot2docker を使ってみたところ、その起動の速さに驚かされた。 そこで、Vagrant でTiny Core Linuxを使ったら起動が早くてカスタマイズしやすいヘッドレスなLinux環境が構築できるのでは?と思ったのでやってみた。

  1. Tiny Core Linuxのインストール 公式サイトからCorePlusのISOイメージをダウンロードしてインストール。

  2. Vagrant Box化するために色々と設定(ここがハマりどころポイントその1) 2.1. vagrant ユーザーの追加 2.2. (ここ重要!) /etc/sudoers に vagrant ユーザーを追加 visudoなんて使えないので、vimなりechoなり。。。 2.3. curl と opensshをインストール tceやtc-load使います。 2.4. (ここ重要!) /opt/bootlocal.sh に 以下を追加

/usr/local/etc/init.d/openssh start

2.5. (ここも重要!) sshd_config を作成 とりあえずコピーで大丈夫です。

cp /usr/local/etc/ssh/sshd_config.orig /usr/local/etc/ssh/sshd_config

2.6. (ここ超重要!!!) /opt/.filetool.lst に変更したファイルパスを追加する こんな感じになります。

opt
home
etc/shadow
etc/passwd
etc/sudoers
usr/local/etc/ssh/sshd_config

2.7. (ここも超重要!!!) filetool.sh -b してバックアップに含める Tiny Core Linux は、起動毎にルートファイルシステムをバックアップから復元している(?)ようで、これをしないで再起動すると、変更が吹き飛びます。 2.8. VM再起動 2.9. (やや重要) ホストのSSH鍵を /opt/.filetool.lstに追加してfiletool.sh -b 2.10. (基本) Vagrantの公開鍵を /home/vagrant/.ssh配下に配置する

  1. Box 化
  2. (やや重要) Vagrantfileの編集(なければ作って) こんなんになります。
module VagrantPlugins
  module GuestTinyCoreLinux
    module Cap
      class Halt
        def self.halt(machine)
          begin
            machine.communicate.sudo('poweroff')
          rescue Net::SSH::Disconnect, IOError
            # Ignore, this probably means connection closed because it
            # shut down and SSHd was stopped.
          end
        end
      end
    end
    class Guest < Vagrant.plugin('2', :guest)
      def detect?(machine)
        machine.communicate.test('[ "$(cat /etc/os-release | grep ^NAME | cut -d = -f2)" = "TinyCore" ]')
      end
    end
  end
end

Vagrant.configure("2") do |config|
  config.vm.define "tinycore" do |config|
    config.vm.box = "poad/tinycorelinux"
    config.ssh.username = "vagrant"
    config.ssh.password = "vagrant"
    config.ssh.shell = "sh"
    config.vm.synced_folder ".", "/vagrant", disabled: true
    config.vm.boot_timeout = 75
    config.vm.graceful_halt_timeout = 35
    config.vm.provider "virtualbox" do |vb|
      vb.memory = "2048"
    end
  end
end

解説

module VagrantPlugins
  module GuestTinyCoreLinux
    module Cap
      class Halt
        def self.halt(machine)
          begin
            machine.communicate.sudo('poweroff')
          rescue Net::SSH::Disconnect, IOError
            # Ignore, this probably means connection closed because it
            # shut down and SSHd was stopped.
          end
        end
      end
    end
    class Guest < Vagrant.plugin('2', :guest)
      def detect?(machine)
        machine.communicate.test('[ "$(cat /etc/os-release | grep ^NAME | cut -d = -f2)" = "TinyCore" ]')
      end
    end
  end
end

は、Vagrant Plugin の記述になります。 Tiny Core Linux か否かを Guest classの detect?メソッドを使用して確認します。(/etc/os-releaseのNAMEが TinyCoreであれば有効となるプラグインとなります。) Tiny Core Linux であれば、vagrant halt コマンドの実体は poweroff を使うようにしています。

これで、Tiny Core Linux の起動と終了には問題が無くなった。 VirtualBoxの共有フォルダーが使えない?は仕方ない。