安卓qemu安装docker

# 安装虚拟机软件
nix-env -iA nixpkgs.qemu
# alpine的包 qemu-img ovmf qemu-system-aarch64 qemu-system-x86_64
# 安装ovmf之后,sudo find / -name *.fd
# /usr/share/qemu/edk2-aarch64-code.fd

# 创建磁盘文件, 只是初始化大小,并不占实际空间
qemu-img create -f qcow2 disk-virt-alpine.img 5000g
# 下载iso镜像文件
wget http://dl-cdn.alpinelinux.org/alpine/v3.10/releases/x86_64/alpine-virt-3.10.1-x86_64.iso
wget https://mirrors.aliyun.com/alpine/v3.18/releases/aarch64/alpine-virt-3.18.3-aarch64.iso
# Ubuntu镜像 https://cdimage.ubuntu.com/releases/20.04/release/
# https://mirrors.aliyun.com/ubuntu-releases/20.04/
# Ubuntu Arm镜像 https://mirrors.aliyun.com/ubuntu-cdimage/ubuntu/releases/22.04/release/

# 启动虚拟机; -smp 2 CPU数量, 安装的时候如果启动不了可以去掉smp参数, 不能超过宿主机的核数量
qemu-system-x86_64 -smp 16 -hda disk-virt-alpine.img -cdrom alpine-virt-3.10.1-x86_64.iso -boot d -m 2048 -nographic

# arm需要 QEMU_EFI.fd 启动文件 https://trustedfirmware-a.readthedocs.io/en/latest/plat/qemu.html#getting-non-tf-images
# nix可以使用包pkgs.OVMFFull.fd pkgs.pkgsCross.aarch64-multiplatform.OVMF.fd

# 居然arm手机运行x86还快些
wget http://releases.linaro.org/components/kernel/uefi-linaro/16.02/release/qemu64/QEMU_EFI.fd
# 防止终端hang住,一直回车刷新终端
qemu-system-aarch64 -smp 16 -m 2048 -cpu cortex-a57 -M virt -bios QEMU_EFI.fd -nographic -drive if=none,file=alpine-virt-3.18.3-aarch64.iso,id=cdrom,media=cdrom -device virtio-scsi-device -device scsi-cd,drive=cdrom -hda disk-virt-alpine.img


# 等待,然后进入系统之后,登录用户名是root,无密码, 然后配置网络,源,最后进行安装


# 启动已经安装到磁盘的alpine系统,等待... 写成一个.sh脚本之后可以方便了
qemu-system-x86_64 -smp 16 -hda disk-virt-alpine.img -boot c -m 2048 -netdev user,id=nde1,hostfwd=tcp::2222-:22 -device e1000,netdev=nde1,id=d-net1 -nographic
# arm启动
qemu-system-aarch64 -smp 16 -bios QEMU_EFI.fd -cpu cortex-a57 -M virt -hda disk-virt-alpine.img -boot c -m 2048 -nographic

# 有的系统使用-nographic会一直提示 Booting from Hard Disk... 但是实际是运行了

# 查看内核数量
cat /proc/cpuinfo | grep "processor" | wc -l

# 配置网络
vi /etc/network/interfaces
# 默认网卡
auto eth0
# dhcp
iface eth0 inet dhcp
# static
iface eth2 inet static
      address 192.168.1.201
      netmask 255.255.255.0
      gateway 192.168.1.1

# 重启一下
service networking restart
rc-update add networking boot


# 编辑源文件,取消注释,且改为阿里源地址
vi /etc/apk/repositories
/media/cdrom/apks
http://mirrors.aliyun.com/alpine/v3.18/main
http://mirrors.aliyun.com/alpine/v3.18/community
http://mirrors.aliyun.com/alpine/edge/main
http://mirrors.aliyun.com/alpine/edge/community
http://mirrors.aliyun.com/alpine/edge/testing


# 初始化系统或者直接安装到磁盘(如果提示缺少syslinux包,需要编辑源之后安装syslinux包)
setup-alpine
setup-disk
# 安装完成之后, 关闭虚拟机退出
poweroff


# 安装到硬盘之后安装docker
apk update
# 最好升级一下
apk upgrade
apk add docker
# 启动docker服务
service docker start
# 开机启动
rc-update add docker boot
  • 虚拟机联网

# 推荐: 使用一个网卡连接外网, 并映射22到宿主机的2222端口
-netdev user,id=nde1,hostfwd=tcp::2222-:22 -device e1000,netdev=nde1,id=d-net1

