Developers Club geek daily blog

1 year, 11 months ago
image

Sometimes editing standard boot images, and also configuring of systems with the subsequent replication on a large number of payments of Raspberry Pi is necessary. For a solution of similar tasks it is convenient to use packets of qemu-user-static and binfmt-support.

We start the terminal and we set packets of qemu-user-static and binfmt-support:

sudo apt-get install qemu qemu-user-static binfmt-support 

QEMU in the qemu-user-static mode allows to start the binary files compiled for one processor, using the processor of other architecture. The packet of binfmt-support allows to start binary files directly.

After installation we watch the list of the 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 we see, support of ARM files is included — qemu-arm (enabled).

We pass to the www.raspberrypi.org/downloads page and we select the necessary system. As an example we will take Raspbian Wheezy, at the moment the actual version 2015-05-05-raspbian-wheezy.img.

We download and unpack 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 

At first we will obtain information on an image:

sudo fdisk -lu 2015-05-05-raspbian-wheezy.img


Диск 2015-05-05-raspbian-wheezy.img: 3276 МБ, 3276800000 байт
255 головок, 63 секторов/треков, 398 цилиндров, всего 6400000 секторов
Units = секторы of 1 * 512 = 512 bytes
Размер сектора (логического/физического): 512 байт / 512 байт
I/O size (minimum/optimal): 512 bytes / 512 bytes
Идентификатор диска: 0xa6202af7
 
                    Устр-во Загр        Начало      Конец       Блоки     Id    Система
2015-05-05-raspbian-wheezy.img1          8192       122879      57344     c     W95 FAT32 (LBA)
2015-05-05-raspbian-wheezy.img2          122880     6399999     3138560   83    Linux


We add 1Gb to an 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 pick up all image to the loop0 device, and the second section (each sector on 512 bytes begins with sector 122880) 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

It will tie device/dev/loop0 to all image and / dev/loop1 to the section which we want to expand.

We start parted, we delete the second section in device/dev/loop0 and we create it with a new size.

sudo parted /dev/loop0 

GNU Parted 2.3
Используется /dev/loop0
Добро пожаловать в GNU Parted! Наберите 'help' для просмотра списка команд

(parted) print 

Модель: Loopback device (loop)
Диск /dev/loop0: 4351MB
Размер сектора (логич./физич.): 512B/512B
Таблица разделов: msdos
 
Номер  Начало  Конец   Размер  Тип      Файловая система  Флаги
 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      

Модель: Loopback device (loop)
Диск /dev/loop0: 4351MB
Размер сектора (логич./физич.): 512B/512B
Таблица разделов: msdos
 
Номер  Начало  Конец   Размер  Тип      Файловая система  Флаги
 1     4194kB  62,9MB  58,7MB  primary  fat16             lba
 2     62,9MB  4351MB  4288MB  primary  ext4

(parted) quit

Then we check and we change the size of the new section:

sudo e2fsck -f /dev/loop1 

e2fsck 1.42.9 (4-Feb-2014)
Проход 1: Проверка inodes, блокs, а также размеров
Pass 2: Checking каталог structure
Pass 3: Checking каталог connectivity
Pass 4: Checking reference counts
Pass 5: Checking группа 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 we will be convinced that the size of the new section increased by 1 Gb:

sudo parted /dev/loop0 

GNU Parted 2.3
Используется /dev/loop0
Добро пожаловать в GNU Parted! Наберите 'help' для просмотра списка команд. 

(parted) print

Модель: Loopback device (loop)
Диск /dev/loop0: 4351MB
Размер сектора (логич./физич.): 512B/512B
Таблица разделов: msdos
 
Номер  Начало  Конец   Размер  Тип      Файловая система  Флаги
 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 

We mount an image:

sudo mkdir ~/rpi_mnt
sudo mount ~/rpi_image/2015-05-05-raspbian-wheezy.img -o loop,offset=$((122880*512)),rw ~/rpi_mnt

(optional) we Mount / boot:

sudo mount ~/rpi_image/2015-05-05-raspbian-wheezy.img -o loop,offset=$((8192*512)),rw ~/rpi_mnt/boot

(optional):

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

That everything worked correctly (for example, a network) before change of the root directory it is necessary to comment out all lines in the file ~ / rpi_mnt/etc/ld.so.preload:

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

for editing key i, enter # before every line, then click: wq Change of the root directory (CHROOT).

First of all, it is necessary to be convinced that binfmt-support will start our code as soon as we change root file system. For this purpose we will copy the file in the root directory of an image:

sudo cp /usr/bin/qemu-arm-static ~/rpi_mnt/usr/bin 

We change a root:

cd ~/rpi_mnt
sudo chroot . bin/bash 

Let's check change of the root directory:

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 it is possible to add and delete programs, to configure system then to copy the received image on a set of Raspberry Pi devices, without the need for connection of the monitor and the keyboard to each separate payment.

Input in the menu of a configuration Raspberry Pi:

sudo raspi-config

Removal of an environment of a desktop:

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

Be convinced that in the list there are no excess packets and start once again without "-dry-run".

Updating of system. For updating to the new version (for example, jessie, stretch, etc.), boot has to be mounted / (also check all 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"

On the SD card it is necessary for record of an image:

1. To leave CHROOT (gather exit)
2. To uncomment lines in file/etc/ld.so.preload:

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

delete the characters added earlier # with clicking x, then click: wq 3. To dismount all sections:

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, we watch a way and we write an image:

sudo fdisk -l
sudo dd if=~/rpi_image/2015-05-05-raspbian-wheezy.img of=/dev/mmcblk0

This article is a translation of the original post at habrahabr.ru/post/272523/
If you have any questions regarding the material covered in the article above, please, contact the original author of the post.
If you have any complaints about this article or you want this article to be deleted, please, drop an email here: sysmagazine.com@gmail.com.

We believe that the knowledge, which is available at the most popular Russian IT blog habrahabr.ru, should be accessed by everyone, even though it is poorly translated.
Shared knowledge makes the world better.
Best wishes.

comments powered by Disqus