Solved

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

Posted on 2004-08-04
4
1,909 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
  • 2
4 Comments
 
LVL 8

Expert Comment

by:da99rmd
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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,…
Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
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 navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now