block-storage

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Block Storage

块存储

Manage block storage volumes including LVM, cloud-based EBS, filesystem creation, snapshots, and RAID configurations. Covers the full lifecycle from provisioning raw disks to extending volumes in production.
管理包括LVM、基于云的EBS、文件系统创建、快照和RAID配置在内的块存储卷。涵盖从配置原始磁盘到在生产环境中扩展卷的完整生命周期。

When to Use

适用场景

  • Adding, partitioning, or formatting new disks on Linux servers
  • Managing LVM logical volumes for flexible storage allocation
  • Provisioning and attaching cloud block storage (AWS EBS)
  • Creating and restoring snapshots for backup or migration
  • Configuring software RAID for redundancy or performance
  • Extending existing volumes without downtime
  • 在Linux服务器上添加、分区或格式化新磁盘
  • 管理LVM逻辑卷以实现灵活的存储分配
  • 配置并挂载云块存储(AWS EBS)
  • 创建和恢复快照以用于备份或迁移
  • 配置软件RAID以实现冗余或提升性能
  • 无需停机即可扩展现有卷

Prerequisites

前提条件

  • Root or sudo access on the target system
  • lvm2
    package installed for LVM operations
  • mdadm
    package installed for software RAID
  • AWS CLI configured for EBS operations
  • Understanding of the workload's I/O characteristics (IOPS, throughput)
  • 目标系统的Root或sudo权限
  • 安装
    lvm2
    包以进行LVM操作
  • 安装
    mdadm
    包以进行软件RAID操作
  • 已配置AWS CLI以进行EBS操作
  • 了解工作负载的I/O特性(IOPS、吞吐量)

Disk Discovery and Partitioning

磁盘发现与分区

bash
undefined
bash
undefined

List all block devices

List all block devices

lsblk lsblk -f # Show filesystem types and mount points
lsblk lsblk -f # Show filesystem types and mount points

Show detailed disk information

Show detailed disk information

fdisk -l /dev/sdb
fdisk -l /dev/sdb

Identify disk model and health (requires smartmontools)

Identify disk model and health (requires smartmontools)

smartctl -a /dev/sda smartctl -H /dev/sda # Quick health check
smartctl -a /dev/sda smartctl -H /dev/sda # Quick health check

Create a GPT partition table and a single partition

Create a GPT partition table and a single partition

parted /dev/sdb mklabel gpt parted /dev/sdb mkpart primary ext4 0% 100%
parted /dev/sdb mklabel gpt parted /dev/sdb mkpart primary ext4 0% 100%

Alternative: use fdisk for MBR partitioning

Alternative: use fdisk for MBR partitioning

fdisk /dev/sdb
fdisk /dev/sdb

n -> new partition, p -> primary, Enter defaults, w -> write

n -> new partition, p -> primary, Enter defaults, w -> write

Inform the kernel of partition table changes

Inform the kernel of partition table changes

partprobe /dev/sdb
partprobe /dev/sdb

Wipe filesystem signatures (prepare for LVM or RAID)

Wipe filesystem signatures (prepare for LVM or RAID)

wipefs -a /dev/sdb1
undefined
wipefs -a /dev/sdb1
undefined

Filesystem Creation and Management

文件系统创建与管理

bash
undefined
bash
undefined

Create an ext4 filesystem

Create an ext4 filesystem

mkfs.ext4 /dev/sdb1
mkfs.ext4 /dev/sdb1

Create an ext4 filesystem with label and reserved block tuning

Create an ext4 filesystem with label and reserved block tuning

mkfs.ext4 -L appdata -m 1 /dev/sdb1 # 1% reserved blocks (default is 5%)
mkfs.ext4 -L appdata -m 1 /dev/sdb1 # 1% reserved blocks (default is 5%)

Create an XFS filesystem (recommended for large volumes)

Create an XFS filesystem (recommended for large volumes)

mkfs.xfs /dev/sdb1
mkfs.xfs /dev/sdb1

Create an XFS filesystem with label

Create an XFS filesystem with label

mkfs.xfs -L appdata /dev/sdb1
mkfs.xfs -L appdata /dev/sdb1

Mount the filesystem

Mount the filesystem

mkdir -p /data mount /dev/sdb1 /data
mkdir -p /data mount /dev/sdb1 /data

Add persistent mount to fstab (use UUID for reliability)

Add persistent mount to fstab (use UUID for reliability)

