Comment reproduire rapidement un schéma de partitionnement entre des disques de même géométrie ?
En partant d’un disque partitionné, il est possible de dupliquer son schéma de partitionnement sur un autre disque de même géométrie par la commande « sfdisk » :
sfdisk -d /dev/XXXX | sfdisk /dev/YYYY |
« sfdisk -d /dev/XXXX » Fait un « dump » du schéma de partitionnement de « /dev/XXX » qu’il renvois à « sfdisk /dev/YYYY » qui applique la modification.
Par exemple :
mafalda ~ # fdisk -l /dev/loop1 Disk /dev/loop1: 1048 MB, 1048576000 bytes 255 heads, 63 sectors/track, 127 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0x5cb39174 Device Boot Start End Blocks Id System /dev/loop1p1 1 65 522081 83 Linux /dev/loop1p2 66 127 498015 83 Linux mafalda ~ # fdisk -l /dev/loop2 Disk /dev/loop2: 1048 MB, 1048576000 bytes 255 heads, 63 sectors/track, 127 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0x0d604f1b Disk /dev/loop2 doesn't contain a valid partition table mafalda ~ # sfdisk -d /dev/loop1 | sfdisk /dev/loop2 Checking that no-one is using this disk right now ... BLKRRPART: Argument invalide OK Disk /dev/loop2: cannot get geometry Disk /dev/loop2: 127 cylinders, 255 heads, 63 sectors/track sfdisk: ERROR: sector 0 does not have an msdos signature /dev/loop2: unrecognized partition table type Old situation: No partitions found New situation: Units = sectors of 512 bytes, counting from 0 Device Boot Start End #sectors Id System /dev/loop2p1 63 1044224 1044162 83 Linux /dev/loop2p2 1044225 2040254 996030 83 Linux /dev/loop2p3 0 - 0 0 Empty /dev/loop2p4 0 - 0 0 Empty Warning: no primary partition is marked bootable (active) This does not matter for LILO, but the DOS MBR will not boot this disk. Successfully wrote the new partition table Re-reading the partition table ... BLKRRPART: Argument invalide If you created or changed a DOS partition, /dev/foo7, say, then use dd(1) to zero the first 512 bytes: dd if=/dev/zero of=/dev/foo7 bs=512 count=1 (See fdisk(8).) mafalda ~ # fdisk -l /dev/loop2 Disk /dev/loop2: 1048 MB, 1048576000 bytes 255 heads, 63 sectors/track, 127 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0x0d604f1b Device Boot Start End Blocks Id System /dev/loop2p1 1 65 522081 83 Linux /dev/loop2p2 66 127 498015 83 Linux |
(Voir « Utiliser les périphériques de loop« )
Pour sauvegarder le schéma de partitionnement, il suffit de rediriger la sortie du dump dans un fichier :
mafalda ~ # sfdisk -d /dev/loop1 > part.sfdisk |
il est alors possible de répliquer le schéma à souhait :
mafalda ~ # sfdisk /dev/loop4 < part.sfdisk ... |
La commande fonctionne aussi pour l’écriture du schéma de partitionnement sur un disque plus grand et même sur un disque plus petit (en ajoutant l’option « -f » au « sfdisk » appliquant le nouveau schéma). Mais attention, toutes les partitions sortant de l’espace du disque seront bien évidement inutilisables…
Autre solution possible, créer un fichier de réponse pour « fdisk ».
En reprenant l’exemple précédent, le partitionnement de « /dev/loop1 » est réalisé via « fdisk » ainsi :
mafalda ~ # fdisk /dev/loop1 Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-127, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-127, default 127): +500M Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (66-127, default 66): Using default value 66 Last cylinder, +cylinders or +size{K,M,G} (66-127, default 127): Using default value 127 Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 22: Argument invalide. The kernel still uses the old table. The new table will be used at the next reboot. Syncing disks. |
Pour reproduire ce schéma il suffit de créer un fichier de réponse contenant l’intégralité de la frappe de l’utilisateur soit le fichier « part.fdisk » :
1 2 3 4 5 6 7 8 9 10 11 | n p 1 +500M n p 2 w |
qui signifie, ligne 1, nouvelle partition ; ligne 2, primaire ; ligne 3, numéro 1 ; ligne 4, valeur par défaut ; ligne 5, de 500Mo ; ligne 6, nouvelle partition ; ligne 7, primaire ; ligne 8, numéro 2 ; ligne 9, commençant au premier secteur disponible (valeur par défaut) ; ligne 10, terminant au dernier secteur ; ligne 11, écrire.
Pour appliquer ce modèle à un disque de même géométrie, il suffit de passer le fichier produit à « fdisk /dev/XXXX » :
mafalda ~ # fdisk /dev/loop3 < part.fdisk ... Syncing disks. mafalda ~ # fdisk -l /dev/loop3 Disk /dev/loop3: 1048 MB, 1048576000 bytes 255 heads, 63 sectors/track, 127 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0xa67f47ab Device Boot Start End Blocks Id System /dev/loop3p1 1 65 522081 83 Linux /dev/loop3p2 66 127 498015 83 Linux |
Si le nouveau disque est de taille supérieure, la dernière partition sera plus grande que celle du volume original est inversement, la dernière partition sera plus petite dans le cas d’un volume de taille inférieure.
Dernière solution proposée, la recopie de la table de partition. Pour cela, rien de tel que l’outil habituel « dd » :
mafalda ~ # dd if=/dev/loop1 of=/dev/loop2 bs=512 count=1 |