# 虚拟一个网卡,net0
-netdev user,id=net0 -device e1000,netdev=net0
# 虚拟一个tap,net1
-netdev tap,id=net1,ifname=tap0,script=no,downscript=no -device e1000,netdev=net1

# 内网,需要使用tap网络适配器虚拟网卡,并配置静态ip和tap同一网段: tab是82,虚拟机就可以配置83
iface eth1 inet static
      address 169.254.222.83
      netmask 255.255.255.0
      gateway 169.254.222.82
# 但是启动tap之后就无法访问外网了
# 启动
ifup eth1
  • sshd

apk add openssh
vi /etc/ssh/sshd_config
# 修改或者增加
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication yes
# 修改
PermitRootLogin yes

# 重启
service sshd restart
rc-update add sshd boot
netstat -anp|grep 22
  • 暴露docker端口

# 脚本实例
#!/run/current-system/sw/bin/env bash
qemu-system-aarch64 -bios QEMU_EFI.fd \
-cpu cortex-a57 -M virt -hda disk-virt-alpine.img \
-boot c -m 2048 -nographic \
-netdev user,id=nde1,hostfwd=tcp::2222-:22,hostfwd=tcp::39012-:39012 -device e1000,netdev=nde1,id=d-net1


vi /etc/init.d/docker
# command_args最后加上,使用tcp和unix
command_args="${DOCKER_OPTS} -H tcp://0.0.0.0:39012 -H unix:///var/run/docker.sock"
# 重启
service docker restart
ps aux|grep dockerd
netstat -anp |grep 39012

# 宿主机使用
export DOCKER_HOST=tcp://0.0.0.0:39012
  • pc使用kvm安装winserver

# 安装类似VBoxGuestAdditions_6.1.34.iso的virtio-win-0.1.173.iso的东西,就可以共享剪切板了
https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/virtio-win-0.1.173-9/virtio-win-0.1.173.iso

# 修改分辨率
[显卡]选择[QXL]
# 进一步可以安装
加载[virtio-win-0.1.173.iso]安装[E:\qxl]目录的驱动
# 安装
"E:\virtio-win-gt-x64.msi"
"E:\virtio-win-guest-tools.exe"

# 之后就可以在系统里面设置分辨率了,以及使用[libvirt kvm 配置 vnc]可以共享剪切板
  • nix arm vnc 安装

# ./nixos使用技巧.md
nix-env -iA nixpkgs.tigervnc --option substituters https://mirrors.ustc.edu.cn/nix-channels/store
nix-env -iA nixpkgs.openbox --option substituters https://mirrors.ustc.edu.cn/nix-channels/store
nix-env -iA nixpkgs.xorg.xinit --option substituters https://mirrors.ustc.edu.cn/nix-channels/store
nix-env -iA nixpkgs.xorg.xclock --option substituters https://mirrors.ustc.edu.cn/nix-channels/store



# 配置xstartup和passwd之后就可以启动了
xinit ~/.vnc/xstartup -- $(realpath $(which Xvnc)) :1 PasswordFile=~/.vnc/passwd
# 测试用, 不启动session
Xvnc :2 -geometry 1024x768 -pn -rfbauth ~/.vnc/passwd -rfbport 5902

# 测试
export DISPLAY=:0 && xclock
  • 把本机器的x11开放到vnc

nix-env -iA nixpkgs.x11vnc --option substituters https://mirrors.ustc.edu.cn/nix-channels/store

# 开放到5900端口, -display使用本机的x11服务
x11vnc -storepasswd
# 直接前台运行
x11vnc -forever -usepw -display :0
x11vnc -forever -usepw -display :0 -passwd 123456

# 后台运行
x11vnc -rfbport 5900 -rfbauth ~/.vnc/passwd -display :2 -forever -bg -repeat -nowf -o ~/.vnc/x11vnc.log
# 查看端口
netstat -tuln|grep :59
# tcp        0      0 0.0.0.0:5900            0.0.0.0:*               LISTEN      24921/x11vnc
  • 脚本

@REM 初次安装
qemu-system-aarch64 -m 2048 -cpu cortex-a57 -M virt -bios QEMU_EFI.fd -nographic -drive if=none,file=alpine-virt-3.18.3-aarch64.iso,id=cdrom,media=cdrom -device virtio-scsi-device -device scsi-cd,drive=cdrom -hda disk-virt-alpine.img
@REM 安装后启动
@REM qemu-system-aarch64 -bios QEMU_EFI.fd -cpu cortex-a57 -M virt -hda disk-virt-alpine.img -boot c -m 2048 -nographic -netdev user,id=network0,hostfwd=tcp::2223-:22 -device virtio-net-pci,netdev=network0


