aliyun添加数据盘parted方式分区格式化和lvm挂载及数据盘的扩容

一、普通磁盘分区管理方式

1.对磁盘进行分区
列出磁盘
# fdisk -l

# 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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x4765885b.

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-2097151999, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151999, default 2097151999): 
Using default value 2097151999
Partition 1 of type Linux and of size 1000 GiB is set

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

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

Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x635e6c7d

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1            2048    83886079    41942016   83  Linux

Disk /dev/vdb: 1073.7 GB, 1073741824000 bytes, 2097152000 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: 0x4765885b

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048  2097151999  1048574976   83  Linux

2.对磁盘格式化成ext4格式
# mkfs.ext4 /dev/vdb1
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
65536000 inodes, 262143744 blocks
13107187 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2409627648
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

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

3.将新的磁盘挂载在/data目录下
mkdir /data
# mount /dev/vdb1 /data

查看是否挂载成功
# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/vda1      ext4       40G  1.3G   37G   4% /
devtmpfs       devtmpfs   32G     0   32G   0% /dev
tmpfs          tmpfs      32G     0   32G   0% /dev/shm
tmpfs          tmpfs      32G  292K   32G   1% /run
tmpfs          tmpfs      32G     0   32G   0% /sys/fs/cgroup
tmpfs          tmpfs     6.3G     0  6.3G   0% /run/user/0
/dev/vdb1      ext4      985G   77M  935G   1% /data

4.添加自动挂载,重启系统后依然生效

vim /etc/fstab
/dev/vdb1 /data ext4 defaults 0 0

 

也可以通过uuid的方式进行挂载

# mkdir /data
# mount /dev/vdb1 /data

①使用lsblk命令可以清晰的获取全局的块设备布局
# lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sr0     11:0    1  1024M  0 rom  
vda    253:0    0    40G  0 disk 
└─vda1 253:1    0    40G  0 part /
vdb    253:16   0   150G  0 disk 
└─vdb1 253:17   0   150G  0 part 

②使用blkid命令可以获取设备的UUID,复制记下vdb1的UUID
# blkid
/dev/vda1: UUID="af414ad8-9936-46cd-b074-528854656fcd" TYPE="ext4" 
/dev/vdb1: UUID="a2dc5e87-a07a-4976-86c3-d13b3d264124" TYPE="ext4" 

③开机挂载磁盘
vim /etc/fstab
UUID=a2dc5e87-a07a-4976-86c3-d13b3d264124 /data ext4 defaults 0 0

 

二、逻辑卷的方式

安装逻辑卷管理、vim、unzip、iftop等工具
yum install -y lvm2 vim unzip iftop

# fdisk -l

Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x635e6c7d

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1            2048    83886079    41942016   83  Linux

Disk /dev/vdb: 2147.5 GB, 2147483648000 bytes, 4194304000 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

# 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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x31ab9b87.

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-4194303999, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-4194303999, default 4194303999): 
Using default value 4194303999
Partition 1 of type Linux and of size 2 TiB is set

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      41  PPC PReP Boot   85  Linux extended  c7  Syrinx         
 5  Extended        42  SFS             86  NTFS volume set da  Non-FS data    
 6  FAT16           4d  QNX4.x          87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS       4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility   
 8  AIX             4f  QNX4.x 3rd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    50  OnTrack DM      93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       52  CP/M            9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 54  OnTrackDM6      a5  FreeBSD         ee  GPT            
 f  W95 Ext'd (LBA) 55  EZ-Drive        a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            56  Golden Bow      a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    5c  Priam Edisk     a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 61  SpeedStor       a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 63  GNU HURD or Sys ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    64  Novell Netware  af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 65  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  70  DiskSecure Mult b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 75  PC/IX           bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 80  Old Minix       be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

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

# 规范的做法是在分区时需要一个修改磁盘分区格式为linux lvm 代码为8e的操作

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

Calling ioctl() to re-read partition table.
Syncing disks.
# cat /proc/partitions
major minor  #blocks  name

  11        0    1048575 sr0
 253        0   41943040 vda
 253        1   41942016 vda1
 253       16 2097152000 vdb
 253       17 2097150976 vdb1

# pvcreate /dev/vdb1
  Physical volume "/dev/vdb1" successfully created.
# vgcreate myvg /dev/vdb1
  Volume group "myvg" successfully created
