天天看点

RH134 UNIT4

管理系统中的简单分区和文件系统

一.简单分区和文件系统

     存储是每个计算机系统的基本需求。 Red Hat EnterpriseLinux 提供了一些强大的工具 , 它们能在       大量的场景中管理多种类型的存储设备

    disk 是用于管理磁盘分区的实用程序。您可以通过选择 -l选项和指定磁盘名称 ( fdisk -cul/dev/vda)         运行该实用程序 , 以查看磁盘及其分区。您可以通过交互式地运行该实用程序 , 并选择相应的菜            单选项 ( fdisk -cu /dev/vda ) 进行更改。 -c 禁用旧的 DOS 兼容模式 , -u 以扇区 ( 而不是柱            面已经过时 ) 的格式显示输出

二.查看系统设备信息

     fdisk -l

        – 显示系统中所有可以使用的设备信息

     blkid

        – 显示系统正在使用的设备 id

三.创建新分区

1.创建新分区

[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): n                           ###m查看帮助 n新建分区

Partition type:

   p   primary (0 primary, 0 extended, 4 free)

   e   extended

Select (default p):                                       ###p主逻辑卷 e扩展分区

Using default response p

Partition number (1-4, default 1): 

First sector (2048-20971519, default 2048):          ###起始点

Using default value 2048

Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +500M ##大小

Partition 1 of type Linux and of size 500 MiB 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: 0x4a3c53fa

   Device Boot      Start         End      Blocks   Id  System

/dev/vdb1            2048     1026047      512000   83  Linux

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 ~]# mkfs.xfs /dev/vdb1  ###格式化新建分区为xfs文件系统

meta-data=/dev/vdb1              isize=256    agcount=4, agsize=32000 blks

         =                       sectsz=512   attr=2, projid32bit=1

         =                       crc=0

data     =                       bsize=4096   blocks=128000, 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

<a href="https://s3.51cto.com/wyfs02/M01/92/39/wKiom1j9flfwBMXlAAzF66GbVbE360.png-wh_500x0-wm_3-wmp_4-s_1898241885.png" target="_blank"></a>

<a href="https://s1.51cto.com/wyfs02/M01/92/38/wKioL1j9fl-CH-LDAAzfjLtqm1s553.png-wh_500x0-wm_3-wmp_4-s_3422160208.png" target="_blank"></a>

2.挂载分区

<a href="https://s3.51cto.com/wyfs02/M00/92/38/wKioL1j9f7SAHv7YAAxQMcuB4sY287.png-wh_500x0-wm_3-wmp_4-s_2919595054.png" target="_blank"></a>

自动挂载

编辑配置文件 /etc/fstab

<a href="https://s5.51cto.com/wyfs02/M01/92/3A/wKiom1j9guDRRt17AAxM3dZxm48638.png-wh_500x0-wm_3-wmp_4-s_2940868821.png" target="_blank"></a>

<a href="https://s3.51cto.com/wyfs02/M01/92/39/wKioL1j9gueCX0NfAAxA9HjtIi8717.png-wh_500x0-wm_3-wmp_4-s_1569924764.png" target="_blank"></a>

3.删除分区

卸载分区

umount /mnt

<a href="https://s2.51cto.com/wyfs02/M00/92/3A/wKiom1j9g63C981VAAy1PIO-OjU391.png-wh_500x0-wm_3-wmp_4-s_2962510688.png" target="_blank"></a>

删除分区

配置文件/etc/fstab

[root@localhost ~]# fdisk  /dev/vdb 

Command (m for help): d

Selected partition 1

Partition 1 is deleted

Command (m for help): p

Command (m for help): wq

[root@localhost ~]# partprobe 

<a href="https://s3.51cto.com/wyfs02/M00/92/39/wKioL1j9hFHiYNy1AAyc2iJb3g0139.png-wh_500x0-wm_3-wmp_4-s_3604827175.png" target="_blank"></a>

四.swap分区的管理

[root@localhost ~]# fdisk /dev/vdb 

Command (m for help): n

Select (default p): 

First sector (2048-20971519, default 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

   p   primary (1 primary, 0 extended, 3 free)

Partition number (2-4, default 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): t        ###修改分区功能标签

Partition number (1,2, default 2): 1

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 &lt;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 &lt;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): t

Partition number (1,2, default 2): 

/dev/vdb1            2048      206847      102400   82  Linux swap / Solaris

/dev/vdb2          206848      411647      102400   82  Linux swap / Solaris

