Linux之系统分区管理

系统分区管理

/sda 系统第一块串口硬盘,a代表第一块
/cdrom,/dev/sr0 代表光驱
/hd0 系统闭口硬盘
/mapper/ 软件虚拟的设备
硬盘的分区:MBR、扩展分区、逻辑分区

设备的管理

查询设备的几种方式:
fdisk -l # 发现磁盘,查看系统当中真实存在的设备,检测硬件信息,不一定被系统识别
df #默认显示大小k,查看系统中正在使用的
cat /proc/partitions #查看系统识别的设备,不一定可以用
blkid #系统中正在用或可以用的设备

下面是用上述方法查看设备的测试:
1、fdisk -l #发现磁盘,查看系统当中真实存在的设备,检测硬件信息,不一定被系统识别

[root@foundation69 Desktop]# fdisk -l
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.

Disk /dev/sda: 1000.2 GB, 1000204886016 bytes, 1953525168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: gpt


#         Start          End    Size  Type            Name
 1         2048       534527    260M  EFI System      EFI System Partition
 2       534528       567295     16M  Microsoft reser Microsoft reserved partition
 3       567296    210282495    100G  Microsoft basic Basic data partition
 4    210282496    783722495  273.4G  Microsoft basic Basic data partition
 5    783722496   1357162495  273.4G  Microsoft basic Basic data partition
 6   1566877696   1923518463  170.1G  Microsoft basic Basic data partition
 7   1923520512   1925568511   1000M  Windows recover Basic data partition
 8   1925568512   1951475711   12.4G  Windows recover Basic data partition
 9   1951475712   1953523711   1000M  unknown         Basic data partition
10   1357162496   1358186495    500M  Microsoft basic 
11   1358186496   1374441471    7.8G  Linux swap      
12   1374441472   1566877695   91.8G  Microsoft basic 

Disk /dev/sdb: 15.5 GB, 15500574720 bytes, 30274560 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: 0x491e3623

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1              63    30274271    15137104+   b  W95 FAT32

2、cat /proc/partitions #查看系统识别的设备,不一定可以用

[root@foundation69 Desktop]# cat /proc/partitions
major minor  #blocks  name

   8        0  976762584 sda
   8        1     266240 sda1
   8        2      16384 sda2
   8        3  104857600 sda3
   8        4  286720000 sda4
   8        5  286720000 sda5
   8        6  178320384 sda6
   8        7    1024000 sda7
   8        8   12953600 sda8
   8        9    1024000 sda9
   8       10     512000 sda10
   8       11    8127488 sda11
   8       12   96218112 sda12
   7        0    3655680 loop0
   7        1    3948544 loop1
   8       16   15137280 sdb
   8       17   15137104 sdb1

3、blkid #系统中正在用或可以用的设备

