Skip to main content

CentOS7中的几个磁盘操作指令

· 4 min read

常用的磁盘操作命令有 fdisk, cfdisk, sfdisk, mkfs, parted, partprobe kpartx, 在 Linux 中挂载一个新磁盘时,常用到如下操作:

1. fdisk

fdisk 可以用于查看指定硬盘的分区或对指定硬盘进行分区:

如显示所有分区:

# fdisk -l

Disk /dev/vda: 26.8 GB, 26843545600 bytes, 52428800 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: 0x0008f170

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 52428749 26213351 83 Linux

查看帮助信息:

# fdisk -h
Usage:
fdisk [options] change partition table
fdisk [options] -l list partition table(s)
fdisk -s give partition size(s) in blocks

Options:
-b sector size (512, 1024, 2048 or 4096)
-c[=] compatible mode: 'dos' or 'nondos' (default)
-h print this help text
-u[=] display units: 'cylinders' or 'sectors' (default)
-v print program version
-C specify the number of cylinders
-H specify the number of heads
-S specify the number of sectors per track

根据向导对硬盘/dev/sdb 进行分区:

# 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): 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
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)

2. mkfs

用 ext4 格式格式化硬盘分区/dev/sdb1

# mkfs -t ext4 /dev/sdb1

# mkfs.ext4 /dev/sdb1

详细帮助信息:

# mkfs --help
Usage:
mkfs [options] [-t ] [fs-options] []

Options:
-t, --type= filesystem type; when unspecified, ext2 is used
fs-options parameters for the real filesystem builder
path to the device to be used
number of blocks to be used on the device
-V, --verbose explain what is being done;
specifying -V more than once will cause a dry-run
-V, --version display version information and exit;
-V as --version must be the only option
-h, --help display this help text and exit

3. df 查看磁盘使用情况:

# df -h

4. blkid 显示 block devices 信息:

# blkid
/dev/sda2: UUID="cc648f16-2695-451d-a133-e90b5ea8add3" TYPE="ext3"
/dev/sda1: UUID="3cb0a414-123d-4728-aca2-6d18e24e272e" TYPE="ext3"
/dev/sda3: UUID="dc6e8463-90c7-419b-8ce0-0f6adf6d870f" TYPE="swap"
/dev/sdb: UUID="840d4ffe-00ce-4a2e-83c8-b8b94e6d005b" TYPE="ext4"

5. mount 装载分区到指定目录。如装载 ext4 分区/dev/sdb1 到/data 目录:

# mount -t ext4 /dev/sdb1 /data

/data 目录要事先存在。

[source]支持多种标识,如:

# mount -t ext4 -U 840d4ffe-00ce-4a2e-83c8-b8b94e6d005b /data

# mount -t ext4 UUID="840d4ffe-00ce-4a2e-83c8-b8b94e6d005b" /data

mount --all 则装载/etc/fstab 中的所有配置。

5. /etc/fstab mount 指令装载在重启后会丢失,修改/etc/fstab 文件可在系统重启后保持装载:

UUID=cc648f16-2695-451d-a133-e90b5ea8add3   /                       ext3    defaults        1 1
UUID=3cb0a414-123d-4728-aca2-6d18e24e272e /boot ext3 defaults 1 2
UUID=dc6e8463-90c7-419b-8ce0-0f6adf6d870f swap swap defaults 0 0
UUID=840d4ffe-00ce-4a2e-83c8-b8b94e6d005b /var/opt ext4 defaults 1 0

fstab 文件也支持 Label 和 UUID 多种分区标识。详见:http://man7.org/linux/man-pages/man5/fstab.5.html

· One min read
#!/usr/bin/expect
set timeout -1

set userid [lindex $argv 0]
set password [lindex $argv 1]
set hostip [lindex $argv 2]

spawn ssh [email protected]
sleep 5



expect "Password: " { send "$password\r"}
expect "Welcome to Terminal Menu" { send "1\r" }
expect "Device


expec {
"Are you sure you want to continue connecting (yes/no)?" {
send "yes\r"
exp_continue
}

"Password:" {
send "$password\r\n"
exp_continue
}

"Welcome to Terminal Menu" {
send "1\r"
exp_continue
}

"Device List" {
send "1\r"
exp_continue
}

"Device Users" {
send "1\r"
exp_continue
}

"Please input access comment:" {
send "2\r"
exp_continue
}

"Last login:" {
#send "stty -echo\r"
send "echo 登录成功!!!\r"
send "ssh -T -i ~/.ssh/root-$hostip.pem root@$hostip 'bash -s' < ~/deploy/$hostip/deploy.sh folder8\r"
#send "ssh -tt -i ~/.ssh/root-$hostip.pem root@$hostip 'mkdir abcdefg' && exit\r"
#sleep 5
exp_continue
}

"FAILURE-FLAG-D53CD3B3-A0DA-4C06-B239-2464328BAB3C" {
#send_user "部署失败!!!\r"
send "echo 部署失败!!!\r"
exit 1
}

"SUCCESS-FLAG-A8F9B1C6-55C2-47F6-9F0F-6A45C6D9CAA1" {
#send_user "部署成功!!!\r"
send "echo 部署成功!!!\r"
exit 0
}
}
ClustrMaps