使用Qemu運行Ubuntu文件系統(1)

參考

 

平臺

Qemu + AArch32(vexpress-ca9)
Linux: Linux-4.14.13
 

概述

        根文件系統採用busybox的優勢是節省空間,缺點是添加一款軟件就須要本身找源代碼編譯,還須要解決依賴關係,費時費力,爲了解決這個問題,可使用ubuntu的arm版本的根文件系統,這樣就能夠充分利用ubuntu軟件倉庫裏已經作好的衆多軟件包,而且依賴關係也已經幫咱們解決了,安裝的方式跟PC機同樣,使用apt命令。
 

正文

使用的平臺仍是Qemu,用它來模擬一個vexpress-ca9開發板,這部分能夠參考下面的博客:

用Qemu搭建aarch32學習環境html

爲Qemu aarch32開發板添加sd卡linux

 
啓動Qemu的命令以下:
kernel_dir=./Linux-4.14.13
kernel_image=${kernel_dir}/arch/arm/boot/zImage
dtb_image=${kernel_dir}/arch/arm/boot/dts/vexpress-v2p-ca9.dtb

sudo qemu-system-arm \
    -M vexpress-a9 \
    -m 1024M \
    -smp 4 \
    -kernel ${kernel_image} \
    -nographic \
    -append "root=/dev/mmcblk0 rw rootfstype=ext4 console=ttyAMA0,115200" \
    -net nic,vlan=2 -net tap,vlan=2,ifname=tap2 \
    -sd ./ubuntu_rootfs/ubuntu.img \
    -dtb ${dtb_image}
說明,其中/dev/mmcblk0表示跟文件系統所在的設備,由於沒有進行分區而且使用sd卡啓動方式,因此是mmcblk0。第13行就是咱們下面要製做的ubuntu根文件系統。

1、安裝Qemu

在Linux PC主機上安裝模擬器:
sudo apt-get install qemu-user-static

2、下載和解壓 ubuntu-core

 

 先從官方上獲取ubuntu core的tar包:http://cdimage.ubuntu.com/ubuntu-base/releases/16.04/release/shell

選擇下載ubuntu-base-16.04-core-armhf.tar.gz,下載完以後,建立臨時文件夾並解壓根文件系統:express

mkdir tmp
sudo tar -xf ubuntu-base-16.04-core-armhf.tar.gz -C tmp/

3、修改根文件系統

一、準備網絡

sudo cp -b /etc/resolv.conf tmp/etc/resolv.conf
這個文件存放了DNS服務器的地址

二、準備qemu

cp /usr/bin/qemu-arm-static tmp/usr/bin/

三、增長軟件源

sudo vim tmp/etc/apt/source.list

將下面的兩行的註釋取消掉:
deb http://ports.ubuntu.com/ubuntu-ports/ xenial universe
deb-src http://ports.ubuntu.com/ubuntu-ports/ xenial universe

四、進入根文件系統進行操做

可使用下面的腳本ch-mount.sh將根目錄切換到tmp下:ubuntu

#!/bin/bash

function mnt() {
    echo "MOUNTING"
    sudo mount -t proc /proc ${2}proc
    sudo mount -t sysfs /sys ${2}sys
    sudo mount -o bind /dev ${2}dev

    sudo chroot ${2}
}

function umnt() {
    echo "UNMOUNTING"
    sudo umount ${2}proc
    sudo umount ${2}sys
    sudo umount ${2}dev

}


if [ "$1" == "-m" ] && [ -n "$2" ] ;
then
    mnt $1 $2
elif [ "$1" == "-u" ] && [ -n "$2" ];
then
    umnt $1 $2
else
    echo ""
    echo "Either 1'st, 2'nd or both parameters were missing"
    echo ""
    echo "1'st parameter can be one of these: -m(mount) OR -u(umount)"
    echo "2'nd parameter is the full path of rootfs directory(with trailing '/')"
    echo ""
    echo "For example: ch-mount -m /media/sdcard/"
    echo ""
    echo 1st parameter : ${1}
    echo 2nd parameter : ${2}
fi

切換根目錄:vim

sudo ch-mount.sh -m tmp/
  • 更新

apt update 
apt upgrade
  • 安裝軟件包

