Skip to content

Instantly share code, notes, and snippets.

@lyshie
Last active June 17, 2019 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lyshie/2f20d8a233b3b8c10160ca097d9f648f to your computer and use it in GitHub Desktop.
Save lyshie/2f20d8a233b3b8c10160ca097d9f648f to your computer and use it in GitHub Desktop.
如何升級臺南市 IaaS 虛擬主機的 Ubuntu (How to upgrade Ubuntu from 12 to the latest version?)

原理

因臺南市 IaaS 虛擬主機沒有提供 console 畫面進行管理,所以必須透過 SSH 遠端登入來操作。網路連線實為必要,如在 VM 上可透過 PXE 優先開機,便可使用以下方法進行移機。透過 PXE Server 的啟用與停用,等同於切換網路開機或本機開機。最後使用 chroot 方式轉移系統。

一、設定 PXE Server

透過 PXE Server 派送 IP 與作業系統給 IaaS 上的 Ubuntu 主機,提供網路開機與簡易的 GNU/Linux 作業系統環境。

二、切換開機模式

  • 設定 VM 開機順序第一個是 PXE,下一個是本機硬碟,停用網路開機就可回到本機硬碟開機。
  • 將 PXE Server 置於同網段內,提供網路開機,派送 SystemRescueCd
  • 開機順序
    • 網路開機:進入 SystemRescueCd 進行移機
    • 本機硬碟開機:測試移機是否正常,待網路正常啟用,即可透過 SSH 連線管理

三、如何移機?

  • 調整檔案系統,縮小容量
# e2fsck -f /dev/ubuntu/root
# resize2fs /dev/ubuntu 30G
  • 調整 LVM,縮小容量
# lvreduce -L 32G /dev/ubuntu/root
# resize2fs /dev/ubuntu/root
  • 建立新的檔案系統
# lvcreate -L+15G -n newroot ubuntu
# mkfs.ext4 /dev/ubuntu/newroot -L newroot
  • 搬移系統
# mount /dev/ubuntu/root /mnt/backup
# mount /dev/sda1 /mnt/backup/boot
# mount -o bind /proc /mnt/backup/proc
# mount -o bind /sys /mnt/backup/sys
# mount -o bind /dev /mnt/backup/dev

# mount /dev/ubuntu/newroot /mnt/custom
# cd /mnt/custom
# rsync -avH --progress /mnt/backup/ .
  • 在 newroot 上安裝新版本或升級
# SHELL=/bin/bash chroot /mnt/backup
  • 設定新系統環境與開機
(newroot)# update-initramfs -k all -c
(newroot)# grub-install /dev/sda
(newroot)# grub-mkconfig -o /boot/grub/grub.cfg
  • 卸載 newroot,重新開機
  • 暫時關閉 PXE Server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment