Arch Linux をインストールして遊ぶ

archlinux-logo-dark-scalable.svg

家で遊びたいことが増えてきたので、クラウド全盛の時代に逆行して古い PC に Linux を入れて遊んでみることにしました。 Arch Linux を入れて遊んでいたのは数年前の話なので、すっかり忘れてしまった部分も多いのですが、それを思い出すのも楽しみの一つかなと思っています。

TOC

準備

インストール用の USB スティックを作成します。iso をダウンロードして dd するのが簡単です。

USB flash installation media:
https://wiki.archlinux.org/index.php/USB_flash_installation_media

ネットワークの接続と sshd の起動

Arch に優しいインストーラーはありません。 私はインストール中にググりたいしコピペもしたい軟弱者なので、何よりも先に IP アドレスを割り当てて ssh で接続します。

dhcpd はあらかじめ有効になっているので、dhcp が有効なネットワークでは固定 IP アドレスを設定しなくても大丈夫です。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# IP アドレスとルーティングの設定
ip address add 192.0.2.0/24 dev eth0
ip link set eth0 up
ip route add default via 192.0.2.1
echo 'nameserver 192.0.2.1' > /etc/resolve.conf

# root のパスワードの設定 (SSH 用)
passwd

# sshd の起動
sytemctl start sshd.service

パーティションの作成とフォーマット

BIOS か UEFI か、私は特にこだわりはないのです。今回は UEFI が使えるマシンだったので UEFI で作ることにします。 ファイルシステムは一般的な感じで作ります。ZFS とか btrfs で遊ぶのもいいですが、今回は手間をかけずに動いてほしいので一般的なの環境を作ります。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# パーティショニング
sgdisk -Z /dev/sda
sgdisk -n 1::+512M -t 1:ef00 -c 1:efi /dev/sda
sgdisk -n 2:: -t 2:8e00 -c 2:vg_local /dev/sda
sgdisk -p /dev/sda

# LVM
pvcreate /dev/sda2
vgcreate vg_local /dev/sda2  
lvcreate --size 512M --name lv_boot /dev/vg_local
lvcreate --size 1G --name lv_swap /dev/vg_local
lvcreate --size 16G --name lv_root /dev/vg_local

# ファイルシステムの作成
mkfs.vfat -n EFI -F32 /dev/sda1
mkfs.ext4 -L BOOT /dev/vg_local/lv_boot
mkfs.xfs -L ROOT /dev/vg_local/lv_root
mkswap -L SWAP /dev/vg_local/lv_swap

# マウント
mount -o defaults,discard LABEL=ROOT /mnt
mkdir /mnt/boot
mount -o defaults LABEL=BOOT /mnt/boot
mkdir /mnt/boot/efi
mount -o defaults LABEL=EFI /mnt/boot/efi
swapon -L SWAP

Unified Extensible Firmware Interface:
https://wiki.archlinux.org/index.php/Unified_Extensible_Firmware_Interface
LVM (Linux):
https://wiki.archlinux.org/index.php/LVM

ミラーサーバの選択

デフォルトでは各国のサーバが有効になっているので日本の距離が近いサーバを選びたいです。mirrorlist の上から順番に使われるので、以下の行をファイルの先頭に持ってきます。

1
2
# /etc/pacman.d/mirrorlist
Server = http://ftp.jaist.ac.jp/pub/Linux/ArchLinux/$repo/os/$arch

なお、万が一 mirrorlist を消してしまった時は、このサイト から元のリストを入手できます。

Mirrors:
https://wiki.archlinux.org/index.php/Mirrors

システムのインストール

基本的なパッケージのインストールなど Arch インストールお決まりの作業。

1
2
3
4
5
pacstrap /mnt base linux linux-firmware
# fstab の作成
genfstab -U /mnt >> /mnt/etc/fstab
# chroot
arch-chroot /mnt

Installation guide: https://wiki.archlinux.org/index.php/Installation_guide#Install_essential_packages

root のパスワード設定

私、この作業をよくやり忘れます・・・。 リブート後に root でログインできずに USB スティックで起動し直す羽目になるので地味に面倒です。忘れずにやりましょう。

また、次の initramfs の設定前に 必須ではないけどやっておくと便利な設定たち の中から気分で選んで設定しています。最低限ネットワークは設定した方がいいかなと思います。

initramfs の作成

lvm2 を hooks に追加します。

1
HOOKS=(... lvm2 ...)

initramfs を作成します

1
mkinitcpio -p linux

mkinitcpio:
https://wiki.archlinux.org/index.php/Mkinitcpio
Install Arch Linux on LVM: https://wiki.archlinux.org/index.php/Install_Arch_Linux_on_LVM#Adding_mkinitcpio_hooks

