在Linux环境下,我们经常需要对img文件进行一些操作,如调整大小、挂载和卸载、更改主机名、安装和卸载应用等。这些操作虽然可以手动完成,但如果能够自动化,将大大提高我们的工作效率。本文将介绍一个bash脚本,它可以帮助我们自动化这些操作。
1. 简介
remake_rootfs.sh.x
是一个bash脚本,用于管理和操作Ubuntu系统的img文件。你可以使用它来调整img文件的大小,挂载和卸载img文件,更改img文件的主机名,安装和卸载应用,以及进入和退出img文件的chroot环境。
2. 使用方法
脚本的基本用法是:
leon@ubuntu:~/Desktop$ ./remake_rootfs.sh.x 用法: ./remake_rootfs.sh.x [选项] <参数> <img 文件> 选项: -r <大小 MB> 调整img文件的大小 -m 挂载img文件 -u 卸载img文件 -h <主机名> 修改img文件的主机名 -i <应用> 在img文件上安装应用 -d <应用> 在img文件上卸载应用 -c 进入img文件的chroot环境 -x 退出img文件的chroot环境 示例: ./remake_rootfs.sh.x -r 1024 ubuntu_rootfs.img # 将ubuntu_rootfs.img的大小增加1024MB ./remake_rootfs.sh.x -m ubuntu_rootfs.img # 将ubuntu_rootfs.img挂载到当前目录的ubuntu_rootfs目录 ./remake_rootfs.sh.x -u ubuntu_rootfs.img # 将当前目录的ubuntu_rootfs目录卸载 ./remake_rootfs.sh.x -h myhostname ubuntu_rootfs.img # 将ubuntu_rootfs.img的主机名改为myhostname ./remake_rootfs.sh.x -i vim ubuntu_rootfs.img # 在ubuntu_rootfs.img上安装vim ./remake_rootfs.sh.x -d vim ubuntu_rootfs.img # 在ubuntu_rootfs.img上卸载vim ./remake_rootfs.sh.x -c ubuntu_rootfs.img # 进入ubuntu_rootfs.img的chroot环境 ./remake_rootfs.sh.x -x ubuntu_rootfs.img # 退出ubuntu_rootfs.img的chroot环境
2.1 给原始rootfs增加空间
执行./remake_rootfs.sh.x -r 1000 ido_rootfs.img
给ido_rootfs.img 增加1000MB的空间。
大概等5分钟左右完成 看电脑性能 , 完成后可以看到当前目录新生成了一个文件 , 对比得出比之前原始文件增加了1000MB大小。
leon@ubuntu:~/Desktop$ ./remake_rootfs.sh.x -r 1000 ido_rootfs.img 正在创建新的img文件 ido_rootfs_add_1000mb.img,并将其大小增加 1000 MB... 1000+0 records in 1000+0 records out 1048576000 bytes (1.0 GB, 1000 MiB) copied, 4.78236 s, 219 MB/s 正在检查 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 正在调整 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. 调整大小操作完成,新的img文件是 ido_rootfs_add_1000mb.img.
2.2 挂载和卸载rootfs
执行以下指令 , 并且能在当前目录看到新的文件夹生成。
./remake_rootfs.sh.x -m ido_rootfs_add_1000mb.img
挂载
./remake_rootfs.sh.x -m ido_rootfs_add_1000mb.img
卸载
leon@ubuntu:~/Desktop$ ./remake_rootfs.sh.x -m ido_rootfs_add_1000mb.img 正在将 ido_rootfs_add_1000mb.img 挂载到 ./ido_rootfs_add_1000mb... ido_rootfs_add_1000mb.img 已经挂载到 ./ido_rootfs_add_1000mb. leon@ubuntu:~/Desktop$ ./remake_rootfs.sh.x -u ido_rootfs_add_1000mb.img 正在从 ./ido_rootfs_add_1000mb 卸载 ido_rootfs_add_1000mb.img... ido_rootfs_add_1000mb.img 已经从 ./ido_rootfs_add_1000mb 卸载.
2.3 修改hostname名字
执行./remake_rootfs.sh.x -h btf ido_rootfs_add_1000mb.img
修改hostname名字 , 如果不修改也没事 , 自己进chroot修改也可以 。
leon@ubuntu:~/Desktop$ ./remake_rootfs.sh.x -h btf ido_rootfs_add_1000mb.img 正在将主机名改为 btf... ido_rootfs_add_1000mb.img 已经挂载到 ./ido_rootfs_add_1000mb. btf 正在从 ./ido_rootfs_add_1000mb 卸载 ido_rootfs_add_1000mb.img... ido_rootfs_add_1000mb.img 已经从 ./ido_rootfs_add_1000mb 卸载. 主机名已经改为 btf. leon@ubuntu:~/Desktop$ ./remake_rootfs.sh.x -c ido_rootfs_add_1000mb.img root@btf:/# #----如果没修改成功 自己确认是不是btf root@ubuntu:/# cat /etc/hosts 127.0.0.1 localhost 127.0.1.1 btf
2.4 apt的安装和卸载
先执行./remake_rootfs.sh.x -d unrar ido_rootfs_add_1000mb.img
验证 , 卸载unrar
可以看到 , 系统本来是没有unrar
的。我们再执行./remake_rootfs.sh.x -i unrar ido_rootfs_add_1000mb.img
可以看到unrar
已经被安装成功。
leon@ubuntu:~/Desktop$ ./remake_rootfs.sh.x -d unrar ido_rootfs_add_1000mb.img 正在从 ido_rootfs_add_1000mb.img 卸载 unrar... 正在将 ido_rootfs_add_1000mb.img 挂载到 ./ido_rootfs_add_1000mb... ido_rootfs_add_1000mb.img 已经挂载到 ./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. 正在从 ./ido_rootfs_add_1000mb 卸载 ido_rootfs_add_1000mb.img... ido_rootfs_add_1000mb.img 已经从 ./ido_rootfs_add_1000mb 卸载. unrar 已经从 ido_rootfs_add_1000mb.img 卸载. leon@ubuntu:~/Desktop$ ./remake_rootfs.sh.x -i unrar ido_rootfs_add_1000mb.img 正在在 ido_rootfs_add_1000mb.img 上安装 unrar... 正在将 ido_rootfs_add_1000mb.img 挂载到 ./ido_rootfs_add_1000mb... ido_rootfs_add_1000mb.img 已经挂载到 ./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%3a5.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) ... 正在从 ./ido_rootfs_add_1000mb 卸载 ido_rootfs_add_1000mb.img... ido_rootfs_add_1000mb.img 已经从 ./ido_rootfs_add_1000mb 卸载. unrar 已经在 ido_rootfs_add_1000mb.img 上安装.
2.5 进入和卸载chroot模式
执行
./remake_rootfs.sh.x -c ido_rootfs_add_1000mb.img
进入chroot模式
./remake_rootfs.sh.x -x ido_rootfs_add_1000mb.img
退出chroot模式
进去之后就和正常运行系统是一样的 可以任意操作。
leon@ubuntu:~/Desktop$ ./remake_rootfs.sh.x -c ido_rootfs_add_1000mb.img root@btf:/# #此时已经进入镜像文件的空间 root@btf:/# exit leon@ubuntu:~/Desktop$ ./remake_rootfs.sh.x -x ido_rootfs_add_1000mb.img
2.6 拷贝文件到系统目录
首先你的确认./remake_rootfs.sh.x -m ido_rootfs_add_1000mb.img
桌面上的文件夹有内容 , 然后你可以拷贝文件到该目录下 , 或者你进去新建文件再拷贝 具体看下面的操作。
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 确认rootfs剩余空间
首先你的确认./remake_rootfs.sh.x -m ido_rootfs_add_1000mb.img
桌面上的文件夹有内容 , 然后执行df -H
可以看到当前PC 系统上挂载了一个分区剩余大小是569M 。
因为2.6操作我拷贝了2次test.rar (总共412M) 还有2.4操作安装了unrar 空间计算一起基本符合。
df -H
2.8 烧录验证
制作完之后直接烧录工具rootfs指定ido_rootfs_add_1000mb.img
文件。
下面是我们刚刚改的部分内容, 包括hostname , 安装unrar , 预置test.rar到/opt , 预置了test.rar到自定义目录/test_dir/test.rar。
3. 注意事项
- 在使用此脚本之前,请确保你的Ubuntu系统具有/bin/bash和chroot命令。
- 在使用此脚本操作img文件之前,请确保img文件存在并且不为空。
- 在调整img文件的大小时,请确保你输入的大小是一个数字。
- 在修改主机名,安装和卸载应用时,脚本会自动挂载img文件,执行操作后再卸载。如果在操作过程中遇到问题,你可能需要手动卸载img文件。
- 在进入和退出chroot环境时,脚本会自动挂载和卸载img文件。如果在操作过程中遇到问题,你可能需要手动卸载img文件。
- 如果
./remake_rootfs.sh.x -x ido_rootfs_add_1000mb.img
退不出chroot模式 需要用-u 卸载。
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 错误: 文件 ido_rootfs_add_1000mb 不存在. leon@ubuntu:~/Desktop$ ./remake_rootfs.sh.x -u ido_rootfs_add_1000mb.img 正在从 ./ido_rootfs_add_1000mb 卸载 ido_rootfs_add_1000mb.img... ido_rootfs_add_1000mb.img 已经从 ./ido_rootfs_add_1000mb 卸载. leon@ubuntu:~/Desktop$ ./remake_rootfs.sh.x -x ido_rootfs_add_1000mb.img umount: ./ido_rootfs_add_1000mb: not mounted.
4. 总结
remake_rootfs.sh.x
脚本提供了一种简单有效的方式来管理和操作Ubuntu系统的img文件。通过使用这个脚本,我们可以自动化一些常见的操作,从而提高我们的工作效率。虽然这个脚本已经提供了很多功能,但根据你的具体需求,你可能需要对它进行一些修改或扩展。希望这篇文章能帮助你更好地理解和使用这个脚本。