# vgdisplay 
  --- Volume group ---
  VG Name               myvg
  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               1.95 TiB
  PE Size               4.00 MiB
  Total PE              511999
  Alloc PE / Size       0 / 0   
  Free  PE / Size       511999 / 1.95 TiB
  VG UUID               RpMeCU-gIV2-kEe5-fe6C-8iVI-xe0i-vf4ocp
   
# lvcreate -L 1.95T -n mylv myvg
  Rounding up size to full physical extent 1.95 TiB
  Logical volume "mylv" created.
# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/myvg/mylv
  LV Name                mylv
  VG Name                myvg
  LV UUID                e1nL7K-0qyc-BHt8-x9oO-1gGC-6qWn-a52o6x
  LV Write Access        read/write
  LV Creation host, time iZ2ze47of4mpxkhhzcgjheZ, 2017-01-07 12:18:26 +0800
  LV Status              available
  # open                 0
  LV Size                1.95 TiB
  Current LE             511181
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0
   
# mke2fs -t ext4 -b 2048 /dev/myvg/mylv 
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=2048 (log=1)
Fragment size=2048 (log=1)
Stride=0 blocks, Stripe width=0 blocks
130863104 inodes, 1046898688 blocks
52344934 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1315438592
63898 block groups
16384 blocks per group, 16384 fragments per group
2048 inodes per group
Superblock backups stored on blocks: 
16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816, 1327104, 
2048000, 3981312, 5619712, 10240000, 11943936, 35831808, 39337984, 
51200000, 107495424, 256000000, 275365888, 322486272, 967458816


Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done   


# mkdir /data
# mount /dev/myvg/mylv /data
# df -Th
Filesystem            Type      Size  Used Avail Use% Mounted on
/dev/vda1             ext4       40G  1.3G   37G   4% /
devtmpfs              devtmpfs   32G     0   32G   0% /dev
tmpfs                 tmpfs      32G     0   32G   0% /dev/shm
tmpfs                 tmpfs      32G  356K   32G   1% /run
tmpfs                 tmpfs      32G     0   32G   0% /sys/fs/cgroup
tmpfs                 tmpfs     6.3G     0  6.3G   0% /run/user/0
/dev/mapper/myvg-mylv ext4      2.0T   24M  1.9T   1% /data


开机自启动挂载分区
vim /etc/fstab
加入以下内容:
/dev/myvg/mylv /data ext4 defaults,noatime 0 0


# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Tue May  3 13:48:10 2016
#
# 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=80b9b662-0a1d-4e84-b07b-c1bf19e72d97 /                       ext4    defaults        1 1
/dev/myvg/mylv /data ext4 defaults,noatime 0 0

逻辑卷扩容的示例

示例:将/aliyun的逻辑卷分区由110G增加到150G

# vgdisplay myvg
  --- Volume group ---
  VG Name               myvg
  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               120.00 GiB
  PE Size               4.00 MiB
  Total PE              30719
  Alloc PE / Size       28160 / 110.00 GiB
  Free  PE / Size       2559 / 10.00 GiB
  VG UUID               v5LSS8-E5HW-htEL-ioet-Up3T-zUFz-vtCzlA


[root@mini2 ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        30G  1.8G   29G   6% /
devtmpfs                devtmpfs  907M     0  907M   0% /dev
tmpfs                   tmpfs     917M     0  917M   0% /dev/shm
tmpfs                   tmpfs     917M  8.6M  908M   1% /run
tmpfs                   tmpfs     917M     0  917M   0% /sys/fs/cgroup
/dev/mapper/centos-data xfs        40G   33M   40G   1% /data
/dev/mapper/myvg-mylv   ext4      109G   19M  103G   1% /aliyun
/dev/sda1               xfs       297M  115M  183M  39% /boot
tmpfs                   tmpfs     184M     0  184M   0% /run/user/0


1.新增一块60G的硬盘,分区
# fdisk /dev/sdc
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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x58d7841e.

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-104857599, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599): 
Using default value 104857599
Partition 1 of type Linux and of size 50 GiB is set

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

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

2.将新增加的分区/dev/sdc1扩容到物理卷myvg中
# vgextend myvg /dev/sdc1
  Physical volume "/dev/sdc1" successfully created
  Volume group "myvg" successfully extended
