Linux(26) According to the ready-made rootfs.img, make a 1:1 restore source code custom implementation

In the Linux environment, we often need to perform some operations on the img file, such as resizing, mounting and uninstalling, changing the host name, installing and uninstalling applications, etc. Although these operations can be done manually, if they can be automated, our work efficiency will be greatly improved. This article will introduce a bash script that can help us automate these operations.

1. Introduction

remake_rootfs.sh.x is a bash script for managing and operating img files of Ubuntu system. You can use it to resize img files, mount and unmount img files, change img file hostnames, install and uninstall applications, and enter and exit img file chroot environments.

2. How to use

The basic usage of the script is:

leon@ubuntu:~/Desktop$ ./remake_rootfs.sh.x
Usage: ./remake_rootfs.sh.x [options] <parameters> <img file>

options:
-r <size MB> resize the img file
-m mount img file
-u unmount img file
-h <hostname> Modify the hostname of the img file
-i <application> install application on img file
-d <application> uninstall application on img file
-c Enter the chroot environment of the img file
-x Exit the chroot environment of the img file

Example:
./remake_rootfs.sh.x -r 1024 ubuntu_rootfs.img # Increase the size of ubuntu_rootfs.img by 1024MB
./remake_rootfs.sh.x -m ubuntu_rootfs.img # Mount ubuntu_rootfs.img to the ubuntu_rootfs directory of the current directory
./remake_rootfs.sh.x -u ubuntu_rootfs.img # Uninstall the ubuntu_rootfs directory of the current directory
./remake_rootfs.sh.x -h myhostname ubuntu_rootfs.img # Change the hostname of ubuntu_rootfs.img to myhostname
./remake_rootfs.sh.x -i vim ubuntu_rootfs.img # Install vim on ubuntu_rootfs.img
./remake_rootfs.sh.x -d vim ubuntu_rootfs.img # Uninstall vim on ubuntu_rootfs.img
./remake_rootfs.sh.x -c ubuntu_rootfs.img # Enter the chroot environment of ubuntu_rootfs.img
./remake_rootfs.sh.x -x ubuntu_rootfs.img # Exit the chroot environment of ubuntu_rootfs.img

2.1 Add space to the original rootfs

Execute ./remake_rootfs.sh.x -r 1000 ido_rootfs.img to add 1000MB space to ido_rootfs.img.
Wait for about 5 minutes to complete. Check the performance of the computer. After the completion, you can see that a new file has been generated in the current directory. Compared with the previous original file, the size has increased by 1000MB.

leon@ubuntu:~/Desktop$ ./remake_rootfs.sh.x -r 1000 ido_rootfs.img
Creating new img file ido_rootfs_add_1000mb.img and increasing its size by 1000 MB...
1000 + 0 records in
1000 + 0 records out
1048576000 bytes (1.0 GB, 1000 MiB) copied, 4.78236 s, 219 MB/s
Checking filesystem for ido_rootfs_add_1000mb.img...
e2fsck 1.45.5 (07-Jan-2020)
ido_rootfs_add_1000mb.img: recovering journal
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 3A: Optimizing directories
Pass 4: Checking reference counts
Pass 5: Checking group summary information
Free blocks count wrong (54025, counted=54024).
Fix? yes


ido_rootfs_add_1000mb.img: ***** FILE SYSTEM WAS MODIFIED *****
ido_rootfs_add_1000mb.img: 167662/232464 files (0.1% non-contiguous), 874232/928256 blocks
Resizing filesystem for ido_rootfs_add_1000mb.img...
resize2fs 1.45.5 (07-Jan-2020)
Resizing the filesystem on ido_rootfs_add_1000mb.img to 1184256 (4k) blocks.
The filesystem on ido_rootfs_add_1000mb.img is now 1184256 (4k) blocks long.

The resize operation is complete and the new img file is ido_rootfs_add_1000mb.img.

image.png

2.2 Mount and unmount rootfs

Execute the following command, and you can see the new folder generated in the current directory.
./remake_rootfs.sh.x -m ido_rootfs_add_1000mb.img mount
./remake_rootfs.sh.x -m ido_rootfs_add_1000mb.img uninstall

