6 echo "Usage: $(basename $0) [options]"
7 echo -e "Creates a Ubuntu root file system image.\n"
8 echo -e " --help\t\t\tDisplay this information."
9 echo -e " --arch {armhf|arm64}\t\tSelects architecture of rootfs image."
10 echo -e " --distro {bionic|focal}\tSelects Ubuntu distribution of rootfs image."
11 echo -e " --size n{K|M|G}\t\tSets size of rootfs image to n Kilo, Mega or Giga bytes."
16 echo "ERROR: Unrecognized argument: $1" >&2
20 update_repositories
() {
21 echo -e "\nUpdating apt repositories. "
22 echo -e "\nPress 'y' to continue or any other key to exit..."
23 read -s -n 1 user_input
24 if [[ $user_input == 'Y' ]] ||
[[ $user_input == 'y' ]]; then
32 while [[ $# -gt 0 ]]; do
33 case "${END_OF_OPT}${1}" in
34 --help) print_usage
0 ;;
35 --arch) rfs_arch
=$2; shift;;
36 --distro) rfs_distro
=$2; shift;;
37 --size) rfs_size
=$2; shift;;
38 *) invalid_arg
"$1" ;;
43 if [ -z "$rfs_arch" ]; then
44 echo "Missing architecture"
47 if [ -z "$rfs_distro" ]; then
48 echo "Missing distribution"
51 if [ -z "$rfs_size" ]; then
56 if [[ "$rfs_arch" != "arm64" && "$rfs_arch" != "armhf" ]]; then
57 echo "Invalid architecture: $rfs_arch"
62 if [[ ! $rfs_size =~
$pat ]]; then
63 echo "Invalid size: $rfs_size"
69 echo "Installing build dependencies ..."
70 sudo apt-get
install debootstrap qemu-user-static schroot qemu-utils
72 image_name
=$rfs_distro-$rfs_arch-"rootfs"
73 echo "Creating $rfs_distro ($rfs_arch) root file system ..."
74 echo "Image name: $image_name.img"
75 echo "Image size: $rfs_size"
77 qemu-img create
$image_name.img
$rfs_size
79 mkfs.ext4
$image_name.img
81 sudo mount
-o loop
$image_name.img
$image_name.dir
83 sudo qemu-debootstrap
--arch $rfs_arch $rfs_distro $image_name.dir
85 sudo chroot
$image_name.dir locale-gen en_US.UTF-8
87 sudo chroot
$image_name.dir
sed -i \
88 's/main/main restricted multiverse universe/g' /etc
/apt
/sources.list
90 sudo chroot
$image_name.dir
sed -i '$ a\nameserver 8.8.8.8' /etc
/resolv.conf
92 sudo chroot
$image_name.dir apt update
93 sudo chroot
$image_name.dir apt
-y install ssh bash-completion
94 sudo chroot
$image_name.dir adduser
--gecos "" $USER
95 sudo chroot
$image_name.dir adduser
$USER sudo
96 sudo umount
$image_name.dir