[root@foundation69 Desktop]# blkid
/dev/sda1: LABEL="SYSTEM_DRV" UUID="2C9D-3FF5" TYPE="vfat" PARTLABEL="EFI System Partition" PARTUUID="42ff9e47-dded-464d-821a-f5a0c208241e" 
/dev/sda2: PARTLABEL="Microsoft reserved partition" PARTUUID="39b1cdff-4bcd-4698-8692-0b869eadf047" 
/dev/sda3: LABEL="Windows" UUID="D69CA0BA9CA09691" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="11d14992-fa38-4605-bbab-7e4fb1ad591a" 
/dev/sda4: UUID="C2762FEE762FE241" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="c4432fec-f9d7-4bd6-ac98-2a5bfc44fd7f" 
/dev/sda5: UUID="A8FE5AD7FE5A9CFE" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="da859020-9702-4ab6-8c89-ab9d0d30bb9a" 
/dev/sda6: LABEL="M-fM-^\M-,M-eM-^\M-0M-gM-#M-^AM-gM-^[M-^X" UUID="FCCC321DCC31D31E" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="adf65f15-1033-4d6d-8433-7bb2dbc0f313" 
/dev/sda7: LABEL="WINRE_DRV" UUID="4A389058389044BB" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="bf76e1f5-4e25-4e4d-abe8-e7b69b5281ac" 
/dev/sda8: LABEL="LENOVO_PART" UUID="9EF894D0F894A7D3" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="a907a59f-84aa-4e45-bf6d-00f711474db0" 
/dev/sda9: LABEL="LRS_ESP" UUID="76AA-2F71" TYPE="vfat" PARTLABEL="Basic data partition" PARTUUID="32751b55-dc09-4c92-9991-c96c0fb4577c" 
/dev/sda10: UUID="e2951939-b44d-461c-aa97-61451d314755" TYPE="xfs" PARTUUID="24e182f5-e5c0-4677-ba99-3cc5bfed71b0" 
/dev/sda11: UUID="78167bd6-fb99-4b6b-93b4-34e8dca06c82" TYPE="swap" PARTUUID="93964b3e-a068-4a32-8c69-5a82b5c0fe63" 
/dev/sda12: UUID="ae1be246-09c6-4293-b590-89f8356a422f" TYPE="xfs" PARTUUID="e0144b46-05cc-417e-912d-11240366c076" 
/dev/loop0: UUID="2014-05-07-03-58-46-00" LABEL="RHEL-7.0 Server.x86_64" TYPE="iso9660" PTTYPE="dos" 
/dev/loop1: UUID="2015-10-30-11-11-49-00" LABEL="RHEL-7.2 Server.x86_64" TYPE="iso9660" PTTYPE="dos" 
/dev/sdb1: LABEL="KINGSTON" UUID="08E4-2A63" TYPE="vfat" 

4、
df #默认显示大小k,查看系统中正在使用的
df -H #10的n次方,1兆为1000k
df -h #2的n次方,1兆为1024k,更精确

[root@foundation69 Desktop]# df
Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/sda12      96171132 19536600  76634532  21% /
devtmpfs         3910380        0   3910380   0% /dev
tmpfs            3925144      552   3924592   1% /dev/shm
tmpfs            3925144     9308   3915836   1% /run
tmpfs            3925144        0   3925144   0% /sys/fs/cgroup
/dev/sda10        505580   150328    355252  30% /boot
/dev/sda1         262144    37276    224868  15% /boot/efi
/dev/loop0       3654720  3654720         0 100% /var/www/html/source7.0
/dev/loop1       3947824  3947824         0 100% /var/www/html/source7.2
tmpfs             785032       24    785008   1% /run/user/1000
/dev/sdb1       15122312  7738320   7383992  52% /run/media/kiosk/KINGSTON
[root@foundation69 Desktop]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda12       92G   19G   74G  21% /
devtmpfs        3.8G     0  3.8G   0% /dev
tmpfs           3.8G  552K  3.8G   1% /dev/shm
tmpfs           3.8G  9.1M  3.8G   1% /run
tmpfs           3.8G     0  3.8G   0% /sys/fs/cgroup
/dev/sda10      494M  147M  347M  30% /boot
/dev/sda1       256M   37M  220M  15% /boot/efi
/dev/loop0      3.5G  3.5G     0 100% /var/www/html/source7.0
/dev/loop1      3.8G  3.8G     0 100% /var/www/html/source7.2
tmpfs           767M   24K  767M   1% /run/user/1000
/dev/sdb1        15G  7.4G  7.1G  52% /run/media/kiosk/KINGSTON
[root@foundation69 Desktop]# df -H
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda12       99G   21G   79G  21% /
devtmpfs        4.1G     0  4.1G   0% /dev
tmpfs           4.1G  566k  4.1G   1% /dev/shm
tmpfs           4.1G  9.6M  4.1G   1% /run
tmpfs           4.1G     0  4.1G   0% /sys/fs/cgroup
/dev/sda10      518M  154M  364M  30% /boot
/dev/sda1       269M   39M  231M  15% /boot/efi
/dev/loop0      3.8G  3.8G     0 100% /var/www/html/source7.0
/dev/loop1      4.1G  4.1G     0 100% /var/www/html/source7.2
tmpfs           804M   25k  804M   1% /run/user/1000
/dev/sdb1        16G  8.0G  7.6G  52% /run/media/kiosk/KINGSTON

sdb1代表系统上的第一个分区
mount /dev/sdb1 /mnt #将设备挂载在mnt上,读写挂载

