怜君 發表於 2024-9-18 11:55:00

制作基于debian的linux live启动u盘(UEFI)

<h4 id="参考">参考</h4>
<ul>
<li>从零开始制作一个linux iso镜像 _</li>
<li>详解:把 Linux 系统做成 Livecd</li>
<li>从零制作 Ubuntu 20.04 LiveCD</li>
<li>LiveCDCustomization(ubuntu官方文档)</li>
<li>从零开始制作 Ubuntu 22.04 Live CD</li>
<li>Create a Custom Debian Live Environment (CD or USB)</li>
</ul>
<h2 id="方法一-live-build">方法一: <code>live-build</code></h2>
<p>这是debian官方的live镜像构建工具,此方法相对第二种更简单</p>
<ol>
<li>安装:<code>apt install live-build</code></li>
<li>新建一个空文件夹,比如<code>liveb</code>,cd进去</li>
<li>进行配置:<code>lb config --mirror-binary https://mirrors.ustc.edu.cn/debian/ \ --mirror-binary-security https://mirrors.ustc.edu.cn/debian-security/ \ --mirror-bootstrap https://mirrors.ustc.edu.cn/debian/ \ --mirror-chroot-security https://mirrors.ustc.edu.cn/debian-security/ \ --distribution bookworm</code><br>
这里的lb就是live-build的简写,这个命令使用了中科大的镜像,不使用镜像会很慢(此处镜像是一会使用lb build下载文件时的镜像),<code>--distribution</code>指定了debian的版本,也可以选择bullseye等别的版本,我选了debian12</li>
<li>此时可以自定义软件包,在<code>liveb/config/package-lists/</code>下面新建文件:<code>[随便命名].list.chroot</code>里面写上要装的软件包,比如:</li>
</ol>
<pre><code>vim
network-manager
htop
</code></pre>
<ul>
<li>此方法构建的<code>liveb/chroot</code>目录,直接chroot进去会不能联网,要手动配置<code>/etc/resolv.conf</code>,所以这里我直接在配置文件里写好要安装的软件包</li>
</ul>
<ol start="5">
<li>在<code>liveb</code>中执行<code>lb build</code>(需要root),一般十几分钟就会完成构建,成功的话有绿色提示,在<code>liveb</code>根目录下有生成的iso文件,<code>liveb/binary</code>下面有iso文件的内容</li>
<li>按照方法二第11步的方法新建分区,然后把binary下面的东西复制进去,就可以启动live系统了</li>
</ol>
<h2 id="方法二-手动构建">方法二: 手动构建</h2>
<p><em><strong>此方法全程<code>sudo su</code>执行即可,很多命令都要root权限</strong></em></p>
<ol>
<li>apt安装<code>debootstrap squashfs-tools</code>,中间有其他需要的软件包我忘了是什么了,提示找不到命令自己安装一下即可</li>
<li>使用<code>debootstrap</code>构建基本根目录:</li>
</ol>
<pre><code>mkdir rootfs
debootstrap bookworm rootfs https://mirrors.ustc.edu.cn/debian/
</code></pre>
<p>此处的第一个参数是发行版,我仍然选择bookworm,第二个参数是文件夹,第三个是使用的镜像;大概需要10分钟下载完成,根目录约300MB<br>
3. <code>chroot rootfs</code>,这会切换根目录<br>
ubuntu官方文档里提到了要mount一下设备文件,然后再<code>chroot</code>,但是我测试发现不mount也没问题,命令如下:</p>
<pre><code>mount --bind /sys ./rootfs/sys
mount --bind /proc ./rootfs/proc
mount --bind /dev/pts ./rootfs/dev/pts
</code></pre>
<p>卸载(离开chroot之后再卸载):</p>
<pre><code>umount ./rootfs/sys
umount ./rootfs/proc
umount ./rootfs/dev/pts
</code></pre>
<ol start="4">
<li>为live系统安装内核,有很多方法,最简单的方法是直接<code>apt install linux-image-6.1.0-25-amd64</code>,但是我对内核有特殊要求,所以自己编译了,方法:debian内核编译手册,然后<code>dpkg -i</code>安装编译好的文件</li>
<li>安装必要软件包:<code>live-boot live-boot-initramfs-tools</code><strong>这两个软件包是必须的!没有将会无法启动live系统</strong></li>
<li>自定义系统,自己想怎么搞都行,我的脚本:</li>
</ol>
<pre><code>apt update
echo -e "deb http://mirrors.ustc.edu.cn/debian bookworm main contrib non-free non-free-firmware\ndeb http://mirrors.ustc.edu.cn/debian bookworm-updates main contrib non-free non-free-firmware\ndeb http://mirrors.ustc.edu.cn/debian bookworm-backports main contrib non-free non-free-firmware\ndeb http://mirrors.ustc.edu.cn/debian-security/ bookworm-security main contrib non-free non-free-firmware" &gt; /etc/apt/sources.list
apt install -y lightdm
apt install -y lxqt-core
apt install -y firefox-esr
apt install -y bash-completion vim sudo htop network-manager gparted mpv nm-tray firmware-linux firmware-iwlwifi rfkill pciutils
sed -i '/# enable bash completion in interactive shells/{n;N;N;N;N;N;N;s/^#//gm}' /etc/bash.bashrc
passwd -d root
apt upgrade -y
apt autoclean
</code></pre>
<p>我这里用了lxqt的桌面,有几个坑:</p>
<ul>
<li>lxqt-core软件包很小,但是不会自动安装显示管理器,所以要先装一个lightdm</li>
<li>debootstrap构建的根目录很小,很多固件都没有,要联网需要安装<code>network-manager nm-tray firmware-iwlwifi rfkill</code></li>
</ul>
<ol start="7">
<li>更新initrd:<code>update-initramfs -c -k all</code>必须执行,否则initrd会无法启动</li>
<li>exit命令退出chroot</li>
<li>构建uefi引导:</li>
</ol>
<pre><code>dd if=/dev/zero of=vefi.img bs=1M count=32 # 创建虚拟硬盘
mkfs.vfat vefi.img # 在虚拟硬盘新建fat32文件系统
mkdir vefi-mnt
mount vefi.img vefi-mnt #挂载虚拟硬盘
mkdir vefi-mnt/boot
grub-install --boot-directory=vefi-mnt/boot --efi-directory=vefi-mnt --removable vefi.img #生成grub的bootx64.efi引导程序和grub.cfg
sed -i 's/^/# /' vefi-mnt/EFI/BOOT/grub.cfg
echo -e "menuentry \"Live system (amd64)\" {\n        linux        /live/vmlinuz boot=live\n        initrd        /live/initrd.img\n}" &gt;&gt; vefi-mnt/EFI/BOOT/grub.cfg # 配置grub
umount vefi.img # 卸载
</code></pre>
<p><em><strong>grub-install这个命令默认选择本机的efi和boot目录,不要无参数执行,否则会更新本机的grub</strong></em><br>
10. 构建squashfs:<code>mksquashfs rootfs/ filesystem.squashfs</code>这会在执行目录下生成filesystem.squashfs文件,live环境一般有manifest文件,但是我没加也成功了,具体怎么生成可以参考开头那几个链接<br>
11. 创建启动u盘:确定自己的u盘已经是gpt分区表,不是的改一下,新建一个fat32分区,我的带lxqt桌面的live系统只有1.1g,分区新建4g应该是够的,这几步用windows的diskgenius操作就行,具体操作百度即可<br>
12. 复制文件:我的u盘目录结构(注意是刚才新建的fat32分区),initrd.img和vmlinuz是<code>rootfs/boot</code>下面的两个文件重命名复制来的(分别是初始文件系统和内核镜像):</p>
<pre><code>main@n960kp:/media/main/linux-live$ ls -R
.:
EFIlive

./EFI:
BOOT

./EFI/BOOT:
BOOTX64.CSVBOOTX64.EFIgrub.cfggrubx64.efimmx64.efi

./live:
filesystem.squashfsinitrd.imgvmlinuz
</code></pre>
<ul>
<li>EFI和boot文件夹在<code>vefi.img</code>虚拟硬盘里,挂载之后再复制到u盘里,不挂载看不到文件夹</li>
<li><code>live</code>这个文件夹名称不固定,但是要和grub.cfg里写的对应</li>
</ul>
<ol start="13">
<li>完成,进入电脑bios启动吧</li>
</ol>
<h3 id="后记">后记</h3>
<ul>
<li>ubuntu那边有一个叫<code>cubic</code>的软件,应该比这种手动方法方便,但是debian好像没有</li>
<li>关于启动u盘,我只会做uefi启动的,bios启动的u盘制作应该更复杂;很多教程都要求烧写u盘,其实这对于uefi启动u盘不必要(bios启动有要求),新建分区把引导文件放进去即可,这样还可以制作多个启动选项的u盘</li>
<li><code>vefi-mnt/boot/grub</code>下面有<code>x86_64-efi i386-efi</code>两个文件夹,下面文件很多,我也不知道有什么用,反正没复制他也能启动,u盘速度慢的可以不复制</li>
<li><code>live-boot live-boot-initramfs-tools</code>两个软件包会修改initrd.img,使他可以在live环境中正常启动</li>
</ul><br><br>
来源:https://www.cnblogs.com/elight2/p/18418013
頁: [1]
查看完整版本: 制作基于debian的linux live启动u盘(UEFI)