如何使用LVM升级Fedora

88201047 2019-08-20

如何使用LVM升级Fedora

大多数用户发现使用标准流程升级从一个 Fedora 版本升级到下一个很简单。但是,Fedora 升级也不可避免地会遇到许多特殊情况。本文介绍了使用 DNF 和逻辑卷管理(LVM)进行升级的一种方法,以便在出现问题时保留可引导备份。这个例子是将 Fedora 26 系统升级到 Fedora 28。

此处展示的过程比标准升级过程更复杂。在使用此过程之前,你应该充分掌握 LVM 的工作原理。如果没有适当的技能和细心,你可能会丢失数据和/或被迫重新安装系统!如果你不知道自己在做什么,那么强烈建议你坚持只使用得到支持的升级方法。

准备系统

在开始之前,请确保你的现有系统已完全更新。

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> dnf update</span>
  2. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> </span><span class="kwd">systemctl</span><span class="pln"> </span><span class="kwd">reboot</span><span class="pln"> </span><span class="com">#</span><span class="pln"> </span><span class="pun">或采用</span><span class="pln"> GUI </span><span class="pun">方式</span>

检查你的根文件系统是否是通过 LVM 挂载的。

  1. <span class="pln">$ </span><span class="kwd">df</span><span class="pln"> </span><span class="pun">/</span>
  2. <span class="typ">Filesystem</span><span class="pln"> </span><span class="lit">1K</span><span class="pun">-</span><span class="pln">blocks </span><span class="typ">Used</span><span class="pln"> </span><span class="typ">Available</span><span class="pln"> </span><span class="typ">Use</span><span class="pun">%</span><span class="pln"> </span><span class="typ">Mounted</span><span class="pln"> on</span>
  3. <span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">mapper</span><span class="pun">/</span><span class="pln">vg_sdg</span><span class="pun">-</span><span class="pln">f26 </span><span class="lit">20511312</span><span class="pln"> </span><span class="lit">14879816</span><span class="pln"> </span><span class="lit">4566536</span><span class="pln"> </span><span class="lit">77</span><span class="pun">%</span><span class="pln"> </span><span class="pun">/</span>
  4.  
  5. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> lvs</span>
  6. <span class="pln">LV VG </span><span class="typ">Attr</span><span class="pln"> </span><span class="typ">LSize</span><span class="pln"> </span><span class="typ">Pool</span><span class="pln"> </span><span class="typ">Origin</span><span class="pln"> </span><span class="typ">Data</span><span class="pun">%</span><span class="pln"> </span><span class="typ">Meta</span><span class="pun">%</span><span class="pln"> </span><span class="typ">Move</span><span class="pln"> </span><span class="typ">Log</span><span class="pln"> </span><span class="typ">Cpy</span><span class="pun">%</span><span class="typ">Sync</span><span class="pln"> </span><span class="typ">Convert</span>
  7. <span class="pln">f22 vg_sdg </span><span class="pun">-</span><span class="pln">wi</span><span class="pun">-</span><span class="pln">ao</span><span class="pun">----</span><span class="pln"> </span><span class="lit">15.00g</span>
  8. <span class="pln">f24_64 vg_sdg </span><span class="pun">-</span><span class="pln">wi</span><span class="pun">-</span><span class="pln">ao</span><span class="pun">----</span><span class="pln"> </span><span class="lit">20.00g</span>
  9. <span class="pln">f26 vg_sdg </span><span class="pun">-</span><span class="pln">wi</span><span class="pun">-</span><span class="pln">ao</span><span class="pun">----</span><span class="pln"> </span><span class="lit">20.00g</span>
  10. <span class="pln">home vg_sdg </span><span class="pun">-</span><span class="pln">wi</span><span class="pun">-</span><span class="pln">ao</span><span class="pun">----</span><span class="pln"> </span><span class="lit">100.00g</span>
  11. <span class="pln">mockcache vg_sdg </span><span class="pun">-</span><span class="pln">wi</span><span class="pun">-</span><span class="pln">ao</span><span class="pun">----</span><span class="pln"> </span><span class="lit">10.00g</span>
  12. <span class="pln">swap vg_sdg </span><span class="pun">-</span><span class="pln">wi</span><span class="pun">-</span><span class="pln">ao</span><span class="pun">----</span><span class="pln"> </span><span class="lit">4.00g</span>
  13. <span class="kwd">test</span><span class="pln"> vg_sdg </span><span class="pun">-</span><span class="pln">wi</span><span class="pun">-</span><span class="pln">a</span><span class="pun">-----</span><span class="pln"> </span><span class="lit">1.00g</span>
  14. <span class="pln">vg_vm vg_sdg </span><span class="pun">-</span><span class="pln">wi</span><span class="pun">-</span><span class="pln">ao</span><span class="pun">----</span><span class="pln"> </span><span class="lit">20.00g</span>

如果你在安装 Fedora 时使用了默认值,你可能会发现根文件系统挂载在名为 root 的逻辑卷(LV)上。卷组(VG)的名称可能会有所不同。看看根卷的总大小。在该示例中,根文件系统名为 f26,大小为 20G

接下来,确保 LVM 中有足够的可用空间。

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> vgs</span>
  2. <span class="pln">VG </span><span class="com">#</span><span class="pln">PV </span><span class="com">#</span><span class="pln">LV </span><span class="com">#</span><span class="pln">SN </span><span class="typ">Attr</span><span class="pln"> </span><span class="typ">VSize</span><span class="pln"> </span><span class="typ">VFree</span>
  3. <span class="pln">vg_sdg </span><span class="lit">1</span><span class="pln"> </span><span class="lit">8</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> wz</span><span class="pun">--</span><span class="pln">n</span><span class="pun">-</span><span class="pln"> </span><span class="lit">232.39g</span><span class="pln"> </span><span class="lit">42.39g</span>

该系统有足够的可用空间,可以为升级后的 Fedora 28 的根卷分配 20G 的逻辑卷。如果你使用的是默认安装,则你的 LVM 中将没有可用空间。对 LVM 的一般性管理超出了本文的范围,但这里有一些情形下可能采取的方法:

1、/home 在自己的逻辑卷,而且 /home 中有大量空闲空间。

你可以从图形界面中注销并切换到文本控制台,以 root 用户身份登录。然后你可以卸载 /home,并使用 lvreduce -r 调整大小并重新分配 /home 逻辑卷。你也可以从现场镜像Live image启动(以便不使用 /home)并使用 gparted GUI 实用程序进行分区调整。

2、大多数 LVM 空间被分配给根卷,该文件系统中有大量可用空间。

你可以从现场镜像启动并使用 gparted GUI 实用程序来减少根卷的大小。此时也可以考虑将 /home 移动到另外的文件系统,但这超出了本文的范围。

3、大多数文件系统已满,但你有个已经不再需要逻辑卷。

你可以删除不需要的逻辑卷,释放卷组中的空间以进行此操作。

创建备份

首先,为升级后的系统分配新的逻辑卷。确保为系统的卷组(VG)使用正确的名称。在这个例子中它是 vg_sdg

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> lvcreate </span><span class="pun">-</span><span class="pln">L20G </span><span class="pun">-</span><span class="pln">n f28 vg_sdg</span>
  2. <span class="typ">Logical</span><span class="pln"> volume </span><span class="str">"f28"</span><span class="pln"> created</span><span class="pun">.</span>

接下来,创建当前根文件系统的快照。此示例创建名为 f26_s 的快照卷。

  1. <span class="pln">$ </span><span class="kwd">sync</span>
  2. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> lvcreate </span><span class="pun">-</span><span class="pln">s </span><span class="pun">-</span><span class="pln">L1G </span><span class="pun">-</span><span class="pln">n f26_s vg_sdg</span><span class="pun">/</span><span class="pln">f26</span>
  3. <span class="typ">Using</span><span class="pln"> </span><span class="kwd">default</span><span class="pln"> stripesize </span><span class="lit">64.00</span><span class="pln"> </span><span class="typ">KiB</span><span class="pun">.</span>
  4. <span class="typ">Logical</span><span class="pln"> volume </span><span class="str">"f26_s"</span><span class="pln"> created</span><span class="pun">.</span>

现在可以将快照复制到新逻辑卷。当你替换自己的卷名时,请确保目标正确。如果不小心,就会不可撤销地删除了数据。此外,请确保你从根卷的快照复制,而不是从你的现在的根卷。

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> </span><span class="kwd">dd</span><span class="pln"> </span><span class="kwd">if</span><span class="pun">=</span><span class="str">/dev/</span><span class="pln">vg_sdg</span><span class="pun">/</span><span class="pln">f26_s of</span><span class="pun">=</span><span class="str">/dev/</span><span class="pln">vg_sdg</span><span class="pun">/</span><span class="pln">f28 bs</span><span class="pun">=</span><span class="lit">256k</span>
  2. <span class="lit">81920</span><span class="pun">+</span><span class="lit">0</span><span class="pln"> records </span><span class="kwd">in</span>
  3. <span class="lit">81920</span><span class="pun">+</span><span class="lit">0</span><span class="pln"> records out</span>
  4. <span class="lit">21474836480</span><span class="pln"> bytes </span><span class="pun">(</span><span class="lit">21</span><span class="pln"> GB</span><span class="pun">,</span><span class="pln"> </span><span class="lit">20</span><span class="pln"> </span><span class="typ">GiB</span><span class="pun">)</span><span class="pln"> copied</span><span class="pun">,</span><span class="pln"> </span><span class="lit">149.179</span><span class="pln"> s</span><span class="pun">,</span><span class="pln"> </span><span class="lit">144</span><span class="pln"> MB</span><span class="pun">/</span><span class="pln">s</span>

给新文件系统一个唯一的 UUID。这不是绝对必要的,但 UUID 应该是唯一的,因此这避免了未来的混淆。以下是在 ext4 根文件系统上的方法:

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> </span><span class="kwd">e2fsck</span><span class="pln"> </span><span class="pun">-</span><span class="pln">f </span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">vg_sdg</span><span class="pun">/</span><span class="pln">f28</span>
  2. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> </span><span class="kwd">tune2fs</span><span class="pln"> </span><span class="pun">-</span><span class="pln">U random </span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">vg_sdg</span><span class="pun">/</span><span class="pln">f28</span>

然后删除不再需要的快照卷:

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> lvremove vg_sdg</span><span class="pun">/</span><span class="pln">f26_s</span>
  2. <span class="typ">Do</span><span class="pln"> you really want to remove active logical volume vg_sdg</span><span class="pun">/</span><span class="pln">f26_s</span><span class="pun">?</span><span class="pln"> </span><span class="pun">[</span><span class="pln">y</span><span class="pun">/</span><span class="pln">n</span><span class="pun">]:</span><span class="pln"> y</span>
  3. <span class="typ">Logical</span><span class="pln"> volume </span><span class="str">"f26_s"</span><span class="pln"> successfully removed</span>

如果你单独挂载了 /home,你可能希望在此处制作 /home 的快照。有时,升级的应用程序会进行与旧版 Fedora 版本不兼容的更改。如果需要,编辑根文件系统上的 /etc/fstab 文件以在 /home 上挂载快照。请记住,当快照已满时,它将消失!另外,你可能还希望给 /home 做个正常备份。

配置以使用新的根

首先,安装新的逻辑卷并备份现有的 GRUB 设置:

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> </span><span class="kwd">mkdir</span><span class="pln"> </span><span class="pun">/</span><span class="pln">mnt</span><span class="pun">/</span><span class="pln">f28</span>
  2. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> </span><span class="kwd">mount</span><span class="pln"> </span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">vg_sdg</span><span class="pun">/</span><span class="pln">f28 </span><span class="pun">/</span><span class="pln">mnt</span><span class="pun">/</span><span class="pln">f28</span>
  3. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> </span><span class="kwd">mkdir</span><span class="pln"> </span><span class="pun">/</span><span class="pln">mnt</span><span class="pun">/</span><span class="pln">f28</span><span class="pun">/</span><span class="pln">f26</span>
  4. <span class="pln">$ </span><span class="kwd">cd</span><span class="pln"> </span><span class="pun">/</span><span class="pln">boot</span><span class="pun">/</span><span class="pln">grub2</span>
  5. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> </span><span class="kwd">cp</span><span class="pln"> </span><span class="pun">-</span><span class="pln">p grub</span><span class="pun">.</span><span class="pln">cfg grub</span><span class="pun">.</span><span class="pln">cfg</span><span class="pun">.</span><span class="pln">old</span>

