Linux下使用fdisk扩大分区容量(不丢失数据)

环境:

模拟新加一块磁盘,并分区,挂载,永久挂载,然后写入数据,然后对分区扩容,测试扩容后分区里面的内容是否丢失。

查看所有分区:

[[email protected] ~]# fdisk -l
Disk /dev/sda: 1073.7 GB, 1073741824000 bytes
255 heads, 63 sectors/track, 130541 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00040b96
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        1070     8388608   82  Linux swap / Solaris
/dev/sda3            1070       13055    96263168   83  Linux
Disk /dev/sdb: 1073.7 GB, 1073741824000 bytes
255 heads, 63 sectors/track, 130541 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

开始分区:

[[email protected] ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xc6e44e94.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): p            #p是打印当前磁盘所有分区

Disk /dev/sdb: 1073.7 GB, 1073741824000 bytes
255 heads, 63 sectors/track, 130541 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xc6e44e94

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n            #n是新建分区
Command action
   e   extended
   p   primary partition (1-4)
p                                  #新建主分区
Partition number (1-4): 1          #给主分区编号为1,从1开始编号
First cylinder (1-130541, default 1):     #回车,默认从1开始,也可以手动输入1
Using default value 1                        #起始分区的位置
Last cylinder, +cylinders or +size{K,M,G} (1-130541, default 130541):     #直接回车,默认把磁盘所有空间分配到此分区。这里支持K,M,G,如果想给此分区分配100M空间,就输入+100M;如果要分配100G那么就输入+100G
Using default value 130541                    #结束分区的位置

Command (m for help): w                #w为写入分区信息并保存,然后退出
The partition table has been altered!

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

查看刚刚分区的信息:/dev/sdb1

[[email protected] ~]# fdisk -l
Disk /dev/sda: 1073.7 GB, 1073741824000 bytes
255 heads, 63 sectors/track, 130541 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00040b96
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        1070     8388608   82  Linux swap / Solaris
/dev/sda3            1070       13055    96263168   83  Linux
Disk /dev/sdb: 1073.7 GB, 1073741824000 bytes
255 heads, 63 sectors/track, 130541 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xc6e44e94
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1      130541  1048570551   83  Linux

格式化分区:mkfs.ext4 /dev/sdb1

[[email protected] ~]# mkfs.ext4 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65536000 inodes, 262142637 blocks
13107131 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
8000 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
        4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 
        102400000, 214990848
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: 
done
This filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

挂载分区:

[[email protected] backup]# mkdir /backup
[[email protected] backup]# mount /dev/sdb1 /backup/
[[email protected] backup]# df -hT
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/sda3      ext4    91G  2.2G   84G   3% /
tmpfs          tmpfs  1.9G     0  1.9G   0% /dev/shm
/dev/sda1      ext4   190M   38M  142M  21% /boot
/dev/sdb1      ext4   985G   72M  935G   1% /backup

挂载磁盘:(永久挂载)

通过vi编辑器编辑/etc/fstab文件,内容如下,(最后一行是刚添加上去的):

[[email protected] ~]# cat /etc/fstab 
#
# /etc/fstab
# Created by anaconda on Mon May 14 02:01:11 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=7d223255-1b77-4f58-bd72-262928547fcf /                       ext4    defaults        1 1
UUID=284d6b31-8723-497b-bc7b-3c12ede23ee6 /boot                   ext4    defaults        1 2
UUID=5d36c1b3-df6e-480d-80ca-72befc66f5e9 swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/sdb1                   /backup                 ext4    defaults        0 0

第四列挂载参数:通过查看man mount 来查看

第五列是否要备份:(0为不备份,1为要备份,一般情况下不用做备份)

第六列自检程序  (0为不自检,1或2为要自检,如果是根分区要设置1,其它分区只能是2)

[[email protected] ~]# echo backup >/backup/backup.txt
[[email protected] ~]# cat /backup/backup.txt 
backup

此时机器不关机,用vmware vspere client扩容磁盘大小。

[[email protected] ~]# fdisk -l /dev/sdb
Disk /dev/sdb: 1073.7 GB, 1073741824000 bytes
255 heads, 63 sectors/track, 130541 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xc6e44e94
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1      130541  1048570551   83  Linux

