📜 ⬆️ ⬇️

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

image

Sometimes you need to edit the standard boot images, as well as the configuration of systems, followed by replication on 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 start 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, see 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. As an example, take Raspbian Wheezy, currently the current version 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
 Disk ID: 0xa6202af7
 
                     Device Zagr 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 loop0 device, and the second section (starting from sector 122880, each sector is 512 bytes) 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 bind the / dev / loop0 device to the whole image and / dev / loop1 to the partition we want to expand.

Run parted, delete the second partition in the / dev / loop0 device and create it with the new size.

 sudo parted /dev/loop0 

 GNU Parted 2.3
 / Dev / loop0 is used
 Welcome to GNU Parted!  Type 'help' to view the 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 we check and resize the new section:

 sudo e2fsck -f /dev/loop1 

 e2fsck 1.42.9 (4-Feb-2014)
 Pass 1: Check inodes, blocks, and sizes
 Pass 2: Checking the structure directory
 Pass 3: Checking the 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
 / Dev / loop0 is used
 Welcome to GNU Parted!  Type 'help' to view the 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 

Clean 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 ~ / rpi_mnt / etc / ld.so.preload file:

 sudo vi ~/rpi_mnt/etc/ld.so.preload 

To edit, press the i key, enter # before each line, then press ESC: wq ENTER

Changing the root directory (CHROOT).

First of all, you need to make sure that 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 multiple 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 

Deleting the desktop environment:

 apt-get remove --dry-run --auto-remove --purge libx11-.* 

Make sure there are no unnecessary packages in the list and run it 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 an update). Before upgrading, 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 record an image on an SD card:

1. Exit CHROOT (type exit)
2. Uncomment the lines in the /etc/ld.so.preload file:

 sudo vi ~/rpi_mnt/etc/ld.so.preload 

remove 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 

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 the article in pdf - http://prom-electric.ru/media/raspi_img_edit.pdf

Source: https://habr.com/ru/post/272523/


All Articles