- Learn Linux
- Learn Electronics
- Raspberry Pi
- LPI certification
- News & Reviews
27 January 2008
On my home Linux server I have two hard disks. The first has my main operating system files, the other is to use as a separate data partition. As I was migrating over from another computer my 2nd drive still held my old data. It is only now that I have copied everything over that I now need to move my data partition over to my new drive.
Here is a summary of my information:
My main disk is /dev/hda
My secondary disk (for data) is /dev/hdb
My data partition is to be mounted as /data (existing information needs to be retained)
Warning some of the commands used are destructive. Enter the wrong information and you can obliterate your entire computer. Ensure you always use the correct device names - the ones on my system may not be the same as yours!
I also recommend you make a backup of all your valuable date before starting.
First unmount any old partitions on the second disk that may be mounted.
sudo umount /dev/hdb1
If you are using a swap partition on the old disk then turn that off
sudo swapoff /dev/hdb5
Now we can clear and repartition the disk drive. My system is Ubuntu 7.10, which comes with cfdisk. Some other Linux distros use fdisk instead, which is slightly less user friendly. If you have a problem using fdisk you could use a Live CD with cdfdisk on.
This is done by running
sudo cfdisk /dev/hdb
WARNING this is a very dangerous command if you don't know what you are doing. We are fairly safe here as this is a completely separate drive with no data on it. Make sure you enter the disk drive name correctly. You will be warned before any actual changes are made to the disk.
cfdisk is fairly intuitive. Navigate using the cursor / tab keys. Basically need to delete all the existing partitions, and then create new ones as appropriate. For my install I created one big partition of 80GB, but you could split it into smaller partitions that could be mounted elsewhere in the system.
The following screenshot shows the end result
cfdisk (util-linux-ng 2.13) Disk Drive: /dev/hdb Size: 81964302336 bytes, 81.9 GB Heads: 255 Sectors per Track: 63 Cylinders: 9964 Name Flags Part Type FS Type [Label] Size (MB) ------------------------------------------------------------------------------ hdb1 Primary Linux ext3 81956.69 [Bootable] [ Delete ] [ Help ] [Maximize] [ Print ] [ Quit ] [ Type ] [ Units ] [ Write ]
When creating a partition you have the choice of a primary or logical partition. I used primary, but there is no reason why it could not have been a logical partition. You are limited to 4 primary partitions per disk (or 3 if you have some logical partitions as well), so if you want to have more then you should use logical partitions for at least some of your disks. The only difference you will notice is that primary partitions will be numbered before logical ones. I suggest you go with one or the other, and don't mix them.
Once the partitions are laid down as required the partitions needs to be have the filesystem setup (formatted). I chose to use ext3 as this is what the rest of my system uses. You could however format using reiser or jfs or any of the other filesystems.
The command for creating the file system (formatting) the disk is mkfs. This can be used with the -t option specifying the appropriate file system type or there are aliases for the common types.
In my case:
sudo mkfs.ext3 /dev/hdb1
Note I am now using hdb1 - which is the first partition, rather than hdb which refers to the entire disk.
Similar commands exist such as mkfs.reiserfs, mkfs.jfs or mkfs.vat for reiser, JFS, or vfat (used by Windows 9x / memory cards etc) respectively.
WARNING - this will delete any data on the specified disk. In this case it won't even warn you. Make sure that you enter the disk name correctly, and double check it before hitting enter. YOU HAVE BEEN WARNED.
The next step would normally be to setup the mount points and mount the new filesystem. However I already had files in /data so they needed to be copied across to the new partition, otherwise they would end up hidden. But I can't copy the files until I've mounted the filesystem - Catch-22.
To get around this the file system needs to be mounted in a temporary folder.
sudo mkdir /media/temp
sudo mount /dev/hdb1 /media/temp
(you may need to give the filesystem type, but mount usually works it out for itself)
sudo cp -r /data/* /media/temp
once the files have been copied across the old entries can be removed
sudo rm -r /data/*
Hopefully I don't need to remind you how dangerous that command is. Make sure that you enter the path correct and DON'T put a space before the * character.
First unmount from the temporary mount point
sudo umount /media/temp
sudo rmdir /media/temp
The file used to determine how filesystems is mounted is /etc/fstab
sudo vi /etc/fstab
If you have any entries for the old disk then you should delete them. If the entries were created during install then they may be listed by the UUID of the partition. In which case look for a comment above the entry with the device name or the mount point where they were mounted.
You then need to add an entry for the new disk. Note I've included the header from the file, but this does not need to be the first entry. It can normally be put on a new line at the end.
# <file system> <mount point> <type> <options> <dump> <pass> /dev/hdb1 /data ext3 defaults,exec 0 0
Note the exec option can be omitted if there is not going to be any executable code that you want to run from the partition. In fact for a data partition it would be a good idea not to include the exec option. Obviously exec must be included if you create a usr or other partition containing executable code.
Finally mount the filesystem. You could mount this filesystem by issuing:
sudo mount /data
Although it may be better to mount using
sudo mount -a
This will mount all filesystems set to mount at startup, so is a good way to check the fstab entry is correct.
You can then check it is mounted correctly using the mount command without any further options. This will list all mounted partitions including:
/dev/hdb1 on /data type ext3 (rw)
You should now have a /data filesystem on a second disk drive. You can view the amount of space in use using the df command:
$df /data Filesystem 1K-blocks Used Available Use% Mounted on /dev/hdb1 77861892 9185904 64720788 13% /data