2025年7月27日 星期日

如何 mount Armbian 的 .img 檔

 Gemini 的回答


Mounting an Armbian .img file on Linux using a loop device is a very common task, typically done to inspect, modify, or extract files from the image before flashing it to an SD card.

Armbian images (like most single-board computer OS images) are usually disk images containing multiple partitions (e.g., a small boot partition and a larger root filesystem partition). Therefore, you need to expose these individual partitions from the .img file and mount them separately.

Here's a step-by-step guide on how to do it:


Prerequisites:

  1. Armbian .img file: Make sure you have the .img file. If it's compressed (e.g., .img.xz, .img.gz), you need to decompress it first.

    • Decompress .xz: unxz your_armbian_image.img.xz

    • Decompress .gz: gunzip your_armbian_image.img.gz

  2. Required Tools: Most Linux distributions have these pre-installed or readily available.

    • losetup: For creating and managing loop devices.1

    • mount: For mounting filesystems.

    • fdisk (or parted): For inspecting partition tables.2

    • sudo: For elevated privileges.3


Step-by-Step Guide:

1. Create Mount Points

You'll need separate directories on your host system where you want to mount the partitions from the image. A typical Armbian image has at least two partitions: a boot partition (often FAT32) and a root filesystem partition (often Ext4).

Bash
sudo mkdir -p /mnt/armbian_boot
sudo mkdir -p /mnt/armbian_rootfs

You can choose any path you prefer, but /mnt/ is a common convention for temporary mounts.

2. Attach the Image to a Loop Device and Expose Partitions

This is the most crucial step. Modern Linux kernels (and losetup versions) can automatically scan for partitions within a disk image.

Bash
sudo losetup -P --find --show your_armbian_image.img
  • sudo: Required for losetup as it interacts with kernel devices.

  • your_armbian_image.img: Replace this with the actual path to your Armbian .img file.

  • -P (or --partscan): This tells losetup to scan the image for partitions and automatically create separate loop devices for each partition found (e.g., /dev/loop0p1, /dev/loop0p2).

  • --find: Finds the first available (unused) loop device.

  • --show: Prints the name of the main loop device it used (e.g., /dev/loop0). Make a note of this device, as you'll need it for detaching later.

Example Output:

If it outputs /dev/loop0, then you should now have /dev/loop0p1 and /dev/loop0p2 (or more, depending on the image) available as distinct partition devices. You can verify this with:

Bash
ls -l /dev/loop0p*

You should see entries like /dev/loop0p1 and /dev/loop0p2.

3. Inspect Partition Types (Optional but Recommended)

Before mounting, you can quickly check the filesystem type of each partition to ensure you use the correct mount options (though mount is often smart enough to auto-detect).

Bash
file -s /dev/loop0p1
file -s /dev/loop0p2

Expected Output (example):

  • /dev/loop0p1: DOS/MBR boot sector, code offset 0x52+2, OEM-ID " mkfs.fat", sectors/cluster 8, root entries 512, Media descriptor 0xf8 (hard disk), sectors 524288 (volumes < 32 MB), FAT (32 bit), LBA, label "boot", serial id 0x12345678

    • This indicates a FAT32 (often shown as vfat) filesystem, which is typical for the boot partition.

  • /dev/loop0p2: Linux rev 1.0 ext4 filesystem data, UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (extents) (large files) (huge files) (dir_nlink) (extra isize) (metadata_csum)

    • This indicates an Ext4 filesystem, typical for the root filesystem.

4. Mount the Partitions

Now, mount the individual partitions to your created mount points.

Bash
# Mount the boot partition (usually FAT32)
sudo mount /dev/loop0p1 /mnt/armbian_boot

# Mount the root filesystem partition (usually Ext4)
sudo mount /dev/loop0p2 /mnt/armbian_rootfs
  • If mount complains about a wrong fs type, you might need to specify it explicitly:

    • sudo mount -t vfat /dev/loop0p1 /mnt/armbian_boot

    • sudo mount -t ext4 /dev/loop0p2 /mnt/armbian_rootfs

5. Access and Modify Files

You can now navigate into the mounted directories and access/modify files as needed.

Bash
ls /mnt/armbian_boot
ls /mnt/armbian_rootfs
cd /mnt/armbian_rootfs
# ... make your changes ...

6. Unmount the Partitions and Detach the Loop Device

This is a critical step to avoid data corruption. Always unmount the partitions and detach the loop device gracefully before removing the image file or doing anything else with it.

Bash
# Unmount the partitions first
sudo umount /mnt/armbian_boot
sudo umount /mnt/armbian_rootfs

# Detach the main loop device (replace /dev/loop0 with the actual device from step 2)
sudo losetup -d /dev/loop0

Alternative (Older/Less Common for Multi-Partition Images): kpartx

If losetup -P doesn't work for some reason (e.g., very old kernel), kpartx is an alternative.

  1. Install kpartx:

    Bash
    sudo apt install kpartx # For Debian/Ubuntu
    # sudo pacman -S kpartx # For Arch Linux
    
  2. Create device mapper entries:

    Bash
    sudo kpartx -av your_armbian_image.img
    

    This will output lines showing where it created mappings, e.g., /dev/mapper/loop0p1, /dev/mapper/loop0p2.

  3. Mount: Use these /dev/mapper/ devices to mount:

    Bash
    sudo mount /dev/mapper/loop0p1 /mnt/armbian_boot
    sudo mount /dev/mapper/loop0p2 /mnt/armbian_rootfs
    
  4. Unmount and remove mappings:

    Bash
    sudo umount /mnt/armbian_boot
    sudo umount /mnt/armbian_rootfs
    sudo kpartx -d your_armbian_image.img
    

By following these steps, you can effectively access and modify the contents of your Armbian disk image.


實例:

$ sudo losetup -P --find --show Armbian_25.5.1_Orangepione_noble_current_6.12.23_minimal.img
/dev/loop0

$ ls -l /dev/loop0p*
brw-rw---- 1 root disk 259, 12  7月 28 09:15 /dev/loop0p1

$ sudo file -s /dev/loop0p1
/dev/loop0p1: Linux rev 1.0 ext4 filesystem data, UUID=1dcda12f-cc77-431b-b193-4e4e56d8afd1, volume name "armbi_root" (extents) (64bit) (large files) (huge files)

$ sudo mount /dev/loop0p1 /mnt/pi-one 
# mount 另一個
$ sudo losetup -P --find --show Armbian_25.5.1_Orangepizero_noble_current_6.12.23_minimal.img
/dev/loop2
$ ls -l /dev/loop2p*
brw-rw---- 1 root disk 259, 14  7月 28 09:22 /dev/loop2p1
$ sudo file -s /dev/loop2p1
/dev/loop2p1: Linux rev 1.0 ext4 filesystem data, UUID=59dbad79-6dbe-433d-b8e1-c4304d8c6f3f, volume name "armbi_root" (extents) (64bit) (large files) (huge files)
$ sudo mount /dev/loop2p1 /mnt/pi-zero/

umount:

$ $ sudo umount /dev/loop0p1
$ sudo umount /dev/loop2p1
$ sudo losetup -d /dev/loop0
$ sudo losetup -d /dev/loop2

u




網誌存檔