磁盘扩容实战

不损坏数据的情况下扩容非lvm磁盘

磁盘中有重要数据时请提前备份数据

## 1. 查看当前磁盘挂载情况
[root@test ~]# lsblk 
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0     11:0    1  436K  0 rom  
vda    253:0    0   20G  0 disk 
└─vda1 253:1    0   20G  0 part /
vdb    253:16   0    1G  0 disk 
└─vdb1 253:17   0 1023M  0 part /mnt
## 2. 在待扩容磁盘中写入数据,以备检查
[root@test ~]# echo hehe > /mnt/check.txt
[root@test ~]# cat /mnt/check.txt
hehe
## 使用fdisk来删除之前的分区,不格式化重新新建分区
## 3. 查看当前sdb1的柱面空间
[root@test ~]# fdisk -l | grep vdb1
/dev/vdb1            2048     2097151     1047552   83  Linux
## 4. 取消挂载vdb
[root@test ~]# umount /mnt/
## 5. 增加vdb大小到2G
[root@test ~]# fdisk -l | grep vdb
Disk /dev/vdb: 2147 MB, 2147483648 bytes, 4194304 sectors
/dev/vdb1            2048     2097151     1047552   83  Linux
## 6. 删除重建分区
[root@test /]# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): d
Selected partition 1
Partition 1 is deleted

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-4194303, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-4194303, default 4194303): 
Using default value 4194303
Partition 1 of type Linux and of size 2 GiB is set

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

Calling ioctl() to re-read partition table.
Syncing disks.
## 7. 查看是否有变化,挂载后还是现实1G容量
[root@test /]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0     11:0    1  436K  0 rom  
vda    253:0    0   20G  0 disk 
└─vda1 253:1    0   20G  0 part /
vdb    253:16   0    2G  0 disk 
└─vdb1 253:17   0    2G  0 part 
[root@test /]# mount /dev/vdb1 /mnt/
[root@test /]# ls /mnt/
check.txt
[root@test /]# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/vda1      xfs        20G  1.3G   19G   7% /
devtmpfs       devtmpfs  236M     0  236M   0% /dev
tmpfs          tmpfs     245M     0  245M   0% /dev/shm
tmpfs          tmpfs     245M  4.3M  241M   2% /run
tmpfs          tmpfs     245M     0  245M   0% /sys/fs/cgroup
tmpfs          tmpfs      49M     0   49M   0% /run/user/0
/dev/vdb1      xfs      1021M   33M  989M   4% /mnt
## 8. 由于磁盘分区格式为xfs,使用resize2fs命令调整分区大小失败,改用xfs_growfs
[root@test /]# resize2fs /dev/vdb1
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/vdb1
Couldn't find valid filesystem superblock.
[root@test /]# xfs_growfs /dev/vdb1
meta-data=/dev/vdb1              isize=256    agcount=5, agsize=65472 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=262144, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 262144 to 524032
## 9. 检查分区大小,及文件是否丢失
[root@test /]# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/vda1      xfs        20G  1.3G   19G   7% /
devtmpfs       devtmpfs  236M     0  236M   0% /dev
tmpfs          tmpfs     245M     0  245M   0% /dev/shm
tmpfs          tmpfs     245M  4.3M  241M   2% /run
tmpfs          tmpfs     245M     0  245M   0% /sys/fs/cgroup
tmpfs          tmpfs      49M     0   49M   0% /run/user/0
/dev/vdb1      xfs       2.0G   33M  2.0G   2% /mnt
[root@test /]# cat /mnt/check.txt 
hehe

扩容LVM类型磁盘

## 格式化磁盘,创建vdb1
[root@test ~]# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): p

Disk /dev/vdb: 2147 MB, 2147483648 bytes, 4194304 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: 0x4081529f

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-4194303, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-4194303, default 4194303): +1G
Partition 1 of type Linux and of size 1 GiB is set

Command (m for help): p

Disk /dev/vdb: 2147 MB, 2147483648 bytes, 4194304 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: 0x4081529f

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048     2099199     1048576   83  Linux

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

