從 chroot 啟動 Linux 系統:有更好的方法嗎?

從 chroot 啟動 Linux 系統:有更好的方法嗎?

我想設定 Grub 選單項目來啟動到 chroot 系統(安裝 chroot 的 debootstrap 以避免過度接觸現有系統)。

目前我做了以下事情來實現它:

  1. 在 chroot 環境中安裝 linux-image 和朋友

  2. 手動重新管理 initramfs 以 chroot 到系統中,而不是通常的行為:

rootmnt=$rootmnt/root/squeeze
...
#exec run-init ${rootmnt} ${init} "$@" <${rootmnt}/dev/console >${rootmnt}/dev/console
exec chroot ${rootmnt} ${init} "$@"  <${rootmnt}/dev/console >${rootmnt}/dev/console

3.. 將條目新增至/boot/grub.cfg:

menuentry 'Chrooted debian Squeeze' {
    ...
    linux   /root/squeeze/boot/vmlinuz root=... rw
    initrd  /root/squeeze/boot/initrd-chroot
}

它可以工作,但設定起來並不容易,而且每次更改 initrd 時都需要手動修改。怎樣才能做得更好?

答案1

我遇到了同樣的問題並結束了寫這個讓它在不同的系統上輕鬆工作(目前是 debian、ubuntu):

執行make_chroot_initrd腳本以從現有映像建立一個新的啟用 chroot 的 initrd 映像:

#  ./make_chroot_initrd /chroot/trusty/boot/initrd.img-3.13.0-32-generic
making new initrd: /chroot/trusty/boot/initrd.img-3.13.0-32-generic.chroot

新映像將完全相同,只是現在它可以處理chroot=啟動參數。

使用 grub2 作為引導程序,您可以添加一個條目/boot/grub/grub.cfg:(
或者也許更好/etc/grub.d/40_custom

menuentry "ubuntu trusty, (linux 3.13.0-32) (chroot)" {
    insmod ext2                       # or whatever you're using ...
    set root='(hd0,7)'                # partition containing the chroot
    set chroot='/chroot/trusty'       # chroot path
    linux   $chroot/boot/vmlinuz-3.13.0-32-generic root=/dev/sda7 chroot=$chroot rw
    initrd  $chroot/boot/initrd.img-3.13.0-32-generic.chroot
}

(更改檔案/分割區以符合您的)

系統範圍內的安裝

一旦您對此感到滿意,您就可以將變更永久化
(直到 initramfs-tools 軟體包升級)。
在 chroot 系統中:

# cd /usr/share/initramfs-tools
# cp -pdrv .  ../initramfs-tools.orig       # backup
# patch -p1 < path_to/boot_chroot/initrd.patch
# rm *.orig */*.orig
# update-initramfs -u

從現在開始,常規 initrd 映像將支援 chroot 啟動。
無需使用單獨的 initrd.chroot,否則可能會與其不同步。

啟動chroot了解詳情。

答案2

為什麼從 run-init 切換到 chroot?你不應該這樣做。 run-init 刪除 initramfs 根目錄中的所有內容,然後 chroots 到 $rootmnt。你想保持這種行為。

至於如何避免每次都手動重建 initramfs,請在 /usr/share/initramfs-tools 中編輯 init 腳本的主副本。在升級 initramfs-tools 軟體包之前,這至少應該有效。

一個永久的解決方案是修補 init 腳本以識別引導參數以將某些內容附加到 rootmnt,並提交該修補程式以包含到 debian 中。然後,您可以將參數新增至 grub 以取得應以這種方式引導的條目。

相關內容