如何調整加密 LVM 的大小以安裝 Ubuntu 的另一個副本?

如何調整加密 LVM 的大小以安裝 Ubuntu 的另一個副本?

我有一台舊的 PowerBook,它已經愉快地運行 Ubuntu Server 幾個月了。現在我想對硬碟重新分割並安裝 Ubuntu 用戶端,同時保持現有安裝不變。

當我第一次安裝Ubuntu時,我選擇擦除整個硬碟並將其用作加密的LVM。現在,安裝程式似乎可以識別磁碟機上的分割區,但它從未提示我輸入 LVM 的密碼。

如何在目前安裝的旁邊騰出空間來安裝其他安裝?

答案1

沒有圖形工具可以調整加密分割區的大小。您可以從命令列手動執行此操作。

調整加密分割區的大小必須從 Live CD 執行,且 Live CD 不包含加密和 LVM 的支援。

啟動 live(桌面)CD 並安裝 lvm2 和 cryptsetup。

sudo apt-get update && sudo apt-get install lvm2 cryptsetup

載入 cryptsetup 模組。

sudo modprobe dm-crypt

解密您的檔案系統。

sudo cryptsetup luksOpen /dev/sda5 crypt1

取得 Live CD 來識別(啟動)您的 LVM。

sudo vgscan --mknodes
sudo vgchange -ay

現在您可以管理加密分割區、安裝它們、複製它們或執行維護(fsck、備份、調整大小)。

使用 resize2fs 減小檔案系統的大小(工具適用於 ext2 和 ext3 分割區)。您需要先檢查檔案系統,然後才能調整其大小。

sudo e2fsck -f /dev/mapper/hardy-root
sudo resize2fs -p /dev/mapper/hardy-root 5g

將“5g”替換為您想要的檔案系統大小(以 GB 為單位)。

使用 e2fsck 檢查檔案系統是否仍然完好無損。

sudo e2fsck -f /dev/mapper/hardy-root

減小根 (LVM) 邏輯磁碟區的大小。 -L 標誌表示您希望將 (LVM) 邏輯磁碟區的大小減少多少,因此請記住這一點。

使用 lvdisplay 顯示您的 (LVM) 邏輯磁碟區。

sudo lvdisplay

請注意您需要將根 (LVM) 邏輯磁碟區減少多少(在我的例子中為 4.3 Gb)。

sudo lvreduce -L -4.3G /dev/hardy/root

注意:您需要將“-4.3G”變更為適當的大小,以將 (LVM) 邏輯磁碟區減小到所需的大小。

重新顯示您的 (LVM) 邏輯磁碟區以檢查最終大小是否正確。

sudo lvdisplay

調整 (LVM) 實體磁碟區的大小。

LVM 使用的實體磁碟區可能會變得“碎片化”,因為 (LVM) 實體磁碟區內的 (LVM) 邏輯磁碟區並不總是依序排列。沒有碎片整理工具,因此如果您可能需要手動移動邏輯分割區(備份資料、刪除(LVM)邏輯磁碟區、重新建立替換(LVM)邏輯磁碟區、從備份還原資料)。

為了調整 (LVM) 實體磁碟區的大小,我必須手動移動(刪除然後重新建立)交換 (LVM) 邏輯磁碟區。

使用 pvdisplay 顯示實體磁碟區的大小

sudo pvdisplay

刪除交換 (LVM) 邏輯卷

sudo lvremove /dev/hardy/swap_1

調整 (LVM) 實體磁碟區的大小。

sudo pvresize --setphysicalvolumesize 5.6G /dev/mapper/crypt1

現在我們將恢復(重新建立)交換(LVM)邏輯磁碟區。

設定 (LVM) 實體磁碟區的權限以允許分配(如果需要)

sudo pvchange -x y /dev/mapper/crypt1

復原交換 (LVM) 邏輯磁碟區。

sudo lvcreate -L 512m -n swap_1 hardy
sudo mkswap -L swap_1 /dev/hardy/swap1

mkswap 指令完成後,它將把新的 uuid 印到終端機。

使用新的 uuid 更新 fstab(使用任何編輯器)

掛載根 (LVM) 邏輯磁碟區。

sudo mount /dev/hardy/root /mnt

編輯/etc/fstab

gksu gedit /mnt//etc/fstab

將新的 uuid 從終端複製貼上到 fstab,更新交換分區的 uuid。儲存並退出 gedit 卸載根 (LVM) 邏輯卷

sudo umount /mnt

新增 (LVM) 邏輯磁碟區交換後重新鎖定 (LVM) 實體磁碟區(鎖定 (LVM) 實體磁碟區可防止其變更)。

sudo pvchange -x n /dev/mapper/crypt1

調整你的地下室的大小。

使用 cryptsetup 顯示你的隱窩的大小。

sudo cryptsetup status crypt1

這顯示了您的地下室的扇區大小。記下偏移量

offset: 2056 sectors

使用 cryptsetup 調整大小。

sudo cryptsetup -o 2056 -b 11800000 resize crypt1

-o = offset (get this from the status command) -b = size in sectors.

使用 fdisk 調整分割區大小。

卸載 LVM 和 crypt :

sudo vgchange -an
sudo cryptsetup luksClose crypt1

現在最可怕的部分是,使用 fdisk 手動調整分割區大小。

如果您不熟悉 fdisk,我建議您閱讀如何使用 fdisk 分割區

fdisk 不會覆蓋數據,因此如果您犯了錯誤,您應該能夠「撤消」更改。

使用 fdisk 列出您的分割區資訊。

sudo fdisk -l

寫下此訊息(或列印出來)。

重寫你的分區表。為此,請使用 fdisk 刪除分割區並重新建立它們,但尺寸較小。

sudo fdisk /dev/sda

這是我的 fdisk 會話:

The number of cylinders for this disk is set to 1305.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): d
Partition number (1-5): 5

Command (m for help): d
Partition number (1-5): 2

Command (m for help): n
Command action
e extended
p primary partition (1-4)
e
Partition number (1-4): 2
First cylinder (32-1305, default 32):
Using default value 32
Last cylinder or +size or +sizeM or +sizeK (32-1305, default 1305): +6000M

Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (32-761, default 32):
Using default value 32
Last cylinder or +size or +sizeM or +sizeK (32-761, default 761):
Using default value 761

Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (762-1305, default 762):
Using default value 762
Last cylinder or +size or +sizeM or +sizeK (762-1305, default 1305):
Using default value 1305

Command (m for help): p

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x000a6bf9

Device Boot Start End Blocks Id System
/dev/sda1 * 1 31 248976 83 Linux
/dev/sda2 32 761 5863725 5 Extended
/dev/sda3 762 1305 4369680 83 Linux
/dev/sda5 32 761 5863693+ 83 Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

取消出現的「身份驗證」對話方塊(Live CD 正在嘗試自動掛載您的新分割區)。

LVM 分割區的系統 ID 也應變更為「Linux LVM」:

Command (m for help): t
Partition number (1-5): 5
Hex code (type L to list codes): 8e
Command (m for help): w

如果不這樣做,可能會導致邏輯分割區的起始磁區與原來的位置不對齊,而 LVM 將無法讀取!

我強烈建議您先閱讀完整的 Wiki 頁面

看:https://help.ubuntu.com/community/ResizeEncryptedPartitions

一旦您有可用空間,您就可以使用它從圖形安裝程式安裝 ubuntu。

我建議您在完成調整大小後和安裝之前重新啟動。

相關內容