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




2025年7月11日 星期五

Ender 3 Max 改 Klipper

參考 neoyagami/kipper-ender3-4.2.2-bltouch-g29-g2-g3.cfg

主控板為 V4.2.2,如下圖。


firmware 的設定。build 之後,把檔案 copy 到 SD卡,再把 SD卡插到機器上,重新開機,即可燒錄。



BL_T 接頭,IN (PB0) 當輸出,控制伺服馬達。OUT (PB1) 當輸入,接 probe 信號。


2025年6月26日 星期四

build armbian image,修改 kernel 原始碼

目的,加上 ILI9488 LCD 的驅動程式。

因為建立的 external module,載入時會失敗,只好把驅動程式加入建立 image 時的 kernel source 中,這樣建立的 module 就可以使用。只是很難debug,就找現成的來用 吧。


先 git clone https://github.com/armbian/build

再執行一次 build,不用到最後,只要建立 kernel source 就好

-----------------
./compile.sh \
BOARD=orangepione MAKE_THREADS=4  \
BRANCH=legacy RELEASE=bookworm \
BUILD_MINIMAL=yes BUILD_DESKTOP=no  \
NETWORKING_STACK="network-manager" \
KERNEL_CONFIGURE=no \
INSTALL_HEADERS=yes \
KERNEL_BTF=yes
-----------------

然後把 cache 下的對應的 kernel source,整個複製到新的目錄,如 [6.6_sunxi_armhf_wk]。然後,到新的目錄下,執行 make clean,把編譯的檔案刪掉。刪掉 .git 檔,再執行下列指令,建立啟始的 git 專案。

-----------------
git init
git add .
git commit -m "Initial kernel state for patching"
-----------------

再把驅動程式,如 fb_ili9488.c,複製到對應的目錄下,如 drivers/staging/fbtft,並修改相關的檔案,如 drivers/staging/fbtft 目錄下的 Kconfig 和 Makefile。

然後執行下列指令,提交程式修改。

-----------------
# 首先,提交你的修改
git add drivers/staging/fbtft/fb_ili9488.c drivers/staging/fbtft/Kconfig drivers/staging/fbtft/Makefile
git commit -s -m "Add: FB_TFT ILI9488 driver"
# 然後生成從上次提交到現在的 patch (或者指定範圍)
# 範例:從上一次提交到 HEAD
git format-patch -1 HEAD
# 這會生成一個類似 0001-Add-FB_TFT-ILI9488-driver.patch 的檔案。
-----------------

再將這個 patch 檔,複製到 patch/kernel/archive/sunxi-6.6/patches.armbian/ 下,並修改 series.armbian 和 series.conf,加入這個 patch 檔。

修改 config/kernel/linux-sunxi-legacy.config,加入 CONFIG_FB_TFT_ILI9488=m。

然後,build image,測試結果。

若是加了自己的 patch,或改了 kernel 的 config,重新 build image 的時間,就會花很多時間。

使用 docker 來 build (ArchLinux)

參考 Gemini 教的步驟。

-----------------

-----------------





2025年6月20日 星期五

Orange Pi One 連接 ST7796 界面電阻觸控 LCD

成果展示

先展示一下成果,放張照片,當作紀念。要走到這邊,實在不容易。



參考連結

動機與背景

因為貪便宜,也為了改 3D印表機不要花太高的成本,在網路上買了好幾片 Orange Pi One 的單板電腦。

可能因為用的晶片太舊,新的 Kernel 在支援上會有問題。例如目前的 kernel 是 6.12.30,同樣的程式,在 Orange Pi Zero 3 上,可以正常執行,但在 Orange Pi One 就是怪怪的,使用 tinydrm 的 module,完全無法啟動 X window。經反覆測試,確認在較舊的 kernel  6.6.75 上,才能正常執行。另外,armbian/build 的 32位元的 sunxi 的 kernel,沒有把 gpio-backlight 編成 module,dts 中,無法控制背光。

使用 panel-mipi-dbi-spi,在 Orange Pi Zero3 下,可以正常運作,也可以執行 KlipperScreen。但在 Orange Pi One,相同的設定檔,會出現下面的錯誤訊息。後來確認,背光控制要有 gpio_backlight 的 kernel module。

$ dmesg | less
[   18.163593] panel-mipi-dbi-spi spi0.0: supply power not found, using dummy regulator
[   18.163853] panel-mipi-dbi-spi spi0.0: supply io not found, using dummy regulator
[   18.164817] spi spi0.0: deferred probe pending: panel-mipi-dbi-spi: Failed to get backlight

$ sudo modprobe gpio_backlight
[sudo] password for klipper: 
modprobe: FATAL: Module gpio_backlight not found in directory /lib/modules/6.12.23-current-sunxi

