Formatting a partition from command line

When you need to format a partition from the command line, a few commands are all you need. Here’s the typical workflow.

First, create the filesystem:

mkfs.ext4 /dev/sdxy

If you want to reserve less space for the root user:

tune2fs -m 0 /dev/sdxy

Check the filesystem for errors before mounting:

fsck.ext4 -fy /dev/sdxy

Then mount it where you need it:

mount /dev/sdxy /wherever/I/want

Replace /dev/sdxy with your actual partition and /wherever/I/want with your mount point.

Enjoy!

Logical Volume Management basics

I have explained how to activate an LVM logical volume before, but never talked much about LVM itself. Here is a brief introduction.

I have been playing with LVM for about a couple of years now and I must say I have found it very practical, even for managing simple home desktops and servers. The main feature I use is the ability to resize volumes online, but LVM is useful for other things too: creating snapshots, combining multiple disks into a single volume group, and adding redundancy through mirroring.

Physical Volumes, Volume Groups, Logical Volumes

LVM works in three layers:

  • Physical Volume (PV) — a disk or partition that LVM can use. Initialised with pvcreate.
  • Volume Group (VG) — a pool of storage made from one or more physical volumes.
  • Logical Volume (LV) — a virtual partition carved out of a volume group. This is what you format and mount.

Start by preparing a partition (use fdisk to set the type to 8e — Linux LVM):

sudo pvcreate /dev/sdb1
sudo vgcreate my_volume_group /dev/sdb1
sudo lvcreate -L 10G -n my_volume my_volume_group

Now format and mount it:

sudo mkfs.ext4 /dev/my_volume_group/my_volume
sudo mount /dev/my_volume_group/my_volume /mnt

Resizing

Need more space? If you have free space in the volume group:

sudo lvextend -L +5G /dev/my_volume_group/my_volume
sudo resize2fs /dev/my_volume_group/my_volume

The filesystem is resized online — no unmounting required.

Snapshots

LVM snapshots let you take a point-in-time copy of a volume. Useful for backups:

sudo lvcreate -L 1G -s -n my_snapshot /dev/my_volume_group/my_volume

The snapshot starts small and grows as changes are made to the original volume. Remove it when no longer needed:

sudo lvremove /dev/my_volume_group/my_snapshot

Enjoy!

How to fix GRUB 2 with Ubuntu live cd

When GRUB fails and your system won’t boot, the errors can be intimidating. But fixing it with a live CD is straightforward.

Boot from an Ubuntu live CD or USB. Once the desktop loads, open a terminal.

First, mount your root partition:

sudo mkdir /mnt/newroot
sudo mount /dev/sdxy /mnt/newroot

Bind the virtual filesystems:

sudo mount --bind /dev /mnt/newroot/dev
sudo mount --bind /proc /mnt/newroot/proc
sudo mount --bind /sys /mnt/newroot/sys

Chroot into your system:

sudo chroot /mnt/newroot

Reinstall GRUB:

sudo grub-install /dev/sdx
sudo update-grub

Exit the chroot, unmount everything, and reboot. Remove the live CD when prompted.

For UEFI systems, the command changes slightly:

sudo grub-install --target=x86_64-efi --efi-directory=/boot/efi

If you can still reach a grub rescue prompt, try booting manually:

set root=(hd0,msdos1)
set prefix=(hd0,msdos1)/boot/grub
insmod normal
normal

Enjoy!

Linux: what is partition UUID and how to use it

If you have worked with Linux disks, you have seen device names like /dev/sda1. They work fine until you add or remove a disk — then the names can shift, breaking your mount points and scripts. UUIDs solve this because they are permanent and tied to the filesystem itself.

Instead of writing /dev/sda1 in fstab, use the UUID:

UUID=65b14742-9610-4289-8ea4-af8edcc5d8da /media/data ext4 defaults 0 2

There are a few ways to find the UUID of a partition:

sudo vol_id -u /dev/sda2
sudo blkid
ls -l /dev/disk/by-uuid/

On older systems vol_id was the standard tool, while blkid is the modern equivalent available on virtually all current distributions. You can also see the PARTUUID with blkid, which identifies the partition table entry rather than the filesystem — this is useful for booting with UEFI.

If UUIDs feel unwieldy, you can label a filesystem and use the label instead:

sudo e2label /dev/sda1 mydata
LABEL=mydata /media/data ext4 defaults 0 2

Labels are easier to read but not guaranteed to be unique. UUIDs are the safer choice for production systems.

Hope it works!

How to install “as many Operating Systems as you want” in one machine

Do you often have the need, or want the pleasure, to try a new operating system?
Do you prefer using a “real machine” rather than creating a virtual one (like VMware or Virtualbox for instance)?
Have you got a spare machine for this purpose?
Ok, if you responded “Yes” to all the above, please read on…

Some theory
A hard disk partition can be:
– primary;
– extended;
– logical.

Primary partition
A hard disk can be divided in no more than four primary partitions. Microsoft Windows requires to be installed in a primary partition in order to boot. In some older versions it even requires to be in the first partition as Wikipedia says: “In DOS and earlier versions of Microsoft Windows systems, the system partition was required to be the first partition. Windows operating systems must be located in a primary partition. Other operating systems do not have this peculiar requirement.”

Extended partition
Instead of creating four primary partitions one can be “sacrificed” to be an extended one. A hard disk may contain only one extended partition which can be subdivided into multiple logical partitions.
The only purpose of an extended partition is to contain logical partitions.

Logical partition
These can be used as normal partitions except for the fact that Windows cannot boot from them. Linux can… :-) Nice penguin!

Practice
So, this is the “secret”. You can use logical partitions for installing all Linux flavours that you like so much and can’t live without (SWAP and other OSs) and keep the primary partitions for the lovely Windows.
To start partitioning your hard drive you can use gParted which is the best tool I’ve ever found for this purpose (or any other partitioning tool you like). gParted is included in Ubuntu but I’m quite sure it can be installed in other distros as well.

Now you should be ready to have a look at my experiment:
GParted - multi Operating Systems
As you can see I created an extended partition in the first one (“/dev/sda1”) and I kept a primary partition for Windows Xp (“/dev/sda2”).
Inside the extended partition I’ve created six logical partitions for some linux flavours all functioning, plus a SWAP (“/dev/sda5”) partition and a DATA (“/dev/sda12”) partition used to share files among the OSs. I’ve choosen “fat32” since some Linux distributions don’t support NTFS out of the box.

After all of this, my little hard disk still has 9 GB free. What other operating system should I install? :-)
All operating systems are managed by a boot loader (Grub2) which gives me the possibility to choose which OS to start. Grub2 is shipped with many recent distributions like Ubuntu Lucid for example.

In the near future I might write a post on how to recover a lost grub which makes the computer unbootable.
Enjoy the freedom to try all the operating systems you are curious about.