ubuntu core镜像制作

本文详细介绍从下载UbuntuCore开始,经过解压、配置、安装组件、格式化、挂载到最终减少镜像大小的全过程。涉及qemu模拟器、软件源修改、串口调试服务配置、以太网配置、主机名修改、模拟器启动、常用工具安装、dd工具使用、ext4文件系统格式化、镜像挂载与检查等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1. 下载ubuntu core

2. 解压ubuntu core

3. PC ubuntu上安装qemu模拟器

4. 拷贝qemu模拟器工具到ubuntu core的解压目录中

5. 拷贝PC ubuntu resolv.conf 到ubuntu core

6. 修改目标ubuntu的下载软件源

7. 配置串口调试服务

8. 以太网配置

9. 修过主机名

10. 启动模拟器,安装组件

10.1 ch-mount.sh脚本拷贝到前面的创建ubuntu的上级目录

10.2 修过密码

10.3 安装常用工具

10.4 退出chroot

10.5 用脚本载缷载退出根文件系统

11. 用dd工具创建镜像文件

12. 格式化镜像文件,并加入linuxroot卷标

13. 创建挂载目录

14. 缷载镜像

15. 检查镜像

16. 减少镜像大小

17. 附

18. 待解决问题:

18.1 镜像挂载时报错

19. 验证



1. 下载ubuntu core

 http://cdimage.ubuntu.com/ubuntu-base/releases/ubuntu-base-16.04-core-armhf.tar.gz
备注:根据自己的需求下载不同的ubuntu core版本,笔者使用的是16.04

2. 解压ubuntu core

mkdir ubuntu-base-16.04-core-armhf
tar zxvf ubuntu-base-16.04-core-armhf.tar.gz -c ./ubuntu-base-16.04-core-armhf

3. PC ubuntu上安装qemu模拟器

sudo apt-get install qemu-user-static

4. 拷贝qemu模拟器工具到ubuntu core的解压目录中

sudo cp /usr/bin/qemu-arm-static ./ubuntu-base-16.04-core-armhf/usr/bin/

5. 拷贝PC ubuntu resolv.conf 到ubuntu core

sudo cp -b /etc/resolv.conf  ./ubuntu-base-16.04-core-armhf/etc/resolv.conf

6. 修改目标ubuntu的下载软件源

sudo vi ./ubuntu-base-16.04-core-armhf/etc/apt/sources.list
增加下面两行
deb http://ports.ubuntu.com/ubuntu-ports/ xenial main universe
deb-src http://ports.ubuntu.com/ubuntu-ports/ xenial main universe

7. 配置串口调试服务

sudo cp etc/init/tty1.conf  ./ubuntu-base-16.04-core-armhf/etc/init/ttyS2.conf
sudo vi ./ubuntu-base-16.04-core-armhf/etc/init/ttyS2.conf
修过 sudo vi etc/rc.local  加上start ttyS2

8. 以太网配置

sudo vi etc/network/interfaces
添加:
auto eth0                                  
iface eth0 inet dhcp hwaddress ether 7e:8d:a5:3a:ce:9e

9. 修过主机名

vi etc/hostname
已经对ubuntu根文件系统做了简单的配置,现在可以用模拟器切换到此根文件系统了。

10. 启动模拟器,安装组件

10.1 ch-mount.sh脚本拷贝到前面的创建ubuntu的上级目录

chmod +x ch-mount.sh./ch-mount.sh -m ubuntu-base-16.04-core-armhf/
-m 创建home目录 (不加这个要手动添加目录,不然开机时,会出现No directory,Logging in with HOME=/ )
useradd -s '/bin/bash' -m -G adm,sudo daluotuo
#!/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 mount -o bind /run ${2}run 
    sudo mount --bind / ${2}host
    #sudo mount -vt tmpfs shm ${2}dev/shm
    #sudo mount -t /dev/shm ${2}dev/shm
    sudo chroot ${2}
}

function umnt() {
    echo "UNMOUNTING"
    sudo umount ${2}proc
    sudo umount ${2}sys
    #sudo umount ${2}dev/shm
    sudo umount ${2}dev
    sudo umount ${2}run
    sudo umount ${2}host
}


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

10.2 修过密码

passwd daluotuo

10.3 安装常用工具

apt-get install vim git openssh-server
apt-get install sudo                    sudo命令
apt-get install net-tools               ifconfig,netstat,route,arp等
apt-get install iputils-ping            ping和ping6       
apt-get install wireless-tools          iwconfig等,显示、修改无线设置
pt-get install htop                     htop工具,交互式进程查看器
apt-get install rsyslog                 系统log服务
apt-get install network-manager         Network Manager服务和框架,高级网络管理50M
apt-get install ifupdown                ifup,ifdown等工具
apt-get install ethtool                 ethtool命令,显示、修改以太网设置  

可以根据需要,安装各种组件。

10.4 退出chroot


  用exit 命令,退出chroot

10.5 用脚本载缷载退出根文件系统

./ch-mount.sh -u ubuntu-base-16.04-core-armhf/

11. 用dd工具创建镜像文件

root@ubuntu-virtual-machine:/share/eciot/ubuntu-core# dd if=/dev/zero of=ubuntu-base-16.04-core-armhf.img bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 11.1279 s, 94.2 MB/s

备注:
	dd if=xxx表示输入的文件,这里选择/dev/zero
	dd of=xxx表示输出设备文件	
	bs = 1M表示每块为1M

12. 格式化镜像文件,并加入linuxroot卷标

root@ubuntu-virtual-machine:/share/eciot/ubuntu-core# mkfs.ext4 -F -L linuxroot ubuntu-base-16.04-core-armhf.img
mke2fs 1.42.9 (4-Feb-2014)
Filesystem label=linuxroot
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
64000 inodes, 256000 blocks
12800 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=264241152
8 block groups
32768 blocks per group, 32768 fragments per group
8000 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

备注:这里使用了ext4文件系统,所以在嵌入式ARM平台上需要linux内核支持ext4文件系统

13. 创建挂载目录

mkdir ubuntu-core-img
sudo mount  ubuntu-base-16.04-core-armhf.img ubuntu-mount
sudo cp -rfp ubuntu-base-16.04-core-armhf/* ubuntu-mount

14. 缷载镜像

sudo umount ubuntu-mount

15. 检查镜像

sudo e2fsck -p -f ubuntu-base-16.04-core-armhf.img

16. 减少镜像大小

sudo resize2fs -M ubuntu-base-16.04-core-armhf.img
du -sh ubuntu-base-16.04-core-armhf.img


17. 附

ext4根文件系统制作:https://www.cnblogs.com/dakewei/p/10150984.html

18. 待解决问题:

18.1 镜像挂载时报错

root@ubuntu:/home/ubuntu-core# mount ubuntu-base-16.04-core-armhf.img /mnt/ubuntu-core-img
mount: /dev/loop0: can't read superblock

19. 验证

1. dd if=/dev/zero of=rootfs.ext4 bs=1024 count=2048	//执行resize2fs命令压缩之后,导致根文件系统在嵌入式平台上挂载报错,不确定是否与这里分配的块大小有关?
2. mkfs.ext4 -F -L linuxroot rootfs.ext4
3. mount rootfs.ext4 /mnt
4. echo "123">/mnt/1.txt //生成文件到根文件系统
5. umount /usb
6. rootfs.ext4下载到终端,然后按照步骤3可以直接挂载,如果执行了resize2fs命令压缩根文件系统,将导致挂载失败,怀疑是与第1步骤分配的块大小有关

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值