此时发现扩容的磁盘容量看不到,所以还是重启一下吧,建议关机时候再扩容。

[[email protected] ~]# umount /dev/sdb1    #取消挂载
[[email protected] ~]# fdisk /dev/sdb    
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').
Command (m for help): p
Disk /dev/sdb: 1181.1 GB, 1181116006400 bytes
255 heads, 63 sectors/track, 143595 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xc6e44e94
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1      130541  1048570551   83  Linux
Command (m for help): d        #因为此磁盘只有一个分区sdb1,所以按d删除时候默认不会让选择要删除的分区,如果有多个分区会提示要删除的分区。
Selected partition 1
Command (m for help): p        #打印当前分区,发现分区已删除
Disk /dev/sdb: 1181.1 GB, 1181116006400 bytes
255 heads, 63 sectors/track, 143595 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xc6e44e94
   Device Boot      Start         End      Blocks   Id  System
Command (m for help): n        #新建分区
Command action
   e   extended
   p   primary partition (1-4)
p                                #新建主分区
Partition number (1-4): 1        #和扩容前的分区标号必须一样
First cylinder (1-143595, default 1):     #默认是1,比如要和扩容前的分区其实点一样,扩容前的分区起始也是1,这个是扩容的重点地方。
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-143595, default 143595): #终止的位置是默认是硬盘柱体的尾部,即使最大值,这个是要扩容的点,回车。
Using default value 143595
Command (m for help): w                           #w报错,并退出。
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

3、调整分区

e2fsck -f /dev/sdb1 #检查分区信息

resize2fs /dev/sdb1 #调整分区大小

[[email protected] ~]# e2fsck -f /dev/sdb1    #由于磁盘容量很大,数据多的情况下需要等待一段时间
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdb1: 12/65536000 files (0.0% non-contiguous), 4164250/262142637 blocks
[[email protected] ~]# resize2fs /dev/sdb1     #由于磁盘容量很大,数据多的情况下需要等待一段时间
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/sdb1 to 288356701 (4k) blocks.
The filesystem on /dev/sdb1 is now 288356701 blocks long.
[[email protected] ~]# mount /dev/sdb1 /backup/
[[email protected] ~]# cat /backup/
backup.txt  lost+found/ 
[[email protected] ~]# cat /backup/backup.txt 
backup

发现扩容成功,扩容前的内容没有丢失。

参考内容:

https://blog.csdn.net/wuweilong/article/details/7538634

https://blog.csdn.net/jackyu651/article/details/53070607

https://blog.csdn.net/a746742897/article/details/52689285

原文地址:http://blog.51cto.com/sandshell/2119523

时间: 2024-08-04 21:43:12

Linux下使用fdisk扩大分区容量(不丢失数据)的相关文章

Linux下使用fdisk扩展分区容量

导读 我们管理的服务器可能会随着业务量的不断增长造成磁盘空间不足的情况,比如:共享文件服务器硬盘空间不足,在这个时候我们就需要增加磁盘空间,来满足线上的业务:又或者我们在使用linux的过程中, 有时会因为安装系统时分区不当导致有的分区空间不足,而有的分区空间过剩的情况,都可以是使用fdisk分区工具来动态调整分区的大小: 扩展磁盘空间 硬盘空间为20G,使用vSphere Client增加磁盘大小,需要再增加10G空间; 扩展完后,重启系统,再次使用fdisk -l查看,会发现硬盘空间变大了:

在Linux下用fdisk创建分区

一.输入 fdisk -l /dev/sda ,观察硬盘之实体使用情形.二.输入 fdisk /dev/sda,可进入分割硬盘模式. 1. 输入 m 显示所有命令列示. 2. 输入 p 显示硬盘分割情形. 3. 输入 a 设定硬盘启动区. 4. 输入 n 设定新的硬盘分割区. 4.1. 输入 e 硬盘为[延伸]分割区(extend). 4.2. 输入 p 硬盘为[主要]分割区(primary). 5. 输入 t 改变硬盘分割区属性. 6. 输入 d 删除硬盘分割区属性. 7. 输入 q 结束不存

linux下使用fdisk进行分区

