在 Debian 內, 安裝 Debian Linux 到 USB 隨身碟

這篇的標題看起來很怪, 主要意思是 在 Debian Linux 的環境內, 直接 Mount 一個 Partition, 來灌新的 Debian 作業系統. (將作業系統灌到另一個 Partition, 然後將開機指向新系統即可)

此篇是想作業系統安裝在 USB 隨身碟上, 但是光碟機掛了, 只好這樣子安裝.(如果光碟機正常, 建議使用光碟來安裝比較簡單~)

除此之外, 遠端重灌系統也可以用此方法來做.

硬體規格

  • USB 隨身碟 16G (/dev/sda) - TDK Trans-IT Edge USB 2.0 16GB (2009/11/16 購入)

需求

  • 將 USB 隨身碟插上音響, 自動播放音樂(音響支援 USB 自動播放).
  • 可以接 Windows 系統, 臨時存取資料.
  • Linux 系統開機, 出外時臨時有事可用. (註: 要可以隨時安裝新的 Package)

磁碟分割規劃

由上述需求, 所以分割成下述:

  • sda1 500M => 音響只讀第一個 Partition (我家音響是這樣, 其它的不確定)
  • sda2 5G => 臨時用
  • sda3 9800M => Debian Linux
  • sda4 Linux swap

於 Debian Linux 安裝 Debian

下述都使用 root 執行: sudo su -

磁碟分割
  • fdisk -l /dev/sda # 此步驟請自行分割, 分割完成, 可將 USB 拔出, 再插入重新讀取 Partition, 即可繼續執行下述步驟.

    Disk /dev/sda: 16.0 GB, 16009658368 bytes
    255 heads, 63 sectors/track, 1946 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Disk identifier: 0x0004a6a9

    Device Boot      Start         End      Blocks   Id  System
    /dev/sda1               1          65      522081    c  W95 FAT32 (LBA) # 500M
    /dev/sda2              66         719     5253255    c  W95 FAT32 (LBA) # 5G
    /dev/sda3   *         720        1931     9735390   83  Linux           # 9800M
    /dev/sda4            1932        1946      120487+  82  Linux swap / Solaris # other

格式化 / 安裝系統建制需要的套件
  1. apt-get install debootstrap mtools # debootstrap - Bootstrap a basic Debian system
  2. mkfs.ext3 /dev/sda3 # 格式化
  3. mkswap /dev/sda4 # swap 建立
  4. sync; sync; sync
  5. swapon /dev/sda4 # 啟用 swap
掛載 Partition
  1. mkdir /mnt/usb
  2. mount /dev/sda3 /mnt/usb
於 /mnt/usb 建立基本系統環境
  1. /usr/sbin/debootstrap --arch i386 squeeze /mnt/usb http://ftp.us.debian.org/debian # alpha, amd64, arm, armel, hppa, i386, ia64, m68k, mips, mipsel, powerpc, s390, or sparc.
Create device file
  1. cd /mnt/usb/dev
  2. MAKEDEV generic
進入 chroot mode
  1. LANG=C chroot /mnt/usb /bin/bash # chroot /mnt/usb 是把 /mnt/usb 當 "/", 已經與之前外面的系統切開了.
  2. export TERM=xterm-color
  3. vim /etc/fstab

    # /etc/fstab: static file system information.
    #
    # file system    mount point   type    options                  dump pass
    proc             /proc         proc    defaults                 0    0
    /dev/sda3        /             ext3    defaults                 0    1
    /dev/sda4        none          swap    sw                       0    0
    /dev/cdrom       /media/cdrom  iso9660 noauto,ro,user,exec      0    0

  4. mount -a # 全部掛載起來
建立 cdrom
  1. cd /media
  2. mkdir cdrom0
  3. ln -s cdrom0 cdrom
  4. cd /
  5. ln -s media/cdrom
建立 proc
  • mount -t proc proc /proc # 或 離開 chroot, 於 外部執行: mount -t proc proc /mnt/usb/proc
設定 Timezone
  1. vim /etc/default/rcS

    #
    # /etc/default/rcS
    #
    # Default settings for the scripts in /etc/rcS.d/
    #
    # For information about these variables see the rcS(5) manual page.
    #
    # This file belongs to the "initscripts" package.
    TMPTIME=0
    SULOGIN=no
    DELAYLOGIN=no
    UTC=yes
    VERBOSE=no
    FSCKFIX=no
    RAMRUN=no
    RAMLOCK=no

  2. dpkg-reconfigure tzdata # 時區選擇
