ubuntu虚拟机上定制文件系统
1. 定制文件系统前期准备
将rootfs.img文件准备好,并创建target文件夹
2. 编写chroot.sh
#!/bin/bash
ARCH=`uname -m`
if [ $# -eq 0 ]; then
echo "Usage : $0 <chrootdir>"
exit
fi
if [ $1 = "mount" ]; then
sudo mount *.img $2
if [ "$ARCH" = "x86_64" ]; then
if [ ! -e $2/usr/bin/qemu-aarch64-static ]; then
echo "please cp /usr/bin/qemu-aarch64-static $2/usr/bin/qemu-aarch64-static"
cp -a /usr/bin/qemu-aarch64-static $2/usr/bin/qemu-aarch64-static
fi
fi
if [ ! -d $2 ]; then
echo "$2 is not a directory"
exit
fi
[ -z "`ls $2/proc`" ] && mount --bind /proc $2/proc
[ -z "`mount |grep $2 |grep -v nodev |grep dev`" ] && mount --bind /dev $2/dev
[ -z "`mount |grep $2 |grep pts`" ] && mount --bind /dev/pts $2/dev/pts
[ -z "`ls $2/dev/shm`" ] && mount --bind /dev/shm $2/dev/shm
[ -z "`ls $2/sys`" ] && mount --bind /sys $2/sys
sudo chroot $2
else
umount $2/proc
umount $2/sys
umount $2/dev/pts
umount $2/dev/shm
umount $2/dev
umount $2
fi
3. chroot.sh脚本用法
sudo ./chroot.sh mount target 挂载文件系统,再进行定制化增删操作
sudo ./chroot.sh umount target 解除文件系统挂载
4. 常见问题
问题1:安装软件提示no space left on device
解决方法:
sudo fsck.ext4 -f rootfs.img 检查img镜像文件
sudo resize2fs rootfs.img 1250000 扩容img镜像文件
问题2:无法联网
解决方法:
sudo cp /etc/resolv.conf ./target/etc/ 将虚拟机resolv.conf文件拷贝到文件系统目录