くんすとの備忘録

IT系技術メモ

移転しました。

15秒後に自動的にリダイレクトします。

Bash on Ubuntu on Windowsのchrootを試してみた

4/11の配信日が待ちきれないので、Insider Programに登録して新しいBash on Ubuntu on Windowsをいち早く試してみましたっ!

セットアップ

Windows Insider Program に登録し、Insiderのレベルを スロー に設定しました。

f:id:kunst1080:20170402234641p:plain

※登録してから更新が降ってくるまで、1日待ちました

Bash on Ubuntu on Windowsのバージョン確認

まずはBash on Ubuntu on Windowsの環境のバージョンを確認します。 uname -acat /etc/lsb-release で確認です。

f:id:kunst1080:20170402234720p:plain

uname -a

Linux DESKTOP-T3CMM04 4.4.0-43-Microsoft #1-Microsoft Wed Dec 31 14:42:53 PST 2014 x86_64 GNU/Linux

cat /etc/lsb-release

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.1 LTS"

ですね。

ここで、Creators Updateしてない版のbash(以降、「現行」)の方も見てみましょう。

f:id:kunst1080:20170402234728p:plain

※別マシンなので解像度は小さいですが…‥

見ての通り、Ubuntuのバージョンが14.04から16.04に上がったのがわかります。

日本語の表示

現行ではターミナルに日本語を入力すると横が切れたりしますが、Creators Update適用後は切れずに表示されるよう改善されたみたいです。

f:id:kunst1080:20170402234744p:plain

chroot対応の確認 - コンテナで遊ぼう

現行のBash on Ubuntu on Windowsではchrootに対応しておらず、中でコンテナを起動させたり、無理やりArchLinuxを動かしてもpacmanでエラーが出るといった問題が発生1していました。 リリースノート2によると、Creators Updateにchroot対応が含まれているようです。そういうことなので、いろいろコンテナを作って試してみたいと思います。

※自分の中ではこれが今回の一番の目玉!!!

Ubuntuのコンテナの作成

必要なパッケージのインストール

# apt install debootstrap

debootstrapを使ったUbuntuのインストール

違いがわかりやすいよう、XenialではなくVividを入れます。

# debootstrap vivid /srv/chroot/vivid http://ftp.riken.go.jp/Linux/ubuntu/
# cp -L /etc/resolv.conf /srv/chroot/vivid/etc

デバイスのマウント

bashを閉じる度に設定し直しになるので注意

# cd /srv/chroot
# mount -t proc none vivid/proc
# mount -t sysfs none vivid/sys
# mount -o bind /dev vivid/dev

ログインと確認

# chroot /srv/chroot/vivid /bin/bash

コンテナ内で

# uname -a
# cat /etc/lsb-release

f:id:kunst1080:20170402234757p:plain

ちゃんと環境が変わってますね!

※少し遊んでみましたが、chroot環境ではターミナル内でスクロールできなかったり日本語を入力できなかったりしますね。()これは普通のLinuxとかでのchrootと同じかな)

f:id:kunst1080:20170402234808p:plain

sudo付きでchrootするとわかりやすいんですが、ホストのプロセスが見えてるっぽいんですよね……

Debianのコンテナの作成

※動かそうとしたらセグフォったのでパス!

ArchLinuxのコンテナの作成(ブートストラップイメージを使う方法)

ArchWiki3を見ながらインストールを進めます。

ブートストラップイメージの取得と展開

# cd /tmp
# wget http://ftp.jaist.ac.jp/pub/Linux/ArchLinux/iso/latest/archlinux-bootstrap-2017.04.01-x86_64.tar.gz
# tar xzf archlinux-bootstrap-2017.04.01-x86_64.tar.gz
# mv root.x86_64 /srv/chroot/arch

デバイスのマウント

# mount --bind /srv/chroot/arch /srv/chroot/arch
# cd /srv/chroot/arch
# cp /etc/resolv.conf etc
# mount -t proc /proc proc
# mount --rbind /sys sys
# mount --rbind /dev dev
# mount --rbind /run run

pacmanの設定

pacmanで使用するmirrorlistのファイルを先に修正しておきます。

# vim /srv/chroot/arch/etc/pacman.d/mirrorlist

好きなミラーサーバのコメントを外しておきます。

ログインと環境のセットアップ

ログイン

# chroot /srv/chroot/arch /bin/bash

コンテナ内で環境をセットアップ

# pacman-key --init
# pacman-key --populate archlinux
# pacman -Syy
# pacman -S base

動作確認(gitのインストール)

# pacman -S git

f:id:kunst1080:20170402234826p:plain

Archちゃん動きました。

ArchLinuxのコンテナの作成(LiveCD イメージを使う方法)

airootfs.sfsの取得

