how to tell if a partition is empty/unused

12,691

Solution 1

My question is, how do I figure out what is in the /dev/sda2 partition?

According to your fdisk printout, it appears that /dev/sda2 is an extended partition. Disks partitioned with a MBR (Master boot record) are only capable of 4 real partitions: extended partitions are a way around that limit. They basically look like this:

your disk: ..............................................................
/dev/sda1:  \---------------------/
/dev/sda2:                         \------------------------------------/
/dev/sda5:                          \-----------------------------------/

Note that this is just a possible ASCII art of your disk: you didn't list your partition sizes and locations, so I'm not sure if the order here is correct, or if you have free space somewhere. (Or other partitions lurking about.)

It appears that /dev/sda2 likely contains /dev/sda5, your swap partition.

You could, as others suggest, attempt to mount /dev/sda2, but it will most likely fail.

gparted does a nice job of visualizing extended partitions. If you have it available, I find it much easier to work with than fdisk, and I think its rendering of extended partitions will make more sense.

What I would like to do is re-partition my HDD so as to begin a LFS build

Make sure any important data on the disk is backed up. It takes only one fat-finger on a partition table editor to make getting data off the disk much more exciting. (Also: make sure you're editing the partitions on the disk you intend to be editing, if you have multiple disks.)

Solution 2

You can check if that partition is already mounted:

mount | grep '/dev/sda2'

This command will show you the mountpoint that partition is mounted under, if any.

Example output:

/dev/sda2 on /home type ext4 (rw,relatime,user_xattr,acl,barrier=1,data=ordered)

In this case, /dev/sda2 is mounted under /home and you can go there directly and figure out "what is in it".

If the command produces no output, you need to mount the partition first in order to read it. Try something like:

sudo mount -t auto /dev/sda2 /mnt

This will mount /dev/sda2 under /mnt so you can go and see what's stored in it.

To find out the space usage for a partition mounted under /mnt, you can do

df -h /mnt

If a partition has enough empty space for your requirements, you can then shrink it and use the newly-created empty space for your LFS build. To shrink partitions easily, you can use a graphical partition editor like gparted

You might also be interested in the following U&L Q&A I answered earlier about something similar:

Solution 3

  1. Check with mount if you have block device mounted
  2. Use blkid tool to find out whether you partition is formatted.
  3. If you have you partition unmounted and formatted, try to mount (blkid helps to determine right filesystem type) and check data on it with mount tool
Share:
12,691

Related videos on Youtube

Japleen
Author by

Japleen

Updated on September 18, 2022

Comments

  • Japleen
    Japleen over 1 year

    This is certainly a really easy question for you all I am sure. So I have Ubuntu (13.04) as the only OS mounted on my HDD. What I would like to do is re-partition my HDD so as to begin a LFS build.
    Using sudo fdisk /dev/sda, and p for print partition table I get:

    /dev/sda1 * ................................. 83 linux
    /dev/sda2   ................................. 5 extended
    /dev/sda5   ................................. 82 linux swap/solaris
    

    I would like to use the swap space that is /dev/sda5 as the swap space for the build so as to not have superfluous swap space.

    Also, I would like to build the system in an extended system so I can make logical partitions for /boot, /, /usr, et al.

    My question is, how do I figure out what is in the /dev/sda2 partition? Is it empty? Is it available for me to build my LFS base in? If it is empty, then I won't partition anything. If it is being used by Ubuntu for something then I will have to build another primary partition for the build.

    Unfortunately I do not remember making the partition, if I did.

  • Japleen
    Japleen over 10 years
    i was unable to mount the device, as i didnt know what /mnt was for /dev/sda2. so i could not use any of this
  • Japleen
    Japleen over 10 years
    when i used the mount command following your advice, the output gave a description of /dev/sda1, but nothing about /dev/sda2. also when i used the blkid, also after your advice, no output wrt /dev/sda2 was produced. only /sda1
  • Japleen
    Japleen over 10 years
    /dev/sda1 starts at 2048-310503423, and has 155250688 blocks /dev/sda2 starts at 310505470 to 312580095, and has 1037313 blocks /dev/sda5 starts at 310505472 to 312580095, and has 1037313 blocks. from this it seems that /dev/sda2 contains /dev/sda5, as you suggested. i did attempt mount /dev/sda2 but no this did not work. i used gparted for the first time and this is what it told me, /dev/sda1, ext4 filesystem, mount point /, size 148.06 Gib,.. /dev/sda2, extended, has nothing for mount point and contains 1013. mib with 0 used
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 10 years
    With PC-style partitions, there can only be one extended partition, and all partitions numbered 5 and above are part of that extended partition. So it is certain that sda2 contains sda5. @alienfetuseater The sda2 partition occupies 512 bytes on its own, plus the space used by the logical partitions that it contains. Since sda2 and sda5 have the same size, there is no free space in sda2.