打造可保存数据的Ubuntu Live USB

前言

之前写过一个打造纯UEFI 启动的Ubuntu 系统U 盘,那种方法跟将Linux安装到硬盘上没有任何区别,是最纯粹的一种安装方式。只是20.04版的Grub2UEFI引导有Bug,需做些处理,详见本文的操作步骤Grub2 配置
本次介绍另一个用Grub2引导ISO文件并使用Persistent的分区或文件来保存数据的方法,但注意这种方法会有些底层上的问题,例如使用Docker安装无线网卡驱动等,本文也会列出解决办法。

材料准备

  1. 至少8GB支持USB 3.0U盘(TF卡加读卡器也行),USB 2.0也可以运行但速度会受到影响
  2. Ubuntu ISO,去官网找离你最近最快的下载镜像,https://launchpad.net/ubuntu/+cdmirrors
    本文以20.0416.04为例
    https://mirror.internet.asn.au/pub/ubuntu/releases/16.04.7/ubuntu-16.04.7-desktop-amd64.iso
    https://mirror.internet.asn.au/pub/ubuntu/releases/20.04.1/ubuntu-20.04.1-desktop-amd64.iso
  3. 分区工具,推荐DiskGenius,免费版即可

实现原理

参考20.04 booting .iso from GRUB menu
Persistence

  • If we want a persistent OS add the word Persistent to the GRUB menuentry, (via /etc/grub.d/40-custom).
    要实现数据持久化,Grub2菜单里的命令必须加上persistent关键字
  • If using a persistent partition make it ext4 and label it casper-rw for 19.10 and previous ISO files.
  • For 20.04 ISO files label the persistent partition writable.
  • Only one persistent partition is allowed per drive,
    使用持久化分区,每块硬盘只能有一个,且必须用ext4格式,19.10及之前的用casper-rw,之后的用writable作为该分区的卷标,可以用满整个分区的空间
  • If using persistent files, each ISO can have it’s own persistence.
  • Each ISO can have a writable (or casper-rw) file up to 4GB and an optional home-rw file up to 4GB.
  • Persistent files must be located on a FAT32 partition.
  • If more than one persistence file is used, a persistence-path must be given. Just the unique name of the persistent files folder is required. One casper-rw/writable file and one home-rw file per folder.
    使用持久化文件,存放文件的分区必须是fat32格式,文件名为writablecasper-rw(19.10及之前)和可选的home-rw,最大4GB
    每个ISO文件都可以有自己对应的持久化文件,用persistence-path指定不同的文件夹来进行区分,如果只有一个ISO则不需要指定该参数

操作步骤

1. U 盘分区格式化

这里以32GU盘为例,分成两个区,MBRGPT都可以,分区均采用主分区形式
第一分区:FAT32格式,10GB,卷标UUU
第二分区:EXT4格式,22GB,卷标writable
在第一分区创建文件夹ISO并放入下载好的Ubuntuiso文件
这样后面打算16.04将采用持久化文件,而20.04则采用持久化分区,两种形式都演示一遍

注意,Windows不识别U盘的多分区,只有U盘第一个分区可以在其下访问
另外,如想多分个区专门存放Grub2,该区至少要有200MB且为FAT32,否则UEFI不识别

2. Grub2 文件提取

ubuntu-20.04.1-desktop-amd64.iso里的EFI解压到U盘的第一分区即UUU

3. Grub2 初始配置

U盘的第一分区即UUU根目录下创建/boot/grub/grub.cfg(两层文件夹),内容如下

set timeout=2

# Remove 'Trusted Platform Module' for 20.04 UEFI booting
rmmod tpm

# PLACEHOLDER_FOR_USING_UUID

menuentry "Ubuntu 20.04 LTS" {
  set isofile=/ISO/ubuntu-20.04.1-desktop-amd64.iso
  loopback loop $isofile
  # fsck means file system check
  linux (loop)/casper/vmlinuz boot=casper iso-scan/filename=$isofile persistent fsck.mode=skip quiet splash
  initrd (loop)/casper/initrd
}

menuentry "Ubuntu 16.04 LTS" {
  # Press C button in Grub Menu and use 'ls' to list partitions and set 'root' location
  set root=(hd0,msdos1)
  set isofile=/ISO/ubuntu-16.04.7-desktop-amd64.iso
  loopback loop $isofile
  linux (loop)/casper/vmlinuz boot=casper iso-scan/filename=$isofile persistent persistent-path=/RW/U1604/ fsck.mode=skip quiet splash
  initrd (loop)/casper/initrd
}

submenu "Others" {
  menuentry "Reboot" {
    reboot
  }

  menuentry "Halt" {
    halt
  }
 
  menuentry "Boot Next" {
    exit
  }
}

4. 持久化文件创建

