如何使用 GRUB 2 直接从硬盘运行 ISO 文件
grub iso 启动
![](/data/attachment/album/201510/18/094435clq6dy8jsvceze6l.png)


大多数 Linux 发行版都会提供一个可以从 USB 启动的 live 环境，以便用户无需安装即可测试系统。我们可以用它来评测这个发行版或仅仅是当成一个一次性系统，并且很容易将这些文件复制到一个 U 盘上，在某些情况下，我们可能需要经常运行同一个或不同的 ISO 镜像。GRUB 2 可以配置成直接从启动菜单运行一个 live 环境，而不需要烧录这些 ISO 到硬盘或 USB 设备。


### 获取和检查可启动的 ISO 镜像


为了获取 ISO 镜像，我们通常应该访问所需的发行版的网站下载与我们架构兼容的镜像文件。如果这个镜像可以从 U 盘启动，那它也应该可以从 GRUB 菜单启动。


当镜像下载完后，我们应该通过 MD5 校验检查它的完整性。这会输出一大串数字与字母合成的序列。


![](/data/attachment/album/201510/18/094435jfzdcd1otefsdj1o.png)


将这个序列与下载页提供的 MD5 校验码进行比较，两者应该完全相同。


### 配置 GRUB 2


ISO 镜像文件包含了整个系统。我们要做的仅仅是告诉 GRUB 2 哪里可以找到 kernel 和 initramdisk 或 initram 文件系统（这取决于我们所使用的发行版）。


