Location>code7788 >text

How to build a Raspberry Pi virtual environment on Arch Linux

Popularity:943 ℃/2024-07-29 22:22:23

How to build a Raspberry Pi virtual environment on Linux

Let's talk about how to use QEMU to emulate the Raspberry Pi environment. Here first divided into two categories. The first category is to run the older, less secure old Raspberry Pi, mainly refers to the 22 years before the release of the version in April, this version, the Raspberry Pi image of their own internal configuration of a default account password. Later versions do not come with such a default account password. Therefore, we need to distinguish between the two loading modes.

To save effort, we use people's already configured

github repository:qemu-rpi-kernel

Pre-existing Environmental Requirements

yay -S qemu-system-arm

Older Version

Consider the old Raspberry Pi image, installed as follows

  1. clone warehouseqemu-rpi-kernelGo to a location and remove the inside:

    kernel-qemu-5.4.51-buster
    versatile-pb-buster-5.4.
    

    These two files go to a directory folder of your own creation. Mine is oldone

  2. Download the old Raspberry Pi image

    /raspios_lite_armhf/images/raspios_lite_armhf-2020-05-28/
    

    wget or throw the browser and click on it yourself, depending on your interest. At the end of the download unzip to the oldone folder created above

  3. Write a simple script

    qemu-system-arm \
      -M versatilepb \
      -cpu arm1176 \
      -m 256 \
      -drive "file=,
       if=none,index=0,media=disk,format=raw,id=disk0"
      -device "virtio-blk-pci,drive=disk0,disable-modern=on,disable-legacy=off" \
      -net "user,hostfwd=tcp::5022-:22" \
      -dtb versatile-pb-buster-5.4. \
      -kernel kernel-qemu-5.4.51-buster \
      -nographic \ # draw attention to sth.,This is the same as the followingconsole=ttyAMA0use in conjunction with,If you want graphical output, please delete the two lines mentioned.
      -append 'root=/dev/vda2 panic=1 console=ttyAMA0' \
      -no-reboot
    

    I'll start by saying what all these parameters mean.

    -M versatilepb:QEMU support boards have this, this is our Raspberry Pi system platform, choose this to run our Raspberry Pi simulation environment

    -cpu arm1176: choose arm1176 for CPU model.

    -m 256 Memory size is 256MB, I see that some Issue said that the maximum on this, can not be bigger, there will be bugs, I have not been here to test, do not comment!

    The next one is a bit longer: it actually specifies our image file and index format:

    file=: Specifies the image file to be used.

    if=none: Specifies that this drive is not automatically assigned to any interface.

    index=0: Sets the drive index to 0.

    media=disk: Specifies the media type as disk.

    format=raw: Specifies that the format of the image file is raw.

    id=disk0: Assign a unique ID (disk0) to the drive.

    The following one specifies the input and output:

    virtio-blk-pci: Specifies that the device model is a VirtIO block device (a VirtIO block device connected via the PCI bus). a VirtIO device is typically used to improve virtualization performance.

    drive=disk0: Compare this device to the previously defined device with the ID ofdisk0of the drive associated with it.

    disable-modern=on: Disable modern (modern) VirtIO device features. These features usually provide higher performance, but may not be compatible with some older systems.

    disable-legacy=off: Enables the legacy VirtIO device features. These features are typically used for compatibility with older systems.

    -net Configure the network:user: Use the user mode network stack. This is a network configuration method for QEMU, suitable for scenarios that do not require complex network configuration.hostfwd=tcp::5022-:22: Set up a port forwarding rule to forward TCP port 5022 of the host to TCP port 22 of the virtual machine.

    -dtb: Specify the device tree, here use the configured

    -kernel is the kernel used: kernel-qemu-5.4.51-buster

    -no-reboot: prevent it from automatically rebooting when the VM shuts down or crashes (how can we read the logs with it turned off hhh)

    -nographic: do not enable graphical, this depends on the mood to specify, note that this is used together with console=ttyAMA0

    The following --append says to append kernel boot parameters:

    root=/dev/vda2: Specifies the device where the root file system is located. It is assumed that the root file system is located on the second partition of the virtual disk.

    panic=1: Specify automatic reboot after 1 second if the kernel encounters a fatal error.

    console=ttyAMA0: Specify the kernel console output tottyAMA0, which is typically used in serial consoles for ARM architectures.

The following can start up, I want to mention is that, because of this thing on the memory to 256M, it can be said that the start is quite slow, therefore, a little bit of patience, several times I thought it was the configuration hangs repeatedly check until the dinner back to find that the run-through was originally

To summarize, if the viewer wishes to use a non-graphical output, the script that needs to be written is:

qemu-system-arm \
  -M versatilepb \
  -cpu arm1176 \
  -m 256 \
  -drive "file=,
   if=none,index=0,media=disk,format=raw,id=disk0" 
  -device "virtio-blk-pci,drive=disk0,disable-modern=on,disable-legacy=off" \
  -net "user,hostfwd=tcp::5022-:22" \
  -dtb versatile-pb-buster-5.4. \
  -kernel kernel-qemu-5.4.51-buster \
  -nographic \
  -append 'root=/dev/vda2 panic=1 console=ttyAMA0' \
  -no-reboot

The following is all that is needed

chmod 777 <script name>.sh
. /<script name>.sh

Soon it will be a screen.

I recommend using the non-graphical way to boot, the logs are more informative and you can see the progress in real time. The graphical way, on the other hand, can be lacking, leading to some long loads being mistaken for a hung kernel (myself)

This is the end of the screen, the default, under the old version of the account password is: login username pi, the account default password is raspberry, after that you want to change the passwd change on it.

If you want to start it graphically:

qemu-system-arm \
  -M versatilepb \
  -cpu arm1176 \
  -m 256 \
  -drive "file=,
   if=none,index=0,media=disk,format=raw,id=disk0" 
  -device "virtio-blk-pci,drive=disk0,disable-modern=on,disable-legacy=off" \
  -net "user,hostfwd=tcp::5022-:22" \
  -dtb versatile-pb-buster-5.4. \
  -kernel kernel-qemu-5.4.51-buster \
  -append 'root=/dev/vda2 panic=1' \
  -no-reboot


New version launch

Let's play with a new one this time, this time we try to run the latest version of the Raspberry Pi image, with the experience of the old one, things are at our fingertips, but this time we have to be careful, we have to run it non-graphically first (can we not use it, yes, but I'm too lazy to change the file)

This is because we need to be guided to set the account password, and newer versions of the Raspberry Pi OS no longer provide a default account password! The rest is the same! I won't go into it again here