![Centos Kickstart 文件 - 硬盘分区,方便增加硬盘容量](https://linux22.com/image/723606/Centos%20Kickstart%20%E6%96%87%E4%BB%B6%20-%20%E7%A1%AC%E7%9B%98%E5%88%86%E5%8C%BA%EF%BC%8C%E6%96%B9%E4%BE%BF%E5%A2%9E%E5%8A%A0%E7%A1%AC%E7%9B%98%E5%AE%B9%E9%87%8F.png)
我正在寻找一种分区方法(最终为虚拟机提供 kickstart 文件),以便可以增加磁盘空间而无需使虚拟机脱机(lvm 没有问题),也不用删除/重新创建分区 - 使用 centos 的板载工具。
有什么可行的办法吗?
谢谢!
编辑:这是我的 kickstart 文件中的实际分区选项:
## 磁盘分区信息
part pv.192 --fstype="lvmpv" --ondisk=sda --size=1 --grow
volgroup centos --pesize=4096 pv.192
logvol / fstype="xfs" --name=root --vgname=centos --maxsize=51200 --size=5024 --grow
logvol swap --fstype="swap" --size=4096 --name=swap --vgname=centos
part pv.193 --fstype="lvmpv" --ondisk=sdb --size=1 --grow
volgroup logs --pesize=4096 pv.193
logvol /var/log --fstype="xfs" --maxsize=51200 --size=8192 --grow --name=log --vgname=logs
这里的缺点是,我在调整卷大小时必须删除/重新创建卷。
它看起来是这样的:
[root@localhost ~]# ssm list
-----------------------------------------------------------
Device Free Used Total Pool Mount point
-----------------------------------------------------------
/dev/sda 21.00 GB PARTITIONED
/dev/sda1 1.00 GB /boot
/dev/sda2 0.00 KB 20.00 GB 20.00 GB centos
/dev/sdb 32.00 GB
/dev/sdb1 0.00 KB 32.00 GB 32.00 GB logs
-----------------------------------------------------------
--------------------------------------------------
Pool Type Devices Free Used Total
--------------------------------------------------
centos lvm 1 0.00 KB 20.00 GB 20.00 GB
logs lvm 1 0.00 KB 32.00 GB 32.00 GB
--------------------------------------------------
--------------------------------------------------------------------------------------
Volume Pool Volume size FS FS size Free Type Mount point
--------------------------------------------------------------------------------------
/dev/centos/root centos 16.00 GB xfs 15.99 GB 14.99 GB linear /
/dev/centos/swap centos 4.00 GB linear
/dev/logs/log logs 32.00 GB xfs 31.98 GB 31.95 GB linear /var/log
/dev/sda1 1.00 GB xfs 1014.00 MB 871.73 MB part /boot
--------------------------------------------------------------------------------------
这就是我想要的(vdb、vdc、vdd)——但也适用于 OS 磁盘:
[root@vm1 isolinux]# ssm list
-------------------------------------------------------------
Device Free Used Total Pool Mount point
-------------------------------------------------------------
/dev/vda 445.00 GB PARTITIONED
/dev/vda1 1.00 GB /boot
/dev/vda2 0.00 KB 439.00 GB 439.00 GB centos
/dev/vdb 0.00 KB 30.00 GB 30.00 GB centos
/dev/vdc 0.00 KB 20.00 GB 20.00 GB testvg
/dev/vdd 0.00 KB 40.00 GB 40.00 GB vg_vdd
-------------------------------------------------------------
----------------------------------------------------
Pool Type Devices Free Used Total
----------------------------------------------------
centos lvm 2 0.00 KB 468.99 GB 468.99 GB
testvg lvm 1 0.00 KB 20.00 GB 20.00 GB
vg_vdd lvm 1 0.00 KB 40.00 GB 40.00 GB
----------------------------------------------------
---------------------------------------------------------------------------------------------
Volume Pool Volume size FS FS size Free Type Mount point
---------------------------------------------------------------------------------------------
/dev/centos/root centos 440.00 GB xfs 439.98 GB 374.89 GB linear /
/dev/centos/swap centos 4.00 GB linear
/dev/vg_vdd/lv_vdd vg_vdd 40.00 GB xfs 39.99 GB 39.99 GB linear
/dev/centos/TEST-SDB centos 25.00 GB xfs 24.99 GB 24.99 GB linear
/dev/testvg/lv_vdc-test testvg 20.00 GB xfs 19.99 GB 19.99 GB linear
/dev/vda1 1.00 GB xfs 1014.00 MB 790.33 MB part /boot
---------------------------------------------------------------------------------------------
我想使用 LVM 并将文件系统(包括操作系统)直接放在磁盘上。在“磁盘分区信息”部分有办法吗,还是我必须在 %post sektor 中进行更改(这将排除操作系统磁盘)。
谢谢任何提示!
答案1
您始终可以使用“onboad”工具进行重新创建。删除 fdisk 中的某个部分并不会将其抹去。您只需删除部分,然后在相同的起始扇区重新创建它,但大小要大一些。不要犹豫,在正在运行的挂载分区上执行此操作。它不会破坏任何东西。
但是,要在重新创建后真正使用额外的大小,您仍然需要一种方法来告诉内核重新加载已使用磁盘的部分表,因此您必须安装 parted 并使用 partprobe(当您写入更改并退出时,fdisk 会建议这样做)。它位于存储库内,因此只需 yum install parted 即可。
然后使用 resize2fs 在线扩大文件系统。
VM 内部的 LVM 是不需要的,因为它只会使事情变得更加复杂。(惊喜!)让主机进行实际的物理卷布局管理。
查看记录(实际上它来自 Debian,但对于 CentOS 来说它完全相同):
root@test:/home/merlin# fdisk -l /dev/sd?
Disk /dev/sda: 8 GiB, 8589934592 bytes, 16777216 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
Disklabel type: dos
Disk identifier: 0x3bc1a4d0
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 1048575 1046528 511M 82 Linux swap / Solaris
/dev/sda2 1048576 16777215 15728640 7.5G 83 Linux
root@test:/home/merlin# df -h
Файловая система Размер Использовано Дост Использовано% Cмонтировано в
udev 494M 0 494M 0% /dev
tmpfs 100M 1.7M 98M 2% /run
/dev/sda2 7.4G 921M 6.1G 13% /
tmpfs 499M 0 499M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 499M 0 499M 0% /sys/fs/cgroup
tmpfs 100M 0 100M 0% /run/user/1000
此时,我在虚拟化主机上的 sda 中添加了 16 Gb。VM 看到:
root@test:/home/merlin# dmesg | tail
[ 3.613736] usb 1-1: SerialNumber: 42
[ 3.858801] random: crng init done
[ 3.927777] hidraw: raw HID events driver (C) Jiri Kosina
[ 3.939081] usbcore: registered new interface driver usbhid
[ 3.939082] usbhid: USB HID core driver
[ 3.963159] input: QEMU QEMU USB Tablet as /devices/pci0000:00/0000:00:01.2/usb1/1-1/1-1:1.0/0003:0627:0001.0001/input/input6
[ 3.963623] hid-generic 0003:0627:0001.0001: input,hidraw0: USB HID v0.01 Mouse [QEMU QEMU USB Tablet] on usb-0000:00:01.2-1/input0
[ 54.108990] sd 0:0:0:0: Capacity data has changed
[ 54.110036] sd 0:0:0:0: [sda] 50331648 512-byte logical blocks: (25.8 GB/24.0 GiB)
[ 54.110235] sda: detected capacity change from 8589934592 to 25769803776
现在,整个过程:
root@test:/home/merlin# fdisk -l /dev/sda
Disk /dev/sda: 24 GiB, 25769803776 bytes, 50331648 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
Disklabel type: dos
Disk identifier: 0x3bc1a4d0
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 1048575 1046528 511M 82 Linux swap / Solaris
/dev/sda2 1048576 16777215 15728640 7.5G 83 Linux
root@test:/home/merlin# fdisk /dev/sda
Welcome to fdisk (util-linux 2.29.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,2, default 2): 2
Partition 2 has been deleted.
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2):
First sector (1048576-50331647, default 1048576):
Last sector, +sectors or +size{K,M,G,T,P} (1048576-50331647, default 50331647):
Created a new partition 2 of type 'Linux' and of size 23.5 GiB.
Partition #2 contains a ext4 signature.
Do you want to remove the signature? [Y]es/[N]o: n
Command (m for help): p
Disk /dev/sda: 24 GiB, 25769803776 bytes, 50331648 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
Disklabel type: dos
Disk identifier: 0x3bc1a4d0
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 1048575 1046528 511M 82 Linux swap / Solaris
/dev/sda2 1048576 50331647 49283072 23.5G 83 Linux
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Устройство или ресурс занято
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).
root@test:/home/merlin# partprobe /dev/sda
root@test:/home/merlin# df -h
Файловая система Размер Использовано Дост Использовано% Cмонтировано в
udev 494M 0 494M 0% /dev
tmpfs 100M 1.7M 98M 2% /run
/dev/sda2 7.4G 921M 6.1G 13% /
tmpfs 499M 0 499M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 499M 0 499M 0% /sys/fs/cgroup
tmpfs 100M 0 100M 0% /run/user/1000
root@test:/home/merlin# resize2fs /dev/sda2
resize2fs 1.43.4 (31-Jan-2017)
Filesystem at /dev/sda2 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 3
The filesystem on /dev/sda2 is now 6160384 (4k) blocks long.
root@test:/home/merlin# df -h
Файловая система Размер Использовано Дост Использовано% Cмонтировано в
udev 494M 0 494M 0% /dev
tmpfs 100M 1.7M 98M 2% /run
/dev/sda2 24G 924M 22G 5% /
tmpfs 499M 0 499M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 499M 0 499M 0% /sys/fs/cgroup
tmpfs 100M 0 100M 0% /run/user/1000
答案2
因为在“磁盘分区信息”部分显然没有办法完成该任务,所以我使用了 %pre 部分。在这里,您几乎可以使用常见的 bash 命令来完成此任务,但需要进行一些修改:
%pre --log /tmp/pre-install.log
echo "Starting Kickstart Pre-Installation..."
echo "Step 1: vgcreate vg_sdb /dev/sdb -f -y"
vgcreate vg_sdb /dev/sdb -f -y
echo "Step 2: lvcreate -l 100%FREE --name sdb vg_sdb"
lvcreate -l 100%FREE --name sdb vg_sdb
echo "Step 3: mkfs.xfs /dev/vg_sdb/sdb"
mkfs.xfs /dev/vg_sdb/sdb
vgchange -ay
%end
因为我的 kickstart 将从另一个工具创建,所以我不需要在这里添加进一步的逻辑(例如,没有硬编码的磁盘和卷组,...)