首页 > 代码库 > Linux学习笔记10

Linux学习笔记10

分区

fdisk /dev/vdb

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)

[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

Partition number (1-5, default 5):

Partition 5 is deleted

 

Command (m for help): n

All primary partitions are in use

Adding logical partition 5

First sector (618496-20971519, default 618496):

Using default value 618496

Last sector, +sectors or +size{K,M,G} (618496-20971519, default 20971519): +100M

Partition 5 of type Linux and of size 100 MiB is set

 

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     102400 vdb1

 253       18     102400 vdb2

 253       19     102400 vdb3

 253       20          1 vdb4

 253       21     102400 vdb5

fdisk -l 查看系统上能识别的设备

cat /proc/partitions

mkfs.xfs /dev/vdb5      格式化分区/dev/vdb5(换个文件系统)之后即可挂载使用

blkid                   查看系统可用的设备

swapon -s               查看swap 分区大小使用情况

                                                                     

磁盘加密

1.磁盘加密

fdisk /dev/vdb            创建磁盘分区

crytestup luksFormat /dev/vdb1    设置加密(设置成功不提示)

crytestup open /dev/vdb1 westos    打开加密磁盘

ll /dev/mapper/            查看是否打开

mkfs.xfs /dev/mapper/westos        格式化

mount /dev/mapper/westos /mnt/    挂载

umount /mnt              卸除挂载

crytestup close westos        关闭磁盘

 

 

2.加密磁盘的永久挂载

vim /etc/crypttab

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

redhat/dev/vdb1/root/lukspsfile

vim /root/lukspsfile            创建加密字符文件

密码

chmod 600 /root/luspsfile            更改文件权限

cryptsetup luksAddKey /dev/vdb1 /root/lukspsfile密码文件的确定(将文件联系在一起)

Enter any passphrase: 需输入密码

vim /etc/fstab

/dev/mapper/redhat  /mnt xfs defaults 0 0        格式

reboot                        重启

 

3.加密的清楚

vim /etc/fstab                    修改文件

> /etc/crypttab                     清空文件内容

rm -rf /root/lukspsfile               删除加密字符文件

umount /mnt                    解除挂载

cryptsetup close redhat                关闭

mkfs.xfs /dev/vdb1 -f               格式化(-f强制执行)

 

4.磁盘阵列

监控命令 watch -n 1 cat /proc/mdstat

fdisk /dev/vdb                    创建磁盘

  Device Boot      Start        End      Blocks   Id  System

/dev/vdb1             2048     2099199     1048576   fd  Linux raid autodetect

/dev/vdb2         2099200     4196351     1048576   fd  Linux raid autodetect

/dev/vdb3         4196352     6293503     1048576   fd  Linux raid autodetect

mdadm -C /dev/md0 -a yes -l 1 -n 2 -x 1 /dev/vdb{1..3}        创建磁盘阵列

-a如果没有该文件自动建立-l级别-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? y

mdadm: Defaulting to version 1.2 metadata

mdadm: array /dev/md0 started.

 

mkfs.xfs /dev/md0         格式化

mount /dev/md0 /mn        挂载


磁盘阵列的卸载

mdadm -f /dev/md0 /dev/vdb1        手动使vdb1损坏

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

 

       0     253       17        -      faulty   /dev/vdb1

  mdadm -r /dev/md0 /dev/vdb1        移除vdb1

  mdadm -f /dev/md0 /dev/vdb2        损坏vdb2

  mdadm -r /dev/md0 /dev/vdb2        移除vdb2

  umount /mnt                解除挂载

  mdadm -S /dev/md0              停止md0

  fdisk /dev/vdb               删除磁盘分区

 

5.配额(针对磁盘)

  mkdir /pub

  fdisk /dev/vdb

  partprobe

  mkfs.xfs /dev/vdb1 -f

  mount -o usrquota /dev/vdb1 /pub                挂载

  chmod 1777 /pub                        更改目录权限

  quotaon -uv /dev/vdb1                    开启配额(u用户v显示详细信息)

  edquota -u student                        更改用户配额

Disk quotas for user student (uid 1000):

  Filesystem                   blocks       soft       hard     inodes     soft     hard

  /dev/vdb1                         0          0      50000          0        0        0blocks所在大小【客观事实】hard最大上线)

  su - student                          切换用户

  dd if=/dev/zero of=/pub/file bs=1M count=100

dd: error writing ‘/pub/file’: Disk quota exceeded    报错(超出配额上线)

49+0 records in

48+0 records out

51200000 bytes (51 MB) copied, 0.0681524 s, 751 MB/s

  quota                                查看状态

Disk quotas for user student (uid 1000):

     Filesystem  blocks   quota   limit   grace   files   quota   limit   grace

      /dev/vdb1   50000*      0   50000               1       0       0        

*代表超出上线,文件会在7天后自动删除)

 

开机自动执行配额

  vim /etc/fstab         写配置文件

/dev/vdb1 /mnt xfs defaults,usrquota 0 0  (注意格式)

  df

Filesystem     1K-blocks    Used Available Use% Mounted on

/dev/vda1       10473900 3808500   6665400  37% /

devtmpfs          481120       0    481120   0% /dev

