img file is a common disk image file format, which can contain one or more file systems. img files can be used to create, back up, or restore operating systems or other data. In Ubuntu, you can use some simple commands to manipulate img files, such as mount, unmount, modify, extend, etc. This article describes how to do these things in Ubuntu and gives some examples.
Why do these things, because I plan to unpack an existing Ubuntu rootfs.img file, and want to add, delete, or modify some files or content on this basis by hanging on the basis.
This article mainly records how to unpack and pack some pitfalls of img (persuade people who think the same). It seems that only the software DiskInternals Linux Reader can mount img on Windows, but it cannot be modified. Pro is required.
Mounting and unmounting img files
To mount an img file is to attach it to the system as a virtual disk device, so that you can access the files in the file system it contains. Unmounting an img file disconnects it from the system, thus freeing the resources it occupies.
First, you need to create a mount point, and then use the mount
command to mount the img file to this mount point. Files can then be added, modified, or deleted from the mounted filesystem. Finally, you can use the umount
command to unmount the img file.
The following are the specific steps:
- Create a mount point. For example, a directory named
mnt
can be created:
mkdir mnt
- Use the
mount
command to mount the img file to this directory:
sudo mount -o loop ubuntu-rootfs.img mnt #This file can be understood as being made by others, and I want to make it ready-made.
- We can now add, modify or delete files in the
mnt
directory. For example, if you want to add a file calledmyfile.txt
, you can use the following command:
echo "This is a new file" > mnt/myfile.txt #Just for example, I wanted to preset some files, install apt, and delete some content.
- When all modifications are done, you need to unmount the img file using the
umount
command:
sudo umount mnt
- Finally, you can delete the mountpoint directory:
rmdir mnt
After the above steps are completed, the ubuntu-rootfs.img
file already contains the new files you added.
Note: When performing these operations, corresponding permissions are required. If you do not have permission, you may need to use the sudo
command.
This is the final conclusion: the file is pre-installed, the problem is that the rootfs.img of tm’s rockchip linux flashes the partition problem, but it cannot be flashed.
View and expand the size of the img file
Well, I was still presetting the file at that time, and found that the preset file was about 500MB, but I found that the preset did not go in. How i want to view or modify the size of img file to add more data in it. Some tools can be used to achieve this purpose, such as df
, dd
and resize2fs
, etc.
View the size of the img file
You can use the df
command to view the disk usage of the file system. After mounting the img file, you can run the following command:
df /mnt
This output looks like:
vmhgfs-fuse 1953512444 1729111644 224400800 89% /mnt
This output tells that the /mnt
directory (that is, the img file) has used 89% of its space and has 11% left. The numbers in this output are in 1K blocks. So, you have about 224400800K left, which is about 214GB? ? ? I don’t know how Linux calculates it, but it should actually be around 220MB.
If you want to see the specific value of the remaining space, you can use the df -h
command, the -h
option will display the disk in a readable format (eg, KB, MB, GB) Usage.
Note: When performing these operations, corresponding permissions are required. If you do not have permission, you may need to use the sudo
command.
This is the final conclusion: this is the reason, because the size of the img file is about 3.8G, and we mounted it through mount. In fact, about 85% of it has been used, and there are about 200MB left. , which is why I can't preset a large 500MB file after unpacking.
Extend the size of the img file
Moving on, if you want to add more files to this file system, you need to make sure there is enough space. If you run out of space, you may need to increase the size of the img file. This can be done with the dd
command and the resize2fs
command.
Here is an example, which increases the size of the img file by 1GB:
- Use the
dd
command to increase the size of the img file:
dd if=/dev/zero bs=1M count=1024 >> ubuntu-rootfs.img
- Use the
e2fsck
command to check the file system:
e2fsck -f ubuntu-rootfs.img
- Use the
resize2fs
command to resize the file system:
resize2fs ubuntu-rootfs.img
After the above command completes, the size of the img file should increase by 1GB. Then, you can try to mount the img file again and add the new one.
Note: When performing these operations, you need to have corresponding permissions. If you don’t have permission, you may need to use the sudo
command.
This is the final conclusion: the size was increased and generated. I realized at that time that the size of the img file was not increased. In fact, our emmc has 64G. This method is completely unsupported and cannot be passed. Increase the img size to increase the file, and the final conclusion is that it cannot be flashed.
Add and set up new users
In the end, I compromised, and then I still planned to make a new Ubuntu rootfs, and re-downloaded the source code. Then here are some simple records
In Ubuntu, you can use some commands to add and set new users, such as adduser
, usermod
, etc. Adding new users allows us to assign different permissions and resources to different purposes or people. Setting up a new user allows you to customize the user’s login method, password, information, etc.
Add new user
In Ubuntu, you can use the adduser
command to add new users. Here is the command to add a new user r58
:
sudo adduser r58
This command will prompt you to enter the new user’s password, and some optional user information, such as full name, room number, phone, etc. (don’t worry about keeping pressing enter).
Then, you can use the usermod
command to add the new user to the sudo
group, so that the new user can use the sudo
command to execute commands that require administrator privileges The operation:
sudo usermod -aG sudo r58
Set the new user as the default login user
As for setting r58
as the default login user, it depends on which login manager you are using. If you are using lightdm
, you can edit the /etc/lightdm/lightdm.conf
file and set autologin-user
to r58
:
[Seat:*] autologin-user=r58
If you are using gdm
, you can edit the /etc/gdm3/custom.conf
file and set AutomaticLogin
to r58
:
[daemon] AutomaticLogin=r58
Note that editing these configuration files requires administrator privileges, possibly using the sudo
command.
Modify hostname
A hostname is a name used to identify and distinguish different computers or devices. In Ubuntu, you can use some commands to modify the hostname, such as hostnamectl
, vi
, etc.
Modify hostname
What you want to modify is the hostname of the system (hostname). You can use hostnamectl
command to modify the hostname. For example, if you want to change the hostname to xxx
, you can run the following command:
hostnamectl set-hostname xxx
Then, you need to edit /etc/hosts
file, changing all references to the old hostname to the new hostname. You can use vi
Wait for a text editor to edit this file:
vi /etc/hosts
In this file, you may see a line similar to this:
127.0.1.1 localhost
You need to put localhost
in this line
Modify to xxx
, then save and close the file.
A new line has been added to the /etc/hosts
file referencing the new hostname btf
. Now, when the system tries to resolve the hostname xxx
, it will be resolved to the IP address 127.0.1.1
, which is a special IP address representing this machine.
Next, we unmount the environment through the script, and then mount it again:
After the system restarts, you can run the hostname
command again to check if the hostname has changed:
hostname
If all is well, this command should output xxx
.
Summary
This article introduces how to operate img files in Ubuntu, including mounting, uninstalling, modifying, extending, etc. This article also explains how to add and set up new users, and how to modify hostnames. These operations allow you to better manage and use your Ubuntu system. Hope this article helps you.
If you have any questions or suggestions, please leave a message in the comment area. Thank you for reading and supporting.