2025年2月16日日曜日

How to Create Docker Image of Raspbian

README

How to Create Docker Image of Raspbian

This guide explains how to create a Docker image for Raspbian on Ubuntu/Debian.

Preparation

First, download the Raspbian OS image and unzip it:

wget https://downloads.raspberrypi.com/raspios_lite_armhf/images/raspios_lite_armhf-2024-11-19/2024-11-19-raspios-bookworm-armhf-lite.img.xz
unzip 2024-11-19-raspios-bookworm-armhf-lite.img.xz

Inspect the OS Image

Use fdisk to inspect the image and identify partitions. We need Sector size and Start Sector of second image for mount.

$ sudo fdisk --list 2024-11-19-raspios-bookworm-armhf-lite.img
Disk 2024-11-19-raspios-bookworm-armhf-lite.img: 2.38 GiB, 2550136832 bytes, 4980736 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x57b902f5

Device                                      Boot   Start     End Sectors  Size Id Type
2024-11-19-raspios-bookworm-armhf-lite.img1         8192 1056767 1048576  512M  c W95 FAT32 (LBA)
2024-11-19-raspios-bookworm-armhf-lite.img2      1056768 4980735 3923968  1.9G 83 Linux

Mount and Create Docker Image

mkdir image
sudo mount -o loop,offset=$((512*1056768)) 2024-11-19-raspios-bookworm-armhf-lite.img image
cd image
sudo tar cf ../docker-image-2024-11-19-raspios-bookworm-armhf-lite.tar .

Import the archive as a Docker image:

docker import docker-image-2024-11-19-raspios-bookworm-armhf-lite.tar raspios-bookworm-armhf-lite:2024-11-19

Check if the image is registered:

docker image list

Install qemu

If your host is x86_64, enable ARM emulation:

sudo apt-get -y install qemu-user-static

Running the Docker Image

Run the container:

docker run -it raspios-bookworm-armhf-lite:2024-11-19 /bin/bash
uname -a
Linux f5f38b46dc55 6.1.0-31-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.128-1 (2025-02-07) armv7l GNU/Linux

Summary

This guide outlines creating a Docker image from a Raspbian image, importing it into Docker, and running it with QEMU emulation on non-ARM hosts.

2025年2月10日月曜日

Static IP Configuration on Raspbian

Static IP Configuration on Raspbian (Bookworm)

Static IP Configuration on Raspbian (Bookworm)

Setting a static IP address on Raspbian Bookworm ensures your Raspberry Pi always uses the same IP, making it easier to access remotely.

1. Open the NetworkManager Configuration

sudo nmcli connection show

2. Modify the Connection to Set a Static IP

sudo nmcli connection modify "Wired connection 1" ipv4.addresses 192.168.1.100/24
sudo nmcli connection modify "Wired connection 1" ipv4.gateway 192.168.1.1
sudo nmcli connection modify "Wired connection 1" ipv4.dns "8.8.8.8 8.8.4.4"
sudo nmcli connection modify "Wired connection 1" ipv4.method manual

Replace 192.168.1.100 with your desired IP, and adjust the gateway and DNS settings as needed.

3. Save and Apply the Configuration

sudo nmcli connection up "Wired connection 1"

4. Verify the Static IP

ip addr show eth0

Check that the IP address matches your static configuration.

Your Raspberry Pi is now set with a static IP address using NetworkManager! 🌐

Power Saving on Rasbian

Power Saving on Raspbian

Power Saving on Raspbian

Raspbian allows you to optimize settings for better power efficiency. This guide focuses on methods to reduce energy consumption, making your Raspberry Pi more efficient for battery-powered or always-on projects.

Check Current CPU Frequency

cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq

Set CPU Governor to 'powersave'

sudo cpufreq-set -g powersave

Set Maximum CPU Frequency

sudo cpufreq-set -u 600000

Disable HDMI to Save Power

sudo /usr/bin/tvservice -o

Turn Off Unused USB Devices

echo '1-1' | sudo tee /sys/bus/usb/drivers/usb/unbind

Reduce GPU Memory Allocation

sudo nano /boot/config.txt
# Add or modify:
gpu_mem=16

Applying these settings can significantly reduce power consumption, ideal for headless setups or long-term operations.

Happy optimizing your Raspberry Pi! ⚡

2014年4月1日火曜日

How to use zram

Optimizing Swap with ZRAM on Raspbian

Optimizing Swap with ZRAM on Raspbian

By default, Raspbian uses a swap file on the SD card for easy setup. While swap helps when system memory is low, using the SD card for swap can shorten its lifespan due to frequent writes and cause slower performance.

ZRAM is a solution to these issues. It’s a kernel feature that creates a compressed swap partition in RAM, improving both performance and longevity of the SD card.

Disable Default Swap Setting

sudo apt-get install chkconfig
sudo chkconfig dphys-swapfile off

Enable Swap on ZRAM

Load the ZRAM module:

sudo modprobe zram

Set a 128MB ZRAM swap space:

echo $((128*1024*1024)) | sudo tee /sys/zram0/disksize

Create the swap space on ZRAM:

sudo mkswap /dev/zram0

Enable the swap:

sudo swapon -p 10 /dev/zram0

With ZRAM enabled, your Raspberry Pi will benefit from faster swap performance and reduced SD card wear.

Enjoy optimizing your Raspberry Pi! 🚀

How to move rootfs to external usb storage

Move RootFS to External USB Storage (Bookworm)

Move RootFS to External USB Storage (Bookworm)

This guide explains how to move the root filesystem (rootfs) of a Raspberry Pi running Bookworm to an external USB storage device.

1. Prepare Your External USB Storage

sudo fdisk /dev/sdX
# Create a new partition (type Linux) and format it
sudo mkfs.ext4 /dev/sdX1

Replace /dev/sdX with the actual device name of your USB storage.

2. Mount the USB Storage

sudo mkdir /mnt/usb
sudo mount /dev/sdX1 /mnt/usb

3. Copy RootFS to USB

sudo rsync -aAXv / --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} /mnt/usb

4. Modify Boot Configuration

sudo nano /boot/cmdline.txt

Change the root= parameter to point to your USB device, e.g., root=/dev/sdX1.

5. Reboot the Raspberry Pi

sudo sync
sudo reboot

Your Raspberry Pi should now boot from the external USB storage. 🚀

2014年3月27日木曜日

CPU frequency scaling

Optimizing CPU Frequency on Raspbian

Optimizing CPU Frequency on Raspbian

Raspbian allows you to optimize CPU frequency settings to balance performance and power consumption. Adjusting CPU frequency can be useful for performance tuning or extending battery life in portable projects.

Check Current CPU Frequency

cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq

List Available Governors

cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors

Set CPU Governor to 'performance'

sudo cpufreq-set -g performance

Set Maximum CPU Frequency

sudo cpufreq-set -u 1500000

Verify Changes

cpufreq-info

Adjusting CPU frequency helps in managing your Raspberry Pi’s thermal and power efficiency. Experiment with different settings to find the optimal balance for your use case.

Happy tuning your Raspberry Pi! 🚀