qemu-system-x86_64 -hda disk-virt-alpine.img -boot c -m 2048 -nographic -netdev user,id=nde1,hostfwd=tcp::2222-:22 -device e1000,netdev=nde1,id=d-net1
  • 使用root用户安装nix包管理器

# 需要先创建目录和构建的用户
mkdir -m 0755 /nix && chown root /nix
addgroup nixbld
adduser nixbld -G nixbld

# 之后下载安装包
wget https://mirrors.tuna.tsinghua.edu.cn/nix/nix-2.9.2/nix-2.9.2-aarch64-linux.tar.xz
tar xf nix-2.9.2-aarch64-linux.tar.xz
cd nix-2.9.2-aarch64-linux/
./install --no-daemon --no-channel-add

# 添加bash环境
vi ~/.bashrc
. /root/.nix-profile/etc/profile.d/nix.sh
# 对应项目
https://jihulab.com/jcleng/kickstart
# 镜像需要ubuntu-legacy-server, 使用qemu启动的时候cpu请使用1或者不指定
https://cdimage.ubuntu.com/ubuntu-legacy-server/releases/20.04/release/ubuntu-20.04.1-legacy-server-amd64.iso
https://mirror.nju.edu.cn/ubuntu-cdimage/ubuntu-legacy-server/releases/20.04/release/
https://mirrors.aliyun.com/ubuntu-cdimage/ubuntu-legacy-server/releases/20.04/release/
# 相关文档
https://github.com/vrillusions/ubuntu-kickstart/blob/master/20.04/README.md
https://help.ubuntu.com/community/KickstartCompatibility
https://help.ubuntu.com/community/StricterDefaults
https://blog.while-true-do.io/kickstart-getting-started/

# 从iso解压出 /isolinux/txt.cfg, 编辑启动项增加ks参数, ks的地址需要网络地址: 自建仓库: https://jihulab.com/jcleng/kickstart/
append  file=/cdrom/preseed/ubuntu-server.seed vga=788 initrd=/install/initrd.gz ks=https://ghproxy.com/https://raw.githubusercontent.com/vrillusions/ubuntu-kickstart/master/20.04/ks-2004-minimalvm.cfg quiet ---

# 然后保存iso文件
# windows用软碟通, linux用xorriso
# 创建对应的文件夹, 然后进行添加(删除outdev历史iso文件才能重新生成文件)
# 增加可启动的目录
xorriso -indev ubuntu-20.04.1-legacy-server-amd64.iso -outdev out.iso -boot_image isolinux dir=/isolinux -add isolinux
file out.iso
# out.iso: ISO 9660 CD-ROM filesystem data (DOS/MBR boot sector) 'Ubuntu-Server 20.04.6 LTS amd64' (bootable)

tree
# .
# ├── disk-virt.img
# ├── isolinux
# │   └── txt.cfg
# ├── out.iso
# ├── run.sh
# └── ubuntu-20.04.1-legacy-server-amd64.iso
  • txt.cfg 原文

default install
label install
  menu label ^Install Ubuntu ServerX
  kernel /install/vmlinuz
  append  file=/cdrom/preseed/ubuntu-server.seed vga=788 initrd=/install/initrd.gz ks=https://ghproxy.com/https://raw.githubusercontent.com/vrillusions/ubuntu-kickstart/master/20.04/ks-2004-minimalvm.cfg quiet ---
label check
  menu label ^Check disc for defects
  kernel /install/vmlinuz
  append   MENU=/bin/cdrom-checker-menu vga=788 initrd=/install/initrd.gz quiet ---
label memtest
  menu label Test ^memory
  kernel /install/mt86plus
label hd
  menu label ^Boot from first hard disk
  localboot 0x80
  • qemu命令增加网桥

# https://blog.christophersmart.com/2016/08/31/configuring-qemu-bridge-helper-after-access-denied-by-acl-file-error/

echo "allow all" | sudo tee /etc/qemu/${USER}.conf
echo "include /etc/qemu/${USER}.conf" | sudo tee --append /etc/qemu/bridge.conf
sudo chown root:${USER} /etc/qemu/${USER}.conf
sudo chmod 640 /etc/qemu/${USER}.conf

This user should now be able to successfully kick up the guest connected to br0.
qemu-system-x86_64 \
-machine accel=kvm \
-cpu host \
-netdev bridge,br=br0,id=net0 \
-device virtio-net-pci,netdev=net0