在linux系统下面经常要使用到fdisk对硬盘进行分区 一.查看系统硬盘信息 [[email protected] ~]# fdisk -l Disk /dev/sda: 214.7 GB, 214748364800 bytes 255 heads, 63 sectors/track, 26108 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 byte

Linux centos下使用fdisk扩展分区容量大小

扩展磁盘空间 硬盘空间为20G,使用vSphere Client增加磁盘大小,需要再增加10G空间; 扩展完后,重启系统,再次使用fdisk -l查看,会发现硬盘空间变大了: [[email protected] ~]# fdisk -l Disk /dev/sda: 32.2 GB, 32212254720 bytes 255 heads, 63 sectors/track, 3916 cylinders Units = cylinders of 16065 * 512 = 8225280 b

Linux下使用fdisk命令和partprobe命令,在不重启系统的情况下新建分区并格式化

由于工作的需要,最近一段时间一直在学习Linux.学习一门新的知识,我是喜欢根据谋一本书或者某一个学习视频系统的学习,这样可以对学习的新知识有一个系统全面的认识和了解.所以学习之前,没了一本鸟哥的私房菜做为参考书,开始系统的学习Linux. 根据鸟哥的建议,安装虚拟机时,预留了一块空的容量用来练习分区使用.所以在虚拟机上安装Linux系统分区时,只划分了一块4G的容量挂载到根目录“/”下,还有就是只划分了一个2G的swap分区.还有14G的容量没有分配. 在学习磁盘管理时,使用fdisk将剩余的

linux下使用fdisk命令进行磁盘分区

linux下使用fdisk命令进行磁盘分区 目录 分区类型 分区方法表示 文件系统 fdisk命令分区过程 分区类型 主分区:总共最多只能分四个 扩展分区:只能有一个,也算作主分区的一种,也就是说主分区加扩展分区最多有四个.但是扩展分区不能存储数据和格式化,必须再划分成逻辑分区才能使用. 逻辑分区:逻辑分区是在扩展分区中划分的,如果是IDE硬盘,Linux最多支持59个逻辑分区,如果是IDE硬盘,Linux最多支持59个逻辑分区,如果是SCSI硬盘Linux最多支持11个逻辑分区 分区方法表示

推荐CentOS Linux下的3款分区工具

简介 伴随着科技的飞速发展,越来越多的企业对于服务器的稳定要求越来越高,越来越多的企业开始采用linux系统来部署自己的服务,以求高效的稳定性,当然任何操作系统都需要一个最基本的基础,那就是硬盘,及硬盘分区,今天来给大家推荐几款CentOS Linux下的分区工具及如何查看分区环境,也会给大家来带一些硬盘的基本知识 一.硬盘的接口类型 分区肯定是对硬盘进行分区,那么先来聊一聊硬盘的接口类型,硬盘就现在来分,分为两类,并行接口和串行接口,现在服务器及PC机上普遍的都是串行接口啦. 并行接口,分为I

Linux磁盘及文件系统(二)Linux下磁盘命名和分区

在为主机添加硬盘之前,首先需要了解Linux系统下对硬盘和分区的命令方法 一.磁盘命名 Linux下对SCSI和SATA设备是以sd命名的,第一个SCSI设备是sda,第二个是sdb....以此类推.一般主板上有两个SCSI接口,因此一共可以安装4个SCSI设备.主SCSI上的两个设备分别对应sda和sdb,第二个SCSI口上的设备对应sdc和sdd.一般硬盘安装在SCSI的主接口上,所以是sda和sdb,而光驱一般安装在第二个SCSI的主接口上,所以是sdc.IDE有两个口,第一个IDE口叫做

linux下给U盘分区&制作文件系统

这几天读到TLCL-Storage Media一节,不由的想要折腾一下U盘,一直以来U盘只是被拿来暂存数据,其内部有没有文件系统,数据怎么管理,那是从来也不清楚,本文就依葫芦画瓢,折腾下手中的Kingston U盘 注:本文非教程,仅学以致乐. 初探 先看看U盘接受windows格式化以后的样子,这应该是我们对付U盘最常用的一招:一言不合,格之. 不同于Ubuntu/Win等桌面发行版本,服务器型(非图形化的系统?)的linux系统通常不主动挂载U盘,因此当U盘插入树莓派后,命令行是不会有任何提