U盘插入电脑,设置为UEFI启动并从U盘启动,选择Ubuntu 16.04 LTS
如提示无此分区,则在Grub2菜单处按C,用ls查看所有的分区标识,再按Esc返回
再次选择Ubuntu 16.04 LTSE,设置set root=U盘第一分区,最后按F10启动
进入Ubuntu 16.04 LTS系统后,按左边Win键并输入terminal来启动终端程序

# 1. 创建持久化文件,1G 为例
dd if=/dev/zero of=casper-rw bs=1M count=1024
## 创建更大的文件可能需要切换到有足够空间的目录,创建1G 文件默认路径足矣

# 2. 格式化持久化文件为ext4
mkfs.ext4 casper-rw

# 3. 关闭ext4 的日志功能
tune2fs -O ^has_journal casper-rw
## 检查是否已经关闭了日志功能
dumpe2fs casper-rw | grep 'has_journal'
## 结果中没有has_journal 即为关闭成功

# 4. 复制持久化文件
cp casper-rw home-rw

# 5. 部署持久化文件
sudo mkdir -p /isodevice/RW/U1604  # /isodevice/ 即U 盘
sudo cp casper-rw home-rw /isodevice/RW/U1604

5. Grub2 完善配置

# 1. 查看U 盘的UUID,之前已将第一分区卷标设为UUU
ls -l /dev/disk/by-uuid/
## 或者使用blkid 获取卷标为UUU 的分区的UUID
blkid | grep UUU

# 2. 注释原文件里的"set root=(hd0,msdos1)"
sudo sed -i 's/set root=(hd0,msdos1)/#set root=(hd0,msdos1)/' /isodevice/boot/grub/grub.cfg

# 3. 往配置文件添加以下内容,使用UUID 定位磁盘
_UUID=$(blkid | grep UUU | awk '{print $3}' | cut -d'=' -f2)
echo UUID=$_UUID
cat > t.txt << EOF
set uuid=$_UUID
# Search partition by UUID and set it to variable 'ubdisk'
search --no-floppy --fs-uuid --set=ubdisk \$uuid
set root=(\$ubdisk)
EOF
sudo sed -i '/PLACEHOLDER_FOR_USING_UUID/ r t.txt' /isodevice/boot/grub/grub.cfg

## 至此,可保存数据的Ubuntu Live USB 打造完毕,重启后进入任一个系统即可看到效果

6. U 盘文件结构图

# 最终U 盘的文件结构如下
# 第一分区 UUU
|____EFI
| |____BOOT
|   |____BOOTx64.EFI
|   |____grubx64.efi
|   |____mmx64.efi
|____ISO
| |____ubuntu-16.04.7-desktop-amd64.iso
| |____ubuntu-20.04.1-desktop-amd64.iso
|____boot
| |____grub
|   |____grub.cfg
|____RW
  |____U1604
    |____casper-rw
    |____home-rw
# 第二分区 writable
## 作为持久化分区其保存的是Linux 系统产生的文件

补充配置

0. 更新source.list

# 由于是LiveCD 引导了,所以得手动添加完整的sources.list,否则连fcitx 都装不上
# 20.04 参考,https://dannyda.com/2020/10/03/default-contents-of-sources-list-in-ubuntu-20-04-1-lts-desktop-server/
# 1. 备份旧源列表
cd /etc/apt && [ -f sources.list.bak ] || sudo mv sources.list sources.list.bak

# 2. 生成新源列表
cd /etc/apt/ && sudo tee sources.list << 'EOF'
#deb cdrom:[Ubuntu 20.04 LTS _Focal Fossa_ - Release amd64 (20200423)]/ focal main restricted
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://us.archive.ubuntu.com/ubuntu/ focal main restricted
# deb-src http://us.archive.ubuntu.com/ubuntu/ focal main restricted
## Major bug fix updates produced after the final release of the
## distribution.
deb http://us.archive.ubuntu.com/ubuntu/ focal-updates main restricted
# deb-src http://us.archive.ubuntu.com/ubuntu/ focal-updates main restricted
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://us.archive.ubuntu.com/ubuntu/ focal universe
# deb-src http://us.archive.ubuntu.com/ubuntu/ focal universe
deb http://us.archive.ubuntu.com/ubuntu/ focal-updates universe
# deb-src http://us.archive.ubuntu.com/ubuntu/ focal-updates universe
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu 
## team, and may not be under a free licence. Please satisfy yourself as to 
## your rights to use the software. Also, please note that software in 
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://us.archive.ubuntu.com/ubuntu/ focal multiverse
# deb-src http://us.archive.ubuntu.com/ubuntu/ focal multiverse
deb http://us.archive.ubuntu.com/ubuntu/ focal-updates multiverse
# deb-src http://us.archive.ubuntu.com/ubuntu/ focal-updates multiverse
## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://us.archive.ubuntu.com/ubuntu/ focal-backports main restricted universe multiverse
# deb-src http://us.archive.ubuntu.com/ubuntu/ focal-backports main restricted universe multiverse
## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu focal partner
# deb-src http://archive.canonical.com/ubuntu focal partner
deb http://security.ubuntu.com/ubuntu focal-security main restricted
# deb-src http://security.ubuntu.com/ubuntu focal-security main restricted
deb http://security.ubuntu.com/ubuntu focal-security universe
# deb-src http://security.ubuntu.com/ubuntu focal-security universe
deb http://security.ubuntu.com/ubuntu focal-security multiverse
# deb-src http://security.ubuntu.com/ubuntu focal-security multiverse
# This system was installed using small removable media
# (e.g. netinst, live or single CD). The matching "deb cdrom"
# entries were disabled at the end of the installation process.
# For information about how to configure apt package sources,
# see the sources.list(5) manual.
EOF