blkid /dev/sdb1 # Get the UUID echo 'UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx /data ext4 defaults,noatime 0 2' >> /etc/fstab
blkid /dev/sdb1 # Get the UUID echo 'UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx /data ext4 defaults,noatime 0 2' >> /etc/fstab

Mount all entries in fstab

Mount all entries in fstab

mount -a
mount -a

Check and repair a filesystem (unmount first)

Check and repair a filesystem (unmount first)

umount /data fsck.ext4 -y /dev/sdb1 xfs_repair /dev/sdb1 # For XFS
umount /data fsck.ext4 -y /dev/sdb1 xfs_repair /dev/sdb1 # For XFS

Resize ext4 (can grow online while mounted)

Resize ext4 (can grow online while mounted)

resize2fs /dev/sdb1
resize2fs /dev/sdb1

Resize XFS (must be mounted to grow)

Resize XFS (must be mounted to grow)

xfs_growfs /data
xfs_growfs /data

Check filesystem usage

Check filesystem usage

df -hT
undefined
df -hT
undefined

LVM Management

LVM管理

Creating an LVM Stack

创建LVM存储栈

bash
undefined
bash
undefined

Step 1: Create physical volumes

Step 1: Create physical volumes

pvcreate /dev/sdb /dev/sdc
pvcreate /dev/sdb /dev/sdc

View physical volumes

View physical volumes

pvs pvdisplay /dev/sdb
pvs pvdisplay /dev/sdb

Step 2: Create a volume group from physical volumes

Step 2: Create a volume group from physical volumes

vgcreate data_vg /dev/sdb /dev/sdc
vgcreate data_vg /dev/sdb /dev/sdc

View volume groups

View volume groups

vgs vgdisplay data_vg
vgs vgdisplay data_vg

Step 3: Create logical volumes

Step 3: Create logical volumes

Fixed size

Fixed size

lvcreate -L 100G -n app_lv data_vg
lvcreate -L 100G -n app_lv data_vg

Use percentage of free space

Use percentage of free space

lvcreate -l 50%FREE -n logs_lv data_vg
lvcreate -l 50%FREE -n logs_lv data_vg

Use all remaining space

Use all remaining space

lvcreate -l 100%FREE -n backup_lv data_vg
lvcreate -l 100%FREE -n backup_lv data_vg

View logical volumes

View logical volumes

lvs lvdisplay /dev/data_vg/app_lv
lvs lvdisplay /dev/data_vg/app_lv

Step 4: Create filesystem and mount

Step 4: Create filesystem and mount

mkfs.ext4 /dev/data_vg/app_lv mkdir -p /data/app mount /dev/data_vg/app_lv /data/app
mkfs.ext4 /dev/data_vg/app_lv mkdir -p /data/app mount /dev/data_vg/app_lv /data/app

Add to fstab

Add to fstab

echo '/dev/data_vg/app_lv /data/app ext4 defaults,noatime 0 2' >> /etc/fstab
undefined
echo '/dev/data_vg/app_lv /data/app ext4 defaults,noatime 0 2' >> /etc/fstab
undefined

Extending Volumes (Online)

在线扩展卷

bash
undefined
bash
undefined

Extend a logical volume by 20 GB

Extend a logical volume by 20 GB

lvextend -L +20G /dev/data_vg/app_lv
lvextend -L +20G /dev/data_vg/app_lv

Extend to fill all free space in the VG

Extend to fill all free space in the VG

lvextend -l +100%FREE /dev/data_vg/app_lv
lvextend -l +100%FREE /dev/data_vg/app_lv

Grow the ext4 filesystem (online, no unmount needed)

Grow the ext4 filesystem (online, no unmount needed)

resize2fs /dev/data_vg/app_lv
resize2fs /dev/data_vg/app_lv

Grow XFS filesystem (online)

Grow XFS filesystem (online)

xfs_growfs /data/app
xfs_growfs /data/app

Combined: extend LV and resize filesystem in one command

Combined: extend LV and resize filesystem in one command

lvextend -L +20G --resizefs /dev/data_vg/app_lv
undefined
lvextend -L +20G --resizefs /dev/data_vg/app_lv
undefined

Adding a New Disk to an Existing VG

为现有卷组添加新磁盘

bash
undefined
bash
undefined

Add a new physical volume

Add a new physical volume

pvcreate /dev/sdd
pvcreate /dev/sdd

Extend the volume group

Extend the volume group

vgextend data_vg /dev/sdd
vgextend data_vg /dev/sdd

Now extend any logical volume using the new space

Now extend any logical volume using the new space

