如何通过 ssh 增加 CentOS 系统中的主分区大小?

如何通过 ssh 增加 CentOS 系统中的主分区大小?

我有这个:

###########################
# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root        20G  2.1G   17G  12% /
devtmpfs        2.0G     0  2.0G   0% /dev
tmpfs           2.0G     0  2.0G   0% /dev/shm
tmpfs           2.0G   34M  1.9G   2% /run
tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup

###########################
# fdisk -l
Disk /dev/sda: 2000.4 GB, 2000398934016 bytes, 3907029168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00044686

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        4096    40962047    20478976   83  Linux
/dev/sda2        40962048    42008575      523264   82  Linux swap / Solaris

###########################
# mount
/dev/sda1 on / type ext4 (rw,relatime,errors=remount-ro,data=ordered)
devtmpfs on /dev type devtmpfs (rw,relatime,size=2009752k,nr_inodes=502438,mode=755)
proc on /proc type proc (rw,relatime)
sysfs on /sys type sysfs (rw,relatime)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw)
devpts on /dev/pts type devpts (rw,relatime,mode=600,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (rw,nosuid,nodev,noexec,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpu,cpuacct)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls,net_prio)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=27,pgrp=1,timeout=300,minproto=5,maxproto=5,direct)
mqueue on /dev/mqueue type mqueue (rw,relatime)
configfs on /sys/kernel/config type configfs (rw,relatime)
fusectl on /sys/fs/fuse/connections type fusectl (rw,relatime)
binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,relatime)

###########################
(parted) print
Model: ATA HGST HUS724020AL (scsi)
Disk /dev/sda: 2000GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system     Flags
 1      2097kB  21.0GB  21.0GB  primary  ext4            boot
 2      21.0GB  21.5GB  536MB   primary  linux-swap(v1)

如您所见,一个 2Tb 的 HDD 分为 20Gb 用于根目录,多一点用于交换。安装和配置所有服务后,我注意到这个问题......现在我想扩展主分区。我不想把剩下的空间安装进去/home之类的东西。是一个具有特定任务的小型生产服务器,因此我想要主分区中的所有空间。

问题是我搜索了 Google,发现的只是“使用 live CD”或“启动进入救援模式”,但我不能这样做,我只能通过 工作ssh,所以我担心我会丢失所有数据并且我必须重新安装和重新配置一切,所以我更喜欢在开始做某事之前询问并搞砸一切。

答案1

好吧,我成功了。具体方法如下:

删除交换分区

# swapoff /dev/sda2

然后开始fdisk

# fdisk /dev/sda

列出分区信息,关心Start第一个分区的列:

Command (m for help): p

Device    Boot      Start         End      Blocks   Id  System
/dev/sda1    *       4096    47029167  1903512536   83  Linux
/dev/sda2        47029168    57029167    50000000   83  Linux

删除两个分区:

Command (m for help): d
Partition number (1,2, default 2): 2
Partition 1 has been deleted.

Command (m for help): d
Partition number (1): 1
Partition 1 has been deleted.

Start (4096)创建了具有新大小的新分区,但从默认分区开始:

Command (m for help): n
Select (default p): p
Partition number (1, default 1): 1
First sector (2048-3907029167, default 2048): 4096 //Important. Notice that is not the same as the default (in my case).
Last sector (default 3907029167): 3807029167 //I've put less to have spare space for partition 2

Command (m for help): n
Select (default p): p
Partition number (2, default 2): 2
First sector (2048-3907029167, default 2048): 3807029168 //Important. Notice that starts with 20148 because is the first empty space detected, but in reality I want it AFTER the first partition, so I've put another number (in my case).
Last sector (default 3907029167): 3907029167

然后我设置了Boot标志sda1

Command (m for help): a
Partition number (1,2, default 2): 1

然后重新启动并运行(重新启动后):

# resize2fs /dev/sda1

然后一切都很好:

################################
# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root       1.8T  2.1G  1.7T   1% /
devtmpfs        2.0G     0  2.0G   0% /dev
tmpfs           2.0G     0  2.0G   0% /dev/shm
tmpfs           2.0G  9.5M  2.0G   1% /run
tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup

################################
# fdisk -l
Disk /dev/sda: 2000.4 GB, 2000398934016 bytes, 3907029168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00044686

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        4096  3807029167  1903512536   83  Linux
/dev/sda2      3807029168  3907029167    50000000   83  Linux

现在我必须再次创建交换并完成。

相关内容