<a href="https://s3.51cto.com/wyfs02/M00/92/3A/wKioL1j9jFXBBzMSAAzT1c6EYJg164.png-wh_500x0-wm_3-wmp_4-s_1929703630.png" target="_blank"></a>

格式化swap分区

mkswap /dev/vdb{1..2}

<a href="https://s3.51cto.com/wyfs02/M00/92/3B/wKiom1j9jTmRqiEtAAy9TNBz2Ds956.png-wh_500x0-wm_3-wmp_4-s_702842807.png" target="_blank"></a>

挂载swap分区

swapon -a /etc/vdb*      ###激活swap分区

wapon -s                       ###查看swap分区状态

<a href="https://s5.51cto.com/wyfs02/M01/92/3B/wKiom1j9jdOyl0u7AAxJtVF6yTw966.png-wh_500x0-wm_3-wmp_4-s_2302937825.png" target="_blank"></a>

开机自动激活

配置/etc/fstab

<a href="https://s2.51cto.com/wyfs02/M02/92/3B/wKiom1j9jy3Bshn8AAxzwEuNyvs829.png-wh_500x0-wm_3-wmp_4-s_2749862788.png" target="_blank"></a>

<a href="https://s4.51cto.com/wyfs02/M01/92/3A/wKioL1j9jzSR79qiAAww1NQfCeY281.png-wh_500x0-wm_3-wmp_4-s_2426235121.png" target="_blank"></a>

优先级priority设定

1激活时加入参数-p num  

    swapon -a /dev/vdb1 -p num 

2配置fstab

<a href="https://s1.51cto.com/wyfs02/M00/92/3B/wKiom1j9kLiwhh8eAAxl6N0tzWs452.png-wh_500x0-wm_3-wmp_4-s_3678054273.png" target="_blank"></a>

五.分区配额

分区并格式化

<a href="https://s1.51cto.com/wyfs02/M02/92/47/wKiom1j92SCRSNKYABJCeqkqw5o402.png-wh_500x0-wm_3-wmp_4-s_664601158.png" target="_blank"></a>

挂载并开启quota

[root@localhost westos]# mount -o usrquota,grpquota /dev/vdb1 /pub/ ###挂载时开启quota

[root@localhost westos]# df -h                       ###查看挂载状态

Filesystem      Size  Used Avail Use% Mounted on

/dev/vda1        10G  3.1G  7.0G  31% /

devtmpfs        906M     0  906M   0% /dev

tmpfs           921M   80K  921M   1% /dev/shm

tmpfs           921M   21M  901M   3% /run

tmpfs           921M     0  921M   0% /sys/fs/cgroup

/dev/vdb1      1014M   33M  982M   4% /pub

<a href="https://s2.51cto.com/wyfs02/M02/92/47/wKiom1j92X6hh8ryABKQez578Jo024.png-wh_500x0-wm_3-wmp_4-s_1192718360.png" target="_blank"></a>

设置用户westos的限制

edquota -u westos

<a href="https://s2.51cto.com/wyfs02/M00/92/46/wKioL1j92eKwQBuYABLgqv90tTU789.png-wh_500x0-wm_3-wmp_4-s_2289315494.png" target="_blank"></a>

<a href="https://s4.51cto.com/wyfs02/M01/92/47/wKiom1j92e7xM5lOABHSbgFwG5M639.png-wh_500x0-wm_3-wmp_4-s_113370908.png" target="_blank"></a>

测试westos可使用的空间

[root@localhost westos]# su westos                                ###切换用户

[westos@localhost ~]$ dd if=/dev/zero of=/pub/file bs=1M count=10000###截取1G到/pub

dd: error writing ‘/pub/file’: Disk quota exceeded

201+0 records in

200+0 records out

209715200 bytes (210 MB) copied, 0.077746 s, 2.7 GB/s

[westos@localhost ~]$ du -sh /pub/                                ###查看/pub下文件大小

200M    /pub/                                                             

<a href="https://s2.51cto.com/wyfs02/M00/92/47/wKioL1j92vGSpf8hABIa4CCYajY297.png-wh_500x0-wm_3-wmp_4-s_433813224.png" target="_blank"></a>

六.磁盘阵列

建立raid功能的三个分区

[root@localhost westos]# fdisk /dev/vdb 

No partition is defined yet!

[root@localhost westos]# partprobe 

[root@localhost westos]# clear

