Editing a Raspberry Pi image using qemu-user-static (Ubuntu 14.04)

Sometimes it is necessary to edit standard boot images, as well as configure systems with subsequent replication to a large number of Raspberry Pi boards. To solve such problems, it is convenient to use the qemu-user-static and binfmt-support packages.
We launch the terminal and install the qemu-user-static and binfmt-support packages:
sudo apt-get install qemu qemu-user-static binfmt-support
QEMU in qemu-user-static mode allows you to run binary files compiled for one processor using a processor of a different architecture. The binfmt-support package allows you to run binary files directly.
After installation, we look at the list of supported binary files:
update-binfmts --display
qemu-aarch64 (enabled): ...... qemu-microblaze (enabled): ...... qemu-arm (enabled): ...... qemu-m68k (enabled): ...... qemu-ppc64abi32 (enabled): ...... qemu-sparc64 (enabled): ...... qemu-sparc (enabled): ...... qemu-sh4 (enabled): ...... qemu-sh4eb (enabled): ...... qemu-sparc32plus (enabled): ...... qemu-ppc64 (enabled): ...... qemu-ppc (enabled): ...... qemu-mipsel (enabled): ...... qemu-alpha (enabled): ...... qemu-mips (enabled): ...... qemu-cris (enabled): ...... qemu-s390x (enabled): ...... qemu-armeb (enabled): ......
As you can see, support for ARM files is enabled - qemu-arm (enabled).
Go to the page www.raspberrypi.org/downloads and select the desired system. Take Raspbian Wheezy as an example, the current version is 2015-05-05-raspbian-wheezy.img.
Download and unpack the archive:
sudo mkdir ~/rpi_image
cd ~/rpi_image
sudo wget http://downloads.raspberrypi.org/raspbian/images/raspbian-2015-05-07/2015-05-05-raspbian-wheezy.zip
sudo unzip 2015-05-05-raspbian-wheezy.zip
sudo rm 2015-05-05-raspbian-wheezy.zip
First we get information about the image:
sudo fdisk -lu 2015-05-05-raspbian-wheezy.img
Disk 2015-05-05-raspbian-wheezy.img: 3276 MB, 3276800000 bytes
255 heads, 63 sectors / tracks, 398 cylinders, total 6,400,000 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
Drive ID: 0xa6202af7
Zagr Device Start End Blocks Id System
2015-05-05-raspbian-wheezy.img1 8192 122879 57344 with W95 FAT32 (LBA)
2015-05-05-raspbian-wheezy.img2 122880 6399999 3138560 83 Linux
Add 1Gb to the image:
sudo chmod 775 2015-05-05-raspbian-wheezy.img
sudo dd if=/dev/zero bs=1M count=1024 >> 2015-05-05-raspbian-wheezy.img
We hook the entire image to the device loop0, and the second section (starts with sector 122880, each sector 512 bytes each) to loop1.
sudo losetup -f --show 2015-05-05-raspbian-wheezy.img
sudo losetup -f --show -o $((122880*512)) 2015-05-05-raspbian-wheezy.img
This will attach the device / dev / loop0 to the whole image and / dev / loop1 to the partition we want to expand.
We start parted, delete the second partition in the device / dev / loop0 and create it with a new size.
sudo parted /dev/loop0
GNU Parted 2.3 Used by / dev / loop0 Welcome to GNU Parted! Type 'help' to see a list of commands
(parted) print
Model: Loopback device (loop) Disk / dev / loop0: 4351MB Sector Size (Logical / Physical): 512B / 512B Partition Table: msdos Number Start End Size Type File System Flags 1 4194kB 62.9MB 58.7MB primary fat16 lba 2 62.9MB 3277MB 3214MB primary ext4
(parted) rm 2
(parted) mkpart primary 62.9 4351
(parted) print
Model: Loopback device (loop) Disk / dev / loop0: 4351MB Sector Size (Logical / Physical): 512B / 512B Partition Table: msdos Number Start End Size Type File System Flags 1 4194kB 62.9MB 58.7MB primary fat16 lba 2 62.9MB 4351MB 4288MB primary ext4
(parted) quit
Then check and resize the new partition:
sudo e2fsck -f /dev/loop1
e2fsck 1.42.9 (4-Feb-2014) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking connectivity directory Pass 4: Checking reference counts Pass 5: Checking group summary information / dev / loop1: 86233/196224 files (0.1% non-contiguous), 630146/784640 blocks
sudo resize2fs /dev/loop1
resize2fs 1.42.9 (4-Feb-2014) Resizing the filesystem on / dev / loop1 to 1046784 (4k) blocks. The filesystem on / dev / loop1 is now 1046784 blocks long.
Now make sure that the size of the new partition has increased by 1 Gb:
sudo parted /dev/loop0
GNU Parted 2.3 Used by / dev / loop0 Welcome to GNU Parted! Type 'help' to see a list of commands.
(parted) print
Model: Loopback device (loop) Disk / dev / loop0: 4351MB Sector Size (Logical / Physical): 512B / 512B Partition Table: msdos Number Start End Size Type File System Flags 1 4194kB 62.9MB 58.7MB primary fat16 lba 2 62.9MB 4351MB 4288MB primary ext4
(parted) quit
Let's clear loop devices:
losetup -d /dev/loop0 /dev/loop1
Mount the image:
sudo mkdir ~/rpi_mnt
sudo mount ~/rpi_image/2015-05-05-raspbian-wheezy.img -o loop,offset=$((122880*512)),rw ~/rpi_mnt
(optional) Mount / boot:
sudo mount ~/rpi_image/2015-05-05-raspbian-wheezy.img -o loop,offset=$((8192*512)),rw ~/rpi_mnt/boot
(not necessary):
cd ~/rpi_mnt
sudo mount --bind /dev dev/
sudo mount --bind /sys sys/
sudo mount --bind /proc proc/
sudo mount --bind /dev/pts dev/pts
For everything to work correctly (for example, a network), before changing the root directory, you need to comment out all the lines in the file ~ / rpi_mnt / etc / ld.so.preload:
sudo vi ~/rpi_mnt/etc/ld.so.preload
to edit, press the i key, type # in front of each line, then press ESC: wq ENTER
Change the root directory (CHROOT).
First of all, you need to make sure binfmt-support will run our code as soon as we change the root file system. To do this, copy the file to the root directory of the image:
sudo cp /usr/bin/qemu-arm-static ~/rpi_mnt/usr/bin
Change the root:
cd ~/rpi_mnt
sudo chroot . bin/bash
Check the root directory change:
uname -a
Linux simm-UX32VD 3.19.0-33-generic # 38 ~ 14.04.1-Ubuntu SMP Fri Nov 6 18:17:28 UTC 2015 armv7l GNU / Linux
Now you can add and remove programs, configure the system, and then copy the resulting image to many Raspberry Pi devices, without the need to connect a monitor and keyboard to each individual board.
Entering the Raspberry Pi configuration menu:
sudo raspi-config
Removing the desktop environment:
apt-get remove --dry-run --auto-remove --purge libx11-.*
Make sure that there are no unnecessary packages in the list and run again without "--dry-run".
System update. To upgrade to a new version (for example, jessie, stretch, etc.), / boot must be mounted (also check all the files in /etc/apt/sources.list.d for updating). Before updating, save the necessary configuration files.
sed -i 's/wheezy/jessie/g' /etc/apt/sources.list
apt-get update
apt-get dist-upgrade -o Dpkg::Options::="--force-confold"
To write an image to an SD card, you need:
1. Exit CHROOT (type exit)
2. Uncomment the lines in the /etc/ld.so.preload file:
sudo vi ~/rpi_mnt/etc/ld.so.preload
delete the previously added # characters by pressing x, then press ESC: wq ENTER
3. Unmount all partitions:
sudo umount ~/rpi_mnt/sys
sudo umount ~/rpi_mnt/proc
sudo umount ~/rpi_mnt/dev/pts
sudo umount ~/rpi_mnt/boot
sudo umount ~/rpi_mnt/dev
cd ..
sudo umount ~/rpi_mnt
We insert the SD card, look at the path and record the image:
sudo fdisk -l
sudo dd if=~/rpi_image/2015-05-05-raspbian-wheezy.img of=/dev/mmcblk0
Download article in pdf - http://prom-electric.ru/media/raspi_img_edit.pdf