Copy disc layout and partitions to another drive
Solution 1
Working with the following conditions:
- The original disk to copy is
/dev/sdx
- The original disk is properly partitioned/labeled/flagged
- The filesystem contents of the original disk will be ignored
- The destination disk, to copy to, will be
/dev/sdy
- The swap partition will be
/dev/sdy4
- The boot partition will be
/dev/sdy1
mounted on/boot
in the final system withext3
filesystem - The root partition will be
/dev/sdy2
mounted on/
in the final system withext4
filesystem - The users partition will be
/dev/sdy3
mounted on/home
in the final system withext4
filesystem - The Debian system you want to copy has been tarred and gzipped to
master_system.tar.gz
- All files, including the script, will be stored in the working directory
- The script will be executed from the same working directory
- The script will be run as
root
, notsudo
but either log in as root, orsu
in a terminal - There is a directory
dupe_mnt
in the working directory
To "copy" the original disk's partition structure, only needed one time, unless the structure is changed.
sfdisk --dump /dev/sdx > master_table
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 it master_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:
#!/bin/sh
target=$1;
sfdisk /dev/${target} < master_table;
# Format the swap partition
mkswap /dev/${target}4;
# Format the data partitions
mkfs.ext3 /dev/${target}1;
mkfs.ext4 /dev/${target}2;
mkfs.ext4 /dev/${target}3;
# Mount the target root filesystem and its parts
mount /dev/${target}2 dupe_mnt;
mount /dev/${target}1 dupe_mnt/boot;
mount /dev/${target}3 dupe_mnt/home;
# Copy the master system to the target
cd dupe_mnt;
tar -zxvpf ../master_system.tar.gz;
cd ..;
# Unmount the new system
cd ..
umount /dev/${target}3;
umount /dev/${target}1;
umount /dev/${target}2;
#done
The file sys_replicate.sh
needs to have the execute
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 a root
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.
Solution 2
As said in a comment, dd will copy the data, partition layout, etc. The only issue is that your source and target disks must be identical (cylinders, heads, sectors, etc)
A better option that becomes somewhat hardware neutral would be to use clonezilla - http://clonezilla.org/
Solution 3
The MBR Partion is sector 0 of the disk, clone it with dd:
sudo dd if=/dev/sda of=/dev/sdb bs=512 count=1
Beware: the data of the target disk may be destroyed. To activate the new partitions, you must run kpartx.
Related videos on Youtube
KevKosDev
Updated on September 18, 2022Comments
-
KevKosDev over 1 year
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?
-
Chindraba about 7 yearsAre the target drives always going to be the same size in total sectors? And How many partitions of what type?
-
KevKosDev about 7 yearsYes, they will always have the same size as the original drive. The layout I want to copy has a swap and a ext2 partition.
-
ridgy about 7 yearsIf there are no (secret) data on the original source, you might use
dd
command to create exact copy of the original:dd if=/dev/sdX of=diskimage
and thendd if=diskimage of=/dev/sdX
. You may speed things up with optionbs
depending on the size of your device. -
KevKosDev about 7 yearsdd is sadly not an option because theres data on the source drive.
-
Chindraba about 7 yearsAs
dd
is not an option, I don't see a single command solution as likely. The option to create a script, on the other hand, exists, which allows you to use a single command in the terminal, or in a different script, which might be good enough for you use-case. Does the partition table include logical partitions, or is it limited to the (max 4) physical partitions? Without logical partitions, you candd
the partition table, then format the partitions, max 5 commands. I'm guessing you also want to recreate the directory structure on the dest, without the files, correct. -
KevKosDev about 7 yearsFolder structure is also completely irrelevant for my case at this point. I will copy an entire Debian-system to the drive, after I created it correctly. I'm also currently looking at the idea of a script with several commands. Something like
parted /dev/sda mklabel msdos mkpart P1 ext3 1MiB 8MiB
and thenmkfs.ext2 /dev/P1
. -
Chindraba about 7 yearsWhy not take the time to make a master image stored on your system and use
dd
to make the copies? You could include the Debian system, after you create it, in that image, and then the whole process will be repeatable with a single command. Additionally, if changes are needed later, Debian system changes, alter the master image, and you're ready to repeat with minimal fuss. -
KevKosDev about 7 yearsThat may be an option. But assuming the size of the original could be 500GB or more, wouldn't the dd dump not become really large? And even if i pipe it through gzip, wouldn't it take a really long to time write such a big image to the target?
-
Chindraba about 7 yearsOK, One more Q, Yes/No: any extended partitions, AKA logical partitions, on the desired disk layout? If there's only physical partitions, numbers 1 to 4 only, then a fairly simple script can be created, assuming that the target disks are mirror images of the master, and that all target disks are at least as large as the master.
-
KevKosDev about 7 yearsI just checked, and there are no logical partitions on the drive. Target drives will always be equal or larger then the original. How would you implement this in a script?
-
-
KevKosDev about 7 yearsI don't think dd is the way to go for my case. Even on an empty drive the dd dump would be really large (if not piped through gzip) and the write process to the target drives would take a really long time. I'm looking more for a command that lets me create the drive in a single command line. Even if I have to set the partition parameters manually.
-
KevKosDev about 7 yearsThank you very much for your time and your detailed explanation! I think I can use this option wit a few adjustments in my application. I will test it and will reply how it goes. And again thank you very much!