leon@ubuntu:~/Desktop$ ./remake_rootfs.sh.x -m ido_rootfs_add_1000mb.img
Mounting ido_rootfs_add_1000mb.img to ./ido_rootfs_add_1000mb...
ido_rootfs_add_1000mb.img has been mounted to ./ido_rootfs_add_1000mb.
leon@ubuntu:~/Desktop$ ./remake_rootfs.sh.x -u ido_rootfs_add_1000mb.img
Uninstalling ido_rootfs_add_1000mb.img from ./ido_rootfs_add_1000mb...
ido_rootfs_add_1000mb.img has been uninstalled from ./ido_rootfs_add_1000mb.

image. png

2.3 Modify hostname name

Execute ./remake_rootfs.sh.x -h btf ido_rootfs_add_1000mb.img to modify the hostname name. If you don’t modify it, it’s okay, you can also enter chroot to modify it yourself.

leon@ubuntu:~/Desktop$ ./remake_rootfs.sh.x -h btf ido_rootfs_add_1000mb.img changing hostname to btf...
ido_rootfs_add_1000mb.img has been mounted to ./ido_rootfs_add_1000mb.
btf
Uninstalling ido_rootfs_add_1000mb.img from ./ido_rootfs_add_1000mb...
ido_rootfs_add_1000mb.img has been uninstalled from ./ido_rootfs_add_1000mb.
The hostname has been changed to btf.
leon@ubuntu:~/Desktop$ ./remake_rootfs.sh.x -c ido_rootfs_add_1000mb.img
root@btf:/#

#----If the modification is not successful, confirm whether it is btf
root@ubuntu:/# cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 btf

2.4 Installation and uninstallation of apt

First execute ./remake_rootfs.sh.x -d unrar ido_rootfs_add_1000mb.img to verify, uninstall unrar, you can see that the system does not have unrar . Let’s execute ./remake_rootfs.sh.x -i unrar ido_rootfs_add_1000mb.img to see that unrar has been installed successfully.

leon@ubuntu:~/Desktop$ ./remake_rootfs.sh.x -d unrar ido_rootfs_add_1000mb.img
Uninstalling unrar from ido_rootfs_add_1000mb.img...
Mounting ido_rootfs_add_1000mb.img to ./ido_rootfs_add_1000mb...
ido_rootfs_add_1000mb.img has been mounted to ./ido_rootfs_add_1000mb.
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package 'unrar' is not installed, so not removed
0 upgraded, 0 newly installed, 0 to remove and 153 not upgraded.
Uninstalling ido_rootfs_add_1000mb.img from ./ido_rootfs_add_1000mb...
ido_rootfs_add_1000mb.img has been uninstalled from ./ido_rootfs_add_1000mb.
unrar has been uninstalled from ido_rootfs_add_1000mb.img.
leon@ubuntu:~/Desktop$ ./remake_rootfs.sh.x -i unrar ido_rootfs_add_1000mb.img
Installing unrar on ido_rootfs_add_1000mb.img...
Mounting ido_rootfs_add_1000mb.img to ./ido_rootfs_add_1000mb...
ido_rootfs_add_1000mb.img has been mounted to ./ido_rootfs_add_1000mb.
Hit: 1 http://mirrors.aliyun.com/ubuntu-ports focal InRelease
Get:2 http://mirrors.aliyun.com/ubuntu-ports focal-updates InRelease [114 kB]
................................
Selecting previously unselected package unrar.
(Reading database ... 153415 files and directories currently installed.)
Preparing to unpack .../unrar_1:5.6.6-2build1_arm64.deb ...
Unpacking unrar (1:5.6.6-2build1) ...
Setting up unrar (1:5.6.6-2build1) ...
update-alternatives: using /usr/bin/unrar-nonfree to provide /usr/bin/unrar (unrar) in auto mode
update-alternatives: warning: skip creation of /usr/share/man/man1/unrar.1.gz because associated file /usr/share/man/man1/unrar-nonfree.1.gz (of link group unrar) doesn\ 't exist
Processing triggers for man-db (2.9.1-1) ...
Uninstalling ido_rootfs_add_1000mb.img from ./ido_rootfs_add_1000mb...
ido_rootfs_add_1000mb.img has been uninstalled from ./ido_rootfs_add_1000mb.
unrar has been installed on ido_rootfs_add_1000mb.img.

2.5 Enter and uninstall chroot mode

implement
./remake_rootfs.sh.x -c ido_rootfs_add_1000mb.img enter chroot mode
./remake_rootfs.sh.x -x ido_rootfs_add_1000mb.img exit chroot mode
After entering, it is the same as the normal operation system and can be operated arbitrarily.