grub のインストール

1
2
3
4
5
6
# パッケージのインストール
pacman -S grub efibootmgr
# grub のインストール
grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=grub
# OR
# grub-install --target=x86_64-efi --efi-directory=/boot/efi --removable

UEFI 環境によっては指定の場所 /EFI/boot/bootx64.efi に置かないと起動できないものがあり、--removable をつけるとそこにインストールしてくれます。手動でコピーしてもいいのですが、ブートエントリが 1 つであればこのオプションが楽です。

1
2
3
# LVM 用のモジュールを追加
# /etc/default/grub
GRUB_PRELOAD_MODULES="... lvm"
1
2
# grub 設定の作成
grub-mkconfig -o /boot/grub/grub.cfg

GRUB:
https://wiki.archlinux.org/index.php/GRUB

OS のシャットダウンと起動

緊張の瞬間ですね。シャットダウン後に USB スティックを抜いて起動します。次に起動した時に無事にローカルディスクから起動してくれれば成功です。もし起動しなかった時は USB スティックから起動して必要な箇所を修正します。

1
2
exit # or Ctrl+D で chroot を終了して
shutdown -h now

必須ではないけどやっておくと便利な設定たち

いずれ使いそうなパッケージたちのインストール

1
pacman -S xfsprogs dosfstools usbutils hdparm lvm2 vim git man tmux gptfdisk dstat

管理者ユーザの作成

1
2
3
4
5
pacman -S sudo
useradd -m -G wheel b1naryd0g
passwd b1naryd0g
cp /etc/sudoers{,.backup}
sed -i 's/# %wheel ALL=(ALL) ALL/%wheel ALL=(ALL) ALL/' /etc/sudoers

Users and groups:
https://wiki.archlinux.org/index.php/Users_and_groups

タイムゾーンとローカライゼーション

サーバとして使うので最低限の設定ですね。デスクトップとして利用される場合は ja_JP.UTF-8 UTF-8 などのロケールを有効にするといいと思います。

1
2
3
4
5
ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
hwclock --systohc --utc
sed -i 's/#en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen
locale-gen
echo LANG=en_US.UTF-8 > /etc/locale.conf

Locale:
https://wiki.archlinux.org/index.php/Locale

ネットワーク

基本的なネットワーク周りを設定していきます。systemd-networkd はシンプルで使いやすいので気に入っています。

hostname の設定

1
2
3
4
5
echo arch > /etc/hostname
cat << EOF >> /etc/hosts
127.0.0.1   localhost localhost.localdomain arch
::1         localhost localhost.localdomain arch
EOF

