制作ubuntu rootfs

制作ubuntu rootfs

下载base镜像:
http://cdimage.ubuntu.com/ubuntu-base/releases/16.04.1/release/
解压:
tar -zxf ubuntu-xx-base-amd64.tar.gz -C ./ubuntu-rootfs/

apt-get install qemu-user-static
cp /usr/bin/qemu-amd64-static usr/bin
cp -b /etc/resolv.conf etc/

#!/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 /dev/pts ${2}dev/pts		
    sudo chroot ${2}
}
function umnt() {
    echo "UNMOUNTING"
    sudo umount ${2}proc
    sudo umount ${2}sys
    sudo umount ${2}dev/pts
    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

sudo bash ./ch_mount.sh -m ./ubuntu-rootfs
挂载到根文件系统中去

先设置下用户名密码:

useradd -s '/bin/bash' -m -G adm,sudo yourusername
echo "Set password for yourusername:"
passwd yourusername
echo "Set password for root:"
passwd root

设置主机名:

echo 'ubuntu.yourusername' > /etc/hostname

安装各种常用的包

apt-get install \
  language-pack-en-base \
  sudo \
  ssh \
  net-tools \
  network-manager \
  iputils-ping \
  rsyslog \
  bash-completion 
  
apt install gcc gdb tmux wget curl vim kmod pciutils ssh resolvconf -y

设置自动更新dns

dpkg-reconfigure resolvconf

打包生成镜像

#!/bin/bash
image_name=$1
set -x
rm -rf tmpfs ${image_name}.img ${image_name}.img.tar.gz
dd if=/dev/zero of=${image_name}.img bs=1G count=4
mkfs.ext4 ${image_name}.img
mkdir -p tmpfs
mount ${image_name}.img tmpfs
cp -rfp ./ubuntu-amd64/* ./tmpfs/
umount tmpfs
e2fsck -p -f ${image_name}.img
resize2fs -M ${image_name}.img
tar zcf ${image_name}.img.tar.gz ${image_name}.img

你可能感兴趣的:(制作ubuntu rootfs)