编辑 grub.conf 并在第一个菜单项 menuentry 之前添加这些,除非你已经有了:

  1. <span class="pln">menuentry </span><span class="str">'Old boot menu'</span><span class="pln"> </span><span class="pun">{</span>
  2. <span class="pln"> configfile </span><span class="pun">/</span><span class="pln">grub2</span><span class="pun">/</span><span class="pln">grub</span><span class="pun">.</span><span class="pln">cfg</span><span class="pun">.</span><span class="pln">old</span>
  3. <span class="pun">}</span>

编辑 grub.conf 并更改默认菜单项以激活并挂载新的根文件系统。改变这一行:

  1. <span class="pln">linux16 </span><span class="pun">/</span><span class="pln">vmlinuz</span><span class="pun">-</span><span class="lit">4.16</span><span class="pun">.</span><span class="lit">11</span><span class="pun">-</span><span class="lit">100.fc26.x86</span><span class="pln">_64 root</span><span class="pun">=</span><span class="str">/dev/</span><span class="pln">mapper</span><span class="pun">/</span><span class="pln">vg_sdg</span><span class="pun">-</span><span class="pln">f26 ro rd</span><span class="pun">.</span><span class="kwd">lvm</span><span class="pun">.</span><span class="pln">lv</span><span class="pun">=</span><span class="pln">vg_sdg</span><span class="pun">/</span><span class="pln">f26 rd</span><span class="pun">.</span><span class="kwd">lvm</span><span class="pun">.</span><span class="pln">lv</span><span class="pun">=</span><span class="pln">vg_sdg</span><span class="pun">/</span><span class="pln">swap rhgb quiet LANG</span><span class="pun">=</span><span class="pln">en_US</span><span class="pun">.</span><span class="pln">UTF</span><span class="pun">-</span><span class="lit">8</span>

如你看到的这样。请记住使用你系统上的正确的卷组和逻辑卷条目名称!

  1. <span class="pln">linux16 </span><span class="pun">/</span><span class="pln">vmlinuz</span><span class="pun">-</span><span class="lit">4.16</span><span class="pun">.</span><span class="lit">11</span><span class="pun">-</span><span class="lit">100.fc26.x86</span><span class="pln">_64 root</span><span class="pun">=</span><span class="str">/dev/</span><span class="pln">mapper</span><span class="pun">/</span><span class="pln">vg_sdg</span><span class="pun">-</span><span class="pln">f28 ro rd</span><span class="pun">.</span><span class="kwd">lvm</span><span class="pun">.</span><span class="pln">lv</span><span class="pun">=</span><span class="pln">vg_sdg</span><span class="pun">/</span><span class="pln">f28 rd</span><span class="pun">.</span><span class="kwd">lvm</span><span class="pun">.</span><span class="pln">lv</span><span class="pun">=</span><span class="pln">vg_sdg</span><span class="pun">/</span><span class="pln">swap rhgb quiet LANG</span><span class="pun">=</span><span class="pln">en_US</span><span class="pun">.</span><span class="pln">UTF</span><span class="pun">-</span><span class="lit">8</span>

编辑 /mnt/f28/etc/default/grub 并改变在启动时激活的默认的根卷:

  1. <span class="pln">GRUB_CMDLINE_LINUX</span><span class="pun">=</span><span class="str">"rd.lvm.lv=vg_sdg/f28 rd.lvm.lv=vg_sdg/swap rhgb quiet"</span>

编辑 /mnt/f28/etc/fstab,将挂载的根文件系统从旧的逻辑卷:

  1. <span class="str">/dev/</span><span class="pln">mapper</span><span class="pun">/</span><span class="pln">vg_sdg</span><span class="pun">-</span><span class="pln">f26 </span><span class="pun">/</span><span class="pln"> ext4 defaults </span><span class="lit">1</span><span class="pln"> </span><span class="lit">1</span>

