System build -- NFS mount root file system

This article introduces the use of NFS to mount the root file system.

NFS service needs to be configured on host first

startnfs.sh:

HOST_IP=192.168.1.1
NET_NUMBER=192.168.1.0
NET_MASK=255.255.255.0

NFS_ROOT=~/work/rootfs

sudo apt-get install nfs-kernel-server
sudo apt-get install uml-utilities

if [ ! -e /etc/exports.bak ];then
       sudo cp /etc/exports /etc/exports.bak
fi

sudo echo "${NFS_ROOT} *(rw,sync,no_subtree_check,no_root_squash)" > /etc/exports
sudo /etc/init.d/nfs-kernel-server restart

TAP=$(ifconfig tap0 | head -n 1 | awk '{print $1}')

if [ x${TAP} = x ];then
       sudo tunctl -u $(whoami) -t tap0
       sudo ifconfig tap0 ${HOST_IP}
       sudo route add -net ${NET_NUMBER} netmask ${NET_MASK} dev tap0
       sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
fi

Create a TAP network on the host computer, and the corresponding network device is tap0.

qemu start target device

qemu-system-arm -M vexpress-a9 \
        -smp 4 \
        -m 1024m \
        -kernel linux-4.0/arch/arm/boot/zImage \
        -append "console=ttyAMA0 loglevel=8 root=/dev/nfs rw nfsroot=${HOST_IP}:${NFS_ROOT},nolock ip=${TARGET_IP}" \
        -dtb linux-4.0/arch/arm/boot/dts/vexpress-v2p-ca9.dtb \
        -net nic -net tap,ifname=tap0,script=no \
        -nographic

The key is to pass in the cmdline of "root=/dev/nfs rw nfsroot=${HOST_IP}" {NFS_ROOT},nolock ip=${TARGET_IP} ", and the corresponding qemu option" - net NIC net tap, ifname = tap0, script = no ". The qemu option here specifies the virtual network card and adds the network of the virtual machine to the tap0.
If we use the arm64 platform, qemu's commands will be different:

qemu-system-aarch64 -machine virt \
        -cpu cortex-a57 \
        -machine type=virt \
        -nographic -m 2048 \
        -smp 2 \
        -kernel linux-4.0/arch/arm64/boot/Image \
        -append "console=ttyAMA0 loglevel=8 root=/dev/nfs rw nfsroot=${HOST_IP}:${NFS_ROOT}/arm64/_install,nolock ip=${TARGET_IP}:${HOST_IP}:::::off::" \
        -netdev tap,id=tap0,ifname=tap0,script=no \
		-device virtio-net-device,netdev=tap0

The main difference is that the options passed in by qemu have changed:

"- netdev tap,id=tap0,ifname=tap0,script=no" specifies the backend of QEMU virtual machine network device.
"- device virtio net device, netdev = tap0" indicates the network device in the virtual machine. You need to specify netdev on the binding.

Tags: sudo network Linux

Posted on Wed, 04 Dec 2019 01:40:54 -0800 by gardner1