lvextend -l +100%FREE --resizefs /dev/data_vg/app_lv
undefined
lvextend -l +100%FREE --resizefs /dev/data_vg/app_lv
undefined

LVM Snapshots

LVM快照

bash
undefined
bash
undefined

Create a snapshot (requires free space in VG)

Create a snapshot (requires free space in VG)

lvcreate -L 10G -s -n app_snap /dev/data_vg/app_lv
lvcreate -L 10G -s -n app_snap /dev/data_vg/app_lv

Mount the snapshot read-only for backup

Mount the snapshot read-only for backup

mkdir -p /mnt/snapshot mount -o ro /dev/data_vg/app_snap /mnt/snapshot
mkdir -p /mnt/snapshot mount -o ro /dev/data_vg/app_snap /mnt/snapshot

Perform backup from the snapshot

Perform backup from the snapshot

tar czf /backup/app-$(date +%Y%m%d).tar.gz -C /mnt/snapshot .
tar czf /backup/app-$(date +%Y%m%d).tar.gz -C /mnt/snapshot .

Unmount and remove the snapshot when done

Unmount and remove the snapshot when done

umount /mnt/snapshot lvremove -f /dev/data_vg/app_snap
umount /mnt/snapshot lvremove -f /dev/data_vg/app_snap

Restore from snapshot (reverts LV to snapshot point -- destructive)

Restore from snapshot (reverts LV to snapshot point -- destructive)

lvconvert --merge /dev/data_vg/app_snap
lvconvert --merge /dev/data_vg/app_snap

Note: if the LV is mounted, merge happens at next activation (reboot)

Note: if the LV is mounted, merge happens at next activation (reboot)

undefined
undefined

Reducing and Removing LVM Components

缩减与移除LVM组件

bash
undefined
bash
undefined

Shrink a logical volume (ext4 only -- XFS cannot shrink)

Shrink a logical volume (ext4 only -- XFS cannot shrink)

MUST unmount first

MUST unmount first

umount /data/app e2fsck -f /dev/data_vg/app_lv resize2fs /dev/data_vg/app_lv 80G lvreduce -L 80G /dev/data_vg/app_lv mount /data/app
umount /data/app e2fsck -f /dev/data_vg/app_lv resize2fs /dev/data_vg/app_lv 80G lvreduce -L 80G /dev/data_vg/app_lv mount /data/app

Remove a logical volume

Remove a logical volume

umount /data/app lvremove /dev/data_vg/app_lv
umount /data/app lvremove /dev/data_vg/app_lv

Remove a disk from a volume group (migrate data off first)

Remove a disk from a volume group (migrate data off first)

pvmove /dev/sdc # Migrate extents to other PVs vgreduce data_vg /dev/sdc # Remove PV from VG pvremove /dev/sdc # Clean PV metadata
undefined
pvmove /dev/sdc # Migrate extents to other PVs vgreduce data_vg /dev/sdc # Remove PV from VG pvremove /dev/sdc # Clean PV metadata
undefined

AWS EBS Management

AWS EBS管理

bash
undefined
bash
undefined

Create a gp3 volume (general purpose SSD)

Create a gp3 volume (general purpose SSD)

aws ec2 create-volume
--availability-zone us-east-1a
--size 100
--volume-type gp3
--iops 3000
--throughput 125
--tag-specifications 'ResourceType=volume,Tags=[{Key=Name,Value=app-data},{Key=Environment,Value=production}]'
aws ec2 create-volume
--availability-zone us-east-1a
--size 100
--volume-type gp3
--iops 3000
--throughput 125
--tag-specifications 'ResourceType=volume,Tags=[{Key=Name,Value=app-data},{Key=Environment,Value=production}]'

Create an io2 volume (provisioned IOPS SSD for databases)

Create an io2 volume (provisioned IOPS SSD for databases)

aws ec2 create-volume
--availability-zone us-east-1a
--size 500
--volume-type io2
--iops 10000
aws ec2 create-volume
--availability-zone us-east-1a
--size 500
--volume-type io2
--iops 10000

List volumes with filters

List volumes with filters

aws ec2 describe-volumes
--filters "Name=tag:Environment,Values=production"
--query 'Volumes[*].{ID:VolumeId,Size:Size,Type:VolumeType,State:State,AZ:AvailabilityZone}'
--output table
aws ec2 describe-volumes
--filters "Name=tag:Environment,Values=production"
--query 'Volumes[*].{ID:VolumeId,Size:Size,Type:VolumeType,State:State,AZ:AvailabilityZone}'
--output table

Attach a volume to an instance

Attach a volume to an instance