設定網路
  1. vim /etc/network/interfaces

    ######################################################################
    # /etc/network/interfaces -- configuration file for ifup(8), ifdown(8)
    # See the interfaces(5) manpage for information on what options are
    # available.
    ######################################################################

    # We always want the loopback interface.
    #
    auto lo
    iface lo inet loopback

    # To use dhcp:
    #
    auto eth0
    iface eth0 inet dhcp

    # An example static IP setup: (broadcast and gateway are optional)
    #
    # auto eth0
    # iface eth0 inet static
    #     address 192.168.0.20
    #     network 192.168.0.0
    #     netmask 255.255.255.0
    #     broadcast 192.168.0.255
    #     gateway 192.168.0.254

  2. vi /etc/resolv.conf

    nameserver 8.8.8.8
    nameserver 8.8.4.4
    nameserver 168.95.1.1

設定主機名稱
  • echo 'debian-usb' > /etc/hostname
設定 hosts
  • vi /etc/hosts # 下述 debian-usb 請置換成與 /etc/hostname 一樣

    127.0.0.1 localhost debian-usb

    # The following lines are desirable for IPv6 capable hosts
    ::1     ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    ff02::3 ip6-allhosts

設定 APT Repository 位置
  • vim /etc/apt/sources.list

    deb http://ftp.us.debian.org/debian squeeze main non-free contrib
    deb-src http://ftp.us.debian.org/debian squeeze main non-free contrib

    deb http://security.debian.org/ squeeze/updates main non-free contrib
    deb-src http://security.debian.org/ squeeze/updates main non-free contrib

使用 APT 安裝系統
  1. apt-get update
  2. apt-get install locales # Locales
  3. dpkg-reconfigure locales # 選 zh_TW.UTF-8
  4. apt-get install console-data
  5. dpkg-reconfigure console-data
  6. apt-cache search linux-image # Kernel, 先找找有哪些版本的可以裝
  7. vi /etc/kernel-img.conf

    # Kernel image management overrides
    # See kernel-img.conf(5) for details
    do_symlinks = yes
    relative_links = yes
    do_bootloader = yes
    do_bootfloppy = no
    do_initrd = yes
    link_in_boot = no

  8. apt-get install linux-image-2.6.30-2-686 # 安裝 Kernel
  9. apt-get install grub grub-pc # 安裝 Grub 開機
設定開機
  1. vi /boot/grub/device.map # (hd0) = /dev/sda

    (hd0) /dev/sda

  2. grub-install /dev/sda
  3. update-grub
設定 Kernel 變更, 自動更新 Grub
  • vi /etc/kernel-img.conf # 於 Kernel 安裝 / 更新 時, 自動執行 update-grub

    # 增加下述
    postinst_hook = update-grub
    postrm_hook   = update-grub

安裝標準系統
  1. tasksel install standard # 選擇 預設標準套件安裝
  2. apt-get dist-upgrade
  3. apt-get clean
設定帳號, 密碼
  1. passwd # 設定 root 密碼
  2. adduser user # 新增使用者
  3. apt-get install ssh sudo pppoe # 以防外出環境需要 pppoe 撥接

上述都做完就可以 reboot, 用此隨身碟開機囉~ (註: 上述設定若嫌複雜, 可以先執行 /usr/sbin/base-config, 再把沒設到的補上即可.)

相關文章

作者: Tsung

對新奇的事物都很有興趣, 喜歡簡單的東西, 過簡單的生活.

在〈在 Debian 內, 安裝 Debian Linux 到 USB 隨身碟〉中有 4 則留言

  1. 把usb mount起来后,直接cp -a本机大部分档案过去。最后mkdir proc sys tmp什么的一把,再chmod +t tmp。grub-install什么的,结束走人。
    我灌过一个这样的debian。

  2. 嗯嗯, 不過目前自己在用的環境已經裝太多東西了, 所以挑選、複製的時間可能不太划算就是了. XD
    很感謝您的建議~ 🙂

scott lin 發表迴響取消回覆

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料