[root@foundation69 Desktop]# mount /dev/sdb1 /mnt  #将设备vdb1挂载在mnt上
[root@foundation69 Desktop]# df  #查看挂载情况
Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/sda12      96171132 19536604  76634528  21% /
devtmpfs         3910380        0   3910380   0% /dev
tmpfs            3925144      552   3924592   1% /dev/shm
tmpfs            3925144     9308   3915836   1% /run
tmpfs            3925144        0   3925144   0% /sys/fs/cgroup
/dev/sda10        505580   150328    355252  30% /boot
/dev/sda1         262144    37276    224868  15% /boot/efi
/dev/loop0       3654720  3654720         0 100% /var/www/html/source7.0
/dev/loop1       3947824  3947824         0 100% /var/www/html/source7.2
tmpfs             785032       24    785008   1% /run/user/1000
/dev/sdb1       15122312  7738320   7383992  52% /mnt

umount /mnt|/dev/sdb1 #卸载设备,当设备有任务执行时不能卸载设备

[root@foundation69 Desktop]# umount /mnt
[root@foundation69 Desktop]# df
Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/sda12      96171132 19536604  76634528  21% /
devtmpfs         3910380        0   3910380   0% /dev
tmpfs            3925144      552   3924592   1% /dev/shm
tmpfs            3925144     9308   3915836   1% /run
tmpfs            3925144        0   3925144   0% /sys/fs/cgroup
/dev/sda10        505580   150328    355252  30% /boot
/dev/sda1         262144    37276    224868  15% /boot/efi
/dev/loop0       3654720  3654720         0 100% /var/www/html/source7.0
/dev/loop1       3947824  3947824         0 100% /var/www/html/source7.2
tmpfs             785032       24    785008   1% /run/user/1000
/dev/sdb1       15122312  7738320   7383992  52% /run/media/kiosk/KINGSTON

lsof /dev/sdb1|/mnt #执行时,底下会显示任务号,然后 kill -9 任务号,就可以结束该任务
Linux之系统分区管理_第1张图片
fuser -kvm /dev/sdb1|/mnt #结束这个任务
fuser -vm /dev/sdb1|/mnt #查看这个任务的进程
Linux之系统分区管理_第2张图片
mount -o ro /dev/sdb1 /mnt #只读挂载
注意:当只读挂载时,可能会提示设备正在忙或着已经挂载,这时我们卸载设备然后进行只读挂载就会成功

[root@foundation69 ~]# mount -o ro /dev/sdb1 /mnt
[root@foundation69 ~]# df
Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/sda12      96171132 19538040  76633092  21% /
devtmpfs         3910380        0   3910380   0% /dev
tmpfs            3925144      160   3924984   1% /dev/shm
tmpfs            3925144     9248   3915896   1% /run
tmpfs            3925144        0   3925144   0% /sys/fs/cgroup
/dev/sda10        505580   150328    355252  30% /boot
/dev/sda1         262144    37276    224868  15% /boot/efi
/dev/loop0       3654720  3654720         0 100% /var/www/html/source7.0
/dev/loop1       3947824  3947824         0 100% /var/www/html/source7.2
tmpfs             785032       24    785008   1% /run/user/1000
/dev/sdb1       15122312  7769376   7352936  52% /mnt
[root@foundation69 ~]# cd /mnt  #可以进入到挂载的目录里面
[root@foundation69 mnt]# touch file
touch: cannot touch ‘file’: Read-only file system  #提示是只读形式,不能创建文件

mount -o remount,rw /dev/sdb1 #重新读写挂载

[root@foundation69 mnt]# mount -o remount,rw /dev/sdb1
[root@foundation69 mnt]# touch file
[root@foundation69 mnt]# ls   #查看文件是否建立
??          linux     python-3.6.5-amd64 (1).exe       rhel-server-7.0-x86_64-dvd.iso
file        ??.py     software

man mount #查看参数

划分分区

