Monday, September 16, 2019

Allocating more space to an emulated Raspbian disk image

The Raspberry Pi Raspbian images from http://downloads.raspberrypi.org/raspbian/images/ for QEMU have limited disk space, just enough for booting up and not much else. In the screenshot below, the image has just 3.3GB to it.

In order to work with ROS or other applications, the disk image has to be resized with more free disk space. This can be done by doing the following.
  1. Optional but recommended. Make a copy of the original Raspbian image and rename it, for example as raspbian.img.

    $ cp 2019-06-20-raspbian-buster.img raspbian.img
  2. Extend the copied image's size with the following command in the Terminal.

    $ qemu-img resize raspbian.img +6G


    Note: +6G means to extend the original size by 6 gigabytes
  3. Next, boot up the newly resized image with QEMU.

    $ qemu-system-arm -kernel kernel-qemu-4.4.34-jessie \
      -cpu arm1176 \
      -m 256 \
      -M versatilepb \
      -serial stdio \
      -append "root=/dev/sda2 rootfstype=ext4 rw" \
      -hda raspbian.img \
      -net nic \
      -net user,hostfwd=tcp::5022-:22 \
      -no-reboot


    The QEMU window appears.

  4. In the QEMU window, open up a Terminal.
  5. Enter the following commands to delete and create a new partition.

    $ sudo fdisk /dev/sda
  6. Print out the current partitions by entering p. Make a note of the starting offset of the second partition e.g. 540672.


  7. Delete the second partition by entering d followed by 2. Then recreate the second partition by entering c, n, p, 2, followed by the starting address noted from the previous steps e.g. 540672. Leave the signature alone when prompted.


  8. Now, reboot the emulated Raspbian OS.
  9. When QEMU has restarted the Raspbian OS, open a Terminal and enter the command to resize the file system.

    $ sudo resize2fs /dev/sda2

  10. To see the available disk space, enter the command:

    $ df -h

No comments: