将根分区迁移到另一个分区

将根分区迁移到另一个分区

我有一台装有 ubuntu 20.04 的远程服务器,但我无法使用 livecd 或 VM 控制台

我想将根分区从 /dev/sda2 迁移到 /dev/sda5

我的磁盘配置是:

根@应用程序服务器:~$lsblk

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   119G  0 disk
├─sda1   8:1    0  1007K  0 part
├─sda2   8:2    0  79,9G  0 part /
├─sda5   8:5    0    28G  0 part /app
├─sda6   8:6    0   4,7G  0 part /home
├─sda7   8:7    0   953M  0 part /srv
├─sda8   8:8    0   1,9G  0 part /tmp
└─sda9   8:9    0   3,7G  0 part /var

根@应用程序服务器:~#blkid

/dev/sda2: UUID="bda07617-0a2a-44cc-97fa-ff72d1bf45d2" TYPE="ext4" PARTLABEL="Linux filesystem" PARTUUID="45957f98-6bfd-4add-ad05-0b5a604c2423"
/dev/sda1: PARTLABEL="BIOS boot partition" PARTUUID="b33941c4-552c-4ff6-9c95-e7824e7a8102"
/dev/sda5: UUID="e0e56bbd-c2c2-4be5-91c8-02b50fc2f183" TYPE="ext4" PARTLABEL="Linux filesystem" PARTUUID="954341b7-1527-4eb2-ad39-04cb6c84ec72"
/dev/sda6: UUID="db1efe11-5dcf-4f03-a85e-ad379406d484" TYPE="ext4" PARTLABEL="Linux filesystem" PARTUUID="01f1ae61-03ec-405b-8b8a-7732baa74b3f"
/dev/sda7: UUID="2914d504-e0af-4acd-84d8-03e1c7e70136" TYPE="ext4" PARTLABEL="Linux filesystem" PARTUUID="d09bbe8c-5c39-4fdf-8503-fdfcdeb5e4d7"
/dev/sda8: UUID="0e8b5591-df05-4d38-82ca-9e395c098133" TYPE="ext4" PARTLABEL="Linux filesystem" PARTUUID="ae393080-4312-40fc-ba95-c7afc6770a12"
/dev/sda9: UUID="1be50204-6b50-48ec-9da9-cca8a88738eb" TYPE="ext4" PARTLABEL="Linux filesystem" PARTUUID="3e53f06f-764c-42ca-9c77-1c056472eee6"

根@应用程序服务器:~$df -Th

Filesystem     Type      Size  Used Avail Use% Mounted on
udev           devtmpfs  932M     0  932M   0% /dev
tmpfs          tmpfs     195M  1,7M  193M   1% /run
/dev/sda2      ext4       79G  6,3G   68G   9% /
tmpfs          tmpfs     971M     0  971M   0% /dev/shm
tmpfs          tmpfs     5,0M  4,0K  5,0M   1% /run/lock
tmpfs          tmpfs     971M     0  971M   0% /sys/fs/cgroup
/dev/sda6      ext4      4,6G   87M  4,2G   2% /home
/dev/sda9      ext4      3,6G  2,7G  792M  78% /var
/dev/sda8      ext4      1,8G  148K  1,7G   1% /tmp
/dev/loop0     squashfs   56M   56M     0 100% /snap/core18/2128
/dev/loop1     squashfs  128K  128K     0 100% /snap/bare/5
/dev/sda5      ext4       28G   34M   26G   1% /app
/dev/loop2     squashfs   56M   56M     0 100% /snap/core18/2714
/dev/loop3     squashfs   64M   64M     0 100% /snap/core20/1852
/dev/sda7      ext4      920M   24K  856M   1% /srv
/dev/loop6     squashfs   92M   92M     0 100% /snap/gtk-common-themes/1535
/dev/loop5     squashfs  219M  219M     0 100% /snap/gnome-3-34-1804/72
/dev/loop7     squashfs   66M   66M     0 100% /snap/gtk-common-themes/1515
/dev/loop8     squashfs   51M   51M     0 100% /snap/snap-store/547
/dev/loop9     squashfs   50M   50M     0 100% /snap/snapd/18596
tmpfs          tmpfs     195M   28K  195M   1% /run/user/1000
/dev/loop10    squashfs  219M  219M     0 100% /snap/gnome-3-34-1804/77
/dev/loop11    squashfs   46M   46M     0 100% /snap/snap-store/638
/dev/loop12    squashfs  347M  347M     0 100% /snap/gnome-3-38-2004/119

