一、进入其它系统挂载待调整的磁盘

调整磁盘时,需要保证对应磁盘无任何读写操作。
可以通过别的系统来重新调整这个磁盘而不影响数据。

1.使用系统安装光盘引导进入live系统
2.使用pxe引导进入网络文件系统
3.把磁盘拆到另外一个linux机器上

具体方法可以根据实际情况采用。

二、做好重新调整分区前的准备工作

1.挂载分区

...
# create mount point 
{
  mkdir -p /run/chroot
  mkdir -p /run/chhome
} &> /dev/null

# mount exist partition
{
  mount /dev/sda1 /run/chroot/
  mount /dev/sda6 /run/chhome/
} &> /dev/null
...

2.备份数据并卸载分区

...
# backup /home

cp -a /run/chhome/* /run/chroot/home/ &> /dev/null
sleep 10

echo -e "Done!"

# umount all partition
{
  umount -l /run/chroot
  umount -l /run/chhome
} &> /dev/null
...

三、开始调整根分区

1.释放分配过大的分区空间

磁盘空间使用ks安装的时候被home目录占用过多,需要还给root分区。
另外给root分区扩展的空间必须保证后面是连续的一段柱面,如下所示:

--------------------------------------   调整为
| root(20G) | swap(80G) | home(500G) |  ========>
--------------------------------------

-------------------------------------- 
| root(520G)             | swap(80G) | 
--------------------------------------

因此需要先把swap和home分区全部删掉,备份操作已在上一步做过。

...
# delete extended partitions
echo -e "\e[92m>>>>> delete extended partitions... \e[0m"
echo -e "\e[93mOriginal partition status: \e[0m"
parted -l /dev/sda
echo -e ">>>>>> Starting..."
{
  parted -s /dev/sda rm 6
  sleep 1
  parted -s /dev/sda rm 5
  sleep 1
  parted -s /dev/sda rm 2
} >> /run/resizing.log
echo -e "\e[93mAfter operation partition status: \e[0m"
parted -l /dev/sda
...

2.调整分区把空间分配给root

具体做法就是使用parted工具将90%的空间都分配给root。使用到的命令:

  • resizepart(parted)
  • e2fsck
  • resize2fs
...
# resize exist / partition

echo -e "\e[92m>>>>> Resizing exist root partition \e[0m" 

echo -e "\e[93mBefore Resizing: \e[0m"
parted -l /dev/sda1

parted -s /dev/sda resizepart 1 90% >> /run/resizing.log
sleep 5

echo -e ">>>>>> Starting..."

echo -e "\e[93mAfter Resizing: \e[0m"
parted -l /dev/sda1


echo -e "\e[92m>>>>> Execute Disk checking and correct error... \e[0m"
e2fsck -fp /dev/sda1 &> /dev/null
sleep 3
echo -e "Done!"

echo -e "\e[92m>>>>> Write back to system... \e[0m"
resize2fs -f /dev/sda1 &> /dev/null
echo -e "Done!"
...

3.重新创建SWAP分区并更新UUID

除了创建SWAP分区,还要更新一下root分区中的/etc/fstab文件。
里面记录了各个分区的UUID信息,这里主要做了这么两个工作:

  • 删除home条目
  • 更新SWAP的UUID
...
# create SWAP

echo -e "\e[92m>>>>> Create new SWAP partition... \e[0m"
parted -s /dev/sda mkpartfs primary linux-swap 90% 98% &> /dev/null
mkfs.ext4 /dev/sda2 &> /dev/null

echo -e "Done!"

# mount root partition

echo -e "\e[92m>>>>> Configurating SWAP Partition... \e[0m"
mount /dev/sda1 /run/chroot &> /dev/null

cat /run/chroot/etc/fstab
blkid

# get /etc/fstab SWAP UUID
SWAP_UUID=`cat /run/chroot/etc/fstab | grep -v '^#' | awk '/swap/{ print $1 }' | cut -d "=" -f2`
sleep 3
sed -i '/home/d' /run/chroot/etc/fstab &> /dev/null 

umount -l /runt/chroot &> /dev/null

echo -e "Done!"

# change SWAP UUID
tune2fs -U $SWAP_UUID /dev/sda2 &> /dev/null

# confirm UUID Change

mount /dev/sda1 /run/chroot &> /dev/null

cat /run/chroot/etc/fstab
blkid

umount -l /runt/chroot &> /dev/null
# Reboot System!
...

完整脚本参考:

#!/bin/bash

clear

echo -e " \e[92m>>>>> Start to Backup the exist /home... \e[0m"

# create mount point 
{
  mkdir -p /run/chroot
  mkdir -p /run/chhome
} &> /dev/null

# mount exist partition
{
  mount /dev/sda1 /run/chroot/
  mount /dev/sda6 /run/chhome/
} &> /dev/null

# backup /home

cp -a /run/chhome/* /run/chroot/home/ &> /dev/null
sleep 10

echo -e "Done!"

# umount all partition
{
  umount -l /run/chroot
  umount -l /run/chhome
} &> /dev/null

# delete extended partitions
echo -e "\e[92m>>>>> delete extended partitions... \e[0m"
echo -e "\e[93mOriginal partition status: \e[0m"
parted -l /dev/sda
echo -e ">>>>>> Starting..."
{
  parted -s /dev/sda rm 6
  sleep 1
  parted -s /dev/sda rm 5
  sleep 1
  parted -s /dev/sda rm 2
} >> /run/resizing.log
echo -e "\e[93mAfter operation partition status: \e[0m"
parted -l /dev/sda

# resize exist / partition

echo -e "\e[92m>>>>> Resizing exist root partition \e[0m" 

echo -e "\e[93mBefore Resizing: \e[0m"
parted -l /dev/sda1

parted -s /dev/sda resizepart 1 90% >> /run/resizing.log
sleep 5

echo -e ">>>>>> Starting..."

echo -e "\e[93mAfter Resizing: \e[0m"
parted -l /dev/sda1


echo -e "\e[92m>>>>> Execute Disk checking and correct error... \e[0m"
e2fsck -fp /dev/sda1 &> /dev/null
sleep 3
echo -e "Done!"

echo -e "\e[92m>>>>> Write back to system... \e[0m"
resize2fs -f /dev/sda1 &> /dev/null
echo -e "Done!"


# create SWAP

echo -e "\e[92m>>>>> Create new SWAP partition... \e[0m"
parted -s /dev/sda mkpartfs primary linux-swap 90% 98% &> /dev/null
mkfs.ext4 /dev/sda2 &> /dev/null

echo -e "Done!"

# mount root partition

echo -e "\e[92m>>>>> Configurating SWAP Partition... \e[0m"
mount /dev/sda1 /run/chroot &> /dev/null

cat /run/chroot/etc/fstab
blkid

# get /etc/fstab SWAP UUID
SWAP_UUID=`cat /run/chroot/etc/fstab | grep -v '^#' | awk '/swap/{ print $1 }' | cut -d "=" -f2`
sleep 3
sed -i '/home/d' /run/chroot/etc/fstab &> /dev/null 

umount -l /runt/chroot &> /dev/null

echo -e "Done!"

# change SWAP UUID
tune2fs -U $SWAP_UUID /dev/sda2 &> /dev/null

# confirm UUID Change

mount /dev/sda1 /run/chroot &> /dev/null

cat /run/chroot/etc/fstab
blkid

umount -l /runt/chroot &> /dev/null
# Reboot System!
parted -l /dev/sda
echo -e "Finish!"
echo -e "\e[93mPlease confirm the partition correct then manual reboot! \e[0m"


linux   os      linux partition

本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!