This is the case: the Ubuntu/Debian installation is made with the root folder placed on one disk together with the swap disk. Unfortunately the disk is beginning to become a bit crowded and the amount of freespace is continually diminishing. So, with a new disk plugged in the machine it's time to move the directory /var to the new disk. Assuming that the disk is in place and recognized by the OS, lets get moving:
Create a partition using fdisk on the new disk, in this case /dev/sdb. In the text below I've choose the commands
- m to display the help
- followed by n to add a new partition
- and then p which makes the partition a primary one
- as I wanted just one partition on the disk I used the default start and end cylinders
- finished the process by w - writing the partition to disk
user@server:~$ sudo fdisk /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0xc31d1040. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. The number of cylinders for this disk is set to 3916. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with: 1) software that runs at boot time (e.g., old versions of LILO) 2) booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK) Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) Command (m for help): m Command action a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition l list known partition types m print this menu n add a new partition o create a new empty DOS partition table p print the partition table q quit without saving changes s create a new empty Sun disklabel t change a partition's system id u change display/entry units v verify the partition table w write table to disk and exit x extra functionality (experts only) Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-3916, default 1): Using default value 1 Last cylinder or +size or +sizeM or +sizeK (1-3916, default 3916): Using default value 3916 Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
The next step is to create the new file system, in this particular case EXT3, and that means journaling enabled.
user@server:~$ sudo mke2fs -j /dev/sdb1
mke2fs 1.40.8 (13-Mar-2008)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
1966080 inodes, 7863809 blocks
393190 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
240 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 24 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
Time to mount the new disk.
user@server:~$ sudo mkdir /media/new_disk user@server:~$ sudo mount /dev/sdb1 /media/new_disk
Copy the directory and verify afterwards:
user@server:~$ sudo cp -a /var/* /media/new_disk/
sudo find /var/ -path /proc -prune -o -path /media/new_disk -prune -o -xtype f -exec cmp {} /media/new_disk{} \;
Ubuntu mounts disks using UUID:s and this is how one can mount the new disk in the same fashion. Start by generating the UUID.
user@server:~$ sudo tune2fs -U random /dev/sdb1
To find out what the new UUID is for /dev/sdb1.
user@server:~$ sudo blkid /dev/sdb1 /dev/sdb1: UUID="b80278d6-b315-4c98-8698-85189acb95b4" TYPE="ext3"
Then add the disk to fstab using the following lines:
# /dev/sdb1 UUID=b80278d6-b315-4c98-8698-85189acb95b4 /var ext3 defaults 0 2
Notice the last number, 2. Only the root filesystem should be 1, all other filesystems should be 2 according to the man page.
Reboot, then check out the new filesystem layout.
user@server:~$ df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 9,4G 1,3G 7,7G 15% / varrun 738M 52K 737M 1% /var/run varlock 738M 0 738M 0% /var/lock udev 738M 52K 737M 1% /dev devshm 738M 0 738M 0% /dev/shm /dev/sdb1 30G 577M 28G 2% /var