FastNetMon

Thursday 19 May 2011

Создание полноценной таблицы разделов в файле

Очевидный подход - создать файл, заполненный нулями, и попробовать создать в нем таблицу разделов. Итак, платформа: Debian 6 Squeeze.

Пробуем:
dd if=/dev/zero of=whole_disk.img bs=512 count=10000000
10000000+0 records in
10000000+0 records out
5120000000 bytes (5.1 GB) copied, 34.7126 s, 147 MB/s

Создаем таблицу разделов:
parted whole_disk.img mklabel msdos

Создаем 1 раздела:
parted whole_disk.img mkpart primary ext3 1 1024
parted whole_disk.img mkpart primary ext3 1024 100%

И в результате получили два аккуратненьких раздела:
parted whole_disk.img
GNU Parted 2.3
Using /root/whole_disk.img
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p
Model: (file)
Disk /root/whole_disk.img: 5161MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 1049kB 1024MB 1023MB primary
2 1024MB 5160MB 4136MB primary

(parted)

Но fdisk на них орет благим матом:
fdisk -l whole_disk.img
You must set cylinders.
You can do this from the extra functions menu.

Disk whole_disk.img: 0 MB, 0 bytes
4 heads, 32 sectors/track, 0 cylinders
Units = cylinders of 128 * 512 = 65536 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0003e6d7

Device Boot Start End Blocks Id System
whole_disk.img1 17 15632 999424 83 Linux
Partition 1 has different physical/logical endings:
phys=(1023, 3, 32) logical=(15631, 3, 32)
whole_disk.img2 15633 78736 4038656 83 Linux
Partition 2 has different physical/logical beginnings (non-Linux?):
phys=(1023, 3, 32) logical=(15632, 0, 1)
Partition 2 has different physical/logical endings:
phys=(1023, 3, 32) logical=(78735, 3, 32)

Но если файл смонтировать как диск:
losetup /dev/loop0 whole_disk.img

То с выдачей fdisk станет все ок:
fdisk -l /dev/loop0

Disk /dev/loop0: 5120 MB, 5120000000 bytes
4 heads, 32 sectors/track, 78125 cylinders
Units = cylinders of 128 * 512 = 65536 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000a619a

Device Boot Start End Blocks Id System
/dev/loop0p1 17 15632 999424 83 Linux
/dev/loop0p2 15633 78112 3998720 83 Linux

Теперь нам нужно каким-то образом подключить массивы на этом диске к системе. Для этого установим kpartx:
apt-get install kpartx

Для начала получим список имеющихся массивов:
kpartx -l /dev/loop0
loop0p1 : 0 1998848 /dev/loop0 2048
loop0p2 : 0 7997440 /dev/loop0 2000896

Отлично, теперь подключим их к системе через device mapper:
kpartx -a /dev/loop0

После этого у нас появятся два раздела в системе:
ls -al /dev/mapper/
total 0
drwxr-xr-x 2 root root 100 May 20 01:17 .
drwxr-xr-x 18 root root 3.2K May 20 01:17 ..
crw------- 1 root root 10, 59 May 19 21:41 control
lrwxrwxrwx 1 root root 7 May 20 01:17 loop0p1 -> ../dm-0
lrwxrwxrwx 1 root root 7 May 20 01:17 loop0p2 -> ../dm-1

Это как раз разделы с нашего виртуального жесткого диска. Все, теперь с ними можно работать как угодно!

После окончания работ отмонтируем все:
kpartx -d /dev/loop0

Все ок:
ls -al /dev/mapper/
total 0
drwxr-xr-x 2 root root 60 May 20 01:19 .
drwxr-xr-x 18 root root 3.1K May 20 01:19 ..
crw------- 1 root root 10, 59 May 19 21:41 control

2 comments :

  1. чтобы не ругался fdisk нужно в образе после создания разделов создать файловую систему
    parted mkfs ...
    или использовать
    parted mkpartfs ...
    вместо parted mkpart ...

    ReplyDelete
  2. Не совсем, стоит просто корректно делать выравнивание, это делается доп ключом parted.

    ReplyDelete

Note: only a member of this blog may post a comment.