Arch in Qemu in Termux

インストール環境構築

qemuのインストール

pkg install qemu-utils qemu-common qemu-system-x86_64-headless

.iso fileのダウンロード

mkdir arch && cd $_
wget https://mirror.aria-on-the-planet.es/archlinux/iso/2025.02.01/archlinux-2025.02.01-x86_64.iso

virtual disk作成

qemu-img create -f qcow2 arch.img 10G

確認

qemu-img info arch.img

↓出力

image: arch.img
file format: qcow2
virtual size: 10 GiB (10737418240 bytes)
disk size: 196 KiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    compression type: zlib
    lazy refcounts: false
    refcount bits: 16
    corrupt: false
    extended l2: false
Child node '/file':
    filename: arch.img
    protocol type: file
    file length: 192 KiB (197120 bytes)
    disk size: 196 KiB

インストール

Archの初回起動

qemu-system-x86_64 \
  -m 4096 \
  -smp 2 \
  -drive file=arch.img,format=qcow2 \
  -cdrom archlinux-2025.02.01-x86_64.iso \
  -boot d \
  -nographic

以下の画面でTabを入力

Fig1

表示されるブートオプションの末尾に次の内容を追加して、Enter
※ブートオプションはスペースで区切られるので、先頭に半角スペースが入っています

 console=ttyS0

archiso login:と出てきたら root と入力

Fig2

パーティションを作ります
対話形式で作成できるコマンド

fdisk /dev/sda

Command (m for help):
n
その後は全部デフォルト
無入力でEnterを押下すれば自動的にデフォルトが選択される

Command (m for help):に戻ってくるので、wで保存して終了

間違えた場合は、dで一度消去してから作り直す

パーティションをフォーマットしてマウントしてカーネルをインストール(?)

mkfs.ext4 /dev/sda1
mount /dev/sda1 /mnt
pacstrap /mnt base linux linux-firmware

マウント情報をfstabに書き込む

genfstab -U /mnt >> /mnt/etc/fstab

qemu用にブート設定

chrootで、/mntをルートとして、中に入る

arch-chroot /mnt

エディタ入れる

pacman -S nano

QemuはデフォルトでGRUBの32bitを使ってブートするので、Archにインストール

pacman -S grub
grub-install --target=i386-pc /dev/sda
grub-mkconfig -o /boot/grub/grub.cfg

grubの設定ファイルを編集

nano /etc/default/grub

多分こんな中身"

# GRUB boot loader configuration
 
GRUB_DEFAULT=0
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="Arch"
GRUB_CMDLINE_LINUX_DEFAULT="loglevel=3 quiet"
GRUB_CMDLINE_LINUX=""

# Preload both GPT and MBR modules so that they are not missed
GRUB_PRELOAD_MODULES="part_gpt part_msdos"

# Uncomment to enable booting from LUKS encrypted devices
#GRUB_ENABLE_CRYPTODISK=y

# Set to 'countdown' or 'hidden' to change timeout behavior,
# press ESC key to display menu.
GRUB_TIMEOUT_STYLE=menu
# Uncomment to use basic console
GRUB_TERMINAL_INPUT=console
# Uncomment to disable graphical terminal
#GRUB_TERMINAL_OUTPUT=console

# The resolution used on graphical terminal
# note that you can use only modes which your graphic card supports via VBE
# you can see them in real GRUB with the command `videoinfo'
GRUB_GFXMODE=auto

# Uncomment to allow the kernel use the same resolution used by grub
GRUB_GFXPAYLOAD_LINUX=keep

# Uncomment if you want GRUB to pass to the Linux kernel the old parameter
# format "root=/dev/xxx" instead of "root=/dev/disk/by-uuid/xxx"
#GRUB_DISABLE_LINUX_UUID=true

# Uncomment to disable generation of recovery mode menu entries
GRUB_DISABLE_RECOVERY=true

# Uncomment and set to the desired menu colors.  Used by normal and wallpaper
# modes only.  Entries specified as foreground/background.
#GRUB_COLOR_NORMAL="light-blue/black"
#GRUB_COLOR_HIGHLIGHT="light-cyan/blue"

# Uncomment one of them for the gfx desired, a image background or a gfxtheme
#GRUB_BACKGROUND="/path/to/wallpaper"
#GRUB_THEME="/path/to/gfxtheme"

変更場所は↑でハイライトした所

  • 7行目のGRUB_CMDLINE_LINUX=""GRUB_CMDLINE_LINUX="console=ttyS0"
  • 19行目のGRUB_TERMINAL_INPUT=consoleGRUB_TERMINAL_INPUT=serial

編集が完了したら反映

grub-mkconfig -o /boot/grub/grub.cfg

インストール後のArchで、設定しても無いのにパスワードを求められてログイン出来ないので、ここで予め設定しておく

passwd

以下でTermuxに戻る

exit
poweroff

セッションを閉じる場合はunmount

unmount /mnt

起動用スクリプト

スクリプトファイルstart-arch.shを作って実行

#!/data/data/com.termux/files/usr/bin/sh

qemu-system-x86_64 \
  -m 4096 \
  -smp 2 \
  -boot c \
  -drive file=/data/data/com.termux/files/home/arch.img,format=qcow2 \
  -net nic \
  -net user,hostfwd=tcp::2222-:22\
  -serial mon:stdio \
  -nographic

イメージファイルはカレントディレクトリからの位置を参照するので、絶対パスで指定してあげると、コマンド化しやすい

chmod +x start-arch.sh
./start-arch.sh

Arch設定

ネットワーク

pacman時にmirrorサイトに繋がらない場合、ネットワークの設定をする

echo "nameserver 8.8.8.8" > /etc/resolv.conf
ip link set ens3 up
ip addr add 10.0.2.15/24 dev ens3
ip route add default via 10.0.2.2

次回から自動化

pacman -Sy dhcpcd
systemctl enable dhcpcd@ens3.service

ログイン時に.bashrcが実行されるようにする

echo 'if [ -f ~/.bashrc ]; then source ~/.bashrc; fi' >> ~/.bash_profile

ユーザ毎なので、新しいユーザでログインしたら実行する

TERM

TERM環境変数が実際の環境と異なると、エディタ画面が残ったりする
TermuxのTERMを設定

touch ~/.bashrc
echo "export TERM=xterm-256color" >> ~/.bashrc

リサイズ

シグナルハンドラが使えないので、フォントサイズを拡縮するとエディタがとんでもないことになる
変更後resizeコマンドを使って、手動でサイズを合わせられるように、xtermをインストール

pacman -S xterm

ユーザ作成

useradd -m abc
passwd abc

sudoインストール

pacman -S sudo
nano /etc/sudoers

↓のコメントアウトを外す

%wheel ALL=(ALL:ALL) ALL

wheelグループの中に属させる

usermod -aG wheel abc

SSH

ssh接続で表示崩れを減らす試み

Arch側の設定

Arch内で、opensshをインストール

sudo pacman -S openssh

サーバーの起動とログイン時の自動起動を設定

sudo systemctl enable sshd
sudo systemctl start sshd

Terminusの場合

Terminusをインストール
Terminus-Google Play

Hosts→右下の+からNew hostを選択

Alias︰任意
Hostname or IP Address︰localhost
Port︰2222
Username︰Archのユーザ名
Password︰ユーザのパスワード

Archを起動すると、Connectionタブから入れるようになります

Termuxの場合

Archを起動したまま、新しいセッションを開く
※左からスワイプするとなんか出るので、「NEW SESSION」押下

pkg install openssh
ssh [ユーザー名]@localhost -p 2222