改为新的:

  1. <span class="str">/dev/</span><span class="pln">mapper</span><span class="pun">/</span><span class="pln">vg_sdg</span><span class="pun">-</span><span class="pln">f28 </span><span class="pun">/</span><span class="pln"> ext4 defaults </span><span class="lit">1</span><span class="pln"> </span><span class="lit">1</span>

然后,出于参考的用途,只读挂载旧的根卷:

  1. <span class="str">/dev/</span><span class="pln">mapper</span><span class="pun">/</span><span class="pln">vg_sdg</span><span class="pun">-</span><span class="pln">f26 </span><span class="pun">/</span><span class="pln">f26 ext4 ro</span><span class="pun">,</span><span class="pln">nodev</span><span class="pun">,</span><span class="pln">noexec </span><span class="lit">0</span><span class="pln"> </span><span class="lit">0</span>

如果你的根文件系统是通过 UUID 挂载的,你需要改变这个方式。如果你的根文件系统是 ext4 你可以这样做:

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> e2label </span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">vg_sdg</span><span class="pun">/</span><span class="pln">f28 F28</span>

现在编辑 /mnt/f28/etc/fstab 使用该卷标。改变该根文件系统的挂载行,像这样:

  1. <span class="pln">LABEL</span><span class="pun">=</span><span class="pln">F28 </span><span class="pun">/</span><span class="pln"> ext4 defaults </span><span class="lit">1</span><span class="pln"> </span><span class="lit">1</span>

重启与升级

重新启动,你的系统将使用新的根文件系统。它仍然是 Fedora 26,但是是带有新的逻辑卷名称的副本,并可以进行 dnf 系统升级!如果出现任何问题,请使用旧引导菜单引导回到你的工作系统,此过程可避免触及旧系统。

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> </span><span class="kwd">systemctl</span><span class="pln"> </span><span class="kwd">reboot</span><span class="pln"> </span><span class="com">#</span><span class="pln"> </span><span class="kwd">or</span><span class="pln"> GUI equivalent</span>
  2. <span class="pun">...</span>
  3. <span class="pln">$ </span><span class="kwd">df</span><span class="pln"> </span><span class="pun">/</span><span class="pln"> </span><span class="pun">/</span><span class="pln">f26</span>
  4. <span class="typ">Filesystem</span><span class="pln"> </span><span class="lit">1K</span><span class="pun">-</span><span class="pln">blocks </span><span class="typ">Used</span><span class="pln"> </span><span class="typ">Available</span><span class="pln"> </span><span class="typ">Use</span><span class="pun">%</span><span class="pln"> </span><span class="typ">Mounted</span><span class="pln"> on</span>
  5. <span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">mapper</span><span class="pun">/</span><span class="pln">vg_sdg</span><span class="pun">-</span><span class="pln">f28 </span><span class="lit">20511312</span><span class="pln"> </span><span class="lit">14903196</span><span class="pln"> </span><span class="lit">4543156</span><span class="pln"> </span><span class="lit">77</span><span class="pun">%</span><span class="pln"> </span><span class="pun">/</span>
  6. <span class="str">/dev/</span><span class="pln">mapper</span><span class="pun">/</span><span class="pln">vg_sdg</span><span class="pun">-</span><span class="pln">f26 </span><span class="lit">20511312</span><span class="pln"> </span><span class="lit">14866412</span><span class="pln"> </span><span class="lit">4579940</span><span class="pln"> </span><span class="lit">77</span><span class="pun">%</span><span class="pln"> </span><span class="pun">/</span><span class="pln">f26</span>

你可能希望验证使用旧的引导菜单确实可以让你回到挂载在旧的根文件系统上的根。

现在按照此维基页面中的说明进行操作。如果系统升级出现任何问题,你还会有一个可以重启回去的工作系统。

进一步的考虑

创建新的逻辑卷并将根卷的快照复制到其中的步骤可以使用通用脚本自动完成。它只需要新的逻辑卷的名称,因为现有根的大小和设备很容易确定。例如,可以输入以下命令:

相关推荐