tmpfs             496708     152    496556   1% /dev/shm

tmpfs             496708   13100    483608   3% /run

tmpfs             496708       0    496708   0% /sys/fs/cgroup

/dev/vdb1         508588   25768    482820   6% /mnt

注:当无法重启时,需用root密码然后更改配置文件

 

1.LVM

1.lvm的建立

1)划分物理分区并修改分区id为8e

fdisk /dev/vdb

   Device Boot      Start         End      Blocks   Id  System

/dev/vdb1            2048     1026047      512000   8e  Linux LVM

/dev/vdb2         1026048     2050047      512000   8e  Linux LVM

pvs|pvdisplay

vgs|vgdisplay

lvs|lvdisplay

监控:watch -n 1 \

 ‘echo "==pvinfo==" \

 ;pvs \

 ;echo "==vginfo==" \

 ;vgs \

 ;echo "==lvinfo==" \

 ;lvs

 ;df -h /mnt‘

  pvcreate /dev/vdb1          创建pv(物理卷)

Physical volume "/dev/vdb1" successfully created

  vgcreate  gou /dev/vdb1        创建vg(卷组)

Volume group "gou" successfully created

  lvcreate  -L 100M -n lv0 gou      创建lv(逻辑卷)

  mkfs.xfs /dev/gou/lv0            格式化

Logical volume "lv0" created

  mount /dev/gou/lv0 /mnt  

 

  lvextend -L 200M /dev/gou/lv0        扩容设备

  xfs_growfs /dev/gou/lv0         更新文件系统

  pvcreate /dev/vdb2            添加磁盘

  vgextend gou /dev/vdb2            扩容vg

  lvextend  -L 600M /dev/gou/lv0

  xfs_growfs /dev/gou/lv0

注:设备大小受文件系统限制最大18EB

 

 

 

设备的缩减

  umount /mnt               卸载

  mkfs.ext4 /dev/gou/lv0         更改格式

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

38400 inodes, 153600 blocks

7680 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=157286400

5 block groups

32768 blocks per group, 32768 fragments per group

7680 inodes per group

Superblock backups stored on blocks:

32768, 98304

 

Allocating group tables: done                            

Writing inode tables: done                            

Creating journal (4096 blocks): done

Writing superblocks and filesystem accounting information: done

 

  e2fsck -f /dev/gou/lv0             检测磁盘内文件

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/gou/lv0: 11/38400 files (0.0% non-contiguous), 6740/153600 blocks

 

  resize2fs  /dev/gou/lv0 200M            缩减文件系统到小

resize2fs 1.42.9 (28-Dec-2013)

Resizing the filesystem on /dev/gou/lv0 to 51200 (4k) blocks.

The filesystem on /dev/gou/lv0 is now 51200 blocks long.

  lvreduce -L 200M /dev/gou/lv0             缩减pv大小

WARNING: Reducing active logical volume to 200.00 MiB

 THIS MAY DESTROY YOUR DATA (filesystem etc.)

Do you really want to reduce lv0? [y/n]: y        确定

Reducing logical volume lv0 to 200.00 MiB

Logical volume lv0 successfully resized

  pvmove /dev/vdb1 /dev/vdb2            数据的转移

/dev/vdb1: Moved: 8.0%

/dev/vdb1: Moved: 100.0%

  vgreduce gou /dev/vdb1移出vg

  pvmove /dev/vdb1                移除磁盘

注:xfs格式无法进行缩减;ext可以;应当先缩减文件系统后缩减设备

 

问题解决

1.先删除了设备

 lvreduce -L 150M /dev/gou/lv0

 mount /dev/gou/lv0 /mnt            挂载(报错)

mount: wrong fs type, bad option, bad superblock on /dev/mapper/gou-lv0,

       missing codepage or helper program, or other error

 

       In some cases useful info is found in syslog - try

       dmesg | tail or so.

  lvextend -L 200M /dev/gou/lv0        扩大设备

  resize2fs /dev/gou/lv0            扩大文件系统

2.将使用中的磁盘删除

 fdisk /dev/vdb                删除磁盘

Welcome to fdisk (util-linux 2.23.2).

Command (m for help): d

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

Partition 1 is deleted

Command (m for help): wq

partprobe                    重新读取


 技术分享

  vgreduce --removemissing gou         删除不能识别的设备

No device found for PV dgSwTp-EqYJ-2RBr-mWSO-6iQV-Jlwd-7HYLKO.

No device found for PV dgSwTp-EqYJ-2RBr-mWSO-6iQV-Jlwd-7HYLKO.

Wrote out consistent volume group gou

 

快照的建立

  lvcreate -L 25M -n lv1 -s /dev/gou/lv0        建立快照(需预估大小)

  mount /dev/gou/lv1 /mnt            挂载快照

  lvremove /dev/gou/lv1                删除快照

 

卸载设备

  umount /mnt                    卸载

  lvremove /dev/gou/lv1                删除快照

  lvremove /dev/gou/lv0                 删除逻辑卷

  vgremove gou                     删除卷组

  pvremove /dev/vdb2                   删除物理卷

  pvs检测

  fdisk /dev/vdb                    删除磁盘(强制)

 

 

                                                                         


Linux学习笔记10