How to List Unmounted partition of a harddisk and Mount them?
Solution 1:
Listing Unmounted Partitions
To address the listing of the unmounted partitions part, there are several ways - lsblk
, fdisk
, parted
, blkid
.
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 111.8G 0 disk
└─sda1 8:1 0 111.8G 0 part /
sdb 8:16 0 232.9G 0 disk
├─sdb1 8:17 0 1.5G 0 part
├─sdb2 8:18 0 138.6G 0 part /media/WINDOWS
├─sdb3 8:19 0 8.1G 0 part
├─sdb4 8:20 0 1K 0 part
├─sdb5 8:21 0 68.5G 0 part
└─sdb6 8:22 0 5.8G 0 part
loop0 7:0 0 100G 0 loop
└─docker-8:1-1589297-pool (dm-0) 252:0 0 100G 0 dm
loop1 7:1 0 2G 0 loop
└─docker-8:1-1589297-pool (dm-0) 252:0 0 100G 0 dm
$ sudo fdisk -l
[sudo] password for xieerqi:
Disk /dev/sda: 120.0 GB, 120034123776 bytes
255 heads, 63 sectors/track, 14593 cylinders, total 234441648 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000b5321
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 234440703 117219328 83 Linux
Disk /dev/sdb: 250.1 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders, total 488397168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x96360d50
Device Boot Start End Blocks Id System
/dev/sdb1 * 2048 3074047 1536000 27 Hidden NTFS WinRE
/dev/sdb2 3074048 293617502 145271727+ 7 HPFS/NTFS/exFAT
/dev/sdb3 471437312 488396799 8479744 17 Hidden HPFS/NTFS
/dev/sdb4 293617662 471437311 88909825 5 Extended
/dev/sdb5 315830272 459382783 71776256 83 Linux
/dev/sdb6 459384832 471437311 6026240 82 Linux swap / Solaris
Partition table entries are not in disk order
Disk /dev/mapper/docker-8:1-1589297-pool: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders, total 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 65536 bytes / 65536 bytes
Disk identifier: 0x00000000
Disk /dev/mapper/docker-8:1-1589297-pool doesn't contain a valid partition table
$ sudo parted -l
[sudo] password for xieerqi:
Model: ATA Radeon R7 (scsi)
Disk /dev/sda: 120GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 1049kB 120GB 120GB primary ext4 boot
Model: ATA TOSHIBA MK2555GS (scsi)
Disk /dev/sdb: 250GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 1049kB 1574MB 1573MB primary ntfs boot, diag
2 1574MB 150GB 149GB primary ntfs
4 150GB 241GB 91.0GB extended
5 162GB 235GB 73.5GB logical ext4
6 235GB 241GB 6171MB logical linux-swap(v1)
3 241GB 250GB 8683MB primary ntfs hidden
Model: Linux device-mapper (thin-pool) (dm)
Disk /dev/mapper/docker-8:1-1589297-pool: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Number Start End Size File system Flags
1 0.00B 107GB 107GB ext4
$ sudo blkid
[sudo] password for xieerqi:
/dev/sda1: UUID="86df21bf-d95f-435c-9292-273bdbcba056" TYPE="ext4"
/dev/sdb1: LABEL="System" UUID="F4F688B2F68876A0" TYPE="ntfs"
/dev/sdb2: LABEL="TI105866W0A" UUID="4EBAAE53BAAE36FD" TYPE="ntfs"
/dev/sdb3: LABEL="HDDRECOVERY" UUID="BC4ED40D4ED3BDF8" TYPE="ntfs"
/dev/sdb5: UUID="0ca7543a-5463-4a07-8bbe-233a7b0bd625" TYPE="ext4"
/dev/sdb6: UUID="3a6e2270-19a2-49d7-aab3-5efb92d3b3d0" TYPE="swap"
/dev/loop0: UUID="a3693b88-7899-4628-848d-d9012205cf56" TYPE="ext4"
/dev/mapper/docker-8:1-1589297-pool: UUID="a3693b88-7899-4628-848d-d9012205cf56" TYPE="ext4"
$
One could use a little bit of AWK
magic to parse output of lsblk
to list all the unmounted partitions :
$ lsblk --noheadings --raw | awk '$1~/s.*[[:digit:]]/ && $7==""'
sdb1 8:17 0 1.5G 0 part
sdb3 8:19 0 8.1G 0 part
sdb4 8:20 0 1K 0 part
sdb5 8:21 0 68.5G 0 part
sdb6 8:22 0 5.8G 0 part
Or alternatively:
$ lsblk --noheadings --raw -o NAME,MOUNTPOINT | awk '$1~/[[:digit:]]/ && $2 == ""'
sdb1
sdb2
sdb3
sdb4
sdb5
What exactly is happening there is that we're listing all the
lines which have first column starting with letter s
(because that's how drives typically are named) and ending with a number (which represent partitions). In my previous output you could see that I have other filesystems, such as for docker, so in the above command we're getting rid of all the unnecessary stuff.
Mounting Partitions
I've found that mount
can be picky: it needs to know exact filesystem, it needs to be run as root, etc. udisksctl mount -b /dev/sXY
is a much better command, can be ran as regular user, and mounts automatically to the /media/$USER/
folder. For example,
$ udisksctl mount -b /dev/sdb5
Mounted /dev/sdb5 at /media/xieerqi/0ca7543a-5463-4a07-8bbe-233a7b0bd625.
Solution 2:
This is what I developed for listing unmounted volumes:
lsblk --noheadings --raw | awk '{print substr($0,0,4)}' | uniq -c | grep 1 | awk '{print "/dev/"$2}'