# 3. 更新软件源索引
sudo apt update

1. 创建新用户

# 1. 创建新的用户abc
sudo adduser abc
## 输入两次密码后,一直回车到结束

# 2. 将abc 加入sudo 组
sudo usermod -aG sudo abc

# 3. 屏幕右上角菜单Log Out 退出,再以abc 登录,简单设置后即可使用

# 4. 删除Live CD 默认的用户
sudo deluser --remove-home ubuntu

2. 增大持久化文件

# 1. 打开终端,切换成root 用户
sudo -i

# 2. 进入casper-rw 或home-rw 或writable 文件所在的目录,最好先备份一下原文件
cd /isodevice/RW/U1604 && \
dd if=/dev/zero bs=1M count=1024 >> casper-rw
## 注:把上面的count=1024 改成你需要的大小,这里用追加>> 增加文件大小,最大可达4GB

# 3. 然后我们还需要check 一下casper-rw 文件,输入以下命令
e2fsck -f casper-rw

# 4. 最后我们重新指定一下分区大小,输入以下命令
resize2fs casper-rw
## 如果以上的命令都正确执行了,那恭喜你,你已经得到一个新尺寸的casper-rw 文件。

3. Broadcom 网卡

# MacBook Pro 2013 自带的无线网卡需要安装以下驱动
sudo apt install bcmwl-kernel-source

# 20.04 由于官方维护得不好,以上命令会报错,正确步骤如下
## 1. 查看网卡芯片
lspci -vnn | grep Network
lshw -C network

## 2. 使用Ubuntu 20.10 的包
wget http://mirrors.kernel.org/ubuntu/pool/restricted/b/bcmwl/bcmwl-kernel-source_6.30.223.271+bdcom-0ubuntu7_amd64.deb
sudo dpkg -i bcmwl-kernel-source_6.30.223.271+bdcom-0ubuntu7_amd64.deb
sudo apt -f install  # -f means fixing broken

## 3. 让内核重新读取wl 模块,使WiFi 可用
lspci -k | grep bcm  # 查看已加载的模块
sudo modprobe -rv bcma wl  
sudo modprobe -v wl

## 4. 创建开机启动脚本
### 由于是persistent livesystem,所以每次开机都要读取一次
mkdir ~/Applications
### 试验发现只有放在/home 下面脚本才生效,可能与持久化加载机制有关
cat > ~/Applications/wifi_fix.sh << 'EOF'
#!/bin/bash
sudo modprobe -r bcma wl
sudo modprobe wl
EOF
chmod +x ~/Applications/wifi_fix.sh
### 左Win 键,找到Startup Applications Preferences,Add
#### Name:wifi_fix.sh
#### Command:/home/abc/Applications/wifi_fix.sh
#### Comment:fixing wifi

## 5. 使sudo 免输密码
sudo vi /etc/sudoers
## %sudo	ALL=(ALL:ALL) ALL   # 注释这一行,添加下面一行
## %sudo ALL=NOPASSWD: ALL    # 注意NOPASSWD 的写法,否则只能从安全模式改了

## 6. 如果开机脚本没有生效也可手动执行
~/Applications/wifi_fix.sh

4. 启动SSH 服务

# 1. 查看有哪些openssh 开头的包可以安装
sudo apt-cache search ^openssh

# 2. 安装openssh-server 即可包含client,net-tools 包含ifconfig
sudo apt update && sudo apt install net-tools openssh-server -y

5. VirtualBox 安装