另外,Orange Pi Zero3 的 image 裡,有 fb_st7796s 的模組,在 Orange Pi One 則沒有。

使用 6.12.30-current-sunxi,雖然 console 是正常的,但啟動 X window,就會出現 Bus error。

$ X

X.Org X Server 1.21.1.7
X Protocol Version 11, Revision 0
Current Operating System: Linux orangepione 6.12.30-current-sunxi #2 SMP Thu May 22 20:29:54 CST 2025 armv7l
Kernel command line: root=UUID=4c8b2287-edb5-431a-904c-a21f8f213ef8 rootwait rootfstype=ext4 splash=verbose console=ttyS0,115200 console=tty1 hdmi.audio=EDID:0 disp.screen0_output_mode=1920x1080p60 consoleblank=0 loglevel=1 ubootpart=d0be82e5-01 ubootsource=mmc usb-storage.quirks=0x2537:0x1066:u,0x2537:0x1068:u   sunxi_ve_mem_reserve=0 sunxi_g2d_mem_reserve=0 sunxi_fb_mem_reserve=16 cgroup_enable=memory
xorg-server 2:21.1.7-3+deb12u9 (https://www.debian.org/support) 
Current version of pixman: 0.42.2
	Before reporting problems, check http://wiki.x.org
	to make sure that you have the latest version.
Markers: (--) probed, (**) from config file, (==) default setting,
	(++) from command line, (!!) notice, (II) informational,
	(WW) warning, (EE) error, (NI) not implemented, (??) unknown.
(==) Log file: "/var/log/Xorg.0.log", Time: Mon Jun 23 14:36:07 2025
(==) Using config directory: "/etc/X11/xorg.conf.d"
(==) Using system config directory "/usr/share/X11/xorg.conf.d"
(EE) 
(EE) Backtrace:
(EE) 
(EE) Bus error at address 0xb6185000
(EE) 
Fatal server error:
(EE) Caught signal 7 (Bus error). Server aborting
(EE) 
(EE) 
Please consult the The X.Org Foundation support 
	 at http://wiki.x.org
 for help. 
(EE) Please also check the log file at "/var/log/Xorg.0.log" for additional information.
(EE) 
(EE) Server terminated with error (1). Closing log file.
Aborted

另外,使用 6.12.30-current-sunxi,自行編譯的 fb_st7796s 或 fb_ili9488 模組,也不能正常運作,會變得一片白屏。

經過反覆的測試,確認 Orange Pi One 的 32位元的系統,要使用 6.6.75 的 kernel 才能正常運作。

再持續地做各種嘗試,發現使用 edge 的 image,6.15 的 kernel 可以正常運作。比對 6.12 和 6.15 的 driver 原始碼,發現程式有不少的變動,6.12 的可能有一些 bug 吧。那就用最新的吧。

至於 ILI9488界面的 LCD,因為必須使用 666 的格式,6.12 之後可以支援,但是使用 X 仍會當,最好暫時不要用。

自行 build image

後來自行照著 armbian 的 build 說明,自行 build image,終於可以正常安裝執行 KlipperScreen。

複製 armbian/build 的程式。

$ git clone https://github.com/armbian/build

然後修改 build/config/kernel/linux-sunxi-legacy.config,增加 build gpio_backlight 和 fb_st7796s 的 kernel module。

.......
CONFIG_BACKLIGHT_QCOM_WLED=m
CONFIG_BACKLIGHT_RT4831=m
CONFIG_BACKLIGHT_GPIO=m
CONFIG_BACKLIGHT_LED=m
........
CONFIG_FB_TFT_ST7735R=m
CONFIG_FB_TFT_ST7789V=m
CONFIG_FB_TFT_ST7796S=m
CONFIG_FB_TFT_TINYLCD=m
........

執行 compile.sh。

$ ./compile.sh \
BOARD=orangepione \
BRANCH=legacy RELEASE=bookworm \
BUILD_MINIMAL=yes BUILD_DESKTOP=no  \
NETWORKING_STACK="network-manager" \
KERNEL_CONFIGURE=no \
INSTALL_HEADERS=yes \
KERNEL_BTF=yes

編譯完成,產生的 image。

$ ls build/output/images/
Armbian-unofficial_25.08.0-trunk_Orangepione_bookworm_legacy_6.6.75_minimal.img
Armbian-unofficial_25.08.0-trunk_Orangepione_bookworm_legacy_6.6.75_minimal.img.sha
Armbian-unofficial_25.08.0-trunk_Orangepione_bookworm_legacy_6.6.75_minimal.img.txt






網誌存檔