I'm working on an application, where I need to create drives with always the exact same partition layout. My initial thought was to once dump the partition table of the original drive with sfdisk.
sfdisk -d /dev/sdX > parttable
And then apply it to all other drives with:
sfdisk /dev/sdX < parttable
But this method doesn't seem to work. I dumped the right partitiontable from an USB-drive, then created some random partitions with gparted and tried then to write the initial partitiontable back to the drive.
But the problem is, the partition isn't recognized. Gparted for example lists the partition as unknown. I figured, I probably have to format the created partition, as the partitiontable stores no information about filesystems.
My question is now: Can I somehow save the partitiontable and information about the partitions (filesystem etc.) and create a new drive this way (at best in one command).
btw.: msdos partiontable
Edit: An alternative would be, to gather all the data about the drives (e.g. parttable, filesystems) myself and create the command manually. Is it possible (maybe with parted) to create the partition table and format multiple partition in one command?
Best Answer
Working with the following conditions:
/dev/sdx
/dev/sdy
/dev/sdy4
/dev/sdy1
mounted on/boot
in the final system withext3
filesystem/dev/sdy2
mounted on/
in the final system withext4
filesystem/dev/sdy3
mounted on/home
in the final system withext4
filesystemmaster_system.tar.gz
root
, notsudo
but either log in as root, orsu
in a terminaldupe_mnt
in the working directoryTo "copy" the original disk's partition structure, only needed one time, unless the structure is changed.
Since only you, at the system in question can determine what to copy from the Debian system that serves as the master, I'm not going to go into any of that. I know you won't copy the
/proc
,/dev/
, and/sys
directories, but there are sure to be others to exclude. Create that archive any way you choose, and name itmaster_system.tar.gz
. This should include the/boot
and/home
directories in it. That's it, setup is done until you change either the Debian system you're copying, or the partitioning of the disk.The script to create, called
sys_replicate.sh
is:The file
sys_replicate.sh
needs to have theexecute
bit set.chmod +x sys_replicate.sh
To use the process, once setup, connect the target disk. If it's a USB, make sure the system has recognized that it's available. If it's an internal HDD, obviously it'll require a reboot, and the system should find it automatically. Once connected, be VERY sure you know which
/dev
it is, since adding disks can rearrange the letters. Once it's ready, in aroot
shell, execute:./sys_replicate.sh sdy
The device name
/dev/sdy
and/dev/sdx
obviously will need to be changed to match your operational system. Also, as it turns out,sfdisk
can handle GPT disks and extended partitions, so my earlier comment question was not needed. If you use a disk, as the copy, that is larger than the original, everything will still work. You will have wasted space that you can't easily reclaim, however, so take that into account before selecting the master disk to copy.The creation of the partition structure, and the copying of the Debian system are independent, so changes to one do not require updating the other.