不损坏数据的情况下扩容非lvm磁盘
磁盘中有重要数据时请提前备份数据
## 1. 查看当前磁盘挂载情况
[root@test ~]# lsblk
NAMEMAJ:MIN RMSIZE RO TYPE MOUNTPOINT
sr011:01436K0 rom
vda253:0020G0 disk
└─vda1 253:1020G0 part /
vdb253:1601G0 disk
└─vdb1 253:170 1023M0 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/vdb120482097151104755283Linux
## 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/vdb120482097151104755283Linux
## 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 deletedCommand (m for help): n
Partition type:
pprimary (0 primary, 0 extended, 4 free)
eextended
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 setCommand (m for help): w
The partition table has been altered!Calling ioctl() to re-read partition table.
Syncing disks.
## 7. 查看是否有变化,挂载后还是现实1G容量
[root@test /]# lsblk
NAMEMAJ:MIN RMSIZE RO TYPE MOUNTPOINT
sr011:01436K0 rom
vda253:0020G0 disk
└─vda1 253:1020G0 part /
vdb253:1602G0 disk
└─vdb1 253:1702G0 part
[root@test /]# mount /dev/vdb1 /mnt/
[root@test /]# ls /mnt/
check.txt
[root@test /]# df -Th
FilesystemTypeSizeUsed Avail Use% Mounted on
/dev/vda1xfs20G1.3G19G7% /
devtmpfsdevtmpfs236M0236M0% /dev
tmpfstmpfs245M0245M0% /dev/shm
tmpfstmpfs245M4.3M241M2% /run
tmpfstmpfs245M0245M0% /sys/fs/cgroup
tmpfstmpfs49M049M0% /run/user/0
/dev/vdb1xfs1021M33M989M4% /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=https://www.it610.com/dev/vdb1isize=256agcount=5, agsize=65472 blks
=sectsz=512attr=2, projid32bit=1
=crc=0finobt=0
data=bsize=4096blocks=262144, imaxpct=25
=sunit=0swidth=0 blks
naming=version 2bsize=4096ascii-ci=0 ftype=0
log=internalbsize=4096blocks=853, version=2
=sectsz=512sunit=0 blks, lazy-count=1
realtime =noneextsz=4096blocks=0, rtextents=0
data blocks changed from 262144 to 524032
## 9. 检查分区大小,及文件是否丢失
[root@test /]# df -Th
FilesystemTypeSizeUsed Avail Use% Mounted on
/dev/vda1xfs20G1.3G19G7% /
devtmpfsdevtmpfs236M0236M0% /dev
tmpfstmpfs245M0245M0% /dev/shm
tmpfstmpfs245M4.3M241M2% /run
tmpfstmpfs245M0245M0% /sys/fs/cgroup
tmpfstmpfs49M049M0% /run/user/0
/dev/vdb1xfs2.0G33M2.0G2% /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): pDisk /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: 0x4081529fDevice BootStartEndBlocksIdSystemCommand (m for help): n
Partition type:
pprimary (0 primary, 0 extended, 4 free)
eextended
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 setCommand (m for help): pDisk /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: 0x4081529fDevice BootStartEndBlocksIdSystem
/dev/vdb120482099199104857683LinuxCommand (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
NAMEMAJ:MIN RMSIZE RO TYPE MOUNTPOINT
sr011:01436K0 rom
vda253:0020G0 disk
└─vda1 253:1020G0 part /
vdb253:1602G0 disk
└─vdb1 253:1701G0 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 Size1.00 GiB
AllocatableNO
PE Size0
Total PE0
Free PE0
Allocated PE0
PV UUIDubeV8j-VJmO-k7qL-iFw6-4CgU-YYaM-KnZfef
## 创建vg
[root@test ~]# vgcreate vg001 /dev/vdb1
Volume group "vg001" successfully created
[root@test ~]# vgdisplay
--- Volume group ---
VG Namevg001
System ID
Formatlvm2
Metadata Areas1
Metadata Sequence No1
VG Accessread/write
VG Statusresizable
MAX LV0
Cur LV0
Open LV0
Max PV0
Cur PV1
Act PV1
VG Size1020.00 MiB
PE Size4.00 MiB
Total PE255
Alloc PE / Size0 / 0
FreePE / Size255 / 1020.00 MiB
VG UUID3iynG9-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 Name1vData
VG Namevg001
LV UUIDtLk0te-ce3n-LfRd-pKsZ-ayCn-X1fu-staj2p
LV Write Accessread/write
LV Creation host, time test, 2017-09-20 09:03:49 +0800
LV Statusavailable
# open0
LV Size1020.00 MiB
Current LE255
Segments1
Allocationinherit
Read ahead sectorsauto
- currently set to8192
Block device252: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, 229376Allocating 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:
pprimary (1 primary, 0 extended, 3 free)
eextended
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 setCommand (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
NAMEMAJ:MIN RMSIZE RO TYPE MOUNTPOINT
sr011:01436K0 rom
vda253:0020G0 disk
└─vda1253:1020G0 part /
vdb253:1602G0 disk
└─vdb1253:1701G0 part
└─vg001-1vData 252:00 1020M0 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
NAMEMAJ:MIN RMSIZE RO TYPE MOUNTPOINT
sr011:01436K0 rom
vda253:0020G0 disk
└─vda1253:1020G0 part /
vdb253:1602G0 disk
├─vdb1253:1701G0 part
│ └─vg001-1vData 252:00 1020M0 lvm/mnt
└─vdb2253:180 1023M0 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, 229376Allocating 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
NRSTARTEND SECTORSSIZE NAME UUID
12048 2099199 20971521G
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 Namevg001
System ID
Formatlvm2
Metadata Areas1
Metadata Sequence No2
VG Accessread/write
VG Statusresizable
MAX LV0
Cur LV1
Open LV1
Max PV0
Cur PV1
Act PV1
VG Size1020.00 MiB
PE Size4.00 MiB
Total PE255
Alloc PE / Size255 / 1020.00 MiB
FreePE / Size0 / 0
VG UUID3iynG9-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 Name1vData
VG Namevg001
LV UUIDtLk0te-ce3n-LfRd-pKsZ-ayCn-X1fu-staj2p
LV Write Accessread/write
LV Creation host, time test, 2017-09-20 09:03:49 +0800
LV Statusavailable
# open1
LV Size1020.00 MiB
Current LE255
Segments1
Allocationinherit
Read ahead sectorsauto
- currently set to8192
Block device252:0[root@test ~]# vgdisplay
--- Volume group ---
VG Namevg001
System ID
Formatlvm2
Metadata Areas2
Metadata Sequence No3
VG Accessread/write
VG Statusresizable
MAX LV0
Cur LV1
Open LV1
Max PV0
Cur PV2
Act PV2
VG Size1.99 GiB
PE Size4.00 MiB
Total PE510
Alloc PE / Size255 / 1020.00 MiB
FreePE / Size255 / 1020.00 MiB
VG UUID3iynG9-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
FilesystemSizeUsed Avail Use% Mounted on
/dev/vda120G1.3G19G7% /
devtmpfs236M0236M0% /dev
tmpfs245M0245M0% /dev/shm
tmpfs245M21M225M9% /run
tmpfs245M0245M0% /sys/fs/cgroup
tmpfs49M049M0% /run/user/0
/dev/mapper/vg001-1vData2.0G3.0M1.9G1% /mnt
fstab 配置详解
[root@test ~]# cat /etc/fstab
UUID=6001f930-2d2b-4e57-b55b-3fde13756c7d /xfsdefaults0 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)