# 使用Oracle 官方的源进行安装
## 官网,https://www.virtualbox.org/wiki/Linux_Downloads
## 网友,https://cloud.tencent.com/developer/article/1636687
## Debian-based Linux distributions
wget -q https://www.virtualbox.org/download/oracle_vbox_2016.asc -O- | sudo apt-key add -
wget -q https://www.virtualbox.org/download/oracle_vbox.asc -O- | sudo apt-key add -
echo "deb [arch=amd64] http://download.virtualbox.org/virtualbox/debian $(lsb_release -cs) contrib" | sudo tee -a /etc/apt/sources.list.d/virtualbox.list
sudo apt update && sudo apt install virtualbox-6.1 -y
## Oracle_VM_VirtualBox_Extension_Pack
wget https://download.virtualbox.org/virtualbox/6.1.8/Oracle_VM_VirtualBox_Extension_Pack-6.1.8.vbox-extpack
sudo VBoxManage extpack install Oracle_VM_VirtualBox_Extension_Pack-6.1.8.vbox-extpack
### 输入y 并回车即可完成安装

6. Docker 安装

# 1. 按照官方的指引正常安装Docker,但先不运行hello-world
## https://docs.docker.com/engine/install/ubuntu/
sudo apt-get install apt-transport-https ca-certificates \
    curl gnupg-agent software-properties-common

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

# 2. 设置Docker 使用 VFS storage driver
## https://docs.docker.com/storage/storagedriver/vfs-driver/
sudo -i
systemctl stop docker

cat > /etc/docker/daemon.json << EOF
{
  "storage-driver": "vfs"
}
EOF

systemctl start docker
exit

# 3. 现在可以运行hello-world 了
sudo docker run hello-world

# 4. 将当前用户加入docker 组以获得执行docker 的权限,需要重启
sudo usermod -aG docker $USER

7. Visual Code

# 1. 下载安装包
## https://code.visualstudio.com/sha/download?build=stable&os=linux-deb-x64

# 2. 安装deb 包
sudo dpkg -i code_1.52.1-1608136922_amd64.deb

# 3. 备份原配置文件
cd /etc/apt/sources.list.d && [ -f vscode.list.bak ] || sudo cp vscode.list vscode.list.bak

# 4. 删除多余的apt 源
cd /etc/apt/sources.list.d/ && \
sudo sed -i 's/arch=amd64,arm64,armhf/arch=amd64/' vscode.list

# 5. 更新软件源索引
sudo apt update

8. 搜狗输入法

# 1. 安装fcitx,须补充0 的完整sources.list
sudo apt update && sudo apt install fcitx -y

# 2. 点击右上角,Settings,Region & Language,Manage Installed Languages
## Install / Remove Languages,只保留简体中文,繁体中文和English
## Keyboard input method system: fcitx
## Apply System-Wide

# 3. 下载搜狗deb 包并安装
## https://pinyin.sogou.com/linux/?r=pinyin
sudo dpkg -i sogoupinyin*.deb
sudo apt -f install

# 4. 重启系统
sudo init 6

# 5. 点击右上角小键盘,Configure,+
## 去掉Only Show Current Language,通过搜索框找到sogoupinyin 添加即可
## 完成后,用Ctrl+Space 切换到搜狗拼音输入法

9. 字体安装及WPS

# 1. 从Windows 的C:/WINDOS/Fonts/ 拷贝字体,这里用硬盘里Windows 8.1 64 位的
sudo fdisk -l  # 查看Windows 所在的分区
sudo mkdir /mnt/windisk  # 创建挂载点,然后用以下命令挂载到Linux 中
sudo mount -t ntfs /dev/sda4 /mnt/windisk -o iocharset=utf8,umask=0

# 2. 复制Windows 所有字体
## 创建Linux 新的字体文件夹,名字暂用windows
sudo mkdir /usr/share/fonts/windows/
cd /mnt/windisk/WINDOS/Fonts/
sudo cp *.ttc /usr/share/fonts/windows/
sudo cp *.ttf /usr/share/fonts/windows/

# 3. 更新字体库
cd /usr/share/fonts/windows/
sudo chmod 755 *
## 为当前目录下的字体建立scale 文件和dir 文件
sudo mkfontscale && sudo mkfontdir
## 更新系统字体库缓存
sudo fc-cache
## 重启系统就可以使用这些字体了
sudo umount /mnt/windisk && sudo init 6

# 4. 安装WPS
https://linux.wps.com/
sudo dpkg -i wps-office*.deb
sudo apt -f install

参考文档

How to Fix: Broadcom BCM43142 WiFi not working on Ubuntu 18.04.3 LTS
关闭ext4文件系统的日志功能
使用persistent模式将livecd安装到优盘
手工创建casper-rw文件(用于liveusb保存文件)
如何在Ubuntu 20.04 上安装VirtualBox
bcmwl-kernel-source broken on kernel: 5.8.0-34-generic
将Windows 下的字体库迁移到Ubuntu 下使用
Ubuntu 16.04 中如何挂载windows 的磁盘
Ventoy 新一代多系统启动U 盘解决方案

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