apt install udev       #不然ttyAMA0沒法找到
apt install vim        #vim編輯器
apt install net-tools  #ifconfig,netstat,route,arp等
apt install iputils-ping #ping
apt install sudo       #sudo命令
#apt install ssh        #ssh的client和server
apt install ethtool    #ethtool命令,顯示、修改以太網設置
#apt install wireless-tools    #iwconfig等,顯示、修改無線設置
apt install ifupdown          #ifup,ifdown等工具
#apt install network-manager   #Network Manager服務和框架,高級網絡管理
apt install rsyslog           #系統log服務
apt install bash-completion   #bash命令行補全
apt install htop              #htop工具,交互式進程查看器
apt install nfs-common #能夠遠程掛載mount -t nfs
apt install telnetd #能夠用telnet登陸

對於bash命令自動補全功能,須要安裝bash-completion,此外還須要修改/etc/bash.bashrc,將下面的命令的註釋去掉:bash

1 # enable bash completion in interactive shells
2 if ! shopt -oq posix; then
3   if [ -f /usr/share/bash-completion/bash_completion ]; then
4     . /usr/share/bash-completion/bash_completion
5   elif [ -f /etc/bash_completion ]; then
6     . /etc/bash_completion
7   fi
8 fi

 而後從新登陸便可。服務器

  • 建立用戶

所有安裝完以後,添加一個用戶pengdl,並設置密碼,同時把root的密碼也修改一下:
useradd -s '/bin/bash' -m -G adm,sudo pengdl  #增長pengdl用戶
passwd pengdl #給pengdl用戶設置密碼
passwd root #修改root密碼
爲pengdl增長sudo權限,修改/etc/sudoers,增長:
pengdl  ALL=(ALL:ALL) ALL
  • 設置ip

編輯/etc/network/interfaces,添加以下內容:網絡

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 192.168.1.5
netmask 255.255.255.0
gateway 192.168.1.1
  • 設置hostname

設置主機名稱:
echo "ubuntu-arm">/etc/hostname

設置本機入口ip:
echo "127.0.0.1 localhost">>/etc/hosts
echo "127.0.1.1 ubuntu-arm">>/etc/hosts

 

在軟件安裝完畢後,退出根目錄:ctrl+D 或者 exit

執行umount:

./ch-mount.sh -u tmp/

 

4、製做根文件系統

  • 查看根文件系統的大小

du -sh tmp/
351M    tmp/
  • 生成鏡像,並格式爲ext4

