Solved

How do I copy a partition table to a new drive ?

Posted on 2004-08-04
4
1,949 Views
Last Modified: 2008-01-09
Hi all,

I'm trying to work out how to do something on Linux that I can currently do on FreeBSD.

I want to copy the partition table from one device (say /dev/sda) to another device of the same size (say /dev/sdb).

On FreeBSD I can do this by using disklabel -w to write a disklabel that I got from disklabel /dev/driveid.

I can get the base output I want in Linux by doing fdisk -l /dev/sda. I then have to edit this and change all occurances of sda to sdb, a step I don't need to do in FreeBSD because partition tables are separate to labels. Now I have a file with all of my partition information, how do I put this onto a drive (e.g. sdb) ?

Thanks,
0
Comment
Question by:Anonymouslemming
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Expert Comment

by:da99rmd
ID: 11714020
Hi Anonymouslemming,
are the disks the same size ?
if they are use this command and you will copy the data and the partitiontable and MBR.
dd if=/dev/sda of=/dev/sdb

If you just want the partition table just create it using fdisk or sfdisk.

/Rob
0
 
LVL 3

Expert Comment

by:pYrania
ID: 11714068
dd if=/dev/sda of=/dev/sdb bs=512 count=1

That command will copy the MBR (512 Bytes) from sda to sdb.

Though, only the 447th through 510th bytes are the actual partition table - each partition has 16 records/bytes.
The first 446 bytes represent the bootloader and the last 2 bytes are unused.
0
 
LVL 20

Expert Comment

by:Gns
ID: 11715703
Only do this if the disk geometry actually match. Otherwise, let's instead script something around fdisk.... Or better yet (and as suggested by Rob) sfdisk.

-- Glenn

0
 
LVL 20

Accepted Solution

by:
Gns earned 500 total points
ID: 11715806
Hm, yes... the -O/-I options of sfdisk would be unuseable (pretty much as the dd case) with "logical partitions", so one would need rely on
sfdisk -d /dev/sda > sda.out
sed -i -e 's#/dev/sda#/dev/sdb#' sda.out
sfdisk /dev/sdb < sda.out
... should do the trick in a rather more "safe" way.

-- Glenn
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question