Ken Dai
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    2
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Raspberry Pi Introduction ###### tags: `Linux` ## Raspberry Pi 3 基本介紹 ![](https://i.imgur.com/4T5MNav.png) ![](https://i.imgur.com/7VVP7cB.png) ## 使用 QEMU 模擬 Raspberry Pi ### 取得 QEMU * 安裝 QEMU * `$ sudo apt install qemu` ### 取得 Raspbian Image * 下載 Raspbian disk image 的 zip 檔 * `$ wget https://downloads.raspberrypi.org/raspbian/images/raspbian-2018-11-15/2018-11-13-raspbian-stretch.zip` * 解壓縮 * `$ unzip 2018-11-13-raspbian-stretch.zip` * 解壓縮後的檔名為 `2018-11-13-raspbian-stretch.img` ### 取得 Raspberry Pi Kernel 由於 QEMU 和真實的 Pi 還是有些不同的,因此無法使用 2018-11-13-raspbian-stretch.zip 裡面提供的 kernel 來開機,需要自行編出 QEMU 用的 kernel * 這邊我們使用了 [dhruvvyas90/qemu-rpi-kernel](https://github.com/dhruvvyas90/qemu-rpi-kernel) 所編譯好的 kernel 來作使用 * `$ curl https://raw.githubusercontent.com/dhruvvyas90/qemu-rpi-kernel/master/kernel-qemu-4.4.34-jessie --output kernel-qemu-4.4.34-jessie` ### 執行 QEMU ```SHELL $ sudo qemu-system-arm \ -kernel ./kernel-qemu-4.4.34-jessie \ -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" \ -hda 2018-11-13-raspbian-stretch.img \ -cpu arm1176 \ -m 256 \ -M versatilepb \ -no-reboot \ -serial stdio \ -net nic \ -net user \ -net tap,ifname=vnet0,script=no,downscript=no ``` ## 使用 QEMU 模擬 Raspberry Pi 3 ### 編譯 QEMU 由於使用 apt 工具安裝的 QEMU 並不支援 Pi 3,因此我們要自己手動編譯 2.12.0 版本的 QEMU * 安裝相關工具 * `$ sudo apt install gcc build-essential automake gcc-arm-linux-gnueabihf` * 下載 QEMU source code * `$ wget https://download.qemu.org/qemu-2.12.0-rc3.tar.xz` * 解壓縮 * `$ tar xvf qemu-2.12.0-rc3.tar.xz` * 配置設定 * `$ cd qemu-2.12.0-rc3` * `$ ./configure --target-list=arm-softmmu,aarch64-softmmu` * 相關設定 * `--target-list`: 指令想要編譯的模擬器 * `--prefix`: 指定程式要安裝的路徑 * 其他設定與選項皆可從 `./configure --help` 得知 * 編譯及安裝 * `$ make -j$(nproc)` * `$ make install` * 設定路徑 * `$ echo "export PATH=$(pwd)/aarch64-softmmu:\$PATH" >> ~/.bashrc` * `$ source ~/.bashrc` ### 取得 Raspbian Image * 下載 Raspbian disk image 的 zip 檔 * `$ wget https://downloads.raspberrypi.org/raspbian/images/raspbian-2018-11-15/2018-11-13-raspbian-stretch.zip` * 解壓縮 * `$ unzip 2018-11-13-raspbian-stretch.zip` * 解壓縮後的檔名為 `2018-11-13-raspbian-stretch.img` ### 取得 Raspberry Pi Kernel 由於 QEMU 和真實的 Pi 還是有些不同的,因此無法使用 2018-11-13-raspbian-stretch.zip 裡面提供的 kernel 來開機,需要自行編出 QEMU 用的 kernel,而在這邊參考了 [dhruvvyas90/qemu-rpi-kernel](https://github.com/dhruvvyas90/qemu-rpi-kernel/tree/master/tools) 的編譯方式 * 下載 kernel source code * `$ git clone git://github.com/raspberrypi/linux.git --branch raspberrypi-kernel_1.20180619-1 --single-branch --depth 1` * 修正編譯上的錯誤 * 到 [dhruvvyas90/qemu-rpi-kernel](https://github.com/dhruvvyas90/qemu-rpi-kernel/tree/master/tools) 上取得 `linux-arm.patch` * 打上 patch * `$ cd linux` * `$ patch -p1 < ./linux-arm.patch` * 編譯 kernel * 編譯 kernel7.img * `$ KERNEL=kernel7` * 初始化 config * `$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- versatile_defconfig` * 設定 config * 到 [dhruvvyas90/qemu-rpi-kernel](https://github.com/dhruvvyas90/qemu-rpi-kernel/tree/master/tools) 上取得 `config_file` 和 `config_ip_tables` * 添加 config * ```SHELL $ cat >> .config << EOF CONFIG_CROSS_COMPILE="arm-linux-gnueabihf" EOF ``` * `$ cat ./config_file >> .config` * `$ cat ./config_ip_tables >> .config` * 開啟 menuconfig * `$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- menuconfig` * 編譯 kernel 和 dtb * `$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bzImage dtbs` * 設定路徑 * `$ cd ..` * `$ cp linux/arch/arm/boot/zImage ./kernel7.img` * `$ cp linux/arch/arm/boot/dts/versatile-pb.dtb ./versatile-pb.dtb` ### 執行 QEMU * 使用圖形介面 ```SHELL $ sudo qemu-system-arm \ -M versatilepb \ -cpu arm1176 \ -m 256 \ -hda 2018-11-13-raspbian-stretch.img \ -kernel kernel7.img \ -dtb versatile-pb.dtb \ -net nic \ -net user,hostfwd=tcp::5022-:22 \ --append "root=/dev/sda2 panic=1" \ -no-reboot ``` * 使用終端機介面 ```SHELL $ sudo qemu-system-arm \ -M versatilepb \ -cpu arm1176 \ -m 256 \ -hda 2018-11-13-raspbian-stretch.img \ -kernel kernel7.img \ -dtb versatile-pb.dtb \ -net nic \ -net user,hostfwd=tcp::5022-:22 \ --append "rw console=ttyAMA root=/dev/sda2 panic=1" \ -nographic \ -no-reboot ``` * Raspberry Pi 預設帳號密碼 * 帳號︰`pi` * 密碼︰`raspberry` * 離開 QEMU * `Ctrl-A + X` ## 如何除錯 Raspberry Pi 3 虛擬機器 ### 掛載 image 檔 有時候想要不藉由開機,希望直接從外面改裡面的程式,故使用之 * 使用 `losetup` 設定 loop device * `$ losetup -P -f --show XXX.img` * 查看 loop device 狀態 * `$ losetup -l` ``` NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE DIO LOG-SEC /dev/loop0 0 0 0 0 XXX.img 0 512 ``` * 掛載 image 檔 * `$ mkdir one two` * `$ sudo mount -o loop /dev/loop0p1 ./one` * `$ sudo mount -o loop /dev/loop0p2 ./two` * 若忘記掛載到哪裡了可以使用 `mount -l` 查看 ### 使用 GDB 除錯 若發生進不去 OS 的情況 (e.g. Kernel panic, ...),就需要藉由外部 debugger 找尋錯誤 * 說明︰QEMU 內建了一個 GDBServer,可與外部的 GDB 進行一個遠端的除錯,其中藉由 ip:port 網路方式或藉由串列 /dev/ttyS* 的方式來進行工作 * 如何使用 * 在 `qemu-system-*` 後面添加 `-S -gdb tcp::1234` * 使用 `arm-linux-gnueabihf-gdb -q` 進入 GDB * 輸入 `(gdb) target remote localhost:1234` 即可 ## 參考資料 * [Linux 電腦第一次玩就上手:Raspberry Pi 3 入門套裝開箱](https://www.techapple.com/archives/17045) * [第一次玩樹莓派就上手 - Raspberry PI 3 model B 新手上路](https://leonxlin.pixnet.net/blog/post/329986608) * [Maker入門手冊-Raspberry Pi 3 安裝及環境建置](http://blog.tibame.com/?p=3016) * [樹莓派 Raspberry Pi 3 介紹與使用](https://lrs.itsa.org.tw/pluginfile.php/23381/mod_resource/content/1/學習手冊-單元一_樹莓派%20Raspberry-Pi3%20介紹與使用.pdf) --- * [QEMU 编译安装](https://www.cnblogs.com/BinBinStory/p/7618303.html) * [从源码编译 Qemu 的流程及简述](http://blog.jcix.top/2017-01-09/qemu_compiling/) * [QEMU 2.10.1 编译安装](https://blog.csdn.net/candcplusplus/article/details/78320602) --- * [Raspbian "stretch" for Raspberry Pi 3 on QEMU ](https://github.com/wimvanderbauwhede/limited-systems/wiki/Raspbian-%22stretch%22-for-Raspberry-Pi-3-on-QEMU) * [Debian "buster" for Raspberry Pi 3 on QEMU](https://github.com/wimvanderbauwhede/limited-systems/wiki/Debian-%22buster%22-for-Raspberry-Pi-3-on-QEMU) * [使用 qemu 虚拟机运行树莓派(linux kernel 4.9)](https://blog.csdn.net/talkxin/article/details/79505826) * [Qemu kernel for emulating Rpi on QEMU](https://github.com/dhruvvyas90/qemu-rpi-kernel) * [RPI3 QEMU](https://www.raspberrypi.org/forums/viewtopic.php?t=195565) * [Debian on QEMU’s Raspberry Pi 3 model](https://translatedcode.wordpress.com/2018/04/25/debian-on-qemus-raspberry-pi-3-model/) * [Using QEMU to emulate a Raspberry Pi](https://blog.agchapman.com/using-qemu-to-emulate-a-raspberry-pi/) * [使用 qemu 來模擬 Raspberry Pi](http://guildwar23.blogspot.com/2012/11/qemu-raspberry-pi.html) --- * [How can I mount a Raspberry Pi Linux distro image ?](https://raspberrypi.stackexchange.com/questions/13137/how-can-i-mount-a-raspberry-pi-linux-distro-image) --- * [樹莓派 Raspberry Pi 編譯 Linux 核心(Kernel)步驟教學](https://blog.gtwang.org/iot/raspberry-pi-compile-linux-kernel) * [wimvanderbauwhede / limited-systems](https://github.com/wimvanderbauwhede/limited-systems) * [Chapter 2. Compiling a kernel - Kernel config options for kgdb](https://www.kernel.org/doc/htmldocs/kgdb/CompilingAKernel.html) * [Debugging the Linux Kernel](http://ownyourbits.com/2018/05/09/debugging-the-linux-kernel/) * [LDD3 - Chapter 4. Debugging Techniques](http://static.lwn.net/images/pdf/LDD3/ch04.pdf) * [gdbserver remote debug 測試](https://kezeodsnx.pixnet.net/blog/post/31901130) * [編譯 linux 0.11,並且使用 QEMU + GDB 調試 kernel](https://wwssllabcd.github.io/blog/2012/08/03/compile-linux011/) * [使用 QEMU 和 GDB 调试 Linux 内核](https://consen.github.io/2018/01/17/debug-linux-kernel-with-qemu-and-gdb/) * [Debugging linux kernel with GDB and QEMU](https://yulistic.gitlab.io/2018/12/debugging-linux-kernel-with-gdb-and-qemu/) * [Linux kernel 調試](https://www.binss.me/blog/how-to-debug-linux-kernel/)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully