Skip to content

Instantly share code, notes, and snippets.

@jkullick
Last active March 24, 2024 14:36
Show Gist options
  • Star 32 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save jkullick/9b02c2061fbdf4a6c4e8a78f1312a689 to your computer and use it in GitHub Desktop.
Save jkullick/9b02c2061fbdf4a6c4e8a78f1312a689 to your computer and use it in GitHub Desktop.
Chroot into Raspberry Pi ARMv7 Image with Qemu
# install dependecies
apt-get install qemu qemu-user-static binfmt-support

# download raspbian image
wget https://downloads.raspberrypi.org/raspbian_latest

# extract raspbian image
unzip raspbian_latest

# extend raspbian image by 1gb
dd if=/dev/zero bs=1M count=1024 >> 2016-05-27-raspbian-jessie.img

# set up image as loop device
losetup /dev/loop0 2016-05-27-raspbian-jessie.img

# check file system
e2fsck -f /dev/loop0p2

#expand partition
resize2fs /dev/loop0p2

# mount partition
mount -o rw /dev/loop0p2  /mnt
mount -o rw /dev/loop0p1 /mnt/boot

# mount binds
mount --bind /dev /mnt/dev/
mount --bind /sys /mnt/sys/
mount --bind /proc /mnt/proc/
mount --bind /dev/pts /mnt/dev/pts

# ld.so.preload fix
sed -i 's/^/#/g' /mnt/etc/ld.so.preload

# copy qemu binary
cp /usr/bin/qemu-arm-static /mnt/usr/bin/

# chroot to raspbian
chroot /mnt /bin/bash
	# do stuff...
	exit

# revert ld.so.preload fix
sed -i 's/^#//g' /mnt/etc/ld.so.preload

# unmount everything
umount /mnt/{dev/pts,dev,sys,proc,boot,}

# unmount loop device
losetup -d /dev/loop0

Source

@elliotboney
Copy link

Awesome! finally got this working after finding ur gist

@pkillnine
Copy link

Note: on Slackware, and presumably other operating systems/versions of losetup, you need to add the -P flag when setting up image as loop device (e.g. losetup -P /dev/loop0 2016-05-27-raspbian-jessie.img) which forces the kernel to scan the partition table, otherwise it doesn't see the partitions in the Raspbian image.

@Nsikak1
Copy link

Nsikak1 commented May 1, 2018

Where can I get o rootfs of the raspbian image (without the kernel included?) I would like to chroot it with the Linux deploy app on my Samsung s4(android)

@Botspot
Copy link

Botspot commented Jan 12, 2020

Doesn't work on current losetup version. Add -P flag to all losetup commands to create partition-level block devices like /dev/loop0p2
Source: https://unix.stackexchange.com/a/447977/369481

@Ghostbird
Copy link

Thanks a lot for this example. I adapted it a bit to work with a physical sdcard (without the losetup bit) to fix my raspberry pi installation.

@1000283
Copy link

1000283 commented Sep 17, 2021

dd if=/dev/zero bs=1M count=1024 >> 2016-05-27-raspbian-jessie.img

This step increased the .img file's size (and thus the device's size), but resize2fs /dev/loop0p2 would not grow the partition:

resize2fs 1.42.13 (17-May-2015)
The filesystem is already 858112 (4k) blocks long.  Nothing to do!

even though cfdisk reported 1G free space after the p2 partition. So i noted the partition type (your typical linux), since it was the last partition and after it was the desired free space to the end of the "disk", i deleted the partition and recreated it with all available space.

Then resize2fs successfully expanded the ex4 filesystem in the partition to match the full size.

@Ghostbird
Copy link

dd if=/dev/zero bs=1M count=1024 >> 2016-05-27-raspbian-jessie.img

This step increased the .img file's size (and thus the device's size), but resize2fs /dev/loop0p2 would not grow the partition:

resize2fs 1.42.13 (17-May-2015)
The filesystem is already 858112 (4k) blocks long.  Nothing to do!

even though cfdisk reported 1G free space after the p2 partition. So i noted the partition type (your typical linux), since it was the last partition and after it was the desired free space to the end of the "disk", i deleted the partition and recreated it with all available space.

Then resize2fs successfully expanded the ex4 filesystem in the partition to match the full size.

As noted above, I used this script with a physical SDCard, so I skipped that step. Looking at it, I think there's no step included to resize the partition. resize2fs resizes the ext4 filesystem to the size of the partition, but the partition is not enlarged first AFAIK. The image is expanded, but that data is not automatically part of the last partition. In fact if the disk uses GPT (which is likely nowadays), you need software that will not only resize the partition, but also modify the GPT headers and move their copies which are written at the end of the medium. I think software like parted and gdisk can do this.

@m00ninite
Copy link

parted was just the hint I needed. For anyone that wants to resize their image properly, this is how I did it:

First, list the partitions:

$ sudo parted 2016-05-27-raspbian-jessie.img print

Model:  (file)
Disk /home/pop_desktop/workspace/raspiblitz_ci/2021-03-04-raspios-buster-arm64.img: 3729MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      4194kB  273MB   268MB   primary  fat32        lba
 2      273MB   3729MB  3456MB  primary  ext4

Use this to determine the partition number you want, and the "end" of the partition. I wanted partition 2, as that's the home partition that needs to grow.

Then grow the partition like so:

loop=$(losetup -f)  # Find the first available loop device

# extend raspbian image by 1gb
dd if=/dev/zero bs=1M count=1024 >> raspios.img
sudo parted raspios.img resizepart 2 4700MB

# set up image as loop device
losetup -P $loop raspios.img

# check file system
e2fsck -y -f ${loop}p2

#expand partition
resize2fs ${loop}p2

In the above example, I changed the "end" of the ext4 partition to 4700MB, roughly increasing it by 1GB.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment