linux 磁盘基本概念和shell脚本

1,GPT概念,和使用

磁盘分区机制有传统的MBR和较新的GPT
MBR主要特点:
		1,支持32位和64位
		2,支持的分区数量有限
		3,不支持超过2T的硬盘,超过2t的只能使用2t空间
GPT分区
		1,64位
		2,分区数几乎没有限制
		2,超过2T的硬盘
		3,需要配合uefi硬件使用
GPT的使用是通过parted命令使用的
	parted [options] [device [command [options...]...]]
		-h 帮助
		-l list
		-s 从不提示用户
		-v 版本
		在磁盘上创建分区
			parted /dev/sdb
		创建 gpt分区表
			mklabel 
				<< gpt
		创建分区大小格式等
			mkpart
		print 显示创建的分区
		删除分区
			rm 分区号
		或者退出parted 通过mkfs 格式化分区
		挂载分区
		mkdir /mnt/newpart
		mount /dev/sdb /mnt/newpart

2,创建一个10G的分区,并格式化为etx4 要求

	```
	[root@centos7 ~]# 
	[root@centos7 ~]# fdisk /dev/sdb
	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 0x146853c4.
	
	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): +9G  ----分区大小
	Partition 1 of type Linux and of size 9 GiB is set
	
	Command (m for help): w    ---------保存
	The partition table has been altered!
	
	Calling ioctl() to re-read partition table.
	Syncing disks.
	[root@centos7 ~]# 

block大小为2048,预留空间20%,卷标 MYDATA

	[root@centos7 ~]# mkfs -t ext4 -b 2048 -m 20 -L MYDATA /dev/sdb1
	mke2fs 1.42.9 (28-Dec-2013)
	Filesystem label=MYDATA
	OS type: Linux
	Block size=2048 (log=1)
	Fragment size=2048 (log=1)
	Stride=0 blocks, Stripe width=0 blocks
	589824 inodes, 4718592 blocks
	943718 blocks (20.00%) reserved for the super user
	First data block=0
	Maximum filesystem blocks=273154048
	288 block groups
	16384 blocks per group, 16384 fragments per group
	2048 inodes per group
	Superblock backups stored on blocks: 
		16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816, 1327104, 
		2048000, 3981312
	
	Allocating group tables: done                            
	Writing inode tables: done                            
	Creating journal (32768 blocks): done
	Writing superblocks and filesystem accounting information: done

挂载/mydata目录,挂载时禁止程序自动运行,且不更新文件的时间戳

	[root@centos7 ~]# mkdir /mydata
	[root@centos7 ~]# mount -o noatime,noexec /dev/sdb1 /mydata

可开机自动挂载

	[root@centos7 ~]# blkid /dev/sdb1
	/dev/sdb1: LABEL="MYDATA" UUID="a173eb08-9720-473a-95e5-856304fc0866" TYPE="ext4"
	[root@centos7 ~]# echo "UUID=a173eb08-9720-473a-95e5-856304fc0866 /mydata                 ext4    defaults        0 0" >> /etc/fstab

3,创建一个1G的swap分区,并启用

	[root@centos7 ~]# fdisk /dev/sdb
	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 (1 primary, 0 extended, 3 free)
	   e   extended
	Select (default p): p
	Partition number (2-4, default 2): 2
	First sector (18876416-20971519, default 18876416): +1G
	Value out of range.
	First sector (18876416-20971519, default 18876416): 
	Using default value 18876416
	Last sector, +sectors or +size{K,M,G} (18876416-20971519, default 20971519): +900M
	Partition 2 of type Linux and of size 900 MiB is set
	
	Command (m for help): t
	Partition number (1,2, default 2): 2
	Hex code (type L to list all codes): 82
	Changed type of partition 'Linux' to 'Linux swap / Solaris'
	
	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@centos7 ~]# fdisk -l /dev/sdb
	
	Disk /dev/sdb: 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: 0x146853c4
	
	   Device Boot      Start         End      Blocks   Id  System
	/dev/sdb1            2048    18876415     9437184   83  Linux
	/dev/sdb2        18876416    20719615      921600   82  Linux swap / Solaris
	[root@centos7 ~]# mkswap /dev/sdb2
	/dev/sdb2: No such file or directory
	
	[root@centos7 ~]# partprobe   ----重读分区表,不重启的情况下重读分区表
	Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
	[root@centos7 ~]# mkswap /dev/sdb2
	Setting up swapspace version 1, size = 921596 KiB
	no label, UUID=972c95ea-8324-4b23-b8a5-34c43381c1d7
	[root@centos7 ~]# swapon /dev/sdb2
	[root@centos7 ~]# echo "UUID=972c95ea-8324-4b23-b8a5-34c43381c1d7                 swap    swap        0 0" >> /etc/fstab

4,编写脚本计算/etc/passwd文件中第10个用户和第20个用户id之和

	#!/bin/bash

	#判断系统是否存在20个用户
	
	usernum=$(cat /etc/passwd|wc -l)
	
	[ $usernum -lt 20 ] && echo "用户个数少于20" && exit 2
	
	uid1=$(cat /etc/passwd |head -10 |tail -1 |cut -d: -f3)
	uid2=$(cat /etc/passwd |head -20 |tail -1 |cut -d: -f3)
	
	echo "第10个用户ID为:$uid1"
	echo "第20个用户ID为:$uid2"
	uidsum=$[$uid1+$uid2]
	
	echo "第10个和第20个的用户ID之和是:$uidsum"

	bash count.sh
	[root@centos7 ~]# bash count.sh 
		第10个用户ID为:11
		第20个用户ID为:996
		第10个和第20个的用户ID之和是:1007

5,将当前主机名保存到hostName变量中,主机名如果为空,或者为localhost.localdomain则将设置为www.magedu.com

	#!/bin/bash
	hostName=$(hostname)
	[ "$hostName" == "localhost.localdomain" -o -z "hostName" -o -n "hostname" ] && hostnamectl set-hostname www.magedu.com;echo 'hostName='$hostName

6 ,编写脚本,通过命令行参数传入一个用户名,判断id号是奇数还是偶数

	#!/bin/bash
	read -p "请输入用户名: " usename
	id=$(id -u $usename)
	echo "The user \"$usename\" id is $id"
	id_yu=$[${id}%2]
	if [ $id_yu -eq 0 ];then
	    echo "是个偶数" 
	else
	    echo "是个奇数"
	fi

7,lvm 基本应用及扩展缩减实现

	对指定磁盘进行分区
	fdisk /dev/sdb
	制作物理卷
	pvcreate /dev/sdb
	创建卷组
	vgcreate testvg /dev/sdb
	创建逻辑卷
	lvcreate -L 5G -n testlv testvg
	创建文件系统
	mkfs.ext4 /dev/testvg/testlv 
	挂载
	mkdir -p /lvdata
	mount /dev/testvg/testlv /lvdata
	扩展
	lvextend -L +1G /dev/testvg/testlv
	resize2fs -p /dev/testvg/testlv
	缩减
	umount /lvdata
	e2fsck -f /dev/testvg/testlv
	resize2fs -p /dev/testvg/testlv 1G
	lvreduce -L 1G /dev/testvg/testlv
	mount /dev/testvg/testlv /lvdata
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值