aws ec2 attach-volume
--volume-id vol-0abc123def456789
--instance-id i-0abc123def456789
--device /dev/xvdf
aws ec2 attach-volume
--volume-id vol-0abc123def456789
--instance-id i-0abc123def456789
--device /dev/xvdf

After attaching, format and mount on the instance

After attaching, format and mount on the instance

lsblk # Identify the new device (e.g., /dev/nvme1n1) mkfs.ext4 /dev/nvme1n1 mkdir -p /data mount /dev/nvme1n1 /data
lsblk # Identify the new device (e.g., /dev/nvme1n1) mkfs.ext4 /dev/nvme1n1 mkdir -p /data mount /dev/nvme1n1 /data

Modify a volume (resize without detaching -- gp3/io2)

Modify a volume (resize without detaching -- gp3/io2)

aws ec2 modify-volume
--volume-id vol-0abc123def456789
--size 200
aws ec2 modify-volume
--volume-id vol-0abc123def456789
--size 200

After resize, grow the filesystem on the instance

After resize, grow the filesystem on the instance

growpart /dev/nvme1n1 1 # If partitioned resize2fs /dev/nvme1n1 # ext4
growpart /dev/nvme1n1 1 # If partitioned resize2fs /dev/nvme1n1 # ext4

xfs_growfs /data # XFS

xfs_growfs /data # XFS

Create a snapshot

Create a snapshot

aws ec2 create-snapshot
--volume-id vol-0abc123def456789
--description "Pre-upgrade snapshot $(date +%Y-%m-%d)"
--tag-specifications 'ResourceType=snapshot,Tags=[{Key=Name,Value=pre-upgrade}]'
aws ec2 create-snapshot
--volume-id vol-0abc123def456789
--description "Pre-upgrade snapshot $(date +%Y-%m-%d)"
--tag-specifications 'ResourceType=snapshot,Tags=[{Key=Name,Value=pre-upgrade}]'

List snapshots

List snapshots

aws ec2 describe-snapshots
--owner-ids self
--query 'Snapshots[*].{ID:SnapshotId,Vol:VolumeId,Size:VolumeSize,Date:StartTime,Desc:Description}'
--output table
aws ec2 describe-snapshots
--owner-ids self
--query 'Snapshots[*].{ID:SnapshotId,Vol:VolumeId,Size:VolumeSize,Date:StartTime,Desc:Description}'
--output table

Create a volume from a snapshot (for restore or migration)

Create a volume from a snapshot (for restore or migration)

aws ec2 create-volume
--snapshot-id snap-0abc123def456789
--availability-zone us-east-1a
--volume-type gp3
aws ec2 create-volume
--snapshot-id snap-0abc123def456789
--availability-zone us-east-1a
--volume-type gp3

Detach a volume

Detach a volume

aws ec2 detach-volume --volume-id vol-0abc123def456789
aws ec2 detach-volume --volume-id vol-0abc123def456789

Delete a volume (ensure it is detached first)

Delete a volume (ensure it is detached first)

aws ec2 delete-volume --volume-id vol-0abc123def456789
undefined
aws ec2 delete-volume --volume-id vol-0abc123def456789
undefined

Software RAID (mdadm)

软件RAID(mdadm)

bash
undefined
bash
undefined

Install mdadm

Install mdadm

apt install -y mdadm # Debian/Ubuntu dnf install -y mdadm # RHEL/CentOS
apt install -y mdadm # Debian/Ubuntu dnf install -y mdadm # RHEL/CentOS

Create RAID 1 (mirror) with 2 disks

Create RAID 1 (mirror) with 2 disks

mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb /dev/sdc
mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb /dev/sdc

Create RAID 5 (striped with parity) with 3 disks + 1 spare

Create RAID 5 (striped with parity) with 3 disks + 1 spare

mdadm --create /dev/md0 --level=5 --raid-devices=3 --spare-devices=1 /dev/sdb /dev/sdc /dev/sdd /dev/sde
mdadm --create /dev/md0 --level=5 --raid-devices=3 --spare-devices=1 /dev/sdb /dev/sdc /dev/sdd /dev/sde

Create RAID 10 (striped mirrors) with 4 disks

Create RAID 10 (striped mirrors) with 4 disks

mdadm --create /dev/md0 --level=10 --raid-devices=4 /dev/sdb /dev/sdc /dev/sdd /dev/sde
mdadm --create /dev/md0 --level=10 --raid-devices=4 /dev/sdb /dev/sdc /dev/sdd /dev/sde