dd if=/dev/zero  of=ubuntu.img   bs=1M   count=1024
mkfs.ext4 ubuntu.img
mkdir mnt
sudo mount  ubuntu.img mnt
sudo cp -rfp tmp/* mnt
sudo umount mnt

#檢查並修復ubuntu.img
e2fsck -p -f ubuntu.img

5、測試

  • 開機:

[    8.915670] Freeing unused kernel memory: 1024K
[   12.875789] systemd[1]: Failed to insert module 'autofs4': No such file or directory
[   13.486345] systemd[1]: systemd 229 running in system mode. (+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN)
[   13.497463] systemd[1]: Detected architecture arm.

Welcome to Ubuntu 16.04 LTS!

[   13.536063] systemd[1]: Set hostname to <ubuntu-arm>.
[   15.590117] systemd-gpt-aut (1023) used greatest stack depth: 4908 bytes left
[   16.987350] systemd[1]: Reached target Swap.
...
[  OK  ] Found device /dev/ttyAMA0.
[  OK  ] Reached target Sound Card.
[  OK  ] Started ifup for eth0.
[  OK  ] Found device /sys/subsystem/net/devices/eth0.
[  OK  ] Started LSB: Set the CPU Frequency Scaling governor to "ondemand".
[  OK  ] Started Raise network interfaces.
[  OK  ] Reached target Network.
         Starting /etc/rc.local Compatibility...
[  OK  ] Started /etc/rc.local Compatibility.
[  OK  ] Started Serial Getty on ttyAMA0.
[  OK  ] Started Getty on tty5.
[  OK  ] Started Getty on tty3.
[  OK  ] Started Getty on tty6.
[  OK  ] Started Getty on tty2.
[  OK  ] Started Getty on tty1.
[  OK  ] Started Getty on tty4.
[  OK  ] Reached target Login Prompts.
[  OK  ] Reached target Multi-User System.
[  OK  ] Reached target Graphical Interface.
         Starting Update UTMP about System Runlevel Changes...
[  OK  ] Started Update UTMP about System Runlevel Changes.

Ubuntu 16.04 LTS ubuntu-arm ttyAMA0

ubuntu-arm login: pengdl
Password: 
Last login: Sun Aug 26 16:14:28 UTC 2018 on ttyAMA0
Welcome to Ubuntu 16.04 LTS (GNU/Linux 4.14.13+ armv7l)

 * Documentation:  https://help.ubuntu.com/
pengdl@ubuntu-arm:~$ ifconfig 
eth0      Link encap:Ethernet  HWaddr 52:54:00:12:34:56  
          inet addr:192.168.1.5  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:13 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:2101 (2.1 KB)  TX bytes:296 (296.0 B)
          Interrupt:31 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

pengdl@ubuntu-arm:~$ ping www.baidu.com
PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data.
64 bytes from 61.135.169.121: icmp_seq=1 ttl=56 time=4.12 ms
64 bytes from 61.135.169.121: icmp_seq=2 ttl=56 time=3.71 ms
64 bytes from 61.135.169.121: icmp_seq=3 ttl=56 time=3.83 ms
^C
--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2017ms
rtt min/avg/max/mdev = 3.717/3.891/4.125/0.186 ms

pengdl@ubuntu-arm:~$ sudo apt install htop
Reading package lists... Done
Reading state information... Done
Suggested packages:
  lsof strace
The following NEW packages will be installed:
  htop
0 upgraded, 1 newly installed, 0 to remove and 51 not upgraded.
Need to get 67.7 kB of archives.
After this operation, 146 kB of additional disk space will be used.
Get:1 http://ports.ubuntu.com/ubuntu-ports xenial/universe armhf htop armhf 2.0.1-1 [67.7 kB]
Fetched 67.7 kB in 2s (27.0 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package htop.
(Reading database ... 10417 files and directories currently installed.)
Unpacking htop (2.0.1-1) ..._2.0.1-1_armhf.deb ...
Processing triggers for mime-support (3.59ubuntu1) ...
  • 關機:

pengdl@ubuntu-arm:~$ sudo shutdown -h now
[  OK  ] Stopped target Timers.
[  OK  ] Stopped Daily apt activities.
[  OK  ] Stopped target System Time Synchronized.
[  OK  ] Stopped target Graphical Interface.
[  OK  ] Stopped Daily Cleanup of Temporary Directories.
[  OK  ] Stopped target Multi-User System.
[  OK  ] Stopped target Login Prompts.
         Stopping Getty on tty6...
         Stopping Getty on tty1...
         Stopping Getty on tty5...
         Stopping Getty on tty4...
[  OK  ] Stopped getty on tty2-tty6 if dbus and logind are not available.
         Stopping Getty on tty2...
         Stopping Getty on tty3...
         Stopping LSB: Set the CPU Frequency Scaling governor to "ondemand"...
[  OK  ] Reached target Unmount All Filesystems.
[  OK  ] Stopped target Sound Card.
         Stopping Serial Getty on ttyAMA0...
[  OK  ] Removed slice system-getty.slice.
[  OK  ] Stopped /etc/rc.local Compatibility.
[  OK  ] Stopped target Network.
         Stopping Raise network interfaces...
         Stopping Permit User Sessions...
[  OK  ] Removed slice system-serial\x2dgetty.slice.
[  OK  ] Stopped LSB: Set the CPU Frequency Scaling governor to "ondemand".
[  OK  ] Stopped Permit User Sessions.
[  OK  ] Stopped target Basic System.
[  OK  ] Stopped target Paths.
[  OK  ] Stopped Forward Password Requests to Wall Directory Watch.
[  OK  ] Stopped Dispatch Password Requests to Console Directory Watch.
[  OK  ] Stopped target Slices.
[  OK  ] Stopped target System Initialization.
[  OK  ] Stopped target Encrypted Volumes.
[  OK  ] Stopped target Swap.
         Stopping Update UTMP about System Boot/Shutdown...
         Stopping Load/Save Random Seed...
         Stopping Network Time Synchronization...
[  OK  ] Stopped target Sockets.
[  OK  ] Stopped target Remote File Systems.
[  OK  ] Stopped target Remote File Systems (Pre).
[  OK  ] Stopped Load/Save Random Seed.
[  OK  ] Stopped Network Time Synchronization.
[  OK  ] Stopped Update UTMP about System Boot/Shutdown.
[  OK  ] Stopped Create Volatile Files and Directories.
[  OK  ] Stopped Raise network interfaces.
[  OK  ] Stopped Apply Kernel Variables.
[  OK  ] Stopped target Local File Systems.
[  OK  ] Stopped target Local File Systems (Pre).
[  OK  ] Stopped Create Static Device Nodes in /dev.
[  OK  ] Stopped Remount Root and Kernel File Systems.
[  OK  ] Stopped Load Kernel Modules.
[  OK  ] Reached target Shutdown.
[  385.729940] reboot: Power down
sudo brctl delif br0 tap2
sudo tunctl -d tap2
TUNSETIFF: Device or resource busy
brctl show
bridge name    bridge id        STP enabled    interfaces
br0        8000.f48e387d73d8    no        enp3s0
virbr0        8000.000000000000    yes        
  • 重啓:

pengdl@ubuntu-arm:~$ sudo reboot
[sudo] password for pengdl: 
[  OK  ] Stopped target Sound Card.
[  OK  ] Reached target Unmount All Filesystems.
[  OK  ] Stopped target Timers.
[  OK  ] Stopped Daily apt activities.
[  OK  ] Stopped target System Time Synchronized.
[  OK  ] Stopped Daily Cleanup of Temporary Directories.
[  OK  ] Stopped target Graphical Interface.
[  OK  ] Stopped target Multi-User System.
         Stopping LSB: Set the CPU Frequency Scaling governor to "ondemand"...
[  OK  ] Stopped target Login Prompts.
         Stopping Getty on tty5...
         Stopping Getty on tty3...
         Stopping Getty on tty2...
         Stopping Getty on tty6...
[  OK  ] Stopped getty on tty2-tty6 if dbus and logind are not available.
         Stopping Getty on tty4...
         Stopping Serial Getty on ttyAMA0...
         Stopping Getty on tty1...
[  OK  ] Stopped Getty on tty4.
[  OK  ] Stopped Serial Getty on ttyAMA0.
[  OK  ] Stopped Getty on tty2.
[  OK  ] Removed slice system-serial\x2dgetty.slice.
         Stopping Permit User Sessions...
[  OK  ] Removed slice system-getty.slice.
[  OK  ] Stopped /etc/rc.local Compatibility.
[  OK  ] Stopped target Network.
         Stopping Raise network interfaces...
[  OK  ] Stopped LSB: Set the CPU Frequency Scaling governor to "ondemand".
[  OK  ] Stopped Permit User Sessions.
[  OK  ] Stopped target Basic System.
[  OK  ] Stopped target Paths.
[  OK  ] Stopped Forward Password Requests to Wall Directory Watch.
[  OK  ] Stopped Dispatch Password Requests to Console Directory Watch.
[  OK  ] Stopped target Sockets.
[  OK  ] Stopped target Slices.
[  OK  ] Stopped target System Initialization.
         Stopping Network Time Synchronization...
[  OK  ] Stopped target Encrypted Volumes.
         Stopping Load/Save Random Seed...
         Stopping Update UTMP about System Boot/Shutdown...
[  OK  ] Stopped target Swap.
[  OK  ] Stopped target Remote File Systems.
[  OK  ] Stopped target Remote File Systems (Pre).
[  OK  ] Stopped Network Time Synchronization.
[  OK  ] Stopped Load/Save Random Seed.
[  OK  ] Stopped Update UTMP about System Boot/Shutdown.
[  OK  ] Stopped Create Volatile Files and Directories.
[  OK  ] Stopped Raise network interfaces.
[  OK  ] Stopped Apply Kernel Variables.
[  OK  ] Stopped Load Kernel Modules.
[  OK  ] Stopped target Local File Systems.
[  OK  ] Stopped target Local File Systems (Pre).
[  OK  ] Stopped Remount Root and Kernel File Systems.
[  OK  ] Stopped Create Static Device Nodes in /dev.
[  OK  ] Reached target Shutdown.
[   85.454918] reboot: Restarting system
[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Linux version 4.14.13+ (pengdonglin@pengdonglin-dell) (gcc version 4.8.3 20140320 (prerelease) (Sourcery CodeBench Lite 2014.05-29)) #11 SMP PREEMPT Thu Apr 5 11:23:40 CST 2018
[    0.000000] CPU: ARMv7 Processor [410fc090] revision 0 (ARMv7), cr=10c5387d
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT nonaliasing instruction cache
[    0.000000] OF: fdt: Machine model: V2P-CA9
[    0.000000] Memory policy: Data cache writealloc
[    0.000000] cma: Reserved 16 MiB at 0x9f000000

 

 未完待續……
相關文章
相關標籤/搜索