Setup New SSD on Ubuntu

Step 1: Shutdown the Computer

Before installing the new SSD, ensure that your computer is completely turned off and disconnected from any power sources to prevent hardware damage or data loss.

Step 2: Install the New SSD

Physically install the new SSD into your computer following the manufacturer’s instructions. Ensure that all connections are secure.

Step 3: Start the Computer

After successfully installing the SSD, power on your computer. Ubuntu should automatically detect the new hardware.

Step 4: Create Partitions Using gparted

To partition the new SSD, you can use the gparted tool, which provides a graphical interface for disk management.

a. Install gparted

If you prefer a graphical interface, install gparted using the following commands:

sudo apt update
sudo apt install gparted

b. Launch gparted

Start the gparted application with administrative privileges:

sudo gparted

c. Select the New Hard Drive

In the top-right dropdown menu of gparted, select your new SSD (e.g., /dev/sdb). Caution: Ensure you select the correct drive to avoid data loss on existing drives.

d. Create a Partition Table

If the SSD is unpartitioned, create a new partition table:

  1. Click on Device > Create Partition Table.
  2. Choose gpt (GUID Partition Table) as the partition table type, which is recommended for modern systems.
  3. Click Apply to create the partition table.

e. Create New Partitions

  1. Right-click on the unallocated space and select New.
  2. Set the File System type (e.g., ext4).
  3. Assign a Label if desired.
  4. Click Add to create the partition.

f. Apply Changes

Click the green checkmark button in the toolbar to apply all pending operations. Wait for the process to complete.

Step 5: Create Mount Points and Mount New Partitions

After partitioning, you need to create mount points and mount the new partitions to make them accessible.

a. Create Mount Point Directories

Create directories where the new partitions will be mounted (e.g., /data1 and /data2):

sudo mkdir /data1
sudo mkdir /data2

b. Mount the Partitions

Mount the newly created partitions to the respective directories. Replace /dev/sdb1 and /dev/sdb2 with your actual partition identifiers if they differ.

sudo mount /dev/sdb1 /data1
sudo mount /dev/sdb2 /data2

c. Verify Mounting

Use the df -h command to confirm that the partitions are mounted correctly:

df -h | grep /data

Example Output:

/dev/sdb1        500G  1G  499G  1% /data1
/dev/sdb2        200G  5G  195G  3% /data2

Step 6: Configure Automatic Mounting at Boot

To ensure that the new SSD partitions are mounted automatically every time the system boots, you need to add them to the /etc/fstab file.

a. Retrieve Partition UUIDs

Using UUIDs in /etc/fstab is recommended over device names to avoid mounting issues if device identifiers change.

Retrieve the UUIDs of the new partitions:

sudo blkid /dev/sdb1
sudo blkid /dev/sdb2

Example Output:

/dev/sdb1: UUID="d713cea4-d903-4ac4-9d48-3a019e6911ce" TYPE="ext4" PARTUUID="00000000-01"
/dev/sdb2: UUID="279266c0-57a2-44b7-a47a-c01a85208195" TYPE="ext4" PARTUUID="00000000-02"

b. Edit the /etc/fstab File

Open the /etc/fstab file with a text editor (using nano in this example):

sudo nano /etc/fstab

c. Add Mount Entries

At the end of the file, add the following lines, replacing the UUIDs with the ones obtained in the previous step and adjusting the mount points if necessary:

# Mount sdb1 to /data1
UUID=d713cea4-d903-4ac4-9d48-3a019e6911ce  /data1  ext4  defaults  0  2

# Mount sdb2 to /data2
UUID=279266c0-57a2-44b7-a47a-c01a85208195  /data2  ext4  defaults  0  2

Explanation of Fields:

  • UUID=... : The unique identifier for the partition.
  • /data1 and /data2 : The mount points.
  • ext4 : The filesystem type.
  • defaults : Default mount options (read/write, auto-mount, etc.).
  • 0 : Dump backup option (0 to disable).
  • 2 : Filesystem check order (1 is reserved for the root filesystem).

d. Save and Exit

