Repository

Looks good to me!

User Tools

Site Tools


kb:intranet:platforms:linux:partitions

Everything boot

https://www.maketecheasier.com/move-home-folder-ubuntu/

To set last boot OS as default boot, courtesy of AskUbuntu:

/etc/default/grub
GRUB_DEFAULT=saved
GRUB_SAVEDEFAULT=true
sudo update-grub

Creating bootable USB stick

In the (un)fortunate event you need to create a new USB stick, can follow the steps below, referenced from AskUbuntu:

# Insert USB stick
lsblk  # check location of disk and filesystem, alternatively with 'df'
sudo umount /dev/sda  # unmount disk/filesystem
 
# Write to flash stick with dd utility
sudo dd bs=4M if={{PATH_TO_ISOFILE}} of={{PATH_TO_DISK}} conv=fdatasync status=progress && sync
 
3825205248 bytes (3,8 GB, 3,6 GiB) copied, 319 s, 12,0 MB/s
912+1 records in
912+1 records out
3826831360 bytes (3,8 GB, 3,6 GiB) copied, 937,909 s, 4,1 MB/s

If just reinitializing the partitions, overwriting the MBR section is sufficient:

dd if=/dev/zero of=/dev/sdX bs=512 count=1 seek=0

To eject or mount a USB:

# No umount necessary, will be handled by eject
sudo eject /dev/sdX
sudo eject -t /dev/sdX

Create new partition

fdisk can be used.

# List disks and partitions
user:~$ sudo fdisk -l
 
# Select unallocated disk, e.g. /dev/sda
user:~$ sudo fdisk /dev/sda
Command (m for help): n  # for new partition
Partition number (1-128, default 1):
First sector (...):
Last sector (...):
 
Created a new partition 1 of type 'Linux filesystem' and of size ...
 
# Commit changes
Command (m for help): w

BTRFS

This was confusing to get into, so some guides to start out:

An executive summary:

  • BTRFS has a two-part file allocation system... to be continued.

Some useful commands:

root:~# mkfs.btrfs -L mylabel /dev/sda

Ran out of space!

Step 0: Check for other culprits

First verify if it is really due to BTRFS, e.g. space could be consumed by excessively verbose logs. The usual du techniques work. Some other tools:

  • du --max-depth=1 -h . summarises the available disk space in the current directory
  • df -i / checks if there are available inodes
  • lsof | grep deleted checks if any processes are still holding on to space allocated to already deleted files

Step 1: Pull up BTRFS information

Check the BTRFS version with btrfs version. Some important information:

  • Modern BTRFS version is tied to the Linux kernel.
  • Balancing can be automatically performed from kernel 5.19 using the sysfs knob

To see how much space BTRFS is consuming, use the btrfs program to check: fi show for device overview, fi df / for data/metadata breakdown, and fi usage / for a more detailed breakdown across devices.

# Or use the shortcut 'btrfs fi show'
root:~# btrfs filesystem show
Label: none  uuid: 080a6971-d262-42cb-af4d-47571f31a03a
	Total devices 1 FS bytes used 15.79GiB
	devid    1 size 26.63GiB used 26.63GiB path /dev/mmcblk0p2
 
root:~# btrfs fi df /
Data, single: total=24.61GiB, used=14.06GiB      <<-- BAD
System, single: total=32.00MiB, used=16.00KiB
Metadata, single: total=1.29GiB, used=1.21GiB
GlobalReserve, single: total=67.75MiB, used=0.00B

btrfs fi usage

The failure mechanism above is due to the inability to allocate more chunks for the metadata block group. This only occurs under the low disk space condition (see UnixSE). Under normal conditions, the metadata group will be automatically allocated more chunks as needed using unallocated disk space.

Step 2: Perform balancing

Balancing of the filesystem is needed, to reallocate data out of chunks to free them up for use. A "null rebalance" may be sufficient, which triggers deletion of empty chunks:

root:~# btrfs balance start -dusage=0 -musage=0 / &
root:~# btrfs balance status -v  # check status of background job
root:~# btrfs balance cancel /   # cancel balancing background job

If null balancing reports that no data was relocated any chunk, then slowly increase the -dusage and -musage parameter to increasingly clear out chunks with allocations lower than the specified percentage:

root:~# btrfs balance start -dusage=1 /
Done, had to relocate 0 out of 39 chunks
root:~# btrfs balance start -dusage=5 /
Done, had to relocate 1 out of 39 chunks
root:~# btrfs balance start -dusage=10 /
Done, had to relocate 0 out of 38 chunks
root:~# btrfs balance start -dusage=20 /
Done, had to relocate 3 out of 38 chunks
root:~# btrfs balance start -dusage=40 /
Done, had to relocate 4 out of 37 chunks

Consider doing this for the metadata group as well (using -musage), though note that . Guides may suggest the use of parameters -dlimit and -mlimit, which limits the number of reallocations performed in the current balancing run. Not particularly useful when clearing up as much space as possible, but useful for cron scripts, e.g.

crontab
24 */1 * * * btrfs balance start -dusage=20,limit=5 / >>/tmp/btrfs_balance.log 2>&1

Note also the existence of scrub command, but this is more for correcting errors across RAID volumes, rather than a space issue.

Step 2.1: No empty space available - Clear space

In other words, an ENOSPC error. If ENOSPC is triggered by the balancing itself, one needs to clear emergency space. Some available options:

  • Truncating existing files, e.g. truncate -s0 FILE. This reduces space without requiring extra metadata.

To clear emergency space, start by deleting old BTRFS snapshots.

root:~# btrfs sub show /
@/.snapshots/1/snapshot
	Name: 			snapshot
	Snapshot(s):
				@/.snapshots/24/snapshot
				@/.snapshots/25/snapshot
 
root:~# btrfs fi du -s /.snapshots/*
     Total   Exclusive  Set shared  Filename
   6.78GiB       0.00B     6.78GiB  /.snapshots/1
   6.78GiB       0.00B     6.78GiB  /.snapshots/24
   6.78GiB       0.00B     6.78GiB  /.snapshots/25
 
root:~# btrfs subvolume delete /.snapshots/24

Step 2.2: No empty space available - Add more devices

If a USB stick or hard drive is available on hand, add the device to BTRFS - this increases the amount of available space on the system. Find the device using lsblk.

root:~# btrfs device add /dev/sda
root:~# btrfs filesystem show /    # verify two devices available
root:~# btrfs filesystem df /      # check available unallocated space

If this device is only a temporary one for running balance, then perform a small rebalance first, before removing it and doing the full balance:

root:~# btrfs balance start -dusage=5 /
root:~# btrfs device remove /dev/sda /
root:~# btrfs balance start -dusage=40 /  # rebalance without the additional device
kb/intranet/platforms/linux/partitions.txt · Last modified: 3 months ago (19 July 2024) by justin