Skip to content

Latest commit

 

History

History
166 lines (111 loc) · 5.18 KB

使用 debootstrap 建立完整的 Debian 系統.org

File metadata and controls

166 lines (111 loc) · 5.18 KB

使用 debootstrap 建立完整的 Debian 系統

在製作嵌入式系統的過程中,常常會儘可能精簡整個系統,因此將 gdb、strace 等除錯的好幫手拿掉。如果精簡的 Linux 系統出了問題,要怎進行除錯呢? debootstrap + chroot 是我最常用的組合。

debootstrap 是 Debian 提供用於建立迷你 Debian 系統的方案,使用他可以在你 所指定的目錄下安裝一個基本的 Debian 系統,除了一些配置以外,其內容與 使用 Debian 安裝光碟第一階段安裝的內容基本相同。除此之外,你也可以指定 產生出來的 Debian 架構,使用 debootstrap 產生出 armel、armhf、mips 等不同 的 CPU 架構的 Debian 系統,再透過 qemu chroot 進行訂製,從而產生自己需 要的 Debian Linux 版本以及 Live USB、Live CD 等工具。

安裝 debootstrap

debootstrap 在任何 Debian 系列的 Linux 皆可隨手取得,若您是 Gentoo Linux 的 用戶,您也可以在 Portage 系統中找到 debootstrap 的 ebuild。

  • Debian / Ubuntu
    sudo apt-get install debootstrap
        
  • Gentoo
    sudo emerge debootstrap
        

建立 x86/amd64 的 Debian 系統

由於 chroot 需要 root 權限 ,下面命令皆為 root 使用者所執行的命令,若 使用一般帳號,請在命令前加上 sudo

要建立和主系統相同架構下的 Debian 系統,是非常簡單的事情。首先我們先創建一個 rootfs-debian 目錄。

mkdir rootfs-debian

接著使用下面這行 debootstrap 命令,Debian 就會安裝到 /rootfs-deebian 下了

debootstrap --arch i386 sid rootfs-debian http://debian.linux.org.tw/debian

整個 debootstrap 的命令架構如下

debootstrap --arch <ARCH> <VERSION> <DIRECTORY>  <MIRROR>
  • ARCH

    目標系統的 CPU 架構,常用的有 i386、amd64、armel、armhf 等。

  • VERSION

    Debian 的版本,你可以使用目前的穩定版本 wheezy ,或是 永遠的測試版 sid ,當然你也可以選擇更不穩定的 testing ,詳細版本名 稱請見 Debian 官網。

  • DIRECTORY

    安裝的目錄,這個根據自己的需求設定即可

  • MIRROR

    下載 Debian 套件的伺服器,通常選擇該使用者區域內的服務器,以下為台灣的 Debian Mirror

    http://ftp.tw.debian.org/debian/

修改你的 Debian 系統

剛建立好的 Debian Rootfs 還是有一些需要你去修改的檔案,比如說 /etc/fstab/etc/inittab 等。

如果你使用 debootstrap 建立 Debian rootfs 的目的是進行 chroot, 你只需要修改 /etc/apt/source.list 即可。

一開始 debootstrap 建立出來的 /etc/apt/source.list 是一個空白檔案,你 需要自己去添加自己想要的 debian mirror,debian mirror 取得的方式可以參 考 Debian Source List Generator ,以下是我常添加的資訊 (for unstable version)。

deb http://ftp.tw.debian.org/debian unstable main contrib
deb-src http://ftp.tw.debian.org/debian unstable main contrib

將你的 Debian 安裝到 USB 隨身碟

如果你使用 debootstrap 的目的,是建立一個輕巧的 Debian 隨身碟,則除了 將剛剛所產生的檔案移到隨身碟下,你同時還要進行以下步驟。

為了簡化說明,這裡假設你的 USB disk 只切割成一個分割區。

  • 1. 產生基本的 Debian Root File System

    請參照前面說明產生 Debian RootFS 到你的隨身碟上

  • 2. 修改 /etc/fstab

    根據你的需求修改 /etc/fstab 檔案,以下為範例

    /dev/sda1  /         etx4 defaults 0 1
    proc       /proc     proc none     0 0
        
  • 3. 掛載系統資訊,並 chroot 進去

    假設你的 usb 隨身碟掛載於系統的 /mnt/usbdisk 上,則你需要再掛載一些 系統目錄以方便使用 chroot

    mount -t proc none /mnt/usbdisk/proc
    mount -o bind dev /mnt/usbdisk/dev
        

    接著使用 chroot 切換到 Debian 系統上

    chroot /mnt/usbdisk
        
  • 4. 更新系統,安裝 grub 與 kernel image
    apt-get update
    apt-get install linux-image-generic
    apt-get install grub
        
  • 5. 將 grub 裝到 MBR 上
  • 6. 進行一些其他雜七雜八的設定

    TODO: 修改 root 密碼

  • 7. 完成收工

建立 ARM 平台的 Debian 系統

參考連結

[1] 通過 debootstrap 安裝 Debian

[2] 使用 debootstrap 安裝 Debian 於 USB 大拇哥

[3] EmDebian/CrossDebootstrap - Debian Wiki

[4] Debootstrap BlankBerry pada Gentoo