Calling ioctl() to re-read partition table.
Syncing disks.
## 刷新分区表
[root@test ~]# partprobe 
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
[root@test ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0     11:0    1  436K  0 rom  
vda    253:0    0   20G  0 disk 
└─vda1 253:1    0   20G  0 part /
vdb    253:16   0    2G  0 disk 
└─vdb1 253:17   0    1G  0 part
## 创建pv 
[root@test ~]# pvcreate /dev/vdb1
WARNING: xfs signature detected on /dev/vdb1 at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/vdb1.
  Physical volume "/dev/vdb1" successfully created.
[root@test ~]# pvdisplay 
  "/dev/vdb1" is a new physical volume of "1.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/vdb1
  VG Name               
  PV Size               1.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               ubeV8j-VJmO-k7qL-iFw6-4CgU-YYaM-KnZfef
## 创建vg   
[root@test ~]# vgcreate vg001 /dev/vdb1 
  Volume group "vg001" successfully created
[root@test ~]# vgdisplay 
  --- Volume group ---
  VG Name               vg001
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               1020.00 MiB
  PE Size               4.00 MiB
  Total PE              255
  Alloc PE / Size       0 / 0   
  Free  PE / Size       255 / 1020.00 MiB
  VG UUID               3iynG9-qJGI-BvuG-Ujo9-NYrr-SwcQ-4atcrf
## 创建lv   
[root@test ~]# lvcreate -L 1020MiB -n 1vData vg001
  Logical volume "1vData" created.
[root@test ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/vg001/1vData
  LV Name                1vData
  VG Name                vg001
  LV UUID                tLk0te-ce3n-LfRd-pKsZ-ayCn-X1fu-staj2p
  LV Write Access        read/write
  LV Creation host, time test, 2017-09-20 09:03:49 +0800
  LV Status              available
  # open                 0
  LV Size                1020.00 MiB
  Current LE             255
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           252:0
## 格式化lv 
[root@test ~]# mkfs.ext4 /dev/vg001/1vData 
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65280 inodes, 261120 blocks
13056 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8160 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
## 挂载lv 
[root@test ~]# mount /dev/vg001/1vData /mnt/
[root@test ~]# ls
[root@test ~]# touch haha > hehe.txt
## 创建新的分区vdb2
[root@test ~]# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 2
First sector (2099200-4194303, default 2099200): 
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-4194303, default 4194303): 
Using default value 4194303
Partition 2 of type Linux and of size 1023 MiB is set

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@test ~]# lsblk
NAME             MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0               11:0    1  436K  0 rom  
vda              253:0    0   20G  0 disk 
└─vda1           253:1    0   20G  0 part /
vdb              253:16   0    2G  0 disk 
└─vdb1           253:17   0    1G  0 part 
  └─vg001-1vData 252:0    0 1020M  0 lvm  /mnt 
## 刷新分区表
[root@test ~]# partprobe 
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
[root@test ~]# lsblk
NAME             MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0               11:0    1  436K  0 rom  
vda              253:0    0   20G  0 disk 
└─vda1           253:1    0   20G  0 part /
vdb              253:16   0    2G  0 disk 
├─vdb1           253:17   0    1G  0 part 
│ └─vg001-1vData 252:0    0 1020M  0 lvm  /mnt
└─vdb2           253:18   0 1023M  0 part 
[root@test ~]# mkfs.ext4 /dev/vdb2
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65536 inodes, 261888 blocks
13094 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
## 查看vdb磁盘分区
[root@test ~]# partx /dev/vdb
NR   START     END SECTORS  SIZE NAME UUID
 1    2048 2099199 2097152    1G      
 2 2099200 4194303 2095104 1023M
## 创建pv 
[root@test ~]# pvcreate /dev/vdb2
WARNING: ext4 signature detected on /dev/vdb2 at offset 1080. Wipe it? [y/n]: y
  Wiping ext4 signature on /dev/vdb2.
  Physical volume "/dev/vdb2" successfully created.