我停止了所有应用程序服务以避免磁盘更改
从 /dev/sda5 删除所有应用程序数据,卸载它
并将所有“/”复制到新的挂载点

root@app-server:~# rm -rf /app/*
root@app-server:~# umount /app
root@app-server:~# mkdir /mnt/newroot
root@app-server:~# mount /dev/sda2 /mnt/newroot
root@kzp-test-gpt:~#ll /mnt/newroot

根@应用程序服务器:~#rsync -aAXv / /mnt/newroot --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found,/app/*,/home/*,/var/*}

我知道我必须修改 grub 配置和 /etc/fstab

当我在 /mnt/newroot/etc/fstab 中更改 UUID 时,没有任何效果

如何配置 grub 将 /dev/sda5 挂载为根分区
我搜索了 sda 和当前根分区的 UUID,但一无所获

root@app-server:/mnt/newroot/etc/grub.d# grep -ri 'sda' /mnt/newroot/etc/grub.d/
root@app-server:/mnt/newroot/etc/grub.d#grep -ri 'bda07617-0a2a-44cc-97fa-ff72d1bf45d2' /mnt/newroot/etc/grub.d/

root@app-server:/mnt/newroot/etc/grub.d# grep -ri 'sda' /mnt/newroot/etc/default/grub
root@app-server:/mnt/newroot/etc/grub.d#grep -ri 'bda07617-0a2a-44cc-97fa-ff72d1bf45d2' /mnt/newroot/etc/default/grub

我的 grub 文件如下所示:

root@app-server:/mnt/newroot/etc/grub.d#cat /mnt/newroot/etc/default/grub

# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.
# For full documentation of the options in this file, see:
#   info -f grub -n 'Simple configuration'

GRUB_DEFAULT=0
GRUB_TIMEOUT_STYLE=hidden
GRUB_TIMEOUT=0
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
GRUB_CMDLINE_LINUX=""

# Uncomment to enable BadRAM filtering, modify to suit your needs
# This works with Linux (no patch required) and with any kernel that obtains
# the memory map information from GRUB (GNU Mach, kernel of FreeBSD ...)
#GRUB_BADRAM="0x01234567,0xfefefefe,0x89abcdef,0xefefefef"

# Uncomment to disable graphical terminal (grub-pc only)
#GRUB_TERMINAL=console

# The resolution used on graphical terminal
# note that you can use only modes which your graphic card supports via VBE
# you can see them in real GRUB with the command `vbeinfo'
#GRUB_GFXMODE=640x480

# Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux
#GRUB_DISABLE_LINUX_UUID=true

# Uncomment to disable generation of recovery mode menu entries
#GRUB_DISABLE_RECOVERY="true"

# Uncomment to get a beep at grub start
#GRUB_INIT_TUNE="480 440 1"

如何在 grub 中进行更改以及我应该更改哪些配置

旧的根/还是新的/mnt/newroot/fstab?有什么帮助吗

答案1

最后我找到了一个解决方案:
运行命令后
rsync -aAXv / /mnt/newroot --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found,/app/*,/home/*,/var/*}

我们必须编辑新的 fstab/mnt/newroot/etc/fstab
删除包含旧根 UUID 的行,并将包含路径的行从更改/app/

然后更改 UUID/boot/grub/grub.cfg
sed -i 's/<OLD_ROOT>/<NEW_ROOT>/g' /boot/grub/grub.cfg
我们将稍后修复 grub
重新启动并且 VM 将从新的根分区启动
检查一下df -h /

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda5        28G   12G   15G  45% /

接下来通过 fstab 删除旧的根分区
并更新 grub
grub-install /dev/sda
update-grub
reboot now

相关内容