调整btrfs分区的大小

背景

我有个应用, 之前分配磁盘空间不够用了(只分了1TB), 现在找管理员分多2TB, 加起来就是3TB可用空间. 后端用的是CEPH文件系统, 所以在我的系统中, 看到的分区是这样的

Model: Virtio Block Device (virtblk)
Disk /dev/vdd: 3299GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  1100GB  1100GB  primary  btrf

可以看到, 我的vdd第一个分区vdd1, 就是我原来的分区(1TB), 现在vdd已经从原来的1TB扩容到3TB, 如何把3TB的空间都移给vdd1?

不能直接 btrfs resize

幸好我用的是btrfs文件系统, 它的扩展性有目共睹, 用网上说明的方法来试下

root@hostname:/home/igi# btrfs filesystem resize +1024G /home/data/
Resize '/home/data/' of '+1024G'
ERROR: unable to resize '/home/data/' - File too large

我先试了加多1TB, 不过直接报错, 提示过大, 无论如何也无法调高, 但可以在目前1TB的大小内调整

root@hostname:/home/igi# btrfs filesystem resize -10G /home/data/
Resize '/home/data/' of '-10G'
root@hostname:/home/igi# df -h | grep data
/dev/vdd1    1014G  725G  288G  72% /home/data
root@hostname:/home/igi# btrfs filesystem resize +10G /home/data/
Resize '/home/data/' of '+10G'
root@hostname:/home/igi# df -h | grep data
/dev/vdd1    1.0T  725G  298G  71% /home/data

在与网友的交流中, nerd有句话提醒了我'文件系统是在分区之内的东东', 嗯, btrfs是构建在分区之上的文件系统, 它应该不能修改分区的大小

而后nerd给了个链接, 里面提到了使用fdisk和resize2fs来调整ext文件系统的大小, 总体思路是: 删除分区->使用新大小新建分区->做fsck->resize2fs->最后就可以重新挂载并且数据一般不会丢失!

参考链接: resize-filesystem-fdisk-resize2fs

这种思路在btrfs应该也行! 来试试

先fdick调整分区大小, 再btrfs resize, 关键是fsck!

root@hostname:/home/igi# umount /home/data/
root@hostname:/home/igi# fdisk /dev/vdd

WARNING: The size of this disk is 3.3 TB (3298534883328 bytes).
DOS partition table format can not be used on drives for volumes
larger than (2199023255040 bytes) for 512-byte sectors. Use parted(1) and GUID
partition table format (GPT).


Command (m for help): d      (删除分区)
Selected partition 1

Command (m for help): p

Disk /dev/vdd: 3298.5 GB, 3298534883328 bytes
3 heads, 2 sectors/track, 1073741824 cylinders, total 6442450944 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 identifier: 0x5cd07a6b

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): w      (保存分区)
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
root@hostname:/home/igi# fdisk /dev/vdd

WARNING: The size of this disk is 3.3 TB (3298534883328 bytes).
DOS partition table format can not be used on drives for volumes
larger than (2199023255040 bytes) for 512-byte sectors. Use parted(1) and GUID
partition table format (GPT).


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-4294967295, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-4294967294, default 4294967294):
Using default value 4294967294

Command (m for help): p

Disk /dev/vdd: 3298.5 GB, 3298534883328 bytes
16 heads, 63 sectors/track, 6391320 cylinders, total 6442450944 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 identifier: 0x5cd07a6b

   Device Boot      Start         End      Blocks   Id  System
/dev/vdd1            2048  4294967294  2147482623+  83  Linux

Command (m for help): w      (保存分区)
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

root@hostname:/home# btrfsck /dev/vdd1     #一定要做fsck!
checking extents
checking fs roots
checking root refs
found 923277586432 bytes used err is 0
total csum bytes: 898793244
total tree bytes: 2683535360
total fs tree bytes: 1454723072
btree space waste bytes: 514669023
file data blocks allocated: 920594051072
 referenced 920594051072
Btrfs Btrfs v0.19

root@hostname:/home/igi# resize2fs /dev/vdd1    #这里证实resize2fs不支持btrfs
resize2fs 1.42.5 (29-Jul-2012)
resize2fs: Bad magic number in super-block while trying to open /dev/vdd1
Couldn't find valid filesystem superblock.
root@hostname:/home/igi# mount -a    #重新挂载修改后的分区
root@hostname:/home/igi# df -h | grep data    #可以看到还是1TB!!!
/dev/vdd1    1.0T  863G  160G  85% /home/data

root@hostname:/home# btrfs filesystem resize max /home/data   #调整大小
Resize '/home/data' of 'max'
root@hostname:/home# df -h | grep data
/dev/vdd1    2.0T  863G  1.2T  43% /home/data

看到最后的大小了么, 不是3T, 而是2T, 我也纳闷, 用默认值来新建, 应该是最大的3T, 后来想到MBR分区的问题(MBR分区只能最大2.2TB), 嗯, 我用的fdisk不支持GPT分区, 得改用parted工具来重新弄了

MBR和GPT参考文档: MBR和GPT分区格式

用parted再来一次

root@hostname:/home# parted /dev/vdd
GNU Parted 2.3
Using /dev/vdd
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: Virtio Block Device (virtblk)
Disk /dev/vdd: 3299GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  2199GB  2199GB  primary  btrfs

(parted) rm 1     (重新删除分区)
(parted) print
Model: Virtio Block Device (virtblk)
Disk /dev/vdd: 3299GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start  End  Size  Type  File system  Flags

(parted) mklabel gpt      (转为GPT分区, 会提醒数据丢失! 勇敢的续续吧)
Warning: The existing disk label on /dev/vdd will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? Yes
(parted) mkpart primary      (新建分区)
File system type?  [ext2]? btrfs
Start? 1049kB
End? 3299GB
(parted) print
Model: Virtio Block Device (virtblk)
Disk /dev/vdd: 3299GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size    File system  Name     Flags
 1      1049kB  3299GB  3299GB  btrfs        primary

(parted) quit
Information: You may need to update /etc/fstab.

root@hostname:/home# btrfsck /dev/vdd1     #一定要记得fsck!
checking extents
checking fs roots
checking root refs
found 923277582336 bytes used err is 0
total csum bytes: 898793244
total tree bytes: 2683531264
total fs tree bytes: 1454723072
btree space waste bytes: 514665137
file data blocks allocated: 920594051072
 referenced 920594051072
Btrfs Btrfs v0.19
root@hostname:/home/igi# mount -a    #重新挂载修改后的分区
root@hostname:/home# btrfs filesystem resize max /home/data
Resize '/home/data' of 'max'
root@hostname:/home# df -h | grep data
/dev/vdd1    3.0T  863G  2.2T  29% /home/data

终于3TB了, 而且一切数据还在, 胜利!

后话: 数据有风险, 操作需谨慎, 请做好备份, 任何一步出错, 都可能丢失数据! 同时也感谢nerd!

2014-04-21 18:321150
  • ky.yang2014-04-22 14:28

    小I,还来俩次,NB~

  • 影痕2014-10-07 08:58

    你这种操作方法太暴力,太危险了,btrfs可以直接附加分区或硬盘到原有空间的,你把你新添加的分区划分为vdd2 使用如下语法
    btrfs device add /dev/vdd2 /home/data 直接就可以把分区添加上去。