# 可以看到由10G扩展到了59.99G
[root@mini2 ~]# vgdisplay myvg
  --- Volume group ---
  VG Name               myvg
  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               169.99 GiB
  PE Size               4.00 MiB
  Total PE              43518
  Alloc PE / Size       28160 / 110.00 GiB
  Free  PE / Size       15358 / 59.99 GiB
  VG UUID               v5LSS8-E5HW-htEL-ioet-Up3T-zUFz-vtCzlA

# lvdisplay /dev/myvg/mylv
  --- Logical volume ---
  LV Path                /dev/myvg/mylv
  LV Name                mylv
  VG Name                myvg
  LV UUID                3H0Qgm-tDJr-BJ96-Y0Vh-TMOE-1EZv-DN3qUE
  LV Write Access        read/write
  LV Creation host, time mini2, 2017-01-07 13:55:37 +0800
  LV Status              available
  # open                 1
  LV Size                110.00 GiB
  Current LE             28160
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

3.扩展物理边界为150G
[root@mini2 ~]# lvextend -L 150G /dev/myvg/mylv
  Size of logical volume myvg/mylv changed from 110.00 GiB (28160 extents) to 150.00 GiB (38400 extents).
  Logical volume mylv successfully resized.
可以看到已经有110变成了150G
[root@mini2 ~]# lvdisplay /dev/myvg/mylv
  --- Logical volume ---
  LV Path                /dev/myvg/mylv
  LV Name                mylv
  VG Name                myvg
  LV UUID                3H0Qgm-tDJr-BJ96-Y0Vh-TMOE-1EZv-DN3qUE
  LV Write Access        read/write
  LV Creation host, time mini2, 2017-01-07 13:55:37 +0800
  LV Status              available
  # open                 1
  LV Size                150.00 GiB
  Current LE             38400
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

