はじめに
Ubuntu 16.04 LTSでZFSが正式にサポートされました。
さくらのVPS(KVM環境)にUbuntu 16.04 LTSでZFS Root環境を構築してみたのでその手順のメモです。
Ubuntu 16.04 Root on ZFS構築手順
環境
OS | Ubuntu 16.04 LTS(4.4.0-21-generic) |
---|---|
プラットフォーム | さくらのVPS |
事前準備
まず、事前準備を行います。
Live CDの取得
64-bit Ubuntu 16.04 Xenial Live CDをダウンロードします。
ISOイメージのアップロード
ダウンロードしたISOイメージをさくらのVPSのISOイメージアップロード機能を使ってアップロードしておきます。
Live CDの起動
さくらのVPSのカスタムイメージインストール機能を使って先ほどアップロードしたubuntu-16.04-desktop-amd64.isoからBootします。
Boot後以下のような画面になります。しばらく待てばWelcom画面が表示されます。
以下の画面で「Ubuntuを試す」を選択します。
起動後、ターミナルを起動します。
Ctrl
+Alt
+t
でターミナルを起動できます。
ubuntuユーザでログインされています。
さくらのVPSに割り当てられているネットワーク情報を元に/etc/network/interfaces
へ設定します。
$ sudo vi /etc/network/interfaces
以下のように記述します。(環境に合わせて読み替えてください。)
# The primary network interface auto ens3 iface ens3 inet static address xxx.xxx.xxx.xxx netmask 255.255.xxx.0 network xxx.xxx.xxx.0 broadcast xxx.xxx.xxx.255 gateway xxx.xxx.xxx.xxx dns-nameservers xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx dns-search sakura.ne.jp
ネットワークインタフェースを有効化します。
$ sudo ifup ens3
OpenSSHのインストール
このままコンソールからの操作だとコマンドのコピペができず不便なので、外部からログインして作業できるようにOpenSSHをインストールします。
$ sudo apt-get update
$ sudo apt-get --yes install openssh-server
ubuntuユーザにpasswordを設定
ubuntu ユーザでログインできるようパスワードを設定します。
$ sudo passwd ubuntu
外部からログイン
外部からubuntuユーザでログインし直します。
$ ssh ubuntu@さくらのVPSのIP
これで準備完了です。
LiveCD環境にZFSのインストール
root権限でシェルを実行します。
$ sudo -i
ZFS環境構築に必要なパッケージをインストールします。
# apt-add-repository universe # apt-get update # apt-get install --yes debootstrap gdisk zfs-initramfs
ディスクフォーマット&パーティション作成
パーティションテーブルがないことを確認します。 ※KVM仮想マシンで実行する場合は/dev/vdaを使う必要がありますが、物理サーバで実行する場合は /dev/disk/by-id/scsi- というエイリアスのデバイスファイルを指定する必要があります。
# sgdisk -p /dev/vda1 Creating new GPT entries. Disk /dev/vda1: 104855552 sectors, 50.0 GiB Logical sector size: 512 bytes Disk identifier (GUID): 192C5AB8-08B9-4947-8423-E0182A6E5C5F Partition table holds up to 128 entries First usable sector is 34, last usable sector is 104855518 Partitions will be aligned on 2048-sector boundaries Total free space is 104855485 sectors (50.0 GiB) Number Start (sector) End (sector) Size Code Name
※データがある場合は初期化します。
# sgdisk -Z /dev/vda1
# sgdisk -a1 -n2:34:2047 -t2:EF02 /dev/vda1 Creating new GPT entries. Warning: The kernel is still using the old partition table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) The operation has completed successfully.
パーティションの最後に余剰空間を作成しておきます。(たぶんなくても良い)
# sgdisk -n9:-8M:0 -t9:BF07 /dev/vda1 Warning: The kernel is still using the old partition table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) The operation has completed successfully.
データ領域用のパーティションを作成します。
# sgdisk -n1:0:0 -t1:BF01 /dev/vda1 Warning: The kernel is still using the old partition table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) The operation has completed successfully.
作成したパーティションを確認します。
# sgdisk -p /dev/vda1 Disk /dev/vda1: 104855552 sectors, 50.0 GiB Logical sector size: 512 bytes Disk identifier (GUID): D00D7469-F38C-4463-8725-86E9354C26AC Partition table holds up to 128 entries First usable sector is 34, last usable sector is 104855518 Partitions will be aligned on 2-sector boundaries Total free space is 0 sectors (0 bytes) Number Start (sector) End (sector) Size Code Name 1 2048 104839133 50.0 GiB BF01 2 34 2047 1007.0 KiB EF02 9 104839134 104855518 8.0 MiB BF07
ZFSストレージプール作成
ZFS ストレージプールを作成します。
# zpool create -f -o ashift=12 -O atime=off -O canmount=off -O compression=lz4 -O normalization=formD -O mountpoint=/ -R /mnt rpool /dev/vda1
作成したZFS ストレージプールを確認します。
# zpool status pool: rpool state: ONLINE scan: none requested config: NAME STATE READ WRITE CKSUM rpool ONLINE 0 0 0 vda1 ONLINE 0 0 0 errors: No known data errors
ZFSファイルシステム作成
# zfs create -o canmount=off -o mountpoint=none rpool/ROOT
# zfs create -o canmount=noauto -o mountpoint=/ rpool/ROOT/ubuntu
# zfs mount rpool/ROOT/ubuntu
他にも必要なデータセットがあれば、作成します。
# zfs create -o setuid=off rpool/home # zfs create -o canmount=off -o setuid=off -o exec=off rpool/var # zfs create rpool/var/log # zfs create rpool/tmp # zfs create rpool/usr # zfs create -o com.sun:auto-snapshot=false -o exec=on rpool/var/tmp
システムのインストール
上記でrpool/var/tmp
を作成している場合は、/mnt/var/tmp
のパーミッションを変更します。
# chmod 1777 /mnt/var/tmp
基本システムをインストールします。
# debootstrap xenial /mnt I: Retrieving InRelease I: Checking Release signature I: Valid Release signature (key id 790BC7277767219C42C86F933B4FE6ACC0B21F32) I: Retrieving Packages I: Validating Packages : I: Configuring systemd... I: Configuring initramfs-tools... I: Configuring ureadahead... I: Configuring resolvconf... I: Base system installed successfully.
# zfs set devices=off rpool
System Configuration
hosts
, hostname
, DNSリゾルバ, ネットワーク設定を、環境に合わせて編集します。
# vi /mnt/etc/hosts
# vi /mnt/etc/hostname
# vi /mnt/etc/resolvconf/resolv.conf.d/base
# vi /mnt/etc/network/interfaces.d/ens3
LiveCD環境の仮想ファイルシステムを新しいシステムのファイルシステムへバインドします。
# mount --rbind /dev /mnt/dev
# mount --rbind /proc /mnt/proc
# mount --rbind /sys /mnt/sys
chrootします。
# chroot /mnt /bin/bash --login
ロケールの設定を行います。
# locale-gen en_US.UTF-8 Generating locales (this might take a while)... en_US.UTF-8... done Generation complete.
# echo 'LANG="en_US.UTF-8"' > /etc/default/locale
タイムゾーンを設定します。Asia/Tokyoを選択します。
# dpkg-reconfigure tzdata
外部リポジトリの設定のため、以下を追記します。
# vi /etc/apt/sources.list deb http://archive.ubuntu.com/ubuntu xenial main universe deb-src http://archive.ubuntu.com/ubuntu xenial main universe deb http://security.ubuntu.com/ubuntu xenial-security main universe deb-src http://security.ubuntu.com/ubuntu xenial-security main universe deb http://archive.ubuntu.com/ubuntu xenial-updates main universe deb-src http://archive.ubuntu.com/ubuntu xenial-updates main universe
/proc/self/mounts を/etc/mtabへリンクします。
# ln -s /proc/self/mounts /etc/mtab
最小のパッケージをインストールします。
# apt-get update Get:1 http://security.ubuntu.com/ubuntu xenial-security InRelease [94.5 kB] Hit:2 http://archive.ubuntu.com/ubuntu xenial InRelease Get:3 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [94.5 kB] Get:4 http://security.ubuntu.com/ubuntu xenial-security/main Sources [30.5 kB] Get:5 http://security.ubuntu.com/ubuntu xenial-security/universe Sources [7840 B] Get:6 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages [117 kB] Get:7 http://security.ubuntu.com/ubuntu xenial-security/main Translation-en [44.8 kB] Get:8 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [33.2 kB] Get:9 http://archive.ubuntu.com/ubuntu xenial/main Sources [868 kB] Get:10 http://security.ubuntu.com/ubuntu xenial-security/universe Translation-en [19.9 kB] Get:11 http://archive.ubuntu.com/ubuntu xenial/universe Sources [7728 kB] Get:12 http://archive.ubuntu.com/ubuntu xenial/main Translation-en [568 kB] Get:13 http://archive.ubuntu.com/ubuntu xenial/universe amd64 Packages [7532 kB] Get:14 http://archive.ubuntu.com/ubuntu xenial/universe Translation-en [4354 kB] Get:15 http://archive.ubuntu.com/ubuntu xenial-updates/main Sources [90.6 kB] Get:16 http://archive.ubuntu.com/ubuntu xenial-updates/universe Sources [55.3 kB] Get:17 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [248 kB] Get:18 http://archive.ubuntu.com/ubuntu xenial-updates/main Translation-en [98.1 kB] Get:19 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [152 kB] Get:20 http://archive.ubuntu.com/ubuntu xenial-updates/universe Translation-en [74.9 kB] Fetched 22.2 MB in 9s (2317 kB/s)
# apt-get install --yes ubuntu-minimal Reading package lists... Done Building dependency tree... Done ubuntu-minimal is already the newest version (1.361). 0 upgraded, 0 newly installed, 0 to remove and 43 not upgraded.
ZFS関連のパッケージをインストールします。
# apt-get install --yes --no-install-recommends linux-image-generic
# apt-get install --yes zfs-initramfs
Grubをインストールします。
# apt-get install --yes grub-pc lqqqqqqu Configuring grub-pc tqqqqqqqk x GRUB install devices: x x x x [*] /dev/vda (53687 MB; ???) x x [ ] /dev/vda1 (53686 MB; ???) x x x x x xx x x mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj
必要なグループを作成します。
# addgroup --system lpadmin perl: warning: Setting locale failed. perl: warning: Please check that your locale settings: LANGUAGE = (unset), LC_ALL = (unset), LANG = "ja_JP.UTF-8" are supported and installed on your system. perl: warning: Falling back to the standard locale ("C"). Adding group `lpadmin' (GID 110) ... Done.
# addgroup --system sambashare perl: warning: Setting locale failed. perl: warning: Please check that your locale settings: LANGUAGE = (unset), LC_ALL = (unset), LANG = "ja_JP.UTF-8" are supported and installed on your system. perl: warning: Falling back to the standard locale ("C"). Adding group `sambashare' (GID 111) ... Done.
rootにパスワードを設定します。
# passwd
GRUB Installation
ZFSが認識されていることを確認します。
# grub-probe / zfs
ZFSモジュールがインストールされていることを確認します。
# ls /boot/grub/*/zfs.mod /boot/grub/i386-pc/zfs.mod
ZFSのカーネルモジュールをインストールしたのでInitramfsを再構築します。
# update-initramfs -c -k all perl: warning: Setting locale failed. perl: warning: Please check that your locale settings: LANGUAGE = (unset), LC_ALL = (unset), LANG = "ja_JP.UTF-8" are supported and installed on your system. perl: warning: Falling back to the standard locale ("C"). update-initramfs: Generating /boot/initrd.img-4.4.0-22-generic
Grubを編集します。
# vi /etc/default/grub : #GRUB_HIDDEN_TIMEOUT=0 : GRUB_CMDLINE_LINUX_DEFAULT="quiet" : GRUB_TERMINAL=console
変更を反映します。
# update-grub
# grub-install /dev/vda Installing for i386-pc platform. Installation finished. No error reported.
First Boot
初期インストール時点でのZFSスナップショットを作成します。
# zfs snapshot rpool/ROOT/ubuntu@install
# exit
すべてのファイルシステムをアンマウントします。
# mount | grep -v zfs | tac | awk '/\/mnt/ {print $3}' | xargs -i{} umount -lf {}
ストレージプールをexportします。
# zpool export rpool
OSを停止します。
# shutdown -h now
さくらのVPSのコントロールパネルからOSを起動し、正常に起動したら、マネジメントコンソールからrootでログインしてユーザを作成し、パスワードを設定します。
# groupadd -g 1000 hoge # useradd -u 1000 -g hoge -s /bin/bash -m -d /home/hoge hoge # usermod -a -G adm,cdrom,dip,lpadmin,plugdev,sambashare,sudo hoge # passwd hoge
openssh-serverのパッケージをインストールします。
# apt-get update # apt-get install openssh-server
ここまでくればもうほとんど問題ありません。 外部からsshログインします。
$ ssh hoge@さくらのVPSのIP
Final Cleanup
作成したスナップショットを削除します。
$ sudo zfs destroy rpool/ROOT/ubuntu@install
rootのパスワードをリセットします。
$ sudo usermod -p '*' root