磁盘扩容实战

不损坏数据的情况下扩容非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)

你可能感兴趣的:(磁盘扩容实战)