# apt install -y squashfs-tools p7zip-full
# wget http://ftp.jaist.ac.jp/pub/Linux/ArchLinux/iso/latest/archlinux-2017.04.01-x86_64.iso
# 7z x archlinux-*.iso
# cp arch/x86_64/airootfs.sfs /srv/chroot/

airootfs.sfsからルートイメージの取り出し

# cd /srv/chroot
# unsquashfs airootfs.sfs
# mv squashfs-root arch2
# rm airootfs.sfs

デバイスのマウント

# mount --bind /srv/chroot/arch2 /srv/chroot/arch2
# cd /srv/chroot/arch2
# cp /etc/resolv.conf etc
# mount -t proc none proc
# mount -t sysfs none sys
# mount -o bind /dev dev
# mount -o bind /dev/pts dev/pts

ログインと環境のセットアップ

ログイン

# chroot /srv/chroot/arch2 /bin/bash

コンテナ内で環境をセットアップ

# pacman-key --init
# pacman-key --populate archlinux
# pacman -Syy

動作確認(gitのインストール)

# pacman -S git

f:id:kunst1080:20170402234840p:plain

どちらの方法でも動くようです。

環境をArchLinuxに入れ替える

前回(http://www.kunst1080.net/entry/2016/08/26/001940)と同じ手順でできました。

違うところは

  • isoのダウンロード用URLが変わってるので要チェック
  • resolv.conf書きましょう
  • pacmanでエラー出なくなってました。

f:id:kunst1080:20170402234854p:plain

前よりちゃんと使えそうです。


ひとまずこんな感じで!
これは本格的に色々コンテナ作って遊べそうです!!

参考URL

Running Archlinux in Bash on Windows

I have installed ArchLinux in Bash on Ubuntu on Windows(Windows Subsystem for Linux).

Installation

1. Install packages

Install tools for installation in Bash on Ubuntu on Windows(BoW).

apt-get install -y squashfs-tools p7zip-full

2. Download ArchLinux ISO

Download an ISO from mirror sites as you like.

wget http://ftp.jaist.ac.jp/pub/Linux/ArchLinux/iso/2016.08.01/archlinux-2016.08.01-dual.iso

3. Pickup the “airootfs.sfs” from ISO

Extract an iso and pickup the airootfs.sfs.

7z x archlinux-*-dual.iso
cp arch/x86_64/airootfs.sfs .

4. Extract the “airootfs.sfs”

Extract the airootfs.sfs with the unsquashfs command.

unsquashfs airootfs.sfs

Error has occurred, but it’s OK. f:id:kunst1080:20160825234414p:plain

5. Move the squashfs-root to a rootfs.

Close ALL BoW windows.
Move the squashfs-root to root of BoW with Windows Explorer.

  • root of BoW - %USERPROFILE%\AppData\Local\lxss\
  • squashfs-root - %USERPROFILE%\AppData\Local\lxss\root\squashfs-root

f:id:kunst1080:20160825234425p:plain

6. Backup a rootfs and substitute squashfs-root for rootfs

f:id:kunst1080:20160825234434p:plain

7. Open BoW

It’s ArchLinux!! f:id:kunst1080:20160825234445p:plain

8. Setup pacman and isntall git

pacman-key --init
pacman-key --populate archlinux
pacman -Syy
pacman -S git

f:id:kunst1080:20160825234453p:plain f:id:kunst1080:20160825234504p:plain Error has occurred when pacman -S (i guess it’s chroot?), I can use git.

That’s ALL!!

References

Bash on Ubuntu on WindowsにArchLinuxをセットアップする

Bash on Ubuntu on Windows(Windows Subsystem for Linux)上に、ArchLinuxをセットアップしました。

手順

1. 必要なパッケージのインストール

Bash on Ubuntu on Windows(以降はBoWと記載)上で、作業に必要なソフトウェアを先にインストールしておきます。
※BoW上での作業はすべてrootユーザで行います。

apt-get install -y squashfs-tools p7zip-full

2. ArchLinuxのISOを取得

好きなミラーサイトからArchLinuxのISOを取得します。

wget http://ftp.jaist.ac.jp/pub/Linux/ArchLinux/iso/2016.08.01/archlinux-2016.08.01-dual.iso

3. ArchLinuxのISOからairootfs.sfsを抽出

isoを展開してairootfs.sfsを取り出します。

7z x archlinux-*-dual.iso
cp arch/x86_64/airootfs.sfs .

4. airootfs.sfsを展開

unsquashfsコマンドを使用してairootfs.sfsを展開します。

unsquashfs airootfs.sfs

一部展開に失敗するファイルがありますが、気にせず続けます。 f:id:kunst1080:20160825234414p:plain

5. 「4.」で作成されたsquashfs-rootをrootfs化

BoWのウィンドウを全て閉じます。
WindowsのExplorerで、「4.」で作成されたsquashfs-rootをBoW環境のルートへ移動させます。

  • BoW環境のルート - %USERPROFILE%\AppData\Local\lxss\
  • squashfs-rootの場所 - %USERPROFILE%\AppData\Local\lxss\root\squashfs-root

f:id:kunst1080:20160825234425p:plain

6. rootfsフォルダを退避し、squashfs-rootフォルダをrootfsフォルダにすり替え

f:id:kunst1080:20160825234434p:plain

7. BoWを起動

ArchLinuxになっている!! f:id:kunst1080:20160825234445p:plain

8. pacmanの初期設定と動作確認

pacman-key --init
pacman-key --populate archlinux
pacman -Syy
pacman -S git

f:id:kunst1080:20160825234453p:plain f:id:kunst1080:20160825234504p:plain pacman -Sでエラーは出ていますが(chrootかな?)、gitコマンドは使えるようになったので、一応インストールはできている模様。

以上!!

参考URL

Bash on Ubuntu on Windows環境を再インストールする方法

手順

コマンドプロンプトを管理者権限で開いて

lxrun /uninstall /y
lxrun /install /y

で再インストールできます。

lxrunコマンドの解説

コマンドのヘルプは、

lxrun /?

で確認することができます。

C:\Users\kunst>lxrun /?
LX サブシステムの管理操作を実行します

使用法:
    /install - サブシステムをインストールします
        オプションの引数:
            /y - ユーザーに許可を求めません
    /uninstall - サブシステムをアンインストールします
        オプションの引数:
            /full - 完全アンインストールを実行します
            /y - ユーザーに許可を求めません
    /setdefaultuser - bash を起動するサブシステム ユーザーを構成します。ユーザーが存在しない場合は作成されます。
        オプションの引数:
            username - ユーザー名を指定します
            /y - ユーザー名が指定された場合、パスワードの作成を求めません
    /update - サブシステムのパッケージのインデックスを更新します

インストール・再インストールの他にも、デフォルトユーザの切り替えもできるみたいです。

参考URL

Git Bashを立ち上げた時に.bashrcが読み込まれないっぽい

っぽい。

検証

設定ファイル

~/.bashrc
echo This is .bashrc
~/.bash_profile
echo This is .bash_profile

ログイン時の挙動

This is .bash_profile

kunst@DESKTOP-T3CMM04 MINGW64 ~
$

→ .bash_profile しか読み込まれてない

追加でbashを起動した時の挙動

$ bash
This is .bashrc

→ OK

対策

.bash_profileから.bashrcを読み込む

~/.bash_profile
echo This is .bash_profile

if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

ログイン時の挙動

This is .bash_profile
This is .bashrc

kunst@DESKTOP-T3CMM04 MINGW64 ~
$

→ OK

追加でbashを起動した時の挙動

$ bash
This is .bashrc

→ OK

コマンドプロンプトでファイルの拡張子を一括変換する

Windowsの標準機能を使って、ファイルの拡張子を一括変換することができます。


まず、コピー前のフォルダの中身です。

a.txt
b.txt
新しいテキスト ドキュメント.txt
新規 Microsoft Word 文書.docx

特定の拡張子を書き換え

拡張子が「.txt」のファイルの拡張子を「.hoge」に書き換えてみましょう

ren *.txt *.hoge

書き換え後のフォルダの中身です。

a.hoge
b.hoge
新しいテキスト ドキュメント.hoge
新規 Microsoft Word 文書.docx

全ての拡張子を書き換え

全てのファイルの拡張子を「.hoge」に書き換えてみましょう

ren * *.fuga

書き換え後のフォルダの中身です。

a.fuga
b.fuga
新しいテキスト ドキュメント.fuga
新規 Microsoft Word 文書.fuga


こんな感じで、この程度であればCygwinを入れたりしなくても簡単にファイル名を書き換えることができます。


コマンドプロンプトでできますが、もちろんBATファイルにして実行することもできます。

Ubuntu on Windowsで危険シェル芸やってみた

ネタです。

環境

VirtualBox上のWindows 10 Home 64bit
f:id:kunst1080:20160411235000j:plain

レポート

実行直前
f:id:kunst1080:20160411235048j:plain

実行中…
f:id:kunst1080:20160411235108p:plain

数十秒後
f:id:kunst1080:20160411235119p:plain

さらに数十秒後
f:id:kunst1080:20160411235129p:plain

エラーメッセージが表示されています
f:id:kunst1080:20160411235140p:plain

bashを閉じると元通りになりました
f:id:kunst1080:20160411235248p:plain


残念ながら(?)、死には至らなかった模様。。。