給虛擬機器的增加空間

zchbaby2000發表於2018-12-04

[root@mqserver ~]# fdisk -l

Disk /dev/sda: 16.1 GB, 16106127360 bytes, 31457280 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: 0x00094a55

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      616447      307200   83  Linux
/dev/sda2          616448     2713599     1048576   82  Linux swap / Solaris
/dev/sda3         2713600    20971519     9128960   83  Linux
[root@mqserver ~]# fdisk /dev/sda
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/sda: 16.1 GB, 16106127360 bytes, 31457280 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: 0x00094a55

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      616447      307200   83  Linux
/dev/sda2          616448     2713599     1048576   82  Linux swap / Solaris
/dev/sda3         2713600    20971519     9128960   83  Linux

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

Command (m for help): t
Partition number (1-4, default 4): 4
Hex code (type L to list all codes): L

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

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@mqserver ~]# fdisk -l

Disk /dev/sda: 16.1 GB, 16106127360 bytes, 31457280 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: 0x00094a55

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      616447      307200   83  Linux
/dev/sda2          616448     2713599     1048576   82  Linux swap / Solaris
/dev/sda3         2713600    20971519     9128960   83  Linux
/dev/sda4        20971520    31457279     5242880   8e  Linux LVM
[root@mqserver ~]#

重啟虛擬機器
reboot

root使用者格式化新分割槽
[root@mqserver ~]# mkfs.ext3 /dev/sda4
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
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 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

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

進入LVM管理
lvm

將物理硬碟分割槽初始化物理卷
lvm> pvcreate /dev/sda4
WARNING: ext3 signature detected on /dev/sda4 at offset 1080. Wipe it? [y/n]: y
  Wiping ext3 signature on /dev/sda4.
  Physical volume "/dev/sda4" successfully created
 
可以檢視物理卷組情況(可跳過此步)
lvm> pvdisplay
  "/dev/sda4" is a new physical volume of "5.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda4
  VG Name               
  PV Size               5.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               x2Ip6F-9OJd-Z3PY-sEbs-3vUC-TTdH-ISd10r
   
lvm>


[root@mqserver /]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   15G  0 disk
├─sda1   8:1    0  300M  0 part /boot
├─sda2   8:2    0    1G  0 part [SWAP]
├─sda3   8:3    0  8.7G  0 part /
└─sda4   8:4    0    5G  0 part
sr0     11:0    1 1024M  0 rom

--Initialize /dev/sda4 as a Physical Volume pvcreate /dev/sda4
[root@mqserver /]# pvcreate /dev/sda4
  Physical volume "/dev/sda4" successfully created
[root@mqserver /]# pvdisplay
  "/dev/sda4" is a new physical volume of "5.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda4
  VG Name               
  PV Size               5.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               xfvZeP-d89d-9ice-0MuM-kGrz-759e-f9Q58j
   
--Create the Volume Group and allocate the PV from step 1 to it vgcreate volgr0 /dev/sda4   
[root@mqserver /]# vgcreate volgr0 /dev/sda4
  Volume group "volgr0" successfully created
 
[root@mqserver /]# pvs
  PV         VG     Fmt  Attr PSize PFree
  /dev/sda4  volgr0 lvm2 a--  5.00g 5.00g
[root@mqserver /]# vgs
  VG     #PV #LV #SN Attr   VSize VFree
  volgr0   1   0   0 wz--n- 5.00g 5.00g
 
--Create a logical volume on your new Volume Group lvcreate -L +20G -n logvol0 volgr0  
[root@mqserver /]# lvcreate -L +4.9G -n logvol0 volgr0  <想加5G是不可能的,有損耗>
  Rounding up size to full physical extent 4.90 GiB
  Logical volume "logvol0" created.
[root@mqserver /]#

--Initialize (format) the logical volume mkfs.xfs /dev/volgr0/logvol0
[root@mqserver ~]# mkfs.xfs /dev/volgr0/logvol0
meta-data=/dev/volgr0/logvol0    isize=256    agcount=4, agsize=321280 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=1285120, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@mqserver ~]#


[root@mqserver ~]# mount /dev/volgr0/logvol0 /var
[root@mqserver ~]# df -Th
Filesystem                 Type      Size  Used Avail Use% Mounted on
/dev/sda3                  xfs       8.7G  4.7G  4.1G  54% /
devtmpfs                   devtmpfs  905M     0  905M   0% /dev
tmpfs                      tmpfs     914M   92K  914M   1% /dev/shm
tmpfs                      tmpfs     914M  8.8M  905M   1% /run
tmpfs                      tmpfs     914M     0  914M   0% /sys/fs/cgroup
/dev/sda1                  xfs       297M  111M  187M  38% /boot
/dev/mapper/volgr0-logvol0 xfs       4.9G  3.3G  1.7G  66% /var

給檔案/etc/fstab新增一行,使得整個檔案系統能夠啟動自動載入
/dev/mapper/volgr0-logvol0 /var xfs defaults 0 0


======================================================================
怎麼刪除分割槽
[root@mqserver ~]# fdisk /dev/sda
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
Partition number (1-4, default 4): 4
Partition 4 is deleted

Command (m for help): p

Disk /dev/sda: 16.1 GB, 16106127360 bytes, 31457280 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: 0x00094a55

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      616447      307200   83  Linux
/dev/sda2          616448     2713599     1048576   82  Linux swap / Solaris
/dev/sda3         2713600    20971519     9128960   83  Linux

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@mqserver ~]#

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/725820/viewspace-2284013/,如需轉載,請註明出處,否則將追究法律責任。

相關文章