首页 > 代码库 > 西部开源学习笔记BOOK2-《unit 4》

西部开源学习笔记BOOK2-《unit 4》

##############################

##### unit4.管理系统存储 #####

##############################


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


Device does not contain a recognized partition table

Building a new DOS disklabel with disk identifier 0x2f15e742.


Command (m for help): m              ##帮助

Command action

 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)


Command (m for help): n

Partition type:

p primary (0 primary, 0 extended, 4 free)            ##分区类型为主分区

e extended                                           ##分区类型为扩展分区

Select (default p): p                                ##默认选p

Partition number (1-4, default 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): 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: 0x2f15e742


Device Boot     Start     End     Blocks     Id     System

/dev/vdb1       2048      206847   102400    83     Linux


Command (m for help): wq                             ##保存退出,如果按q表示放弃更改退出

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

253     17    102400    vdb1

253     20    1         vdb4

注意:扩展分区的分区起始位置和分区大小都选择建议选择默认。

扩展分区的大小显示为1



###########2.磁盘分区格式化##########

[root@localhost ~]# blkid                      ##显示可用的磁盘分区

/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs"


[root@localhost ~]# mount /dev/vdb1 /mnt/      ##挂载分区

mount: /dev/vdb1 is write-protected, mounting read-only

mount: unknown filesystem type ‘(null)‘        ##该磁盘分区不可挂载,因为文件系统无法识别


[root@localhost ~]# mkfs.                     ##按tab之后会显示该系统可格式化的文件系统类型

mkfs.btrfs mkfs.ext2 mkfs.ext4 mkfs.minix mkfs.vfat

mkfs.cramfs mkfs.ext3 mkfs.fat mkfs.msdos mkfs.xfs

[root@localhost ~]# mkfs.xfs /dev/vdb1         ##linux一般选择xfs文件系统

meta-data=http://www.mamicode.com/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


注意:扩展分区不用修改文件系统


###########3.swap分区###########

