Creating a new filesystem with fdisk, lvm, and mkfs
Our Task: Create a new 10GB filesystem to store a package repository for yum
Challenges: The existing hard drive has been fully allocated using LVM.
- Add a new hard drive to the server (virtual server in this case)
- Partition the drive and add it to the main logical volume
- Create a new filesystem
This article assumes that by now you have physically added the hard drive to the server. In my case I am using a virtual server but the tasks are essentially the same.
Partition the new disk drive
Before starting I will verify there are no partitions on the new disk.
[[email protected] ~]# fdisk -l /dev/sdb Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders, total 41943040 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: 0x00000000 Disk /dev/sdb doesn't contain a valid partition table
Since there are no partitions it is safe to proceed. I will now create one big partition as I plan to add this disk to LVM and use LVM to divvy up my disk space.
[[email protected] ~]# fdisk /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0xe1410716. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) 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): Using default value 1 First sector (2048-41943039, default 2048): Using default value 2048 Last sector, +sectors or +size (2048-41943039, default 41943039): Using default value 41943039 Command (m for help): p Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders, total 41943040 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: 0xe1410716 Device Boot Start End Blocks Id System /dev/sdb1 2048 41943039 20970496 83 Linux Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
Once you type w the changes are written to disk.
Add the partition to the volume group
My volume group name is vg_fedoratest and I will now add the new partition to that volume group with the vgextend command. This adds the additional partition space to the volume group for lvm to use.
[[email protected] ~]# ls -la /dev/sdb* brw-rw----. 1 root disk 8, 16 Dec 6 15:08 /dev/sdb brw-rw----. 1 root disk 8, 17 Dec 6 15:08 /dev/sdb1 [[email protected] ~]# pvcreate /dev/sdb1 [[email protected] ~]# vgextend vg_fedoratest /dev/sdb1 No physical volume label read from /dev/sdb1 Writing physical volume data to disk "/dev/sdb1" Physical volume "/dev/sdb1" successfully created Volume group "vg_fedoratest" successfully extended [[email protected] ~]# vgdisplay vg_fedoratest --- Volume group --- VG Name vg_fedoratest System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 4 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 2 Act PV 2 VG Size 40.69 GiB PE Size 32.00 MiB Total PE 1302 Alloc PE / Size 663 / 20.72 GiB Free PE / Size 639 / 19.97 GiB VG UUID 0gvY4O-I3tT-WKK0-ClU2-sXDz-Nq0c-eExZz2
Create a logical volume & filesystem
I am first using lvcreate to create the logical volume, after verifying that the logical volume created correctly I will create a new filesystem on top with mkfs.
[[email protected] ~]# lvcreate -L 10GB -n lv_yumrepo vg_fedoratest Logical volume "lv_yumrepo" created [[email protected] ~]# lvdisplay vg_fedoratest/lv_yumrepo --- Logical volume --- LV Name /dev/vg_fedoratest/lv_yumrepo VG Name vg_fedoratest LV UUID rpl7q1-oi6N-nZot-bjPT-Y1qi-2W28-xlBg6Q LV Write Access read/write LV Status available # open 0 LV Size 10.00 GiB Current LE 320 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:2 [[email protected] ~]# mkfs -t ext3 /dev/vg_fedoratest/lv_yumrepo mke2fs 1.41.14 (22-Dec-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 655360 inodes, 2621440 blocks 131072 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2684354560 80 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 28 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
Mount the filesystem
Now that my filesystem is created I need to mount it; but before then I need to create the directory that the mount point will overlay and add the appropriate lines to the /etc/fstab file.
[[email protected] ~]# mkdir /var/repo [[email protected] ~]# vi /etc/fstab ## Append to bottom /dev/mapper/vg_fedoratest-lv_yumrepo /var/repo ext3 defaults 0 0 [[email protected] ~]# mount /dev/mapper/vg_fedoratest-lv_yumrepo on /var/repo type ext3 (rw,relatime,seclabel,user_xattr,acl,barrier=1,nodelalloc,data=ordered)
Шпаргалка по управлению разделами LVM
Гугль располагает сотнями отличных статей, содержащих теорию о Linux LVM (Linux Volume Manager) — встроенной системе управления логическими дисками. В этой статье собран «карманный» набор команд для решения типовых задач.
На приведенной схеме наглядно изображена структура LVM
Посмотреть информацию о существующих физических разделах (PV), группах разделов (VG) и логических разделов (LV) можно с помощью команд
Находим физический диск (в моем случае это будет sdc — свежедобавленный диск без разделов)
$ ls -1 /dev | grep sd sda sda1 sda2 sdb sdb1 sdc
Посмотреть информацию о разделе можно с помощью команды
Создаем раздел
$ pvcreate /dev/sdc Physical volume "/dev/sdc" successfully created. $ dd if=/dev/zero of=/dev/sdc bs=1k count=1 1+0 записей получено 1+0 записей отправлено скопировано 1024 байта (1,0 kB), 0,00115763 c, 885 kB/c $ blockdev --rereadpt /dev/sdc $ fdisk /dev/sdc
В fdisk`е жмем n для создания нового раздела, затем p, чтобы сделать его primary. Номер раздела — 1, первый и последний сектор оставляем по умолчанию (если хотим использовать все пространство диска). Для выхода с записью изменений нажимаем w. Создастся раздел sdc1, который займет все свободное пространство sdc. Проверить можно с помощью команды:
$ ls -1 /dev | grep sdc sdc sdc1
Создаем группу томов (если добавляем несколько разделов — пишем их через пробел)
или добавляем к существующей группе томов
$ vgscan Reading volume groups from cache. Found volume group "vg1" using metadata type lvm2
Добавим новый раздел swap
$ lvcreate -L500M -n new_swap vg1 Logical volume "new_swap" created. $ mkswap /dev/vg1/new_swap Setting up swapspace version 1, size = 511996 KiB без метки, UUID=b64aae09-ec70-4b5a-b4e2-ba0fe39ec1df $ swapon /dev/vg1/new_swap
Теперь проверим, подцепился ли новый swap раздел
/dev/vg1/new_swap swap swap defaults 0 0
Создадим новый раздел и смонтируем его в папку /share
$ cd / $ mkdir /share $ lvcreate -L1G -n lv_share vg1 Logical volume "lv_share" created. $ mke2fs /dev/vg1/lv_share . Allocating group tables: done Writing inode tables: done Writing superblocks and filesystem accounting information: done $ mount /dev/vg1/lv_share /share
Проверим, что новый раздел смонтировался
$ df -h | grep lv_share /dev/mapper/vg1-lv_share 1008M 1,3M 956M 1% /share
Теперь выясним UUID раздела и добавим его в fstab
$ blkid | grep lv_share /dev/mapper/vg1-lv_share: UUID="4295775f-672f-4fd4-a1ff-ef3103d82fa8" TYPE="ext2" $ vi /etc/fstab
. UUID=4295775f-672f-4fd4-a1ff-ef3103d82fa8 /share ext2 defaults 0 0
Изменить размер логического диска можно с помощью команд (предварительно отмонтировать). После внесения изменений необходимо использовать команду resize2fs!
$ lvextend -L+500M /dev/vg1/lv_share Size of logical volume vg1/lv_share changed from 1,00 GiB (256 extents) to 1,49 GiB (381 extents). Logical volume vg1/lv_share successfully resized.
$ lvreduce -L700M /dev/vg1/lv_share WARNING: Reducing active and open logical volume to 700,00 MiB. THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce vg1/lv_share? [y/n]: y Size of logical volume vg1/lv_share changed from 1,49 GiB (381 extents) to 700,00 MiB (175 extents). Logical volume vg1/lv_share successfully resized.
$ lvresize -r -L2G /dev/vg1/lv_share . Resizing the filesystem on /dev/mapper/vg1-lv_share to 524288 (4k) blocks. The filesystem on /dev/mapper/vg1-lv_share is now 524288 blocks long.
Удаление логических томов (предварительно удалить их из fstab)
$ lvremove /dev/vg1/new_swap Do you really want to remove active logical volume vg1/new_swap? [y/n]: y Logical volume "new_swap" successfully removed
Сменить тип раздела на Linux LVM можно с помощью fdisk
$ fdisk /dev/sdc Command: t Selected partition: 1 Hex code (type L to list codes): 8e Command: w The partition table has been altered!