ProperBlog

Security for the masses

Skip to: Content | Sidebar | Footer

Moving /dev to new disk in a VM

29 September, 2010 (09:26) | LAMP | By: Blogkeep

When I built a development server on VirtualBox, I thought 8Gb would be enough disk space. It wasn’t. I decided that moving /dev to another virtual disk would be a solution. I could have imaged my vm created a larger disk and wrote the image back to the new bigger disk. However, this is what I did to get more space:

This was done with VirtualBox running 64bit Ubuntu.

Create a new virtual disk (vdi) of the size required, and add this disk to your Ubuntu VM
Boot the VM and login. Start a terminal(if using a desktop)

Make sure the disk has been detected:


> sudo fdisk -l

This command lists the disks in the system for my system the following is returned:


Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000aaa52


Device Boot Start End Blocks Id System
/dev/sda1 * 1 32 248832 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 32 1045 8136705 5 Extended
/dev/sda5 32 1045 8136704 8e Linux LVM

Disk /dev/sdb: 8213 MB, 8213495808 bytes
255 heads, 63 sectors/track, 998 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


disk /dev/sdb doesn't contain a valid file system

We have determined the new virtual disk /dev/sdb has been detected.

OK it doesn’t contain a valid file system, so we create one.
Firstly we partition the disk, I decided to use the whole disk. My responses to prompts are in bold:


> 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 0xc7872ee1.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.


Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').


Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-998, default 1): 1
Last cylinder, +cylinders or +size{K,M,G} (1-998, default 998):998

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Let us check the disk has been partitioned properly:


>sudo fdisk -l /dev/sdb

Disk /dev/sdb: 8213 MB, 8213495808 bytes
255 heads, 63 sectors/track, 998 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xc7872ee1

Device Boot Start End Blocks Id System
/dev/sdb1 1 998 8016403+ 83 Linux

There is our new partition sdb1 on disk sdb.
Now to create the file system, I used ext4:


> sudo mkfs.ext4 -v /dev/sdb1

mke2fs 1.41.11 (14-Mar-2010)
fs_types for mke2fs.conf resolution: 'ext4', 'default'
Calling BLKDISCARD from 0 to 8208793600 failed.
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
501952 inodes, 2004100 blocks
100205 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2055208960
62 block groups
32768 blocks per group, 32768 fragments per group
8096 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 38 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

Create a directory to mount the new disk:


> sudo mkdir /mnt/new

Mount the new disk:


> sudo mount /dev/sdb1 /mnt/new

Now we are ready to copy /dev to the new disk.
Firstly we need to drop run level into single user mode because we do not want disk writes happening in /var whilst we are copying it.


>sudo init 1

Copy data in var, not the /var directory itself.


> cd /var
> cp -ax * /mnt/new

Rename the /var directory, don’t delete until we are sure it all works.


> cd /
> mv var var.old

Make new var directory,


> mkdir var

unmount the new partition,


> umount /dev/sdb1

remount it as /var.


> mount /dev/sdb1 /var

Finally edit /etc/fstab to include the new partition, with /var given as the mount point, so that it will be included automatically at boot.

I added this line to my fstab: /dev/sdb1 /var ext4 defaults 0 0

After rebooting and you are sure everything works as it should you can delete the var.old directory.

Write a comment