Working with Raspian VMs in QEMU in Ubuntu 14.04 x64

Discussion in 'all things UNIX' started by mirimir, Feb 8, 2015.

  1. mirimir

    mirimir Registered Member

    Joined:
    Oct 1, 2011
    Posts:
    6,028
    Instructions follow for working with Raspian VMs in QEMU in Ubuntu 14.04 x64. We start with a bootable image from XEC Design. Before starting, review the guides http://xecdesign.com/qemu-emulating-raspberry-pi-the-easy-way/ and http://paulscott.co.za/blog/full-raspberry-pi-raspbian-emulation-with-qemu/ for basics, and http://www.raspberrypi.org/forums/viewtopic.php?f=51&t=45265 for extending the root filesystem using parted and resize2fs.
    First bring Ubuntu up to date:
    Code:
    # sudo apt-get update
    # sudo apt-get dist-upgrade
    # sudo reboot
    Then install QEMU and download the latest image and kernel:
    Code:
    $ sudo apt-get install qemu-system qemu-user-static binfmt-support
    $ mkdir qemu_vms
    $ cd qemu_vms
    $ wget http://downloads.raspberrypi.org/raspbmc_latest
    $ mv raspbmc_latest sd.img.gz
    $ wget http://xecdesign.com/downloads/linux-qemu/kernel-qemu
    $ gunzip sd.img.gz
    $ ls
      kernel-qemu  sd.img
    Then start the VM, and restart it four more times, to configure and update:
    Code:
    $ qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" -hda sd.img
    => did step 1 of 2
    $ qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" -hda sd.img
    => did step 2 of 2
    $ qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" -hda sd.img
    => Raspbmc Updater started
    => updated kernel
    $ qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" -hda sd.img
    => downloaded stuff
    $ qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" -hda sd.img
    => Raspbmc Updater started
    => Updating service management
    => Downloading new Kodi build
    => Extracting Kodi
    => wait for loop to start: "Relax, Kodi will restart shortly; Press ESC key for a command line"
    Now login, using the terminal, not the QEMU window, and do some maintenance:
    Code:
    $ sudo nano /etc/udev/rules.d/90-qemu.rules
      KERNEL=="sda", SYMLINK+="mmcblk0"
      KERNEL=="sda?", SYMLINK+="mmcblk0p%n"
      KERNEL=="sda2", SYMLINK+="root"
    $ sudo apt-get update
    $ sudo apt-get install parted
    $ sudo shutdown -hP now
    Now boot again to make sure that the image is ready to use:
    Code:
    $ qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" -hda sd.img
    And login using terminal, not the QEMU window:
    Code:
    $ df -ah
      Filesystem  Size  Used  Avail  Use%  Mounted on
      /dev/sda2  1.2G  799M  299M  73%  /
      proc  0  0  0  -  /proc
      devpts  0  0  0  -  /dev/pts
      /dev/sda1  69M  6.4M  63M  10%  /boot
    $ sudo shutdown -hP now
    Now that we have a functional sd.img, we need two copies:
    1) "working.img" to enlarge and work in
    2) "boot.img" to boot from while working on "working.img"
    Code:
    $ cp sd.img working.img
    $ cp sd.img boot.img
    Now we add 5 GB to working.img:
    Code:
    $ qemu-img resize working.img +5G
      Image resized.
    Then we boot from boot.img, with working.img attached as /dev/sdb, so that we can remove the Linux partition information (but not the data) and recreate the partition filling the disk:
    Code:
    $ qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" -hda boot.img -hdb working.img
    And login using terminal, not the QEMU window:
    Code:
    $ sudo su
    # fdisk /dev/sdb
      Command (m for help): p
      Disk /dev/sdb: 6731 MB, 6731857920 bytes
      4 heads, 32 sectors/track, 102720 cylinders, total 13148160 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 identifier: 0x3ac32cfc
      Device Boot  Start  End  Blocks  Id  System
      /dev/sdb1  4096  147455  71680  c  W95 FAT32 (LBA)
      /dev/sdb2  151552  2662399  1255424  83  Linux
      Command (m for help): q
    # parted /dev/sdb
      GNU Parted 2.3
      Using /dev/sdb
      Welcome to GNU Parted! Type 'help' to view a list of commands.
      (parted) unit chs
      (parted) print
      Model: QEMU QEMU HARDDISK (scsi)
      Disk /dev/sdb: 818,110,59
      Sector size (logical/physical): 512B/512B
      BIOS cylinder,head,sector geometry: 818,255,63.  Each cylinder is 8225kB.
      Partition Table: msdos
      Number  Start  End  Type  File system  Flags
      1  0,65,1  9,45,35  primary  fat32  lba
      2  9,110,37  165,185,19  primary  ext4
      (parted) rm 2
      (parted) print
      Model: QEMU QEMU HARDDISK (scsi)
      Disk /dev/sdb: 818,110,59
      Sector size (logical/physical): 512B/512B
      BIOS cylinder,head,sector geometry: 818,255,63.  Each cylinder is 8225kB.
      Partition Table: msdos
      Number  Start  End  Type  File system  Flags
      1  0,65,1  9,45,35  primary  fat32  lba
      (parted) mkpart primary 9,110,37 818,110,59
      (parted) print
      Model: QEMU QEMU HARDDISK (scsi)
      Disk /dev/sdb: 818,110,59
      Sector size (logical/physical): 512B/512B
      BIOS cylinder,head,sector geometry: 818,255,63.  Each cylinder is 8225kB.
      Partition Table: msdos
      Number  Start  End  Type  File system  Flags
      1  0,65,1  9,45,35  primary  fat32  lba
      2  9,110,37  818,110,59  primary  ext4
      (parted) quit
      Information: You may need to update /etc/fstab.
    # shutdown -hP now
    Now we can boot resized working.img, in order to expand the filesystem to fill the partition.
    Code:
    $ qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" -hda working.img
    And login using terminal, not the QEMU window:
    Code:
    $ sudo su
    # df -ah
      Filesystem  Size  Used  Avail  Use%  Mounted on
      /dev/sda2  1.2G  800M  298M  73%  /
      proc  0  0  0  -  /proc
      devpts  0  0  0  -  /dev/pts
      /dev/sda1  69M  6.4M  63M  10%  /boot
    # resize2fs /dev/mmcblk0p2
      resize2fs 1.42.5 (29-Jul-2012)
      Filesystem at /dev/mmcblk0p2 is mounted on /; on-line resizing required
      old_desc_blocks = 1, new_desc_blocks = 1
      The filesystem on /dev/mmcblk0p2 is now 1624576 blocks long.
    # df -ah
      Filesystem  Size  Used  Avail  Use%  Mounted on
      /dev/sda2  6.1G  801M  5.1G  14%  /
      proc  0  0  0  -  /proc
      devpts  0  0  0  -  /dev/pts
      /dev/sda1  69M  6.4M  63M  10%  /boot
    # fdisk /dev/sda
      Command (m for help): p
      Disk /dev/sda: 6731 MB, 6731857920 bytes
      4 heads, 32 sectors/track, 102720 cylinders, total 13148160 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 identifier: 0x3ac32cfc
      Device Boot  Start  End  Blocks  Id  System
      /dev/sda1  4096  147455  71680  c  W95 FAT32 (LBA)
      /dev/sda2  151552  13148159  6498304  83  Linux
      Command (m for help): q
    # shutdown -hP now
    Now we have a Raspian VM with lots of free space to play with :)

     
    Last edited: Feb 8, 2015
Loading...