[3;J

   p   primary (2 primary, 0 extended, 2 free)

Partition number (3,4, default 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

Partition number (1-3, default 3): 1

Hex code (type L to list all codes): l

Hex code (type L to list all codes): fd

Changed type of partition 'Linux' to 'Linux raid autodetect'

Partition number (1-3, default 3): 2

Partition number (1-3, default 3): 3

/dev/vdb1            2048      206847      102400   fd  Linux raid autodetect

/dev/vdb2          206848      411647      102400   fd  Linux raid autodetect

/dev/vdb3          411648      616447      102400   fd  Linux raid autodetect

<a href="https://s4.51cto.com/wyfs02/M00/92/48/wKiom1j93Vnxe8UAABLZyFL7Ccc951.png-wh_500x0-wm_3-wmp_4-s_3903823083.png" target="_blank"></a>

新建raid分区

mdadm -C /dev/md0 -l 1 -n 2 -x 1 /dev/vdb{1..3}

<a href="https://s4.51cto.com/wyfs02/M01/92/47/wKioL1j931_ScJYXAA6IAHkp37s037.png-wh_500x0-wm_3-wmp_4-s_1417229621.png" target="_blank"></a>

<a href="https://s2.51cto.com/wyfs02/M02/92/48/wKiom1j932jQPFGcAA8bZ49L68E118.png-wh_500x0-wm_3-wmp_4-s_827810119.png" target="_blank"></a>

mdadm -D /dev/md0    ##设备配置信息

mdadm /dev/md0    -f /dev/vdb1    ##损坏设备1

mdadm /dev/md0 -r /dev/vdb1     ##移除设备1

mdadm /dev/md0 -a /dev/vdb1    ##恢复设备1

mdadm -S /dev/md0        ##停止设备

七.分区加密

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

WARNING!

========

This will overwrite data on /dev/vdb1 irrevocably.

Are you sure? (Type uppercase yes): YES            ###是否加密 

Enter passphrase:                                               ###请输入密码

Verify passphrase:                                              ###确认密码

<a href="https://s3.51cto.com/wyfs02/M02/92/48/wKiom1j95ZWghueEABItgQKfNB8223.png-wh_500x0-wm_3-wmp_4-s_789412472.png" target="_blank"></a>

设备解密

[root@desktop ~]# cryptsetup open /dev/vdb1 disk           ###打开加密设备

Enter passphrase for /dev/vdb1: 

[root@desktop ~]# mkfs.xfs /dev/mapper/disk -f                ###格式化设备

meta-data=/dev/mapper/disk       isize=256    agcount=4, agsize=6272 blks

data     =                       bsize=4096   blocks=25088, imaxpct=25

[root@desktop ~]# mount /dev/mapper/disk /mnt/         ###挂载设备

[root@desktop ~]# umount /mnt/                                   ###卸载设备

[root@desktop ~]# cryptsetup close disk                       ###关闭设备加密层

<a href="https://s5.51cto.com/wyfs02/M02/92/49/wKiom1j96HTikzE4ABNGmLCvIHo577.png-wh_500x0-wm_3-wmp_4-s_410858045.png" target="_blank"></a>

<a href="https://s4.51cto.com/wyfs02/M00/92/49/wKiom1j96IGhzESWABNMKpE07dQ190.png-wh_500x0-wm_3-wmp_4-s_4046056798.png" target="_blank"></a>

自动挂载加密设备

设定开机解密

vim /etc/cryptab

<a href="https://s3.51cto.com/wyfs02/M00/92/49/wKiom1j96nyCDBqEABKOt6NHLuo576.png-wh_500x0-wm_3-wmp_4-s_2148897168.png" target="_blank"></a>

<a href="https://s4.51cto.com/wyfs02/M02/92/48/wKioL1j96t-i5BvdABLgD-hB8yE716.png-wh_500x0-wm_3-wmp_4-s_2976815359.png" target="_blank"></a>

写密码文件

vim /root/passwd

<a href="https://s5.51cto.com/wyfs02/M00/92/49/wKiom1j96x_BbzlRABKAwwtZehU021.png-wh_500x0-wm_3-wmp_4-s_892889267.png" target="_blank"></a>

配置解密密码

cryptsetup   luksAddKey   /dev/vdb1   /root/passwd

<a href="https://s2.51cto.com/wyfs02/M01/92/49/wKiom1j968yCNQKiABKQtuHDvxU932.png-wh_500x0-wm_3-wmp_4-s_593670479.png" target="_blank"></a>

本文转自Super_MONKEY 51CTO博客,原文链接:http://blog.51cto.com/supermk/1918981

继续阅读