In nano, press CTRL + O to save the changes, then CTRL + X to exit the editor.

e. Test the /etc/fstab Configuration

Before rebooting, test the new /etc/fstab entries to ensure there are no syntax errors:

sudo mount -a

If there are no error messages, the configuration is correct.

f. Confirm Automatic Mounting

Verify that the partitions are mounted as specified:

df -h | grep /data

Expected Output:

/dev/sdb1        500G  1G  499G  1% /data1
/dev/sdb2        200G  5G  195G  3% /data2

Step 7: Set Permissions and Ownership

Configure the permissions and ownership of the mount points to control access.

a. Change Ownership

Assign ownership of the mount points to a specific user and group. Replace <username> and <groupname> with your desired user and group (e.g., handsomeyun and developers):

sudo chown handsomeyun:developers /data1
sudo chown handsomeyun:developers /data2

b. Set Directory Permissions

Set the appropriate permissions to control access. The following command grants read, write, and execute permissions to the owner and group, and no permissions to others:

sudo chmod 770 /data1
sudo chmod 770 /data2

Explanation:

  • 770
    

    :

    • 7 (owner): Read, write, execute
    • 7 (group): Read, write, execute
    • 0 (others): No permissions

Step 8: (Optional) Configure Disk Quotas

If you wish to limit the disk usage for specific users or groups on the new SSD, you can set up disk quotas.

a. Enable Quotas in /etc/fstab

Edit the /etc/fstab file to add quota options. Open the file:

sudo nano /etc/fstab

Modify the entries for /data1 and /data2 by adding usrquota and grpquota to the mount options:

# Mount sdb1 to /data1 with quotas
UUID=d713cea4-d903-4ac4-9d48-3a019e6911ce  /data1  ext4  defaults,usrquota,grpquota  0  2

# Mount sdb2 to /data2 with quotas
UUID=279266c0-57a2-44b7-a47a-c01a85208195  /data2  ext4  defaults,usrquota,grpquota  0  2

Save and exit (CTRL + O, then CTRL + X in nano).

b. Remount the Filesystems

Apply the new mount options without rebooting:

sudo mount -o remount /data1
sudo mount -o remount /data2

c. Create Quota Database Files

Initialize the quota system by creating the necessary quota files:

sudo quotacheck -cum /data1
sudo quotacheck -cgum /data1

sudo quotacheck -cum /data2
sudo quotacheck -cgum /data2

Explanation of Options:

  • -c : Create quota files.
  • -u : Check user quotas.
  • -g : Check group quotas.
  • -m : Do not remount the filesystem.

d. Enable Quotas

Turn on the quota system for both mount points:

sudo quotaon /data1
sudo quotaon /data2

e. Assign Quotas to Users or Groups

For a Single User

To set disk quotas for a user (e.g., handsomeyun):

sudo edquota handsomeyun

This command will open an editor where you can specify soft and hard limits for disk blocks and inodes. For example:

Disk quotas for user handsomeyun (uid 1001):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /data1 ext4                    0       50000000  60000000          0          0        0
  • blocks : Disk space (typically in KB).
  • soft : Soft limit (warning threshold).
  • hard : Hard limit (enforced threshold).
  • inodes : Number of files (set to 0 if not limiting).

For a Group

To set disk quotas for a group (e.g., developers):

  1. Ensure the Group Exists

    getent group developers
    

    If the group does not exist, create it:

    sudo groupadd developers
    
  2. Set Group Quotas

    Calculate the block limits (assuming 1KB blocks). For example, to set a soft limit of 64GB and a hard limit of 80GB:

    • Soft Limit: 64GB = 64 * 1024 * 1024 KB = 67,108,864 KB
    • Hard Limit: 80GB = 80 * 1024 * 1024 KB = 83,886,080 KB
    sudo setquota -g developers 67108864 83886080 0 0 /data1
    sudo setquota -g developers 67108864 83886080 0 0 /data2
    