[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/vdb6     swap     swap     defaults     0     0

/dev/vdb5     /mnt     xfs      defaults     0     0

    ||         ||       ||         ||        ||    ||

设备名称     挂载点  设备类型  挂载参数  是否备份 是否检测


#########4.磁盘加密##########

1.磁盘加密

(1).创建一个磁盘

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

....                     ##此过程参照第一小节

[root@desktop19 ~]# cat /proc/partitions

major minor #blocks name


253     0     10485760     vda

253     1     10484142     vda1

253     16    10485760     vdb

253     17    102400       vdb1


(2).加密

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

WARNING!

========

This will overwrite data on /dev/vdb1 irrevocably.

Are you sure? (Type uppercase yes): YES

Enter passphrase:                              ##密码必须是大于8位的字符数字组合

Verify passphrase:


[root@desktop19 ~]# mount /dev/vdb1 /mnt/

mount: unknown filesystem type ‘crypto_LUKS‘   ##无法挂载,因为无法识别该分区的文件系统


(3).打开锁

[root@desktop19 ~]# cryptsetup open /dev/vdb1 tbr

Enter passphrase for /dev/vdb1:


[root@desktop19 ~]# ll /dev/mapper/

total 0

crw-------. 1 root root 10, 236 11月 5 20:58 control

lrwxrwxrwx. 1 root root 7 11月 5 21:27 tbr -> ../dm-0


(4).创建文件系统

[root@desktop19 ~]# mkfs.xfs /dev/mapper/tbr

meta-data=http://www.mamicode.com/dev/mapper/tbr isize=256 agcount=4, agsize=6272 blks

         =                    sectsz=512 attr=2, projid32bit=1

         =                    crc=0

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


(5).挂载

[root@desktop19 ~]# mount /dev/mapper/tbr /mnt/

============这之后就可以在挂载好的/mnt/路径下作操作了===========


(6).卸载

[root@desktop19 ~]# cryptsetup close tbr

device-mapper: remove ioctl on tbr failed: Device or resource busy

[root@desktop19 ~]# umount /dev/mapper/tbr


(7).关闭锁

[root@desktop19 ~]# df

Filesystem 1K-blocks Used Available Use% Mounted on

/dev/vda1 10473900 3809024 6664876 37% /

devtmpfs 927072 0 927072 0% /dev

tmpfs 942660 84 942576 1% /dev/shm

tmpfs 942660 17092 925568 2% /run

tmpfs 942660 0 942660 0% /sys/fs/cgroup


2.加密磁盘的永久挂载

[root@desktop19 ~]# df                 ##当前状态/dev/mapper/tbr并未挂载到/mnt下

Filesystem     1K-blocks     Used     Available     Use%     Mounted on

/dev/vda1      10473900     3809024     6664876     37%       /

devtmpfs       927072       0            927072     0%        /dev

tmpfs          942660       84           942576     1%        /dev/shm

tmpfs          942660       17092        925568     2%        /run

tmpfs          942660       0            942660     0%        /sys/fs/cgroup


[root@desktop19 ~]# vim /etc/crypttab

tbr                 /dev/vdb1     /root/lukspsfile

||                     ||             ||

解密后设备管理文件     设备         加密字符存放文件

[root@desktop19 ~]# vim /root/lukspsfile     ##配置加密字符存放文件

tbr952q8                                     ##加密时用的密码


[root@desktop19 ~]# chmod 600 /root/lukspsfile      ##修改加密配置文件的权限为只有root可rw


[root@desktop19 ~]# cryptsetup luksAddKey /dev/vdb1 /root/lukspsfile

Enter any passphrase:

                                                    ##关联磁盘,加密文件


[root@desktop19 ~]# vim /etc/fstab                  ##设置开机自动挂载

/dev/mapper/tbr /mnt xfs defaults 0 0


[root@desktop19 ~]# reboot                          ##重启


[root@desktop19 ~]# df                              ##当重启之后/dev/mapper/tbr就已经挂载到/mnt下了

Filesystem     1K-blocks     Used     Available     Use%     Mounted on

/dev/vda1     10473900     3808176     6665724     37%         /

devtmpfs       927072       0          927072      0%          /dev

tmpfs          942660       84          942576     1%          /dev/shm

tmpfs          942660       17036       925624     2%          /run

tmpfs          942660       0           942660     0%          /sys/fs/cgroup

/dev/mapper/tbr 96940       5176         91764     6%          /mnt


3.加密清除

[root@desktop19 ~]# vim /etc/fstab          ##删除开机自动挂载的配置

/dev/mapper/tbr     /mnt     xfs     defaults     0     0         ##删除该行

[root@desktop19 ~]# > /etc/crypttab         ##删除

[root@desktop19 ~]# rm -fr /root/lukspsfile

[root@desktop19 ~]# umount /mnt/

[root@desktop19 ~]# cryptsetup close tbr

[root@desktop19 ~]# mkfs.xfs /dev/vdb1

mkfs.xfs: /dev/vdb1 appears to contain an existing filesystem (crypto_LUKS).

mkfs.xfs: Use the -f option to force overwrite.

[root@desktop19 ~]# mkfs.xfs /dev/vdb1 -f

meta-data=http://www.mamicode.com/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



###########5.磁盘阵列############

软raid====硬raid

raid 0 1 5

0:2块磁盘同时存1份数据

1:2块磁盘同时存两份数据

5:0+1


1.创建磁盘阵列

监控命令:

[root@desktop19 ~]# watch -n 1 cat /proc/mdstat

Personalities : [raid1]

md0 : active raid1 vdb3[2](S) vdb2[1] vdb1[0]     ##(S)-spare表示该设备闲置

      102272 blocks super 1.2 [2/2] [UU]


(1).先创建3个磁盘,并依次将其id改为fd

Command (m for help): t

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

1e Hidden W95 FAT1 80 Old Minix

Hex code (type L to list all codes): fd         ##在L中查看id号是:fd Linux raid auto

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


(2).创建磁盘阵列

[root@desktop19 ~]# mdadm -C /dev/md0 -a yes -l 1 -n 2 -x 1 /dev/vdb{1..3}

##-C 创建磁盘阵列     ##-a 如果文件不存在则自动创建     ##-l 磁盘阵列level(raid 0,1,5)

##-n 设备个数         ##-x 闲置设备个数

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=http://www.mamicode.com/0.90

Continue creating array?

Continue creating array? (y/n) y

mdadm: Defaulting to version 1.2 metadata

mdadm: array /dev/md0 started.


[root@desktop19 ~]# mkfs.xfs /dev/md0

meta-data=http://www.mamicode.com/dev/md0 isize=256 agcount=4, agsize=6392 blks

         =             sectsz=512 attr=2, projid32bit=1

         =             crc=0

data     =             bsize=4096 blocks=25568, 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@desktop19 ~]# mount /dev/md0 /mnt/