分区是硬件信息,在分区表里面
硬盘引导最少可写446,最接近的共有512字节,其中
mbr 主引导,最少可写446字节
mpt 主分区表,64字节
55aa 硬盘的有效性标识,2字节
一个分区用16个字节记录
分区有主分区p和扩展分区e,主分区有4个
扩展分区e里面的分区为逻辑分区
分区步骤:
1、fdisk -l #查看设备,fdisk是用于管理磁盘分区的实用程序
2、fdisk /dev/vdb #给vdb划分分区
3、cat /proc/partitions #查看系统识别的分区信息
4、如果没有分区信息:partprobe #手动同步分区表到内核
分区时的一些参数:

a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition #删除分区
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types #列出系统可用的分区类型
m print this menu
n add a new partition #新建分区
o create a new empty DOS partition table
p print the partition table #显示分区
q quit without saving changes #推出
s create a new empty Sun disklabel
t change a partition’s system id #修改分区功能id
u change display/entry units
v verify the partition table
w write table to disk and exit #保存更改到分区表中
x extra functionality (experts only)

具体步骤如下:

[root@localhost ~]# fdisk -l

Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 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: 0x00013f3e

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    20970332    10484142+  83  Linux

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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
[root@localhost ~]# 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 0x630ec371.

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   #主分区id
First sector (2048-20971519, default 2048):   #此分区起始位置,一般默认 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +100M   #分区大小,用+大小的方式指定
Partition 1 of type Linux and of size 100 MiB is set

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

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

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 (616448-20971519, default 616448): 
Using default value 616448
Last sector, +sectors or +size{K,M,G} (616448-20971519, default 20971519): +100M
Partition 4 of type Linux and of size 100 MiB is set

Command (m for help): n
If you want to create more than four partitions, you must replace a
primary partition with an extended partition first.

Command (m for help): p

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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   #标签类型dos
Disk identifier: 0x630ec371

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848      411647      102400   83  Linux
/dev/vdb3          411648      616447      102400   83  Linux
/dev/vdb4          616448      821247      102400   83  Linux

Command (m for help): d
Partition number (1-4, default 4): 4
Partition 4 is deleted

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

Command (m for help): p    #显示分区

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x630ec371

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848      411647      102400   83  Linux
/dev/vdb3          411648      616447      102400   83  Linux
/dev/vdb4          616448    20971519    10177536    5  Extended

Command (m for help): wq   #保存分区策略,退出fdisk界面
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# cat /proc/partitions   #查看分区表
major minor  #blocks  name

 253        0   10485760 vda
 253        1   10484142 vda1
 253       16   10485760 vdb
 253       17     102400 vdb1
 253       18     102400 vdb2
 253       19     102400 vdb3
 253       20          1 vdb4

设备挂载

划分好的分区还不可以用,需要将其格式化,才可用
1、mkfs.xfs /dev/vdb1 #将设备vdb1格式化,在设备上安装文件系统xfs
2、blkid #查看可用设备,可以看到被格式化好的/dev/vdb1
3、mount /dev/vdb1 /mnt #临时挂载
测试:

[root@localhost ~]# mount /dev/vdb1
mount: can't find /dev/vdb1 in /etc/fstab
[root@localhost ~]# mkfs.xfs /dev/vdb1
meta-data=/dev/vdb1              isize=256    agcount=4, agsize=6400 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=25600, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost ~]# blkid
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs" 
/dev/vdb1: UUID="35c735ac-b6e7-4ad8-8638-0c54bf6634aa" TYPE="xfs" 
[root@localhost ~]# mount /dev/vdb1 /mnt
[root@localhost ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       10473900 3190484   7283416  31% /
devtmpfs          469344       0    469344   0% /dev
tmpfs             484932      84    484848   1% /dev/shm
tmpfs             484932   12796    472136   3% /run
tmpfs             484932       0    484932   0% /sys/fs/cgroup
/dev/vdb1          98988    5280     93708   6% /mnt

设置gpt分区格式

解决分区不能超过1TB的问题
1、先将原分区删掉,同部分区表,看是否删除
2、parted /dev/vdb #更改分区类型
3、格式化,挂载
测试:

[root@localhost ~]# fdisk -l  #查看是否有原分区

Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 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: 0x00013f3e

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    20970332    10484142+  83  Linux

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x0002b23a

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
删除掉这些原分区
[root@localhost ~]# cat /proc/partitions   #查看分区表
major minor  #blocks  name

 253        0   10485760 vda
 253        1   10484142 vda1
 253       16   10485760 vdb
 253       17     102400 vdb1
 253       18     102400 vdb2
 253       19     102400 vdb3
 253       20          1 vdb4
[root@localhost ~]# partprobe   #删除之后没有同步分区信息,此命令用来同步
[root@localhost ~]# cat /proc/partitions   #查看分区表,原分区已删除
major minor  #blocks  name

 253        0   10485760 vda
[root@localhost ~]# parted /dev/vdb   #更改分区类型
GNU Parted 3.1
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) help     #查找帮助                                                      
  align-check TYPE N                        check partition N for TYPE(min|opt)
        alignment
  help [COMMAND]                           print general help, or help on
        COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition
        table)
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  name NUMBER NAME                         name partition NUMBER as NAME
  print [devices|free|list,all|NUMBER]     display the partition table,
        available devices, free space, all found partitions, or a particular
        partition
  quit                                     exit program
  rescue START END                         rescue a lost partition near START
        and END
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  disk_set FLAG STATE                      change the FLAG on selected device
  disk_toggle [FLAG]                       toggle the state of FLAG on selected
        device
  set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition
        NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  display the version number and
        copyright information of GNU Parted