在下面的例子中，一个 Kubuntu 15.04 live 环境将被配置到 Ubuntu 14.04 机器的 Grub 启动菜单项。这应该能在大多数新的以 Ubuntu 为基础的系统上运行。如果你是其它系统并且想实现一些其它的东西，你可以从[这些文件](http://git.marmotte.net/git/glim/tree/grub2)了解更多细节，但这会要求你拥有一点 GRUB 使用经验。


这个例子的文件 `kubuntu-15.04-desktop-amd64.iso` 放在位于 `/dev/sda1` 的 `/home/maketecheasier/TempISOs/` 上。


为了使 GRUB 2 能正确找到它，我们应该编辑



```
/etc/grub.d40-custom

```

![](/data/attachment/album/201510/18/094436h1yhby1hhxyozfh8.png)



```
menuentry "Kubuntu 15.04 ISO" {
set isofile="/home/maketecheasier/TempISOs/kubuntu-15.04-desktop-amd64.iso"
loopback loop (hd0,1)$isofile
echo "Starting $isofile..."
linux (loop)/casper/vmlinuz.efi boot=casper iso-scan/filename=${isofile} quiet splash
initrd (loop)/casper/initrd.lz
}

```

![](/data/attachment/album/201510/18/094436m1k6oicxcod5w6iw.png)


### 分析上述代码


首先设置了一个变量名 `$menuentry` ，这是 ISO 文件的所在位置 。如果你想换一个 ISO ，你应该修改 `isofile="/path/to/file/name-of-iso-file-.iso"`.


下一行是指定回环设备，且必须给出正确的分区号码。



```
loopback loop (hd0,1)$isofile

```

注意 hd0,1 这里非常重要，它的意思是第一硬盘，第一分区 (`/dev/sda1`)。


GRUB 的命名在这里稍微有点困惑，对于硬盘来说，它从 “0” 开始计数，第一块硬盘为 #0 ，第二块为 #1 ，第三块为 #2 ，依此类推。但是对于分区来说，它从 “1” 开始计数，第一个分区为 #1 ，第二个分区为 #2 ，依此类推。也许这里有一个很好的原因，但肯定不是明智的（明显用户体验很糟糕）..


在 Linux 中第一块硬盘，第一个分区是 `/dev/sda1` ，但在 GRUB2 中则是 `hd0,1` 。第二块硬盘，第三个分区则是 `hd1,3`, 依此类推.


下一个重要的行是：



```
linux (loop)/casper/vmlinuz.efi boot=casper iso-scan/filename=${isofile} quiet splash

```

这会载入内核镜像，在新的 Ubuntu Live CD 中，内核被存放在 `/casper` 目录，并且命名为 `vmlinuz.efi` 。如果你使用的是其它系统，可能会没有 `.efi` 扩展名或内核被存放在其它地方 (可以使用归档管理器打开 ISO 文件在 `/casper` 中查找确认)。最后一个选项， `quiet splash` ，是一个常规的 GRUB 选项，改不改无所谓。


最后



```
initrd (loop)/casper/initrd.lz

```

这会载入 `initrd` ，它负责载入 RAMDisk 到内存用于启动。


### 启动 live 系统


做完上面所有的步骤后，需要更新 GRUB2：



```
sudo update-grub

```

![](/data/attachment/album/201510/18/094436h5zl1nvplhlommm7.png)


当重启系统后，应该可以看见一个新的、并且允许我们启动刚刚配置的 ISO 镜像的 GRUB 条目：


![](/data/attachment/album/201510/18/094437n49lir14269ab692.png)


选择这个新条目就允许我们像从 DVD 或 U 盘中启动一个 live 环境一样。




---


via: <https://www.maketecheasier.com/run-iso-files-hdd-grub2/>


作者：[Attila Orosz](https://www.maketecheasier.com/author/attilaorosz/) 译者：[Locez](https://github.com/locez) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Most Linux distros offer a live environment, which you can boot up from a USB drive, for you to test the system without installing. You can either use it to evaluate the distro or as a disposable OS. While it is easy to copy these onto a USB disk, in certain cases one might want to run the same ISO image often or run different ones regularly. GRUB 2 can be configured so that you do not need to burn the ISOs to disk or use a USB drive, but need to run a live environment directly form the boot menu.

## Obtaining and checking bootable ISO images

To obtain an ISO image, you should usually visit the website of the desired distribution and download any image that is compatible with your setup. If the image can be started from a USB, it should be able to start from the GRUB menu as well.

Once the image has finished downloading, you should check its integrity by running a simple md5 check on it. This will output a long combination of numbers and alphanumeric characters

which you can compare against the MD5 checksum provided on the download page. The two should be identical.

## Setting up GRUB 2

ISO images contain full systems. All you need to do is direct GRUB2 to the appropriate file, and tell it where it can find the kernel and the initramdisk or initram filesystem (depending on which one your distribution uses).

In this example, a Kubuntu 15.04 live environment will be set up to run on an Ubuntu 14.04 box as a Grub menu item. It should work for most newer Ubuntu-based systems and derivatives.

In this example the file `kubuntu-15.04-desktop-amd64.iso`


lives in `/home/maketecheasier/TempISOs/`

on `/dev/sda1`

.

To make GRUB2 look for it in the right place, you need to edit the

/etc/grub.d/40_custom

file which allows you to add your own menu entries. The file should already exist and contain a few lines.

To start Kubuntu from the above location, add the following code (after adjusting it to your needs) below the commented section, without modifying the original content.

menuentry "Kubuntu 15.04 ISO" { set isofile="/home/maketecheasier/TempISOs/kubuntu-15.04-desktop-amd64.iso" loopback loop (hd0,1)$isofile echo "Starting $isofile..." linux (loop)/casper/vmlinuz.efi boot=casper iso-scan/filename=${isofile} quiet splash initrd (loop)/casper/initrd.lz }

## Breaking down the above code

First set up a variable named `$menuentry`

. This is where the ISO file is located. If you want to change to a different ISO, you need to change the bit where it says set `isofile="/path/to/file/name-of-iso-file-.iso"`

.

The next line is where you specify the loopback device; you also need to give it the right partition number. This is the bit where it says

loopback loop (hd0,1)$isofile

Note the `hd0,1`

bit; it is important. This means first HDD, first partition (`/dev/sda1`

).

GRUB’s naming here is slightly confusing. For HDDs, it starts counting from “0”, making the first HDD #0, the second one #1, the third one #2, etc. However, for partitions, it will start counting from 1. First partition is #1, second is #2, etc. There might be a good reason for this but not necessarily a sane one (UX-wise it is a disaster, to be sure)..

This makes fist disk, first partition, which in Linux would usually look something like `/dev/sda1`

become `hd0,1`

in GRUB2. The second disk, third partition would be `hd1,3`

, and so on.

The next important line is

linux (loop)/casper/vmlinuz.efi boot=casper iso-scan/filename=${isofile} quiet splash

It will load the kernel image. On newer Ubuntu Live CDs, this would be in the `/casper`

directory and called `vmlinuz.efi`

. If you use a different system, your kernel might be missing the `.efi`

extension or be located somewhere else entirely (You can easily check this by opening the ISO file with an archive manager and looking inside `/casper.`

). The last options, `quiet splash,`

would be your regular GRUB options, if you care to change them.

Finally

initrd (loop)/casper/initrd.lz

will load `initrd`

, which is responsible to load a RAMDisk into memory for bootup.

## Booting into your live system

To make it all work, you will only need to update GRUB2

`sudo update-grub`

When you reboot your system, you should be presented with a new GRUB entry which will allow you to load into the ISO image you’ve just set up.

Selecting the new entry should boot you into the live environment, just like booting from a DVD or USB would.

Our latest tutorials delivered straight to your inbox