leon@ubuntu:~/Desktop$ ./remake_rootfs.sh.x -c ido_rootfs_add_1000mb.img
root@btf:/# #At this time, it has entered the space of the image file
root@btf:/# exit
leon@ubuntu:~/Desktop$ ./remake_rootfs.sh.x -x ido_rootfs_add_1000mb.img

2.6 Copy files to system directory

First of all, you should make sure that the ./remake_rootfs.sh.x -m ido_rootfs_add_1000mb.img folder on the desktop has content, and then you can copy the file to this directory, or you can create a new file and copy it to see The following operation.

leon@ubuntu:~/Desktop$ sudo cp ./test.rar ./ido_rootfs_add_1000mb/opt/
leon@ubuntu:~/Desktop$ cd ./ido_rootfs_add_1000mb/
leon@ubuntu:~/Desktop/ido_rootfs_add_1000mb$ mkdir test_dir
mkdir: cannot create directory 'test_dir': Permission denied
leon@ubuntu:~/Desktop/ido_rootfs_add_1000mb$ sudo mkdir test_dir
leon@ubuntu:~/Desktop/ido_rootfs_add_1000mb$ cd ..
leon@ubuntu:~/Desktop$ cp ./test.rar ./ido_rootfs_add_1000mb/test_dir/
cp: cannot create regular file './ido_rootfs_add_1000mb/test_dir/test.rar': Permission denied
leon@ubuntu:~/Desktop$ sudo cp ./test.rar ./ido_rootfs_add_1000mb/test_dir/

2.7 Confirm the remaining space of rootfs

First, you confirm that ./remake_rootfs.sh.x -m ido_rootfs_add_1000mb.img has content in the folder on the desktop, and then execute df -H to see the current PC system A partition is mounted and the remaining size is 569M.

Because of the 2.6 operation, I copied test.rar twice (total 412M) and the 2.4 operation installed unrar, and the space calculation is basically consistent.

df -H

image.png

2.8 Burning Verification

After the production is completed, directly burn the tool rootfs to specify the ido_rootfs_add_1000mb.img file.
image.png
The following is part of the content we just changed, including hostname, unrar installation, pre-installed test.rar to /opt, pre-installed test.rar to custom directory /test_dir/test.rar.

image. png

3. Precautions

  • Before using this script, make sure your Ubuntu system has /bin/bash and chroot command.
  • Before using this script to manipulate the img file, please make sure the img file exists and is not empty.
  • When resizing img files, make sure you enter a number for the size.
  • When modifying the host name, installing and uninstalling the application, the script will automatically mount the img file, and then uninstall after performing the operation. If you encounter problems during the operation, you may need to manually uninstall the img file.
  • The script automatically mounts and unmounts img files when entering and exiting the chroot environment. If you encounter problems during the operation, you may need to manually uninstall the img file.
  • If ./remake_rootfs.sh.x -x ido_rootfs_add_1000mb.img cannot exit chroot mode, you need to use -u to uninstall.
leon@ubuntu:~/Desktop$ ./remake_rootfs.sh.x -x ido_rootfs_add_1000mb.img
umount: /home/leon/Desktop/ido_rootfs_add_1000mb: target is busy.
leon@ubuntu:~/Desktop$ ./remake_rootfs.sh.x -x ido_rootfs_add_1000mb.img
umount: /home/leon/Desktop/ido_rootfs_add_1000mb: target is busy.
leon@ubuntu:~/Desktop$ ./remake_rootfs.sh.x -u ido_rootfs_add_1000mb
ERROR: File ido_rootfs_add_1000mb does not exist.
leon@ubuntu:~/Desktop$ ./remake_rootfs.sh.x -u ido_rootfs_add_1000mb.img
Uninstalling ido_rootfs_add_1000mb.img from ./ido_rootfs_add_1000mb...
ido_rootfs_add_1000mb.img has been uninstalled from ./ido_rootfs_add_1000mb.
leon@ubuntu:~/Desktop$ ./remake_rootfs.sh.x -x ido_rootfs_add_1000mb.img
umount: ./ido_rootfs_add_1000mb: not mounted.

image.png

4. Summary

The remake_rootfs.sh.x script provides a simple and effective way to manage and manipulate the img files of the Ubuntu system. By using this script, we can automate some common operations to improve our work efficiency. Although this script already provides a lot of functionality, depending on your specific needs, you may need to make some modifications or extensions to it. Hope this article can help you better understand and use this script.