Check RAID status

Check RAID status

cat /proc/mdstat mdadm --detail /dev/md0
cat /proc/mdstat mdadm --detail /dev/md0

Save RAID configuration (persists across reboot)

Save RAID configuration (persists across reboot)

mdadm --detail --scan >> /etc/mdadm/mdadm.conf # Debian mdadm --detail --scan >> /etc/mdadm.conf # RHEL update-initramfs -u # Debian
mdadm --detail --scan >> /etc/mdadm/mdadm.conf # Debian mdadm --detail --scan >> /etc/mdadm.conf # RHEL update-initramfs -u # Debian

Create filesystem on RAID device

Create filesystem on RAID device

mkfs.ext4 /dev/md0 mkdir -p /data mount /dev/md0 /data
mkfs.ext4 /dev/md0 mkdir -p /data mount /dev/md0 /data

Replace a failed disk

Replace a failed disk

mdadm --manage /dev/md0 --fail /dev/sdc mdadm --manage /dev/md0 --remove /dev/sdc
mdadm --manage /dev/md0 --fail /dev/sdc mdadm --manage /dev/md0 --remove /dev/sdc

Insert new disk, then:

Insert new disk, then:

mdadm --manage /dev/md0 --add /dev/sdf
mdadm --manage /dev/md0 --add /dev/sdf

Monitor rebuild progress

Monitor rebuild progress

watch cat /proc/mdstat
watch cat /proc/mdstat

RAID level recommendations:

RAID level recommendations:

RAID 1: 2+ disks, mirroring, good for OS / boot drives

RAID 1: 2+ disks, mirroring, good for OS / boot drives

RAID 5: 3+ disks, single parity, good read performance

RAID 5: 3+ disks, single parity, good read performance

RAID 6: 4+ disks, double parity, survives 2 disk failures

RAID 6: 4+ disks, double parity, survives 2 disk failures

RAID 10: 4+ disks, mirrored stripes, best I/O performance

RAID 10: 4+ disks, mirrored stripes, best I/O performance

undefined
undefined

Troubleshooting

故障排查

SymptomDiagnostic CommandCommon Fix
Disk not showing up
lsblk
,
dmesg | tail
Check physical connection; rescan SCSI bus
Filesystem read-only
dmesg | grep error
,
mount
Filesystem errors detected; run
fsck
after unmount
LVM: no free space in VG
vgs
,
pvs
Add a new PV with
vgextend
EBS volume not visible
lsblk
on instance
Check attach status in AWS console; NVMe naming differs
RAID degraded
cat /proc/mdstat
Replace failed disk with
mdadm --manage --add
Cannot resize filesystem
lvs
,
df -h
Extend LV first, then resize FS; XFS needs to be mounted
Slow I/O on EBS
iostat -x 2
, check volume type
Upgrade to gp3/io2, increase IOPS/throughput
Snapshot taking too longAWS Console: snapshot progressSnapshots are incremental; first one takes longest
症状诊断命令常见修复方法
磁盘未显示
lsblk
,
dmesg | tail
检查物理连接;重新扫描SCSI总线
文件系统只读
dmesg | grep error
,
mount
检测到文件系统错误;卸载后运行
fsck
LVM:卷组无可用空间
vgs
,
pvs
使用
vgextend
添加新的PV
EBS卷不可见在实例上执行
lsblk
在AWS控制台检查挂载状态;NVME命名规则不同
RAID降级
cat /proc/mdstat
使用
mdadm --manage --add
替换故障磁盘
无法调整文件系统大小
lvs
,
df -h
先扩展LV,再调整文件系统大小;XFS需要处于挂载状态
EBS I/O缓慢
iostat -x 2
, 检查卷类型
升级到gp3/io2,增加IOPS/吞吐量
快照创建耗时过长AWS控制台:查看快照进度快照为增量式;首次创建耗时最长

Related Skills

相关技能

  • linux-administration
    -- Disk and filesystem basics
  • performance-tuning
    -- I/O scheduler and benchmarking with fio
  • nfs-storage
    -- Network filesystems built on top of block storage
  • backup-recovery
    -- Snapshot-based and file-level backup strategies
  • object-storage
    -- Alternative storage model for unstructured data
  • linux-administration
    —— 磁盘与文件系统基础
  • performance-tuning
    —— I/O调度器与fio基准测试
  • nfs-storage
    —— 基于块存储构建的网络文件系统
  • backup-recovery
    —— 基于快照与文件级的备份策略
  • object-storage
    —— 非结构化数据的替代存储模型