(parted) mklabel     #更改新类型                                                      
New disk label type?    #查看需要更改的分区类型                                                   
aix    amiga  bsd    dvh    gpt    loop   mac    msdos  pc98   sun 
New disk label type? gpt     #修改分区为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) quit                                                             
Information: You may need to update /etc/fstab.

[root@localhost ~]# fdisk /dev/vdb   #创建新的分区
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
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 number (1-128, default 1): 1
First sector (34-20971486, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971486, default 20971486): +100M
Created partition 1


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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# fdisk -l    #查看分区

Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 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: 0x00013f3e

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    20970332    10484142+  83  Linux
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: gpt    #新的分区标识为gpt


#         Start          End    Size  Type            Name
 1         2048       206847    100M  Linux filesyste 


注意:
当没有删除原分区,而直接修改成其他类型时,因为已经使用了原分区,则会有提示
Warning: Partition(s) on /dev/vdb are being used.
Ignore/Cancel? I #选择忽略即可

永久挂载设备

mount 为临时挂载
vim /etc/fstab #编辑永久挂载文件
挂载设备 挂载点 文件系统类型 文件系统选项 是否备份 是否检测
/dev/vdb1 /mnt xfs defaults 0 0
退出保存
mount -a #将文件的挂载策略生效,相当于激活
测试:

[root@localhost ~]# vim /etc/fstab  #编辑永久挂载文件
#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# 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=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vdb1 /mnt xfs defaults 0 0   #文件新编辑的内容
:wq  #退出保存
[root@localhost ~]# mount -a
[root@localhost ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       10473900 3191012   7282888  31% /
devtmpfs          469344       0    469344   0% /dev
tmpfs             484932      80    484852   1% /dev/shm
tmpfs             484932   12756    472176   3% /run
tmpfs             484932       0    484932   0% /sys/fs/cgroup
/dev/vdb1          98988    5280     93708   6% /mnt

添加swap交换分区

1、fdisk /dev/vdb #建立分区
2、cat /proc/partitions #查看是否有swap分区信息
partprobe #如果没有新的分区信息则手动同步分区信息,再次查看分区信息
3、mkswap /dev/vdb2 #准备好将分区用作交换区
4、swapon -a /dev/vdb2 #激活新交换区,是临时的
5、swapon -s #查看交换区的状态
测试:

[root@localhost ~]# fdisk /dev/vdb   #创建分区并将其改成swap分区
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 (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-20971519, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +1000M
Partition 1 of type Linux and of size 1000 MiB is set

Command (m for help): t   #修改分区
Selected partition 1
Hex code (type L to list all codes): l   #查看改变成swap分区标识的序号 

 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): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'

Command (m for help): p

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x000a7e30

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048     2050047     1024000   82  Linux swap / Solaris

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# cat /proc/partitions
major minor  #blocks  name

 253        0   10485760 vda
 253        1   10484142 vda1
 253       16   10485760 vdb
 253       17    1024000 vdb1
[root@localhost ~]# mkswap /dev/vdb1
mkswap: /dev/vdb1: warning: wiping old xfs signature.
Setting up swapspace version 1, size = 1023996 KiB
no label, UUID=ce8733bd-9ca9-44b1-aef9-31e9cb0d3389
[root@localhost ~]# swapon -a /dev/vdb1
[root@localhost ~]# swapon -s
Filename				Type		Size	Used	Priority
/dev/vdb1                              	partition	1023996	0	-1

1、永久设定swap分区

1、vim /etc/fstab #编写使分区永久成为swap分区
/dev/vdb2 swap swap defaults 0 0 #编写内容,第一个swap表示不需要挂载
2、swapon -a #将文件刷新,并激活使swap分区永久存在
测试:

[root@localhost ~]# vim /etc/fstab   #编写使分区永久成为swap分区 
#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# 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=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vdb1 swap swap defaults 0 0   #编写内容
[root@localhost ~]# swapon -a    #将文件刷新,并激活使swap分区永久存在

2、删除swap分区

1、vim /etc/fstab #将变成swap分区那行删掉
2、swapoff /dev/vdb2 #关闭swap分区
3、fdisk /dev/vdb #删掉分区
4、partprobe #同部分区信息
5、cat /proc/partitions #查看分区信息
测试:

[root@localhost ~]# vim /etc/fstab  #将变成swap分区那行删掉
[root@localhost ~]# swapoff /dev/vdb1     #关闭swap分区
[root@localhost ~]# 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): wq
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partprobe
[root@localhost ~]# cat /proc/partitions 
major minor  #blocks  name

 253        0   10485760 vda
 253        1   10484142 vda1
 253       16   10485760 vdb

LUKS加密

1、fdisk /dev/vdb #新建设备
2、partprobe #同步分区信息
3、cryptsetup luksFormat /dev/vdb1 #luks加密
4、mount /dev/vdb1 /mnt/ #尝试挂载失败
5、cryptsetup open /dev/vdb1 hahaa #以hahaa形态打开盖子,会提示输入密码
6、mkfs.xfs /dev/mapper/hahaa #将hahaa格式化
7、mount /dev/mapper/hahaa /mnt/ #挂载hahaa
8、touch /mnt/see{1…5} #建立文件
9、umount /mnt/ #卸载
10、cryptsetup close hahaa #盖上盖子
11、ll /dev/mapper/ #查看不到设备
想再次查看时可打开盖子进行以上设置

测试:

[root@localhost ~]# cryptsetup luksFormat /dev/vdb1

WARNING!
========
This will overwrite data on /dev/vdb1 irrevocably.

Are you sure? (Type uppercase yes): YES  #这里可能有bug,一定要写大写YES
Enter passphrase:    #输入密码,密码不得少于8位
Verify passphrase:     #再次输入密码
[root@localhost ~]# mount /dev/vdb1 /mnt  #加密后进行挂载
mount: unknown filesystem type 'crypto_LUKS'  #提示加密,不能挂载
[root@localhost ~]# cryptsetup open /dev/vdb1 haha   #以haha的形式打开设备的加密,相当于打开一个箱子的盖子
Enter passphrase for /dev/vdb1: 
[root@localhost ~]# ll /dev/mapper/   #查看设备,在关闭盖子的时候,就看不到以haha打开的设备
total 0
crw-------. 1 root root 10, 236 May  2 03:37 control
lrwxrwxrwx. 1 root root       7 May  2 03:57 haha -> ../dm-0
[root@localhost ~]# mkfs.xfs /dev/mapper/haha  #格式化
meta-data=/dev/mapper/haha       isize=256    agcount=4, agsize=12672 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=50688, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost ~]# mount /dev/mapper/haha /mnt
[root@localhost ~]# touch /mnt/see{1..5}
[root@localhost ~]# ls /mnt/*
/mnt/see1  /mnt/see2  /mnt/see3  /mnt/see4  /mnt/see5
[root@localhost ~]# umount /mnt   #卸载设备
[root@localhost ~]# cryptsetup close haha   #盖上盖子
[root@localhost ~]# ll /dev/mapper/    #查看不到设备
total 0
crw-------. 1 root root 10, 236 May  2 03:37 control