nic 名の変更。MAC アドレスか DEVPATH かどちかを指定して固定します。

  • MAC アドレスは ip link show <dev> で調べられます
  • DEVPATH は file /sys/class/net/* で調べられます
1
2
3
4
5
6
7
8
9
# /etc/udev/rules.d/10-network.rules
SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="aa:bb:cc:dd:ee:ff", NAME="eth0"
# OR
SUBSYSTEM=="net", DEVPATH=="/devices/platform/wemac.*", NAME="eth0"

# udevadm コマンドでルールが適用されるか調べることができます
#  e.g.) 元の nic が eno1 の場合
#  # udevadm test /sys/class/net/eno1/ 2>/dev/null | grep SYSTEMD_ALIAS0
#  SYSTEMD_ALIAS=/sys/subsystem/net/devices/eth0

systemd-networkd 設定ファイルの作成

  • DHCP の場合
1
2
3
4
5
6
7
cat << EOF > /etc/systemd/network/20-wired.network
[Match]
Name=eth0

[Network]
DHCP=yes
EOF
  • Static の場合
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
cat << EOF > /etc/systemd/network/20-wired.network
[Match]
Name=eth0

[Link]
# MTUBytes=9000

[Network]
Address=192.0.2.2/24
Gateway=192.0.2.1
DNS=192.2.0.1
# DNS=1.1.1.1
# DNS=1.0.0.1
EOF

systemd-networkd 自動起動の設定

1
2
systemctl enable systemd-networkd.service
systemctl enable systemd-resolved.service

Network configuration:
https://wiki.archlinux.org/index.php/Network_configuration
systemd-networkd:
https://wiki.archlinux.org/index.php/Systemd-networkd

ファイアウォール

iptables より使いやすくなった(らしい)ntfables を使ってみたいと思います。私は今回が初めてのチャレンジです。

1
2
3
# パッケージのインストールと有効化
pacman -S nftables
systemctl enable nftables

デフォルトでは icmp, ipmpv6, ssh が許可されています。インストール直後はこれで困りませんのでこのまま利用します。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# デーモンの起動後に nft list ruleset で確認できます
# 設定ファイルは /etc/nftables.conf です
table inet filter {
        chain input {
                type filter hook input priority filter; policy accept;
                ct state { established, related } accept
                ct state invalid drop
                iifname "lo" accept
                ip protocol icmp accept
                ip6 nexthdr ipv6-icmp accept
                tcp dport 22 accept
                reject
        }

        chain forward {
                type filter hook forward priority filter; policy accept;
                drop
        }

        chain output {
                type filter hook output priority filter; policy accept;
        }
}

nftables:
https://wiki.archlinux.org/index.php/Nftables

ミラーの選択

1
2
3
4
5
pacman -S pacman-contrib
cp /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.backup
curl -o /etc/pacman.d/mirrorlist.generator 'https://www.archlinux.org/mirrorlist/?country=JP&protocol=http&protocol=https&ip_version=4&ip_version=6&use_mirror_status=on'
sed -i 's/^#Server/Server/' /etc/pacman.d/mirrorlist.generator
rankmirrors -n 6 /etc/pacman.d/mirrorlist.generator > /etc/pacman.d/mirrorlist

lvm

LVM の discard サポートを有効にします。lvremove などで物理ディスクの領域を必要としなくなった時に破棄してくれるようになります。(この設定は SSD の Trim とは関連しないようです。)

1
2
# /etc/lvm/lvm.conf 
issue_discards = 1

lvm.conf(5) - Linux man page:
https://linux.die.net/man/5/lvm.conf

issue_discards - Issue discards to a logical volumes’s underlying physical volume(s) when the logical volume is no longer using the physical volumes' space (e.g. lvremove, lvreduce, etc). Discards inform the storage that a region is no longer in use. Storage that supports discards advertise the protocol specific way discards should be issued by the kernel (TRIM, UNMAP, or WRITE SAME with UNMAP bit set). Not all storage will support or benefit from discards but SSDs and thinly provisioned LUNs generally do. If set to 1, discards will only be issued if both the storage and kernel provide support.

ntpd

一般的な ntpd をセットアップします。お好みで systemd-timesyncd とか chrony とかを使ってもいいと思います。

1
pacman -S ntp

インターネットマルチフィード(MFEED) が提供してくれている NTP サーバを設定 ntpd を有効にします。

1
2
3
4
# /etc/ntp.conf 
server ntp1.jst.mfeed.ad.jp
server ntp2.jst.mfeed.ad.jp
server ntp3.jst.mfeed.ad.jp
1
systemctl enable ntpd

NTP:
https://wiki.archlinux.org/index.php/Network_Time_Protocol_daemon

sshd

1
2
pacman -S openssh
systemctl enable sshd.service

Secure Shell:
https://wiki.archlinux.org/index.php/Secure_Shell

Arch User Repository (AUR)

Arch には、公式リポジトリとは別にコミュニティで運用されている AUR と呼ばれるリポジトリがあります。通常、AUR のパッケージは自分でビルドする必要がありますが、この作業をやってくれるヘルパープログラムがあるのでインストールします。yaourtというパッケージが人気かつ有名でしたが今は廃止されていました。AUR ヘルパーはいろいろあるのでお好きなものを使えばいいと思うのですが、みんなが使ってるパッケージな方が情報も多くなにかと便利なので、yayをインストールします。

  • makepkg コマンドは root 以外のユーザで実行しないと怒られてしまいますのでご注意ください
  • yay コマンドは root 以外のユーザで実行してください
1
2
3
4
sudo pacman -S --needed base-devel
git clone https://aur.archlinux.org/yay.git
cd yay
makepkg -si

AUR (en) - yay:
https://aur.archlinux.org/packages/yay/

rsyslog

ElasticSearch に直接ログを投げ込みたいなーともくろんでいるので rsyslog を使います。rsyslog は AUR で提供されているので、yay でインストールします。

1
2
yay -S rsyslog
mkdir /var/spool/rsyslog

systemd-journald からログを引き取るために imjournal モジュールを追加します。

1
2
# /etc/rsyslog.conf
$ModLoad imjournal
1
sudo systemctl enable rsyslog

Rsyslog:
https://wiki.archlinux.org/index.php/Rsyslog

Bash Tab 補完

bash-completion のインストール

1
pacman -C bash-completion
1
2
3
4
5
6
7
8
9
# .bashrc に設定の追記
cat << 'EOF' >> ~/.bashrc
# Use bash-completion, if available
[[ $PS1 && -f /usr/share/bash-completion/bash_completion ]] && \
    . /usr/share/bash-completion/bash_completion
EOF

# ~/.bashrc のリロード
source ~/.bashrc

scop/bash-completion: Programmable completion functions for bash:
https://github.com/scop/bash-completion#Installation

The Latest