[root@mini2 ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        30G  1.8G   29G   6% /
devtmpfs                devtmpfs  907M     0  907M   0% /dev
tmpfs                   tmpfs     917M     0  917M   0% /dev/shm
tmpfs                   tmpfs     917M  8.6M  908M   1% /run
tmpfs                   tmpfs     917M     0  917M   0% /sys/fs/cgroup
/dev/mapper/centos-data xfs        40G   33M   40G   1% /data
/dev/mapper/myvg-mylv   ext4      109G   19M  103G   1% /aliyun
/dev/sda1               xfs       297M  115M  183M  39% /boot
tmpfs                   tmpfs     184M     0  184M   0% /run/user/0


4.扩展逻辑边界,可以看到磁盘分区/aliyun由109G变成了148G
[root@mini2 ~]# resize2fs /dev/myvg/mylv 
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/myvg/mylv is mounted on /aliyun; on-line resizing required
old_desc_blocks = 110, new_desc_blocks = 150
The filesystem on /dev/myvg/mylv is now 78643200 blocks long.


[root@mini2 ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        30G  1.8G   29G   6% /
devtmpfs                devtmpfs  907M     0  907M   0% /dev
tmpfs                   tmpfs     917M     0  917M   0% /dev/shm
tmpfs                   tmpfs     917M  8.6M  908M   1% /run
tmpfs                   tmpfs     917M     0  917M   0% /sys/fs/cgroup
/dev/mapper/centos-data xfs        40G   33M   40G   1% /data
/dev/mapper/myvg-mylv   ext4      148G   17M  141G   1% /aliyun
/dev/sda1               xfs       297M  115M  183M  39% /boot
tmpfs                   tmpfs     184M     0  184M   0% /run/user/0

扩容阿里云数据盘由1T变为2T,非lvm

1.登录云服务器管理控制台。
更多 > 磁盘扩容将磁盘容量改为2000G

2.重启服务器,进入实例列表页面,点击 更多,选择 重启(注意:一定要通过控制台重启,通过shell进去重启ecs不生效) 

3.取消挂载
# umount /data
umount: /data: target is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))
# 提示/data目标繁忙,找到这些进程强制结束
# fuser -m /data
/data:                1080c  1081cm
# ps -ef|grep 1080
yunva     1080     1  0 15:05 ?        00:00:00 bash -c cd /data/elasticsearch-5.0.1/bin && /bin/bash elasticsearch &
yunva     1081  1080 98 15:05 ?        00:08:11 /usr/java/jdk1.8.0_111/bin/java -Xms32g -Xmx32g -XX:+UseG1GC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -server -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djna.nosys=true -Dio.netty.noUnsafe=true -Dio.netty.noKeySetOptimization=true -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -Dlog4j.skipJansi=true -XX:+HeapDumpOnOutOfMemoryError -Xss256k -Des.path.home=/data/elasticsearch-5.0.1 -cp /data/elasticsearch-5.0.1/lib/elasticsearch-5.0.1.jar:/data/elasticsearch-5.0.1/lib/* org.elasticsearch.bootstrap.Elasticsearch
root      2027  1296  0 15:13 pts/1    00:00:00 grep --color=auto 1080
# kill -9 1080
# kill -9 1081

# 再次卸载成功
# umount /data
# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/vda1      ext4       40G  2.0G   36G   6% /
devtmpfs       devtmpfs   32G     0   32G   0% /dev
tmpfs          tmpfs      32G     0   32G   0% /dev/shm
tmpfs          tmpfs      32G  300K   32G   1% /run
tmpfs          tmpfs      32G     0   32G   0% /sys/fs/cgroup
tmpfs          tmpfs     6.3G     0  6.3G   0% /run/user/0

# fdisk /dev/vdb
删除原有的/dev/vdb1分区
d
1
新建分区
n
p
1
w
# 如下提示表示成功
The partition table has been altered!

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

4.检查文件系统,并变更文件系统大小

# e2fsck -f /dev/vdb1
e2fsck 1.42.9 (28-Dec-2013)
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/vdb1: 6083/65536000 files (6.5% non-contiguous), 221329845/262143744 blocks

# resize2fs /dev/vdb1
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vdb1 to 524287744 (4k) blocks.
The filesystem on /dev/vdb1 is now 524287744 blocks long.

重新挂载,看到已经成功了,启动里面的业务程序即可
# mount /dev/vdb1 /data
# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/vda1      ext4       40G  2.0G   36G   6% /
devtmpfs       devtmpfs   32G     0   32G   0% /dev
tmpfs          tmpfs      32G     0   32G   0% /dev/shm
tmpfs          tmpfs      32G  300K   32G   1% /run
tmpfs          tmpfs      32G     0   32G   0% /sys/fs/cgroup
tmpfs          tmpfs     6.3G     0  6.3G   0% /run/user/0
/dev/vdb1      ext4      2.0T  829G  1.1T  45% /data


当硬盘或者单个磁盘分区大于2T时的分区方法:

1.查看磁盘基本信息


# fdisk -l 


Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x635e6c7d


   Device Boot      Start         End      Blocks   Id  System
/dev/vda1            2048    83886079    41942016   83  Linux


Disk /dev/vdb: 6442.5 GB, 6442450944000 bytes, 12582912000 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: 0x9ac7dffd


   Device Boot      Start         End      Blocks   Id  System


2.使用parted工具进行分区(fdisk无法支持单个分区大于2T)


# parted /dev/vdb
GNU Parted 3.1
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print # 1. 打印基本分区信息                                              
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 6442GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start  End  Size  Type  File system  Flags

(parted) mklabel gpt# 2. 标记为gpt                              
Warning: The existing disk label on /dev/vdb will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? yes                                                               
(parted) mkpart # 3. 新建分区                                              
Partition name?  []?                                            # 4. 保持默认
File system type?  [ext2]? ext4                                 # 5. 文件系统分区格式为ext4                                                                            
Start? 0                                                        # 6. 起始位置         
End? -1# 7. 结束(整个磁盘)
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? Ignore# 8. 选择Ignore(忽略)                                    
(parted) print# 9. 再次打印分区信息,此时已经分好区为6442G了                                
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 6442GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name  Flags
 1      17.4kB  6442GB  6442GB

# 如果不使用lvm逻辑分卷,此时直接退出然后 # mkfs.ext4 /dev/vdb1 分区,挂载即可

(parted) toggle 1 lvm# 标记分区为lvm                                 
(parted) p                                                                
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 6442GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name  Flags
 1      17.4kB  6442GB  6442GB                     lvm

(parted) quit# 退出            
Information: You may need to update /etc/fstab.

# 再次查看磁盘分区信息,此时已经分区成功了
# fdisk -l                                

Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x635e6c7d

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1            2048    83886079    41942016   83  Linux

Disk /dev/vdb: 6442.5 GB, 6442450944000 bytes, 12582912000 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: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1               1  4294967295  2147483647+  ee  GPT

3.分区已经划分好,此时需要创建lvm:
①创建物理卷
# pvcreate /dev/vdb1
  Physical volume "/dev/vdb1" successfully created.
# pvdisplay
  "/dev/vdb1" is a new physical volume of "5.86 TiB"
  --- NEW Physical volume ---
  PV Name               /dev/vdb1
  VG Name               
  PV Size               5.86 TiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               tbHjVp-Szmc-5q3O-S3eY-waTF-Ybzx-l3I7bR
②创建逻辑卷组
# vgcreate myvg /dev/vdb1
  Volume group "myvg" successfully created
# vgdisplay
  --- Volume group ---
  VG Name               myvg
  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               5.86 TiB
  PE Size               4.00 MiB
  Total PE              1535999
  Alloc PE / Size       0 / 0   
  Free  PE / Size       1535999 / 5.86 TiB
  VG UUID               mQjQYq-uN4O-RUdV-0aZ3-he8g-aD4l-a2lhSc

③创建lv逻辑卷
# lvcreate -L 5.86T -n mylv myvg
  Rounding up size to full physical extent 5.86 TiB
  Volume group "myvg" has insufficient free space (1535999 extents): 1536164 required.  # 超出大小
# lvcreate -L 5.8T -n mylv myvg
  Rounding up size to full physical extent 5.80 TiB
  Logical volume "mylv" created.
# lvdisplay
  --- Logical volume ---
  LV Path                /dev/myvg/mylv
  LV Name                mylv
  VG Name                myvg
  LV UUID                0xzuSU-BeVP-uUit-8DRl-8ytZ-az8E-W64gxp
  LV Write Access        read/write
  LV Creation host, time iZ2zehv5i5lv499i7u6i5vZ, 2017-02-27 15:48:54 +0800
  LV Status              available
  # open                 0
  LV Size                5.80 TiB
  Current LE             1520436
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0

④格式化
# mke2fs -t ext4 -b 4096 /dev/myvg/mylv 
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
194617344 inodes, 1556926464 blocks
77846323 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=3705667584
47514 block groups
32768 blocks per group, 32768 fragments per group
4096 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, 512000000, 550731776, 644972544

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done     

⑤挂载
# mkdir /data
# mount /dev/myvg/mylv /data
# df -Th
Filesystem            Type      Size  Used Avail Use% Mounted on
/dev/vda1             ext4       40G  1.8G   36G   5% /
devtmpfs              devtmpfs   32G     0   32G   0% /dev
tmpfs                 tmpfs      32G     0   32G   0% /dev/shm
tmpfs                 tmpfs      32G  340K   32G   1% /run
tmpfs                 tmpfs      32G     0   32G   0% /sys/fs/cgroup
tmpfs                 tmpfs     6.3G     0  6.3G   0% /run/user/0
/dev/mapper/myvg-mylv ext4      5.8T   89M  5.5T   1% /data

开机自启动挂载分区
vim /etc/fstab
加入以下内容:
/dev/myvg/mylv /data ext4 defaults,noatime 0 0


移除pv、移除vg的命令
pvremove /dev/vdb1
vgremove myvg

 

新购买的ECS数据盘1T,用了阿里云的400G数据盘作为模板
登录进去后数据盘只有400G,在控制台关闭ECS,重新初始化磁盘


[root@eus-timed-task01:~]# df -Th
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/vda1      ext4    79G  9.0G   66G  13% /
tmpfs          tmpfs  3.9G     0  3.9G   0% /dev/shm
[root@eus-timed-task01:~]# mount -a
[root@eus-timed-task01:~]# mount
/dev/vda1 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)

# 想通过格式化/dev/vdb1 成ext4

[root@eus-timed-task01:~]# mkfs.ext4 /dev/vdb1
mke2fs 1.41.12 (17-May-2010)
/dev/vdb1 is apparently in use by the system; will not make a filesystem here!

解决办法:
[root@eus-timed-task01:~]# dmsetup status
VG_Data-lv_data: 0 838852608 linear 
[root@eus-timed-task01:~]# dmsetup remove_all
[root@eus-timed-task01:~]# demesup status
-bash: demesup: command not found
[root@eus-timed-task01:~]# dmsetup status
No devices found
[root@eus-timed-task01:~]# pvcreate /dev/vdb1
  Physical volume "/dev/vdb1" successfully created

# lvm分区

[root@eus-timed-task01:~]# vgcreate myvg /dev/vdb1
  Volume group "myvg" successfully created
[root@eus-timed-task01:~]# vgdisplay 
  --- Volume group ---
  VG Name               myvg
  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               1000.00 GiB
  PE Size               4.00 MiB
  Total PE              255999
  Alloc PE / Size       0 / 0   
  Free  PE / Size       255999 / 1000.00 GiB
  VG UUID               oeMYkq-8l6C-dHZx-In5b-3m8m-3jj5-dihQ3M
   
  --- Volume group ---
  VG Name               VG_Data
  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               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               400.00 GiB
  PE Size               4.00 MiB
  Total PE              102399
  Alloc PE / Size       102399 / 400.00 GiB
  Free  PE / Size       0 / 0   
  VG UUID               6dlcbY-mLx3-spKv-DUQ3-Av2S-7fVD-ye8JVC

# 磁盘使用率设置为100%
[root@eus-timed-task01:~]# lvcreate -l 100%VG -n mylv myvg
  Logical volume "mylv" created.
[root@eus-timed-task01:~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/myvg/mylv
  LV Name                mylv
  VG Name                myvg
  LV UUID                iq0LKn-bDSO-QPAn-C0mV-jADG-vc4W-PRPawO
  LV Write Access        read/write
  LV Creation host, time eus-timed-task01, 2018-08-08 03:00:50 -0700
  LV Status              available
  # open                 0
  LV Size                1000.00 GiB
  Current LE             255999
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/VG_Data/lv_data
  LV Name                lv_data
  VG Name                VG_Data
  LV UUID                Yn1sTI-gAop-kodn-cvsN-OYnN-fNES-n7l9uk
  LV Write Access        read/write
  LV Creation host, time iZrj9citgdpobfu83gsgodZ, 2018-06-15 00:05:23 -0700
  LV Status              NOT available
  LV Size                400.00 GiB
  Current LE             102399
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
   
[root@eus-timed-task01:~]# mke2fs -t ext4 -b 2048 /dev/myvg/mylv 
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=2048 (log=1)
Fragment size=2048 (log=1)
Stride=0 blocks, Stripe width=0 blocks
65536000 inodes, 524285952 blocks
26214297 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1061158912
32000 block groups
16384 blocks per group, 16384 fragments per group
2048 inodes per group
Superblock backups stored on blocks: 
    16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816, 1327104, 
    2048000, 3981312, 5619712, 10240000, 11943936, 35831808, 39337984, 
    51200000, 107495424, 256000000, 275365888, 322486272

Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 30 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@eus-timed-task01:~]# ls /data
[root@eus-timed-task01:~]# mount /dev/myvg/mylv /data
[root@eus-timed-task01:~]# df -Th
Filesystem           Type   Size  Used Avail Use% Mounted on
/dev/vda1            ext4    79G  6.2G   69G   9% /
tmpfs                tmpfs  3.9G     0  3.9G   0% /dev/shm
/dev/mapper/myvg-mylv
                     ext4   985G   23M  935G   1% /data
[root@eus-timed-task01:~]# vim /etc/fstab
/dev/myvg/mylv /data ext4 defaults,noatime 0 0

# 重新启动看能否正常挂载

 # 为了避免阿里云磁盘扩容麻烦直接跳过fdisk分区
fdisk -l
pvcreate /dev/vdb
vgcreate myvg /dev/vdb
lvcreate -l 100%VG -n mylv myvg
mkfs.xfs -f /dev/myvg/mylv
mkdir /data
mount /dev/myvg/mylv /data
df -Th
echo "/dev/myvg/mylv /data xfs defaults,noatime 0 0" >> /etc/fstab


# aws

pvcreate /dev/nvme1n1
vgcreate myvg /dev/nvme1n1
lvcreate -l +100%VG -n mylv myvg
mkfs.xfs -f /dev/myvg/mylv
mkdir /data
mount /dev/myvg/mylv /data


# vim /etc/fstab
/dev/myvg/mylv /data xfs defaults,noatime 0 0
# /dev/myvg2/mylv2 /ssd xfs defaults,noatime 0 0


# 新版的在线扩容,不需要重启服务器的方法,直接在原有磁盘上加容量,然后执行如下命令
pvresize -v /dev/vdb && lvextend -l +100%FREE /dev/myvg/mylv && xfs_growfs /dev/myvg/mylv

&& resize2fs /dev/myvg/mylv

posted @ 2017-01-07 15:08  reblue520  阅读(1295)  评论(0编辑  收藏  举报