2.删除磁盘阵列

(1).破坏md0中的vdb1,2--->删除md0中的vdb1,2---->注意:剩下最后一个vdb3只能用(2)之后的步骤来删除了

[root@desktop19 ~]# mdadm -f /dev/md0 /dev/vdb1 ##-f(faulty) 破坏md0的vdb1

[root@desktop19 ~]# mdadm -D /dev/md0      ##-D 显示md0的使用情况

/dev/md0:

    Version : 1.2

    Creation Time : Sat Nov 5 22:59:22 2016

    Raid Level : raid1

    Array Size : 102272 (99.89 MiB 104.73 MB)

    Used Dev Size : 102272 (99.89 MiB 104.73 MB)

    Raid Devices : 2

    Total Devices : 3

    Persistence : Superblock is persistent

    Update Time : Sat Nov 5 23:32:37 2016

    State : clean

    Active Devices : 2

    Working Devices : 2

    Failed Devices : 1

    Spare Devices : 0

    Name : desktop19.example.com:0 (local to host desktop19.example.com)

    UUID : 111646c2:8aa85c7a:c5f0b2c6:23a93cdc

    Events : 36

    Number     Major     Minor     RaidDevice     State

    2          253         19         0         active sync /dev/vdb3

    1          253         18         1         active sync /dev/vdb2

    0          253         17         -         faulty /dev/vdb1


[root@desktop19 ~]# mdadm -r /dev/md0 /dev/vdb1 ##-r(remove) 删除md0中的vdb1

[root@desktop19 ~]# mdadm -D /dev/md0

    Number     Major     Minor     RaidDevice     State

    2          253         19         0         active sync /dev/vdb3

    1          253         18         1         active sync /dev/vdb2

(补充:

[root@desktop19 ~]# mdadm -a /dev/md0 /dev/vdb1     ##-a(add) 添加vdb1到md0

[root@desktop19 ~]# mdadm -D /dev/md0

Number Major Minor RaidDevice State

2 253 19 0 active sync /dev/vdb3

1 253 18 1 active sync /dev/vdb2

3 253 17 - spare /dev/vdb1


[root@desktop19 ~]# mdadm -f /dev/md0 /dev/vdb3

mdadm: set device faulty failed for /dev/vdb3: Device or resource busy

##当破坏到vdb3的时候报错,只能使用以下步骤来继续删除


(2).卸载md0

[root@desktop19 ~]# umount /mnt/


(3).停止磁盘阵列md0

[root@desktop19 ~]# mdadm -S /dev/md0

mdadm: stopped /dev/md0


(4).依次删除磁盘阵列所用的磁盘

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


Command (m for help): d ##删除分区

Partition number (1-3, default 3):


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: 0x27721115

    

    Device  Boot     Start     End     Blocks     Id     System


Command (m for help): wq      ##保存退出

The partition table has been altered!


Calling ioctl() to re-read partition table.

Syncing disks.


[root@desktop19 ~]# partprobe  ##注意:做完之后不要忘记同步分区表


###########6.配额#############

超出的限额会在7天后清除。

注意:配额是针对于分区的,只在该分区里生效。是该用户在该分区下的限额。


西部开源学习笔记BOOK2-《unit 4》