How to dd to a NFS server
I bought a new hard drive for my laptop and just wanted to copy the old disk to the new one because I didn't feel like installing everything again. On a desktop box that's pretty easy, just put the new disk in together with the old one and use dd to copy it across. Most laptops can only take one disk so that won't work. I have a NFS server with enough space to hold the laptop disk but NFS won't allow root to write on a NFS mounted directory and dd won't work as a normal user. This is how I did it.
Boot from the *ubuntu Live CD with the old disk in the laptop.
If portmap is not installed the NFS mount will take a while. You will also need nfs-client to be able to mount NFS filesystems. To install portmap type
sudo aptitude install portmap nfs-client
Mount the NFS directory to /mnt .
sudo mount 192.168.1.1:/home/username /mnt
Check the permissions on the NFS directory to make sure you can write to it
ls -l /mnt
It might show something like this
-rw-r--r-- 1 1000 1000 108 2007-09-07 01:35 myfile.txt
That means only the user with user id 1000 can write to the directory. The user id for the ubuntu user on the live CD is 999.
So lets add a user with uid 1000 that can write to the NFS directory...
sudo adduser diskuser --uid 1000
To see the uid of a user type
sudo cat /etc/passwd | grep diskuser
Now we have a user with write permission on the mounted NFS directory. The other problem is that to read from /dev/ you need to be root. Lets take a look at /dev/hda
sudo ls -l /dev/hda
brw-rw---- 1 root disk 3, 0 2007-10-08 20:21 /dev/hda
which means only user "root" and group "disk" can read from it. Sudo to the NFS directory won't work because sudo will dd as root. To fix this we have to add "diskuser" to the "disk" group like this
sudo adduser diskuser disk
Now we can dd as a normal user. Log on as the "diskuser" with
Then to copy the whole disk to a disk image on the NFS server type
dd if=/dev/hda of=/mnt/disk_image.img
When it is done it will go back to the prompt and you'll see
Exit "diskuser" by typing
Unmount the NFS directory
sudo umount /mnt
Shut down the laptop and put the new hard drive in. Boot from the *ubuntu Live CD and mount the NFS directory on /mnt as shown above. Now you have to copy the disk image back to the new hard drive.
sudo dd if=/mnt/disk_image.img of=/dev/hda
When it's done you'll have a copy of your old disk on the new one. Shutdown the laptop and boot from the hard drive. Please note that if the new disk is bigger than the old one you'll have to resize the partitions to be able to use all the space. The UUID of the disk might change as well. If that happens just boot from the *ubuntu Live CD again, mount the hard drive and edit the UUID's in /etc/fstab and /boot/grub/menu.list . To get the UUID of a partition or disk run
ls -l /dev/disk/by-uuid/
I hope this will be helpful to someone.
PS: Thanks to all the guys in #kubuntu on freenode for their help in understanding NFS permissions.