Explanation of Command:

  • -g : Specifies that the quota is for a group.
  • developers : Group name.
  • 67108864 : Soft block limit (64GB).
  • 83886080 : Hard block limit (80GB).
  • 0 0 : No inode limits.

f. Verify Quota Settings

Check User Quotas

quota -u handsomeyun

Check Group Quotas

sudo repquota /data1
sudo repquota /data2

Example Output:

*** Report for filesystem /data1
Block grace time: 7days; Inode grace time: 7days
User quotas:
              blocks      soft      hard    grace    inodes      soft      hard    grace
handsomeyun  20000000  50000000 60000000          0          0        0        

Group quotas on /data1:
              blocks      soft      hard    grace    inodes      soft      hard    grace
developers    0       67108864 83886080          0          0        0        

Step 9: Troubleshooting and Best Practices

a. Quotas Not Being Applied

  • Check Mount Options

    Ensure usrquota and grpquota are present:

    mount | grep /data1
    mount | grep /data2
    

    Expected Output:

    /dev/sdb1 on /data1 type ext4 (rw,relatime,usrquota,grpquota)
    /dev/sdb2 on /data2 type ext4 (rw,relatime,usrquota,grpquota)
    
  • Verify Quota Files Exist

    ls /data1/aquota.user /data1/aquota.group
    ls /data2/aquota.user /data2/aquota.group
    

    If missing, rerun quotacheck:

    sudo quotacheck -cum /data1
    sudo quotacheck -cgum /data1
      
    sudo quotacheck -cum /data2
    sudo quotacheck -cgum /data2
    
  • Enable Quotas

    sudo quotaon /data1
    sudo quotaon /data2
    

b. Permission Denied or Errors

  • Ensure Commands Are Run with sudo

    Most quota management commands require superuser privileges.

  • Check Filesystem Support

    Ensure you are using a filesystem that supports quotas (e.g., ext4).

c. Users Unable to Reach Quota Limits

  • Verify Quota Assignments

    Use quota and repquota to check quota status.


Step 10: Best Practices

  • Use UUIDs in /etc/fstab

    Prevent issues caused by device name changes by using UUIDs.

  • Regularly Monitor Disk Usage and Quotas

    Utilize repquota and quota commands to keep track of disk usage.

    sudo repquota /data1
    sudo repquota /data2
    quota -u handsomeyun
    
  • Set Reasonable Quota Limits

    Adjust soft and hard limits based on user and group needs to avoid over-restriction or underutilization.

  • Backup Important Data and Configuration Files

    Always backup critical data and configuration files like /etc/fstab before making changes.


Summary

By following these steps, you can successfully set up and configure a new SSD on your Ubuntu system. The key actions include:

  1. Shut Down and Install SSD: Safely install the new SSD into your computer.
  2. Boot and Identify SSD: Start the computer and verify that the SSD is recognized.
  3. Partition the SSD: Use gparted to create partitions on the SSD.
  4. Mount Partitions: Create mount points and mount the new partitions.
  5. Configure Automatic Mounting: Edit /etc/fstab to ensure the SSD mounts automatically at boot.
  6. Set Permissions: Assign appropriate ownership and permissions to the mount points.
  7. Configure Disk Quotas (Optional): Set up user and group quotas to manage disk usage.
  8. Troubleshoot and Follow Best Practices: Ensure quotas are applied correctly and maintain system integrity.

Reference Commands and Tools

  • Partitioning Tools:

    • fdisk : Command-line partitioning tool.
    • parted : Advanced partitioning tool.
    • gparted : Graphical partitioning tool (install with sudo apt install gparted).
  • Filesystem Tools:

    • mkfs
      

      : Format a filesystem.

      sudo mkfs.ext4 /dev/sdb1
      
  • Mounting Tools:

    • mount : Mount a filesystem.
    • umount : Unmount a filesystem.
  • Quota Management Tools:

    • quota : View quota limits.
    • edquota : Edit quota settings.
    • setquota : Set quotas for users or groups.
    • repquota : Report quota usage.
    • quotacheck : Check and repair quota information.
    • quotaon / quotaoff : Enable or disable quota enforcement.

Further Learning