[root@test ~]# vgdisplay 
  --- Volume group ---
  VG Name               vg001
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               1020.00 MiB
  PE Size               4.00 MiB
  Total PE              255
  Alloc PE / Size       255 / 1020.00 MiB
  Free  PE / Size       0 / 0   
  VG UUID               3iynG9-qJGI-BvuG-Ujo9-NYrr-SwcQ-4atcrf
## 将pv vdb2 加入到vg001   
[root@test ~]# vgextend vg001 /dev/vdb2
  Volume group "vg001" successfully extended
[root@test ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/vg001/1vData
  LV Name                1vData
  VG Name                vg001
  LV UUID                tLk0te-ce3n-LfRd-pKsZ-ayCn-X1fu-staj2p
  LV Write Access        read/write
  LV Creation host, time test, 2017-09-20 09:03:49 +0800
  LV Status              available
  # open                 1
  LV Size                1020.00 MiB
  Current LE             255
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           252:0
   
[root@test ~]# vgdisplay 
  --- Volume group ---
  VG Name               vg001
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               1.99 GiB
  PE Size               4.00 MiB
  Total PE              510
  Alloc PE / Size       255 / 1020.00 MiB
  Free  PE / Size       255 / 1020.00 MiB
  VG UUID               3iynG9-qJGI-BvuG-Ujo9-NYrr-SwcQ-4atcrf
## 扩展lv大小   
[root@test ~]# lvextend -L 2040MiB /dev/vg001/1vData
  Size of logical volume vg001/1vData changed from 1020.00 MiB (255 extents) to 1.99 GiB (510 extents).
  Logical volume vg001/1vData successfully resized.
## 调整lv大小
[root@test ~]# resize2fs /dev/vg001/1vData 
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg001/1vData is mounted on /mnt; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vg001/1vData is now 522240 blocks long.
## 检查挂载情况
[root@test ~]# df -h
Filesystem                Size  Used Avail Use% Mounted on
/dev/vda1                  20G  1.3G   19G   7% /
devtmpfs                  236M     0  236M   0% /dev
tmpfs                     245M     0  245M   0% /dev/shm
tmpfs                     245M   21M  225M   9% /run
tmpfs                     245M     0  245M   0% /sys/fs/cgroup
tmpfs                      49M     0   49M   0% /run/user/0
/dev/mapper/vg001-1vData  2.0G  3.0M  1.9G   1% /mnt

fstab 配置详解

[root@test ~]# cat /etc/fstab 
UUID=6001f930-2d2b-4e57-b55b-3fde13756c7d /                       xfs     defaults        0 0
  • 第一列:设备名或者设备卷标名,(/dev/sda10 或者 LABEL=/)
  • 第二列:设备挂载目录 (例如上面的“/”或者“/mnt/D/”)
  • 第三列:设备文件系统 (例如上面的“ext3”或者“vfat”)
  • 第四列:挂载参数 (看帮助man mount)

    对于已经挂载好的设备,例如上面的/dev/sda2,现在要改变挂载参数,这时可以不用卸载该设备,而可以使用下面的命令(没有挂载的设备,remount 这个参数无效)
    #mount /mnt/D/ -o remount,ro (改defaults为ro)
    为了安全起见,可以指明其他挂载参数,例如:
    noexec(不允许可执行文件可执行,但千万不要把根分区挂为noexec,那就无法使用系统了,连mount 命令都无法使用了,这时只有重新做系统了!
    nodev(不允许挂载设备文件)
    nosuid,nosgid(不允许有suid和sgid属性)
    nouser(不允许普通用户挂载)

  • 第五列:指明是否要备份,(0为不备份,1为要备份,一般根分区要备份)
  • 第六列:指明自检顺序。 (0为不自检,1或者2为要自检,如果是根分区要设为1,其他分区只能是2)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,686评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,668评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,160评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,736评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,847评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,043评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,129评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,872评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,318评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,645评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,777评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,470评论 4 333
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,126评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,861评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,095评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,589评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,687评论 2 351

推荐阅读更多精彩内容