LUKS加密永久挂载

1、vim /root/diskpass #编写含密码的文件
2、chmod 600 /root/diskpass #修改权限,仅用户可读写
3、cryptsetup luksAddKey /dev/vdb1 /root/diskpass #编写开机自动识别含密码文件进行加密的文件
4、vim /etc/crypttab #编写以haha形态进行解密的文件
haha /dev/vdb1 /root/diskpass
5、vim /etc/fstab #编写开机自动挂载文件
/dev/mapper/hahaa /mnt xfs defaults 0 0
6、reboot #重启
7、df #查看挂载
测试:

[root@localhost ~]# vim /root/passwd
ying12345haha  #文件写入内容
[root@localhost ~]# chmod 600 /root/passwd
[root@localhost ~]# cryptsetup luksAddKey /dev/vdb1 /root/passwd
Enter any passphrase: 
[root@localhost ~]# vim /etc/crypttab
haha /dev/vdb1 /root/passwd   #文件写入内容
[root@localhost ~]# vim /etc/fstab   #打开文件,下面显示的是文件的内容
#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# 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=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/mapper/haha /mnt xfs defaults 0 0    #文件写入内容
[root@localhost ~]# reboot  #重启
Connection to 172.25.254.196 closed by remote host.
Connection to 172.25.254.196 closed.
[kiosk@foundation69 Desktop]$ ssh [email protected] -X   #重新连接
[email protected]'s password: 
Last login: Wed May  2 04:35:46 2018
[root@localhost ~]# df  #查看挂载
Filesystem       1K-blocks    Used Available Use% Mounted on
/dev/vda1         10473900 3191564   7282336  31% /
devtmpfs            469344       0    469344   0% /dev
tmpfs               484932      84    484848   1% /dev/shm
tmpfs               484932   12796    472136   3% /run
tmpfs               484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/haha    199340   10296    189044   6% /mnt  #已挂载

暴力破解

1、vim /etc/fstab #清除自动挂载策略
2、vim /etc/crypttab #删除以hahaa形态进行解密的策略
3、rm -fr /root/diskpass #删除含密码文件
4、umount /mnt/ #卸载
5、cryptsetup close hahaa #关闭hahaa
6、mkfs.xfs /dev/vdb1 -f #强行格式化
测试:

[root@localhost ~]# vim /etc/fstab
[root@localhost ~]# vim /etc/crypttab
[root@localhost ~]# rm -fr /root/passwd
[root@localhost ~]# umount /mnt
[root@localhost ~]# cryptsetup close haha
[root@localhost ~]# mkfs.xfs -f /dev/vdb1
meta-data=/dev/vdb1              isize=256    agcount=4, agsize=12800 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=51200, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost ~]# mount /dev/vdb1 /mnt
[root@localhost ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       10473900 3191528   7282372  31% /
devtmpfs          469344       0    469344   0% /dev
tmpfs             484932      84    484848   1% /dev/shm
tmpfs             484932   12792    472140   3% /run
tmpfs             484932       0    484932   0% /sys/fs/cgroup
/dev/vdb1         201388   10400    190988   6% /mnt

磁盘阵列raid

1、fdisk /dev/vdb #建立分区
2、Command (m for help): t #修改id
3、打开另外一个终端进行监控
watch -n 1 cat /proc/mdstat #监控
4、在本终端
mdadm -C /dev/md0 -a yes -l 1 -n 2 -x 1 /dev/vdb{1…3}
-C 创建
-a yes 自动创建
-l 表示级别
-n 表示有几块设备
-x 闲置设备
5、mkfs.xfs /dev/md0 #格式化
6、mount /dev/md0 /mnt/ #挂载
测试:

[root@localhost ~]# fdisk /dev/vdb   #因为已经分好区,所以直接改id 
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): t
Partition number (1-3, default 3): 1
Hex code (type L to list all codes): fd    #修改id为fd
Changed type of partition 'Linux' to 'Linux raid autodetect'

Command (m for help): t
Partition number (1-3, default 3): 2
Hex code (type L to list all codes): fd
Changed type of partition 'Linux' to 'Linux raid autodetect'

Command (m for help): t
Partition number (1-3, default 3): 3
Hex code (type L to list all codes): fd
Changed type of partition 'Linux' to 'Linux raid autodetect'
Command (m for help): p

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x000ee072

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      411647      204800   fd  Linux raid autodetect
/dev/vdb2          411648      821247      204800   fd  Linux raid autodetect
/dev/vdb3          821248     1230847      204800   fd  Linux raid autodetect

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

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

打开一个新终端:

[root@localhost ~]# watch -n 1 cat /proc/mdstat   #监控

在原终端创建磁盘列阵:

[root@localhost ~]# mdadm -C /dev/md0 -a yes -l 1 -n 2 -x 1 /dev/vdb{1..3}
mdadm: Note: this array has metadata at the start and
    may not be suitable as a boot device.  If you plan to
    store '/boot' on this device please ensure that
    your boot-loader understands md/v1.x metadata, or use
    --metadata=0.90
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.

显示如图:
Linux之系统分区管理_第3张图片
格式化,挂载:

[root@localhost ~]# mkfs.xfs /dev/md0
meta-data=/dev/md0               isize=256    agcount=4, agsize=12788 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=51152, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost ~]# mount /dev/md0 /mnt

设备的管理

mdadm -D /dev/md0 #查看设备状态

[root@localhost ~]# mdadm -D /dev/md0
/dev/md0:
        Version : 1.2
  Creation Time : Wed May  2 04:59:42 2018
     Raid Level : raid1
     Array Size : 204608 (199.85 MiB 209.52 MB)
  Used Dev Size : 204608 (199.85 MiB 209.52 MB)
   Raid Devices : 2
  Total Devices : 3
    Persistence : Superblock is persistent

    Update Time : Wed May  2 05:00:29 2018
          State : clean 
 Active Devices : 2
Working Devices : 3
 Failed Devices : 0
  Spare Devices : 1

           Name : localhost:0  (local to host localhost)
           UUID : 365c389b:aa6b5b46:f0e754b1:9f8079ad
         Events : 17

    Number   Major   Minor   RaidDevice State
       0     253       17        0      active sync   /dev/vdb1
       1     253       18        1      active sync   /dev/vdb2

       2     253       19        -      spare   /dev/vdb3

mdadm /dev/md0 -f /dev/vdb2 #损坏设备vdb2
测试:

[root@localhost ~]# mdadm /dev/md0 -f /dev/vdb2
mdadm: set /dev/vdb2 faulty in /dev/md0

监控显示如图:
Linux之系统分区管理_第4张图片
mdadm /dev/md0 -r /dev/vdb2 #删除设备
测试:

[root@localhost ~]# mdadm /dev/md0 -r /dev/vdb2
mdadm: hot removed /dev/vdb2 from /dev/md0

监控显示如图:
Linux之系统分区管理_第5张图片
mdadm /dev/md0 -a /dev/vdb2 #添加设备
测试:

[root@localhost ~]# mdadm /dev/md0 -a /dev/vdb2
mdadm: added /dev/vdb2

监控显示如图:
Linux之系统分区管理_第6张图片

设备的删除

1、umount /mnt/ #卸载
2、mdadm -S /dev/md0 #停止
3、fdisk /dev/vdb #删除设备
4、partprobe #同步分区信息
测试:

[root@localhost ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       10473900 3192152   7281748  31% /
devtmpfs          469344       0    469344   0% /dev
tmpfs             484932     140    484792   1% /dev/shm
tmpfs             484932   12864    472068   3% /run
tmpfs             484932       0    484932   0% /sys/fs/cgroup
/dev/md0          201196   10388    190808   6% /mnt
[root@localhost ~]# umount /mnt
[root@localhost ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       10473900 3192152   7281748  31% /
devtmpfs          469344       0    469344   0% /dev
tmpfs             484932     140    484792   1% /dev/shm
tmpfs             484932   12864    472068   3% /run
tmpfs             484932       0    484932   0% /sys/fs/cgroup
[root@localhost ~]# mdadm -S /dev/md0
mdadm: stopped /dev/md0

停止后:
这里写图片描述
接下来删除设备和同步分区信息的步骤见上文

你可能感兴趣的:(Linux)